-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathinterop_test.go
652 lines (581 loc) · 17.3 KB
/
interop_test.go
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package rsync_test
import (
"bytes"
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/BurntSushi/toml"
maincmd "github.com/gokrazy/rsync/internal/daemonmaincmd"
"github.com/gokrazy/rsync/internal/rsyncdconfig"
"github.com/gokrazy/rsync/internal/rsynctest"
"github.com/gokrazy/rsync/rsyncd"
"github.com/google/go-cmp/cmp"
)
func TestMain(m *testing.M) {
if len(os.Args) > 1 && os.Args[1] == "localhost" {
// Strip first 2 args (./rsync.test localhost) from command line:
// rsync(1) is calling this process as a remote shell.
os.Args = os.Args[2:]
if err := maincmd.Main(context.Background(), os.Args, os.Stdin, os.Stdout, os.Stderr, nil); err != nil {
log.Fatal(err)
}
} else {
os.Exit(m.Run())
}
}
func TestRsyncVersion(t *testing.T) {
// This function is not an actual test, just used to include the rsync
// version in test output.
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
"--version")
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
}
func TestModuleListing(t *testing.T) {
tmp := t.TempDir()
// start a server to sync from
srv := rsynctest.New(t, rsynctest.InteropModule(tmp))
// request module list
var buf bytes.Buffer
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"--port="+srv.Port,
"rsync://localhost")
rsync.Stdout = &buf
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
output := buf.String()
if want := "interop\tinterop"; !strings.Contains(output, want) {
t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output)
}
}
func TestInterop(t *testing.T) {
tmp := t.TempDir()
source := filepath.Join(tmp, "source")
dest := filepath.Join(tmp, "dest")
// create files in source to be copied
if err := os.MkdirAll(source, 0755); err != nil {
t.Fatal(err)
}
dummy := filepath.Join(source, "dummy")
want := []byte("heyo")
if err := os.WriteFile(dummy, want, 0644); err != nil {
t.Fatal(err)
}
linkToDummy := filepath.Join(source, "link_to_dummy")
if err := os.Symlink("dummy", linkToDummy); err != nil {
t.Fatal(err)
}
if os.Getuid() == 0 {
rsynctest.CreateDummyDeviceFiles(t, source)
}
// start a server to sync from
srv := rsynctest.New(t, rsynctest.InteropModule(source))
// {
// config := filepath.Join(tmp, "rsyncd.conf")
// rsyncdConfig := `
// use chroot = no
// # 0 = no limit
// max connections = 0
// pid file = ` + tmp + `/rsyncd.pid
// exclude = lost+found/
// transfer logging = yes
// timeout = 900
// ignore nonreadable = yes
// dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 *.zst
// [interop]
// path = /home/michael/i3/docs
// #` + source + `
// comment = interop
// read only = yes
// list = true
// `
// if err := os.WriteFile(config, []byte(rsyncdConfig), 0644); err != nil {
// t.Fatal(err)
// }
// srv := exec.Command("rsync",
// "--daemon",
// "--config="+config,
// "--verbose",
// "--address=localhost",
// "--no-detach",
// "--port=8730")
// srv.Stdout = os.Stdout
// srv.Stderr = os.Stderr
// if err := srv.Start(); err != nil {
// t.Fatal(err)
// }
// go func() {
// if err := srv.Wait(); err != nil {
// t.Error(err)
// }
// }()
// defer srv.Process.Kill()
//
// time.Sleep(1 * time.Second)
// }
// dry run (slight differences in protocol)
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"--port="+srv.Port,
"--dry-run",
"rsync://localhost/interop/", // copy contents of interop
//source+"/", // sync from local directory
dest) // directly into dest
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
// sync into dest dir
rsync = exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"--port="+srv.Port,
"rsync://localhost/interop/", // copy contents of interop
//source+"/", // sync from local directory
dest) // directly into dest
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
{
got, err := os.ReadFile(filepath.Join(dest, "dummy"))
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff)
}
}
{
got, err := os.Readlink(filepath.Join(dest, "link_to_dummy"))
if err != nil {
t.Fatal(err)
}
if want := "dummy"; got != want {
t.Fatalf("unexpected symlink target: got %q, want %q", got, want)
}
}
if os.Getuid() == 0 {
rsynctest.VerifyDummyDeviceFiles(t, source, dest)
}
// Run rsync again. This should not modify any files, but will result in
// rsync sending sums to the sender.
rsync = exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
// "--debug=all4",
"--archive",
// TODO: should this be --checksum instead?
"--ignore-times", // disable rsync’s “quick check”
"-v", "-v", "-v", "-v",
"--port="+srv.Port,
"rsync://localhost/interop/", // copy contents of interop
//source+"/", // sync from local directory
dest) // directly into dest
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
}
func createSourceFiles(t *testing.T) (string, string, string) {
t.Helper()
tmp := t.TempDir()
source := filepath.Join(tmp, "source")
dest := filepath.Join(tmp, "dest")
// create files in source to be copied
subDirs := []string{"expensive", "cheap"}
for _, subdir := range subDirs {
dummy := filepath.Join(source, subdir, "dummy")
if err := os.MkdirAll(filepath.Dir(dummy), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(dummy, []byte(subdir), 0644); err != nil {
t.Fatal(err)
}
}
return tmp, source, dest
}
func sourcesArgs(t *testing.T) []string {
if strings.HasPrefix(rsynctest.RsyncVersion(t), "3.") {
// rsync 3.0.0 (March 2008) introduced multiple source args.
return []string{
"rsync://localhost/interop/expensive/", // copy contents of interop
"rsync://localhost/interop/cheap", // copy cheap directory
}
}
// Older rsync only supports a single source arg.
return []string{
"rsync://localhost/interop/expensive/", // copy contents of interop
}
}
func sourceFullySyncedTo(t *testing.T, dest string) error {
{
want := []byte("expensive")
got, err := os.ReadFile(filepath.Join(dest, "dummy"))
if err != nil {
return err
}
if diff := cmp.Diff(want, got); diff != "" {
return fmt.Errorf("unexpected file contents: diff (-want +got):\n%s", diff)
}
}
if !strings.HasPrefix(rsynctest.RsyncVersion(t), "3.") {
return nil
}
{
want := []byte("cheap")
got, err := os.ReadFile(filepath.Join(dest, "cheap", "dummy"))
if err != nil {
return err
}
if diff := cmp.Diff(want, got); diff != "" {
return fmt.Errorf("unexpected file contents: diff (-want +got):\n%s", diff)
}
}
return nil
}
func TestInteropSubdir(t *testing.T) {
_, source, dest := createSourceFiles(t)
// start a server to sync from
srv := rsynctest.New(t, rsynctest.InteropModule(source))
// sync into dest dir
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"--port=" + srv.Port,
}, sourcesArgs(t)...),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
if err := sourceFullySyncedTo(t, dest); err != nil {
t.Fatal(err)
}
}
func TestInteropSubdirExclude(t *testing.T) {
_, source, dest := createSourceFiles(t)
// start a server to sync from
srv := rsynctest.New(t, rsynctest.InteropModule(source))
// sync into dest dir
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
// TODO: implement support for include rules
//"-f", "+ *.o",
// NOTE: Using -f is the more modern replacement
// for using --exclude like so:
//"--exclude=dummy",
"-f", "- expensive",
"-v", "-v", "-v", "-v",
"--port=" + srv.Port,
}, "rsync://localhost/interop/"),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
expensiveFn := filepath.Join(dest, "expensive", "dummy")
if _, err := os.ReadFile(expensiveFn); !os.IsNotExist(err) {
t.Fatalf("ReadFile(%s) did not return -ENOENT, but %v", expensiveFn, err)
}
cheapFn := filepath.Join(dest, "cheap", "dummy")
if _, err := os.ReadFile(cheapFn); err != nil {
t.Fatalf("ReadFile(%s): %v", cheapFn, err)
}
}
func TestInteropSubdirExcludeMultipleNested(t *testing.T) {
_, source, dest := createSourceFiles(t)
nested := filepath.Join(source, "nested")
// create files in source to be copied
subDirs := []string{"nested-expensive", "nested-cheap"}
for _, subdir := range subDirs {
dummy := filepath.Join(nested, subdir, "dummy")
if err := os.MkdirAll(filepath.Dir(dummy), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(dummy, []byte(subdir), 0644); err != nil {
t.Fatal(err)
}
}
// start a server to sync from
srv := rsynctest.New(t, rsynctest.InteropModule(source))
// sync into dest dir
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
// TODO: implement support for include rules
//"-f", "+ *.o",
// NOTE: Using -f is the more modern replacement
// for using --exclude like so:
//"--exclude=dummy",
"-f", "- nested/nested-expensive",
"-f", "- nested/nested-cheap",
"-v", "-v", "-v", "-v",
"--port=" + srv.Port,
}, "rsync://localhost/interop/"),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
expensiveFn := filepath.Join(dest, "nested", "nested-expensive", "dummy")
if _, err := os.ReadFile(expensiveFn); !os.IsNotExist(err) {
t.Fatalf("ReadFile(%s) did not return -ENOENT, but %v", expensiveFn, err)
}
cheapFn := filepath.Join(dest, "nested", "nested-cheap", "dummy")
if _, err := os.ReadFile(cheapFn); !os.IsNotExist(err) {
t.Fatalf("ReadFile(%s) did not return -ENOENT, but %v", cheapFn, err)
}
}
func TestInteropRemoteCommand(t *testing.T) {
_, source, dest := createSourceFiles(t)
sourcesArgs := []string{
"localhost:" + source + "/expensive/", // copy contents of interop
}
if strings.HasPrefix(rsynctest.RsyncVersion(t), "3.") {
sourcesArgs = append(sourcesArgs, ":"+source+"/cheap") // copy cheap directory
}
// sync into dest dir
rsync := exec.Command("rsync", //*/ "/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
"--protocol=27",
"-v", "-v", "-v", "-v",
"-e", os.Args[0],
}, sourcesArgs...),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
if err := sourceFullySyncedTo(t, dest); err != nil {
t.Fatal(err)
}
}
func TestInteropRemoteDaemon(t *testing.T) {
tmp, source, dest := createSourceFiles(t)
homeDir := filepath.Join(tmp, "home")
// Use os.Setenv so that the os.UserConfigDir() call below returns the
// correct path.
os.Setenv("HOME", homeDir)
os.Setenv("XDG_CONFIG_HOME", homeDir+"/.config")
{
// in remote daemon mode, rsync needs a config file, so we create one and
// set the HOME environment variable such that gokr-rsyncd will pick it up.
cfg := rsyncdconfig.Config{
Modules: []rsyncd.Module{
{Name: "interop", Path: source},
},
}
configDir, err := os.UserConfigDir()
if err != nil {
t.Fatal(err)
}
configPath := filepath.Join(configDir, "gokr-rsyncd.toml")
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
t.Fatal(err)
}
f, err := os.Create(configPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
if err := toml.NewEncoder(f).Encode(&cfg); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
}
// TODO: this does not seem to work when using openrsync?
// does openrsync send the wrong command?
// sync into dest dir
rsync := exec.Command("rsync", //*/ "/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"-e", os.Args[0],
}, sourcesArgs(t)...),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
// TODO: does os.Environ() reflect changes by os.Setenv()?
rsync.Env = append(os.Environ(),
"HOME="+homeDir,
"XDG_CONFIG_HOME="+homeDir+"/.config")
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
if err := sourceFullySyncedTo(t, dest); err != nil {
t.Fatal(err)
}
}
func TestInteropRemoteDaemonAnonSSH(t *testing.T) {
tmp, source, dest := createSourceFiles(t)
// start a server to sync from
srv := rsynctest.New(t,
rsynctest.InteropModule(source),
rsynctest.Listeners([]rsyncdconfig.Listener{
{AnonSSH: "localhost:0"},
}))
// ensure the user running the tests (root when doing the privileged run!)
// has an SSH private key:
privKeyPath := filepath.Join(tmp, "ssh_private_key")
genKey := exec.Command("ssh-keygen",
"-N", "",
"-t", "ed25519",
"-f", privKeyPath)
genKey.Stdout = os.Stdout
genKey.Stderr = os.Stderr
if err := genKey.Run(); err != nil {
t.Fatalf("%v: %v", genKey.Args, err)
}
// sync into dest dir
rsync := exec.Command("rsync", //*/ "/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"-e", "ssh -vv -o IdentityFile=" + privKeyPath + " -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -p " + srv.Port,
}, sourcesArgs(t)...),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
if err := sourceFullySyncedTo(t, dest); err != nil {
t.Fatal(err)
}
}
func TestInteropRemoteDaemonAuthorizedSSHFail(t *testing.T) {
tmp, source, dest := createSourceFiles(t)
// ensure the user running the tests (root when doing the privileged run!)
// has an SSH private key:
privKeyPath := filepath.Join(tmp, "ssh_private_key")
genKey := exec.Command("ssh-keygen",
"-N", "",
"-t", "ed25519",
"-f", privKeyPath)
genKey.Stdout = os.Stdout
genKey.Stderr = os.Stderr
if err := genKey.Run(); err != nil {
t.Fatalf("%v: %v", genKey.Args, err)
}
authorizedKeysPath := filepath.Join(tmp, "authorized_keys")
if err := os.WriteFile(authorizedKeysPath, []byte("# no keys authorized"), 0644); err != nil {
t.Fatal(err)
}
// start a server to sync from
srv := rsynctest.New(t,
rsynctest.InteropModule(source),
rsynctest.Listeners([]rsyncdconfig.Listener{
{
AuthorizedSSH: rsyncdconfig.SSHListener{
Address: "localhost:0",
AuthorizedKeys: authorizedKeysPath,
},
},
}))
// sync into dest dir
rsync := exec.Command("rsync", //*/ "/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"-e", "ssh -vv -o IdentityFile=" + privKeyPath + " -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -p " + srv.Port,
}, sourcesArgs(t)...),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err == nil {
t.Fatalf("rsync unexpectedly succeeded")
}
}
func TestInteropRemoteDaemonAuthorizedSSHPass(t *testing.T) {
tmp, source, dest := createSourceFiles(t)
// ensure the user running the tests (root when doing the privileged run!)
// has an SSH private key:
privKeyPath := filepath.Join(tmp, "ssh_private_key")
genKey := exec.Command("ssh-keygen",
"-N", "",
"-t", "ed25519",
"-f", privKeyPath)
genKey.Stdout = os.Stdout
genKey.Stderr = os.Stderr
if err := genKey.Run(); err != nil {
t.Fatalf("%v: %v", genKey.Args, err)
}
authorizedKeysPath := filepath.Join(tmp, "authorized_keys")
pubKey, err := os.ReadFile(privKeyPath + ".pub")
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(authorizedKeysPath, pubKey, 0644); err != nil {
t.Fatal(err)
}
// start a server to sync from
srv := rsynctest.New(t,
rsynctest.InteropModule(source),
rsynctest.Listeners([]rsyncdconfig.Listener{
{
AuthorizedSSH: rsyncdconfig.SSHListener{
Address: "localhost:0",
AuthorizedKeys: authorizedKeysPath,
},
},
}))
// sync into dest dir
rsync := exec.Command("rsync", //*/ "/home/michael/src/openrsync/openrsync",
append(
append([]string{
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"-e", "ssh -vv -o IdentityFile=" + privKeyPath + " -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -p " + srv.Port,
}, sourcesArgs(t)...),
dest)...)
rsync.Stdout = os.Stdout
rsync.Stderr = os.Stderr
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
if err := sourceFullySyncedTo(t, dest); err != nil {
t.Fatal(err)
}
}