Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
338 lines (324 loc) · 11.4 KB
/
Copy pathintegration_test.go
File metadata and controls
338 lines (324 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package rtapi
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"os"
"path"
"slices"
"sort"
"strings"
"testing"
"time"
)
// bencode encodes strings, ints, lists, and dictionaries, for test torrents.
func bencode(value any) []byte {
var out bytes.Buffer
var encode func(any)
encode = func(value any) {
switch v := value.(type) {
case string:
fmt.Fprintf(&out, "%d:%s", len(v), v)
case int:
fmt.Fprintf(&out, "i%de", v)
case []any:
out.WriteByte('l')
for _, item := range v {
encode(item)
}
out.WriteByte('e')
case map[string]any:
keys := make([]string, 0, len(v))
for key := range v {
keys = append(keys, key)
}
sort.Strings(keys)
out.WriteByte('d')
for _, key := range keys {
encode(key)
encode(v[key])
}
out.WriteByte('e')
default:
panic(fmt.Sprintf("bencode: %T", value))
}
}
encode(value)
return out.Bytes()
}
// testTorrent returns a .torrent file for info, and its info-hash.
func testTorrent(info map[string]any, announce string) ([]byte, string) {
info["piece length"] = 16384
info["pieces"] = strings.Repeat("\x01", 20)
sum := sha1.Sum(bencode(info))
metainfo := map[string]any{"info": info}
if announce != "" {
metainfo["announce"] = announce
}
return bencode(metainfo), strings.ToUpper(hex.EncodeToString(sum[:]))
}
// TestIntegrationAgainstRTorrent runs the client against a real rTorrent,
// whose SCGI address RTAPI_TEST_RTORRENT gives; CI starts one. It loads two
// stopped torrents, uses every call, and removes them again.
func TestIntegrationAgainstRTorrent(t *testing.T) {
address := os.Getenv("RTAPI_TEST_RTORRENT")
if address == "" {
t.Skip("set RTAPI_TEST_RTORRENT to an rTorrent SCGI address to run")
}
ctx := context.Background()
rt, err := NewRtorrentContext(ctx, address)
if err != nil {
t.Fatal(err)
}
t.Logf("rTorrent %s", rt.Version)
dir := t.TempDir()
singleName := `Ünïcødé & <single> "file" 日本.bin`
single, singleHash := testTorrent(map[string]any{"name": singleName, "length": 1000}, "http://tracker.invalid:6969/announce")
multi, multiHash := testTorrent(map[string]any{"name": "multi folder", "private": 1, "files": []any{
map[string]any{"length": 10, "path": []any{"a.txt"}},
map[string]any{"length": 20, "path": []any{"sub", "b.txt"}},
}}, "http://other.invalid/announce")
if err := rt.DownloadRawContext(ctx, single, &DotTorrentWithOptions{Stopped: true, Dir: dir, Label: "rtapi test"}); err != nil {
t.Fatal(err)
}
// rTorrent parses a load's directory and label as command arguments, so
// quotes, backslashes, dollar signs, and characters strconv.Quote would
// escape must arrive as they are.
oddDir, oddLabel := path.Join(dir, `quote" back\slash $1 zero`+"width é"), `rtapi "label" \ $2`
if err := rt.DownloadRawContext(ctx, multi, &DotTorrentWithOptions{Stopped: true, Dir: oddDir, Label: oddLabel}); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
rt.DeleteMetadata(&Torrent{Hash: singleHash}, &Torrent{Hash: multiHash})
})
waitLoaded(t, rt, singleHash, multiHash)
// rTorrent's own responses take the fast decoding path.
req, err := buildTorrentsRequest()
if err != nil {
t.Fatal(err)
}
body, err := rt.send(ctx, encode(req))
if err != nil {
t.Fatal(err)
}
payload, err := readResponse(body, DefaultMaxResponseSize, -1)
body.Close()
if err != nil {
t.Fatal(err)
}
if _, ok := decodeFast(payload[bytes.Index(payload, []byte("<methodResponse")):]); !ok {
t.Fatalf("rTorrent's torrent list falls back to encoding/xml:\n%.2000s", payload)
}
torrents, err := rt.ListContext(ctx, ListOptions{})
if err != nil {
t.Fatal(err)
}
find := func(torrents Torrents, hash string) *Torrent {
t.Helper()
for _, torrent := range torrents {
if torrent.Hash == hash {
return torrent
}
}
t.Fatalf("torrent %s is missing", hash)
return nil
}
s, m := find(torrents, singleHash), find(torrents, multiHash)
if s.Name != singleName || s.Size != 1000 || s.MultiFile || s.Label != "rtapi test" || s.State != Stopped || s.Tracker != nil || s.Private || s.TrackerCount != 1 {
t.Errorf("single-file torrent = %+v", s)
}
if m.Name != "multi folder" || m.Size != 30 || !m.MultiFile || m.Directory != path.Join(oddDir, "multi folder") || !m.Private || m.Label != oddLabel || m.TrackerCount != 1 {
t.Errorf("multi-file torrent = %+v", m)
}
if s.Directory != dir {
t.Errorf("single-file directory = %q, want %q", s.Directory, dir)
}
// Torrents loaded stopped have not started yet.
if s.Age == 0 || s.Started != 0 {
t.Errorf("loaded stopped: Age = %d, Started = %d; want Age set and Started 0", s.Age, s.Started)
}
transfers, err := rt.TransfersContext(ctx)
if err != nil {
t.Fatal(err)
}
if !slices.Contains(transfers, Transfer{Hash: singleHash}) || !slices.Contains(transfers, Transfer{Hash: multiHash}) {
t.Errorf("Transfers = %+v, want both new torrents, with nothing transferred", transfers)
}
if err := rt.TrackersContext(ctx, Torrents{s, m}); err != nil {
t.Fatal(err)
}
if s.Tracker == nil || s.Tracker.Host != "tracker.invalid:6969" {
t.Errorf("tracker = %v", s.Tracker)
}
all, err := rt.TorrentsContext(ctx)
if err != nil {
t.Fatal(err)
}
if got := find(all, singleHash); got.Tracker == nil || got.Name != singleName {
t.Errorf("Torrents gave %+v", got)
}
got, err := rt.GetTorrentContext(ctx, singleHash)
if err != nil || got.Name != singleName || got.Tracker == nil {
t.Errorf("GetTorrent = %+v, %v", got, err)
}
if _, err := rt.GetTorrentContext(ctx, strings.Repeat("0", 40)); err == nil {
t.Error("GetTorrent found a torrent that is not loaded")
}
files, err := rt.FilesContext(ctx, multiHash)
if err != nil {
t.Fatal(err)
}
if len(files) != 2 || files[0].Path != "a.txt" || files[1].Path != "sub/b.txt" || files[1].Size != 20 || files[0].Priority != FileNormal {
t.Fatalf("files = %+v", files)
}
if err := rt.SetFilePrioritiesContext(ctx, multiHash, map[int]FilePriority{0: FileSkip, 1: FileHigh}); err != nil {
t.Fatal(err)
}
if files, err = rt.FilesContext(ctx, multiHash); err != nil || files[0].Priority != FileSkip || files[1].Priority != FileHigh {
t.Fatalf("files after setting priorities = %+v, %v", files, err)
}
if err := rt.SetLabelContext(ctx, "rtapi%20relabeled", m); err != nil {
t.Fatal(err)
}
if got, err := rt.GetTorrentContext(ctx, multiHash); err != nil {
t.Error(err)
} else if got.Label != "rtapi%20relabeled" {
t.Errorf("label after SetLabel = %q", got.Label)
}
down, up, err := rt.GlobalLimitsContext(ctx)
if err != nil {
t.Fatal(err)
}
if err := rt.SetGlobalLimitsContext(ctx, 3<<20, 1<<20); err != nil {
t.Fatal(err)
}
if newDown, newUp, err := rt.GlobalLimitsContext(ctx); err != nil || newDown != 3<<20 || newUp != 1<<20 {
t.Errorf("limits = %d, %d, %v", newDown, newUp, err)
}
if err := rt.SetGlobalLimitsContext(ctx, down, up); err != nil {
t.Fatal(err)
}
if stats, err := rt.StatsContext(ctx); err != nil || stats.PID <= 0 {
t.Errorf("Stats = %+v, %v; want rTorrent's process ID", stats, err)
}
if _, _, err := rt.SpeedsContext(ctx); err != nil {
t.Error(err)
}
// rTorrent learns where a torrent's data is only when it opens the
// torrent, so until then it reports no free space.
if free, err := rt.FreeDiskSpaceContext(ctx, singleHash); err != nil || free != 0 {
t.Errorf("free disk space of an unopened torrent = %d, %v; want 0", free, err)
}
if err := rt.StartContext(ctx, s); err != nil {
t.Fatal(err)
}
if free, err := rt.FreeDiskSpaceContext(ctx, singleHash); err != nil || free == 0 {
t.Errorf("free disk space of a started torrent = %d, %v", free, err)
}
if started := waitStarted(t, rt, singleHash); started < s.Age {
t.Errorf("Started = %d, before Age %d", started, s.Age)
}
if free, err := rt.FreeDiskSpacesContext(ctx, singleHash, multiHash); err != nil || len(free) != 2 || free[0] == 0 || free[1] != 0 {
t.Errorf("free disk space of a started and an unopened torrent = %v, %v", free, err)
}
// p.multicall nested in the list works on a real rTorrent; with no
// peers to reach, there are no connections.
if count, err := rt.ConnectionsContext(ctx); err != nil || count != (Connections{}) {
t.Errorf("Connections = %+v, %v", count, err)
}
for _, mutate := range []func(context.Context, ...*Torrent) error{rt.StopContext, rt.CheckContext} {
if err := mutate(ctx, s); err != nil {
t.Error(err)
}
}
if err := rt.DeleteMetadataContext(ctx, s, m); err != nil {
t.Fatal(err)
}
hashes, err := rt.HashesContext(ctx)
if err != nil || slices.Contains(hashes, singleHash) || slices.Contains(hashes, multiHash) {
t.Fatalf("after DeleteMetadata, hashes = %v, %v", hashes, err)
}
}
// waitLoaded waits for rTorrent to list every one of hashes; it loads
// torrents in the background.
func waitLoaded(t *testing.T, rt *Rtorrent, hashes ...string) {
t.Helper()
deadline := time.Now().Add(10 * time.Second)
for {
loaded, err := rt.Hashes()
if err != nil {
t.Fatal(err)
}
if !slices.ContainsFunc(hashes, func(hash string) bool { return !slices.Contains(loaded, hash) }) {
return
}
if time.Now().After(deadline) {
t.Fatalf("the torrents did not load; loaded: %v", loaded)
}
time.Sleep(100 * time.Millisecond)
}
}
// waitStarted waits for rTorrent to record that the torrent with hash has
// started, which it does once the torrent's first hash check is done, and
// returns when it started.
func waitStarted(t *testing.T, rt *Rtorrent, hash string) uint64 {
t.Helper()
deadline := time.Now().Add(10 * time.Second)
for {
torrent, err := rt.GetTorrent(hash)
if err != nil {
t.Fatal(err)
}
if torrent.Started != 0 {
return torrent.Started
}
if time.Now().After(deadline) {
t.Fatalf("rTorrent did not record that %s started", hash)
}
time.Sleep(100 * time.Millisecond)
}
}
// TestIntegrationAcrossRestart checks what an rTorrent restart keeps, in two
// runs that CI makes around one. With RTAPI_TEST_RESTART=before, it loads
// and starts a torrent and leaves it loaded; with after, it checks that
// rTorrent kept when the torrent started but loaded it anew, and removes it.
func TestIntegrationAcrossRestart(t *testing.T) {
address, phase := os.Getenv("RTAPI_TEST_RTORRENT"), os.Getenv("RTAPI_TEST_RESTART")
if address == "" || phase == "" {
t.Skip("set RTAPI_TEST_RTORRENT, and RTAPI_TEST_RESTART to before and then after an rTorrent restart, to run")
}
ctx := context.Background()
rt, err := NewRtorrentContext(ctx, address)
if err != nil {
t.Fatal(err)
}
data, hash := testTorrent(map[string]any{"name": "rtapi restart probe", "length": 1000}, "http://tracker.invalid:6969/announce")
switch phase {
case "before":
if err := rt.DownloadRawContext(ctx, data, &DotTorrentWithOptions{Stopped: true}); err != nil {
t.Fatal(err)
}
waitLoaded(t, rt, hash)
if err := rt.StartContext(ctx, &Torrent{Hash: hash}); err != nil {
t.Fatal(err)
}
waitStarted(t, rt, hash)
// Save the torrent's session now, rather than rely on the shutdown.
if _, err := rt.call(ctx, "d.save_full_session", newStringValue(hash)); err != nil {
t.Fatal(err)
}
case "after":
torrent, err := rt.GetTorrentContext(ctx, hash)
if err != nil {
t.Fatalf("the torrent did not survive the restart: %v", err)
}
t.Cleanup(func() { rt.DeleteMetadata(torrent) })
if torrent.Started == 0 || torrent.Age <= torrent.Started {
t.Fatalf("after the restart, Started = %d and Age = %d; want Started kept and Age later", torrent.Started, torrent.Age)
}
default:
t.Fatalf("RTAPI_TEST_RESTART = %q; want before or after", phase)
}
}