| 1 | package interop_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/BurntSushi/toml" |
| 15 | "github.com/gokrazy/rsync/internal/rsyncdconfig" |
| 16 | "github.com/gokrazy/rsync/internal/rsynctest" |
| 17 | "github.com/gokrazy/rsync/internal/testlogger" |
| 18 | "github.com/gokrazy/rsync/rsyncd" |
| 19 | "github.com/google/go-cmp/cmp" |
| 20 | ) |
| 21 | |
| 22 | func TestMain(m *testing.M) { |
| 23 | if err := rsynctest.CommandMain(m); err != nil { |
| 24 | log.Fatal(err) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestRsyncVersion(t *testing.T) { |
| 29 | // This function is not an actual test, just used to include the rsync |
| 30 | // version in test output. |
| 31 | rsync := exec.Command(rsynctest.AnyRsync(t), "--version") |
| 32 | rsync.Stdout = testlogger.New(t) |
| 33 | rsync.Stderr = testlogger.New(t) |
| 34 | if err := rsync.Run(); err != nil { |
| 35 | t.Fatalf("%v: %v", rsync.Args, err) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestTridgeRsyncVersion(t *testing.T) { |
| 40 | // This function is not an actual test, just used to include the rsync |
| 41 | // version in test output. |
| 42 | rsyncBin := rsynctest.TridgeOrGTFO(t, "--version") |
| 43 | tridgeRsync := exec.Command(rsyncBin, "--version") |
| 44 | tridgeRsync.Stdout = testlogger.New(t) |
| 45 | tridgeRsync.Stderr = testlogger.New(t) |
| 46 | if err := tridgeRsync.Run(); err != nil { |
| 47 | t.Fatalf("%v: %v", tridgeRsync.Args, err) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestModuleListingServer(t *testing.T) { |
| 52 | t.Parallel() |
| 53 | |
| 54 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: add reason") |
| 55 | |
| 56 | tmp := t.TempDir() |
| 57 | |
| 58 | // start a server to sync from |
| 59 | srv := rsynctest.New(t, rsynctest.InteropModule(tmp)) |
| 60 | |
| 61 | // request module list |
| 62 | var buf bytes.Buffer |
| 63 | rsync := exec.Command(rsyncBin, |
| 64 | // "--debug=all4", |
| 65 | "--archive", |
| 66 | "-v", "-v", "-v", "-v", |
| 67 | "--port="+srv.Port, |
| 68 | "rsync://localhost") |
| 69 | rsync.Stdout = &buf |
| 70 | rsync.Stderr = testlogger.New(t) |
| 71 | if err := rsync.Run(); err != nil { |
| 72 | t.Fatalf("%v: %v", rsync.Args, err) |
| 73 | } |
| 74 | |
| 75 | output := buf.String() |
| 76 | if want := "interop\tinterop"; !strings.Contains(output, want) { |
| 77 | t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestModuleListingClient(t *testing.T) { |
| 82 | t.Parallel() |
| 83 | |
| 84 | tmp := t.TempDir() |
| 85 | |
| 86 | // start a server to sync from |
| 87 | srv := rsynctest.New(t, rsynctest.InteropModule(tmp)) |
| 88 | |
| 89 | // request module list |
| 90 | args := []string{ |
| 91 | "gokr-rsync", |
| 92 | "-aH", |
| 93 | "rsync://localhost:" + srv.Port + "/", |
| 94 | } |
| 95 | stdout, _ := rsynctest.Output(t, args...) |
| 96 | |
| 97 | if want := "interop\tinterop"; !strings.Contains(string(stdout), want) { |
| 98 | t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, string(stdout)) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestModuleListingClientPort(t *testing.T) { |
| 103 | t.Parallel() |
| 104 | |
| 105 | tmp := t.TempDir() |
| 106 | |
| 107 | // start a server to sync from |
| 108 | srv := rsynctest.New(t, rsynctest.InteropModule(tmp)) |
| 109 | |
| 110 | // request module list |
| 111 | args := []string{ |
| 112 | "gokr-rsync", |
| 113 | "-aH", |
| 114 | "--port=" + srv.Port, |
| 115 | "rsync://localhost/", |
| 116 | } |
| 117 | stdout, _ := rsynctest.Output(t, args...) |
| 118 | |
| 119 | if want := "interop\tinterop"; !strings.Contains(string(stdout), want) { |
| 120 | t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, string(stdout)) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestModuleContentsListingDirs(t *testing.T) { |
| 125 | t.Parallel() |
| 126 | |
| 127 | tmp := t.TempDir() |
| 128 | source := filepath.Join(tmp, "source") |
| 129 | |
| 130 | // create files in source to be copied |
| 131 | if err := os.MkdirAll(source, 0755); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | dummy := filepath.Join(source, "dummy") |
| 135 | want := []byte("heyo") |
| 136 | if err := os.WriteFile(dummy, want, 0644); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | |
| 140 | // start a server to sync from |
| 141 | srv := rsynctest.New(t, rsynctest.InteropModule(tmp)) |
| 142 | |
| 143 | // request module content listing |
| 144 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 145 | "--dirs", |
| 146 | "--list-only", |
| 147 | "--port="+srv.Port, |
| 148 | "rsync://localhost/interop") |
| 149 | rsync.Stdout = testlogger.New(t) |
| 150 | rsync.Stderr = testlogger.New(t) |
| 151 | if err := rsync.Run(); err != nil { |
| 152 | t.Fatalf("%v: %v", rsync.Args, err) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestInterop(t *testing.T) { |
| 157 | t.Parallel() |
| 158 | |
| 159 | tmp := t.TempDir() |
| 160 | source := filepath.Join(tmp, "source") |
| 161 | dest := filepath.Join(tmp, "dest") |
| 162 | |
| 163 | // create files in source to be copied |
| 164 | if err := os.MkdirAll(source, 0755); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | dummy := filepath.Join(source, "dummy") |
| 168 | want := []byte("heyo") |
| 169 | if err := os.WriteFile(dummy, want, 0644); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | |
| 173 | linkToDummy := filepath.Join(source, "link_to_dummy") |
| 174 | if err := os.Symlink("dummy", linkToDummy); err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | |
| 178 | if os.Getuid() == 0 { |
| 179 | rsynctest.CreateDummyDeviceFiles(t, source) |
| 180 | } |
| 181 | |
| 182 | // start a server to sync from |
| 183 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 184 | |
| 185 | // { |
| 186 | // config := filepath.Join(tmp, "rsyncd.conf") |
| 187 | // rsyncdConfig := ` |
| 188 | // use chroot = no |
| 189 | // # 0 = no limit |
| 190 | // max connections = 0 |
| 191 | // pid file = ` + tmp + `/rsyncd.pid |
| 192 | // exclude = lost+found/ |
| 193 | // transfer logging = yes |
| 194 | // timeout = 900 |
| 195 | // ignore nonreadable = yes |
| 196 | // dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 *.zst |
| 197 | |
| 198 | // [interop] |
| 199 | // path = /home/michael/i3/docs |
| 200 | // #` + source + ` |
| 201 | // comment = interop |
| 202 | // read only = yes |
| 203 | // list = true |
| 204 | |
| 205 | // ` |
| 206 | // if err := os.WriteFile(config, []byte(rsyncdConfig), 0644); err != nil { |
| 207 | // t.Fatal(err) |
| 208 | // } |
| 209 | // srv := exec.Command(rsynctest.AnyRsync(t), |
| 210 | // "--daemon", |
| 211 | // "--config="+config, |
| 212 | // "--verbose", |
| 213 | // "--address=localhost", |
| 214 | // "--no-detach", |
| 215 | // "--port=8730") |
| 216 | // srv.Stdout = os.Stdout |
| 217 | // srv.Stderr = os.Stderr |
| 218 | // if err := srv.Start(); err != nil { |
| 219 | // t.Fatal(err) |
| 220 | // } |
| 221 | // go func() { |
| 222 | // if err := srv.Wait(); err != nil { |
| 223 | // t.Error(err) |
| 224 | // } |
| 225 | // }() |
| 226 | // defer srv.Process.Kill() |
| 227 | // |
| 228 | // time.Sleep(1 * time.Second) |
| 229 | // } |
| 230 | |
| 231 | // dry run (slight differences in protocol) |
| 232 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 233 | // "--debug=all4", |
| 234 | "--archive", |
| 235 | "-v", "-v", "-v", "-v", |
| 236 | "--port="+srv.Port, |
| 237 | "--dry-run", |
| 238 | "rsync://localhost/interop/", // copy contents of interop |
| 239 | //source+"/", // sync from local directory |
| 240 | filepath.Base(dest)) // directly into dest |
| 241 | rsync.Dir = filepath.Dir(dest) |
| 242 | rsync.Stdout = testlogger.New(t) |
| 243 | rsync.Stderr = testlogger.New(t) |
| 244 | if err := rsync.Run(); err != nil { |
| 245 | t.Fatalf("%v: %v", rsync.Args, err) |
| 246 | } |
| 247 | |
| 248 | // sync into dest dir |
| 249 | rsync = exec.Command(rsynctest.AnyRsync(t), |
| 250 | // "--debug=all4", |
| 251 | "--archive", |
| 252 | "-v", "-v", "-v", "-v", |
| 253 | "--port="+srv.Port, |
| 254 | "rsync://localhost/interop/", // copy contents of interop |
| 255 | //source+"/", // sync from local directory |
| 256 | filepath.Base(dest)) // directly into dest |
| 257 | rsync.Dir = filepath.Dir(dest) |
| 258 | rsync.Stdout = testlogger.New(t) |
| 259 | rsync.Stderr = testlogger.New(t) |
| 260 | if err := rsync.Run(); err != nil { |
| 261 | t.Fatalf("%v: %v", rsync.Args, err) |
| 262 | } |
| 263 | |
| 264 | { |
| 265 | got, err := os.ReadFile(filepath.Join(dest, "dummy")) |
| 266 | if err != nil { |
| 267 | t.Fatal(err) |
| 268 | } |
| 269 | if diff := cmp.Diff(want, got); diff != "" { |
| 270 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Symlinks work differently on Windows. |
| 275 | if runtime.GOOS != "windows" { |
| 276 | got, err := os.Readlink(filepath.Join(dest, "link_to_dummy")) |
| 277 | if err != nil { |
| 278 | t.Fatal(err) |
| 279 | } |
| 280 | if want := "dummy"; got != want { |
| 281 | t.Fatalf("unexpected symlink target: got %q, want %q", got, want) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if os.Getuid() == 0 { |
| 286 | rsynctest.VerifyDummyDeviceFiles(t, source, dest) |
| 287 | } |
| 288 | |
| 289 | // Run rsync again. This should not modify any files, but will result in |
| 290 | // rsync sending sums to the sender. |
| 291 | rsync = exec.Command(rsynctest.AnyRsync(t), |
| 292 | // "--debug=all4", |
| 293 | "--archive", |
| 294 | // TODO: should this be --checksum instead? |
| 295 | "--ignore-times", // disable rsync’s “quick check” |
| 296 | "-v", "-v", "-v", "-v", |
| 297 | "--port="+srv.Port, |
| 298 | "rsync://localhost/interop/", // copy contents of interop |
| 299 | //source+"/", // sync from local directory |
| 300 | filepath.Base(dest)) // directly into dest |
| 301 | rsync.Dir = filepath.Dir(dest) |
| 302 | rsync.Stdout = testlogger.New(t) |
| 303 | rsync.Stderr = testlogger.New(t) |
| 304 | if err := rsync.Run(); err != nil { |
| 305 | t.Fatalf("%v: %v", rsync.Args, err) |
| 306 | } |
| 307 | |
| 308 | } |
| 309 | |
| 310 | func createSourceFiles(t *testing.T) (string, string, string) { |
| 311 | t.Helper() |
| 312 | |
| 313 | tmp := t.TempDir() |
| 314 | source := filepath.Join(tmp, "source") |
| 315 | dest := filepath.Join(tmp, "dest") |
| 316 | |
| 317 | // create files in source to be copied |
| 318 | subDirs := []string{"expensive", "cheap"} |
| 319 | for _, subdir := range subDirs { |
| 320 | dummy := filepath.Join(source, subdir, "dummy") |
| 321 | if err := os.MkdirAll(filepath.Dir(dummy), 0755); err != nil { |
| 322 | t.Fatal(err) |
| 323 | } |
| 324 | if err := os.WriteFile(dummy, []byte(subdir), 0644); err != nil { |
| 325 | t.Fatal(err) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return tmp, source, dest |
| 330 | } |
| 331 | |
| 332 | func sourcesArgs(t *testing.T) []string { |
| 333 | if strings.HasPrefix(rsynctest.RsyncVersion(t), "3.") { |
| 334 | // rsync 3.0.0 (March 2008) introduced multiple source args. |
| 335 | return []string{ |
| 336 | "rsync://localhost/interop/expensive/", // copy contents of interop |
| 337 | "rsync://localhost/interop/cheap", // copy cheap directory |
| 338 | } |
| 339 | } |
| 340 | // Older rsync only supports a single source arg. |
| 341 | return []string{ |
| 342 | "rsync://localhost/interop/expensive/", // copy contents of interop |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | func sourceFullySyncedTo(t *testing.T, dest string) error { |
| 347 | { |
| 348 | want := []byte("expensive") |
| 349 | got, err := os.ReadFile(filepath.Join(dest, "dummy")) |
| 350 | if err != nil { |
| 351 | return err |
| 352 | } |
| 353 | if diff := cmp.Diff(want, got); diff != "" { |
| 354 | return fmt.Errorf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if !strings.HasPrefix(rsynctest.RsyncVersion(t), "3.") { |
| 359 | return nil |
| 360 | } |
| 361 | |
| 362 | { |
| 363 | want := []byte("cheap") |
| 364 | got, err := os.ReadFile(filepath.Join(dest, "cheap", "dummy")) |
| 365 | if err != nil { |
| 366 | return err |
| 367 | } |
| 368 | if diff := cmp.Diff(want, got); diff != "" { |
| 369 | return fmt.Errorf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 370 | } |
| 371 | } |
| 372 | return nil |
| 373 | } |
| 374 | |
| 375 | func TestInteropSingleFile(t *testing.T) { |
| 376 | t.Parallel() |
| 377 | |
| 378 | _, source, dest := createSourceFiles(t) |
| 379 | |
| 380 | // start a server to sync from |
| 381 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 382 | |
| 383 | // sync into dest dir |
| 384 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 385 | append( |
| 386 | []string{ |
| 387 | // "--debug=all4", |
| 388 | "--archive", |
| 389 | "-v", "-v", "-v", "-v", |
| 390 | "--port=" + srv.Port, |
| 391 | "rsync://localhost/interop/expensive/dummy", |
| 392 | }, |
| 393 | filepath.Base(dest)+"/")...) |
| 394 | rsync.Dir = filepath.Dir(dest) |
| 395 | rsync.Stdout = testlogger.New(t) |
| 396 | rsync.Stderr = testlogger.New(t) |
| 397 | if err := rsync.Run(); err != nil { |
| 398 | t.Fatalf("%v: %v", rsync.Args, err) |
| 399 | } |
| 400 | |
| 401 | { |
| 402 | want := []byte("expensive") |
| 403 | got, err := os.ReadFile(filepath.Join(dest, "dummy")) |
| 404 | if err != nil { |
| 405 | t.Fatal(err) |
| 406 | } |
| 407 | if diff := cmp.Diff(want, got); diff != "" { |
| 408 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Like TestInteropSingleFile, but without a trailing slash |
| 414 | // in the dest argument (no subdirectory to create). |
| 415 | func TestInteropSingleFileNoSlash(t *testing.T) { |
| 416 | t.Parallel() |
| 417 | |
| 418 | _, source, dest := createSourceFiles(t) |
| 419 | |
| 420 | // start a server to sync from |
| 421 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 422 | |
| 423 | // sync into dest dir |
| 424 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 425 | append( |
| 426 | []string{ |
| 427 | // "--debug=all4", |
| 428 | "--archive", |
| 429 | "-v", "-v", "-v", "-v", |
| 430 | "--port=" + srv.Port, |
| 431 | "rsync://localhost/interop/expensive/dummy", |
| 432 | }, |
| 433 | filepath.Base(dest))...) |
| 434 | rsync.Dir = filepath.Dir(dest) |
| 435 | rsync.Stdout = testlogger.New(t) |
| 436 | rsync.Stderr = testlogger.New(t) |
| 437 | if err := rsync.Run(); err != nil { |
| 438 | t.Fatalf("%v: %v", rsync.Args, err) |
| 439 | } |
| 440 | |
| 441 | { |
| 442 | want := []byte("expensive") |
| 443 | got, err := os.ReadFile(dest) |
| 444 | if err != nil { |
| 445 | t.Fatal(err) |
| 446 | } |
| 447 | if diff := cmp.Diff(want, got); diff != "" { |
| 448 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | func TestInteropSubdir(t *testing.T) { |
| 454 | t.Parallel() |
| 455 | |
| 456 | _, source, dest := createSourceFiles(t) |
| 457 | |
| 458 | // start a server to sync from |
| 459 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 460 | |
| 461 | // sync into dest dir |
| 462 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 463 | append( |
| 464 | append([]string{ |
| 465 | // "--debug=all4", |
| 466 | "--archive", |
| 467 | "-v", "-v", "-v", "-v", |
| 468 | "--port=" + srv.Port, |
| 469 | }, sourcesArgs(t)...), |
| 470 | filepath.Base(dest))...) |
| 471 | rsync.Dir = filepath.Dir(dest) |
| 472 | rsync.Stdout = testlogger.New(t) |
| 473 | rsync.Stderr = testlogger.New(t) |
| 474 | if err := rsync.Run(); err != nil { |
| 475 | t.Fatalf("%v: %v", rsync.Args, err) |
| 476 | } |
| 477 | |
| 478 | if err := sourceFullySyncedTo(t, dest); err != nil { |
| 479 | t.Fatal(err) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | func TestInteropSubdirNested(t *testing.T) { |
| 484 | t.Parallel() |
| 485 | |
| 486 | _, source, dest := createSourceFiles(t) |
| 487 | |
| 488 | // Move the "expensive" directory one level deeper: |
| 489 | // now "treasure/expensive". |
| 490 | treasure := filepath.Join(source, "treasure") |
| 491 | if err := os.MkdirAll(treasure, 0755); err != nil { |
| 492 | t.Fatal(err) |
| 493 | } |
| 494 | if err := os.Rename(filepath.Join(source, "expensive"), filepath.Join(treasure, "expensive")); err != nil { |
| 495 | t.Fatal(err) |
| 496 | } |
| 497 | |
| 498 | // start a server to sync from |
| 499 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 500 | |
| 501 | // sync into dest dir |
| 502 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 503 | append( |
| 504 | []string{ |
| 505 | // "--debug=all4", |
| 506 | "--archive", |
| 507 | "-v", "-v", "-v", "-v", |
| 508 | "--port=" + srv.Port, |
| 509 | "rsync://localhost/interop/treasure/expensive/", // copy contents of expensive |
| 510 | }, |
| 511 | filepath.Base(dest))...) |
| 512 | rsync.Dir = filepath.Dir(dest) |
| 513 | rsync.Stdout = testlogger.New(t) |
| 514 | rsync.Stderr = testlogger.New(t) |
| 515 | if err := rsync.Run(); err != nil { |
| 516 | t.Fatalf("%v: %v", rsync.Args, err) |
| 517 | } |
| 518 | |
| 519 | want := []byte("expensive") |
| 520 | got, err := os.ReadFile(filepath.Join(dest, "dummy")) |
| 521 | if err != nil { |
| 522 | t.Fatal(err) |
| 523 | } |
| 524 | if diff := cmp.Diff(want, got); diff != "" { |
| 525 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | func TestInteropSubdirExclude(t *testing.T) { |
| 530 | t.Parallel() |
| 531 | |
| 532 | _, source, dest := createSourceFiles(t) |
| 533 | |
| 534 | // start a server to sync from |
| 535 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 536 | |
| 537 | // sync into dest dir |
| 538 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 539 | append( |
| 540 | append([]string{ |
| 541 | // "--debug=all4", |
| 542 | "--archive", |
| 543 | // TODO: implement support for include rules |
| 544 | //"-f", "+ *.o", |
| 545 | // NOTE: Using -f is the more modern replacement |
| 546 | // for using --exclude like so: |
| 547 | //"--exclude=dummy", |
| 548 | "-f", "- expensive", |
| 549 | "-v", "-v", "-v", "-v", |
| 550 | "--port=" + srv.Port, |
| 551 | }, "rsync://localhost/interop/"), |
| 552 | filepath.Base(dest))...) |
| 553 | rsync.Dir = filepath.Dir(dest) |
| 554 | rsync.Stdout = testlogger.New(t) |
| 555 | rsync.Stderr = testlogger.New(t) |
| 556 | if err := rsync.Run(); err != nil { |
| 557 | t.Fatalf("%v: %v", rsync.Args, err) |
| 558 | } |
| 559 | |
| 560 | expensiveFn := filepath.Join(dest, "expensive", "dummy") |
| 561 | if _, err := os.ReadFile(expensiveFn); !os.IsNotExist(err) { |
| 562 | t.Fatalf("ReadFile(%s) did not return -ENOENT, but %v", expensiveFn, err) |
| 563 | } |
| 564 | cheapFn := filepath.Join(dest, "cheap", "dummy") |
| 565 | if _, err := os.ReadFile(cheapFn); err != nil { |
| 566 | t.Fatalf("ReadFile(%s): %v", cheapFn, err) |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | func TestInteropSubdirExcludeGokrazy(t *testing.T) { |
| 571 | t.Parallel() |
| 572 | |
| 573 | _, source, dest := createSourceFiles(t) |
| 574 | |
| 575 | // start a server to sync from |
| 576 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 577 | |
| 578 | // sync into dest dir |
| 579 | args := []string{ |
| 580 | "gokr-rsync", |
| 581 | "-f", "- expensive", |
| 582 | "-avH", |
| 583 | "rsync://localhost:" + srv.Port + "/interop/", |
| 584 | dest, |
| 585 | } |
| 586 | rsynctest.Run(t, args...) |
| 587 | |
| 588 | expensiveFn := filepath.Join(dest, "expensive", "dummy") |
| 589 | if _, err := os.ReadFile(expensiveFn); !os.IsNotExist(err) { |
| 590 | t.Fatalf("ReadFile(%s) did not return -ENOENT, but %v", expensiveFn, err) |
| 591 | } |
| 592 | cheapFn := filepath.Join(dest, "cheap", "dummy") |
| 593 | if _, err := os.ReadFile(cheapFn); err != nil { |
| 594 | t.Fatalf("ReadFile(%s): %v", cheapFn, err) |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | func TestInteropSubdirExcludeMultipleNested(t *testing.T) { |
| 599 | t.Parallel() |
| 600 | |
| 601 | _, source, dest := createSourceFiles(t) |
| 602 | |
| 603 | nested := filepath.Join(source, "nested") |
| 604 | |
| 605 | // create files in source to be copied |
| 606 | subDirs := []string{"nested-expensive", "nested-cheap"} |
| 607 | for _, subdir := range subDirs { |
| 608 | dummy := filepath.Join(nested, subdir, "dummy") |
| 609 | if err := os.MkdirAll(filepath.Dir(dummy), 0755); err != nil { |
| 610 | t.Fatal(err) |
| 611 | } |
| 612 | if err := os.WriteFile(dummy, []byte(subdir), 0644); err != nil { |
| 613 | t.Fatal(err) |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | // start a server to sync from |
| 618 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 619 | |
| 620 | // sync into dest dir |
| 621 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 622 | append( |
| 623 | append([]string{ |
| 624 | // "--debug=all4", |
| 625 | "--archive", |
| 626 | // TODO: implement support for include rules |
| 627 | //"-f", "+ *.o", |
| 628 | // NOTE: Using -f is the more modern replacement |
| 629 | // for using --exclude like so: |
| 630 | //"--exclude=dummy", |
| 631 | "-f", "- nested/nested-expensive", |
| 632 | "-f", "- nested/nested-cheap", |
| 633 | "-v", "-v", "-v", "-v", |
| 634 | "--port=" + srv.Port, |
| 635 | }, "rsync://localhost/interop/"), |
| 636 | filepath.Base(dest))...) |
| 637 | rsync.Dir = filepath.Dir(dest) |
| 638 | rsync.Stdout = testlogger.New(t) |
| 639 | rsync.Stderr = testlogger.New(t) |
| 640 | if err := rsync.Run(); err != nil { |
| 641 | t.Fatalf("%v: %v", rsync.Args, err) |
| 642 | } |
| 643 | |
| 644 | expensiveFn := filepath.Join(dest, "nested", "nested-expensive", "dummy") |
| 645 | if _, err := os.ReadFile(expensiveFn); !os.IsNotExist(err) { |
| 646 | t.Fatalf("ReadFile(%s) did not return -ENOENT, but %v", expensiveFn, err) |
| 647 | } |
| 648 | cheapFn := filepath.Join(dest, "nested", "nested-cheap", "dummy") |
| 649 | if _, err := os.ReadFile(cheapFn); !os.IsNotExist(err) { |
| 650 | t.Fatalf("ReadFile(%s) did not return -ENOENT, but %v", cheapFn, err) |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | func TestInteropRemoteCommand(t *testing.T) { |
| 655 | t.Parallel() |
| 656 | |
| 657 | if runtime.GOOS == "windows" { |
| 658 | t.Skip("stdin not supported on Windows") |
| 659 | } |
| 660 | |
| 661 | _, source, dest := createSourceFiles(t) |
| 662 | |
| 663 | sourcesArgs := []string{ |
| 664 | "localhost:" + source + "/expensive/", // copy contents of interop |
| 665 | } |
| 666 | if strings.HasPrefix(rsynctest.RsyncVersion(t), "3.") { |
| 667 | sourcesArgs = append(sourcesArgs, ":"+source+"/cheap") // copy cheap directory |
| 668 | } |
| 669 | |
| 670 | // sync into dest dir |
| 671 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 672 | append( |
| 673 | append([]string{ |
| 674 | // "--debug=all4", |
| 675 | "--archive", |
| 676 | "--protocol=27", |
| 677 | "-v", "-v", "-v", "-v", |
| 678 | "-e", `"` + os.Args[0] + `"`, |
| 679 | }, sourcesArgs...), |
| 680 | filepath.Base(dest))...) |
| 681 | rsync.Dir = filepath.Dir(dest) |
| 682 | rsync.Stdout = testlogger.New(t) |
| 683 | rsync.Stderr = testlogger.New(t) |
| 684 | if err := rsync.Run(); err != nil { |
| 685 | t.Fatalf("%v: %v", rsync.Args, err) |
| 686 | } |
| 687 | |
| 688 | if err := sourceFullySyncedTo(t, dest); err != nil { |
| 689 | t.Fatal(err) |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | func TestInteropRemoteDaemon(t *testing.T) { |
| 694 | t.Parallel() |
| 695 | |
| 696 | if runtime.GOOS == "windows" { |
| 697 | t.Skip("stdin not supported on Windows") |
| 698 | } |
| 699 | |
| 700 | rsyncBin := rsynctest.TridgeOrGTFO(t, "https://github.com/gokrazy/rsync/issues/33") |
| 701 | |
| 702 | tmp, source, dest := createSourceFiles(t) |
| 703 | |
| 704 | homeDir := filepath.Join(tmp, "home") |
| 705 | // Use os.Setenv so that the os.UserConfigDir() call below returns the |
| 706 | // correct path. |
| 707 | os.Setenv("HOME", homeDir) |
| 708 | os.Setenv("XDG_CONFIG_HOME", homeDir+"/.config") |
| 709 | |
| 710 | { |
| 711 | // in remote daemon mode, rsync needs a config file, so we create one and |
| 712 | // set the HOME environment variable such that gokr-rsyncd will pick it up. |
| 713 | cfg := rsyncdconfig.Config{ |
| 714 | Modules: []rsyncd.Module{ |
| 715 | {Name: "interop", Path: source}, |
| 716 | }, |
| 717 | } |
| 718 | configDir, err := os.UserConfigDir() |
| 719 | if err != nil { |
| 720 | t.Fatal(err) |
| 721 | } |
| 722 | configPath := filepath.Join(configDir, "gokr-rsyncd.toml") |
| 723 | if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil { |
| 724 | t.Fatal(err) |
| 725 | } |
| 726 | f, err := os.Create(configPath) |
| 727 | if err != nil { |
| 728 | t.Fatal(err) |
| 729 | } |
| 730 | defer f.Close() |
| 731 | if err := toml.NewEncoder(f).Encode(&cfg); err != nil { |
| 732 | t.Fatal(err) |
| 733 | } |
| 734 | if err := f.Close(); err != nil { |
| 735 | t.Fatal(err) |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | // sync into dest dir |
| 740 | rsync := exec.Command(rsyncBin, |
| 741 | append( |
| 742 | append([]string{ |
| 743 | // "--debug=all4", |
| 744 | "--archive", |
| 745 | "-v", "-v", "-v", "-v", |
| 746 | "-e", `"` + os.Args[0] + `"`, |
| 747 | }, sourcesArgs(t)...), |
| 748 | filepath.Base(dest))...) |
| 749 | rsync.Dir = filepath.Dir(dest) |
| 750 | rsync.Stdout = testlogger.New(t) |
| 751 | rsync.Stderr = testlogger.New(t) |
| 752 | // TODO: does os.Environ() reflect changes by os.Setenv()? |
| 753 | rsync.Env = append(os.Environ(), |
| 754 | "HOME="+homeDir, |
| 755 | "XDG_CONFIG_HOME="+homeDir+"/.config") |
| 756 | if err := rsync.Run(); err != nil { |
| 757 | t.Fatalf("%v: %v", rsync.Args, err) |
| 758 | } |
| 759 | |
| 760 | if err := sourceFullySyncedTo(t, dest); err != nil { |
| 761 | t.Fatal(err) |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | func TestInteropRemoteDaemonSSH(t *testing.T) { |
| 766 | t.Parallel() |
| 767 | |
| 768 | if runtime.GOOS == "windows" { |
| 769 | t.Skip("/dev/null not supported on Windows") |
| 770 | } |
| 771 | |
| 772 | // ensure the user running the tests (root when doing the privileged run!) |
| 773 | // has an SSH private key: |
| 774 | privKeyPath := filepath.Join(t.TempDir(), "ssh_private_key") |
| 775 | genKey := exec.Command("ssh-keygen", |
| 776 | "-N", "", |
| 777 | "-t", "ed25519", |
| 778 | "-f", privKeyPath) |
| 779 | genKey.Stdout = testlogger.New(t) |
| 780 | genKey.Stderr = testlogger.New(t) |
| 781 | if err := genKey.Run(); err != nil { |
| 782 | t.Fatalf("%v: %v", genKey.Args, err) |
| 783 | } |
| 784 | |
| 785 | t.Run("Anon", func(t *testing.T) { |
| 786 | t.Parallel() |
| 787 | |
| 788 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 789 | |
| 790 | _, source, dest := createSourceFiles(t) |
| 791 | |
| 792 | // start a server to sync from |
| 793 | srv := rsynctest.New(t, |
| 794 | rsynctest.InteropModule(source), |
| 795 | rsynctest.Listeners([]rsyncdconfig.Listener{ |
| 796 | { |
| 797 | AnonSSH: "localhost:0", |
| 798 | HostKeyPath: filepath.Join(t.TempDir(), "gokr-rsyncd", "ssh_host_ed25519_key"), |
| 799 | }, |
| 800 | })) |
| 801 | |
| 802 | // sync into dest dir |
| 803 | rsync := exec.Command(rsyncBin, |
| 804 | append( |
| 805 | append([]string{ |
| 806 | // "--debug=all4", |
| 807 | "--archive", |
| 808 | "-v", "-v", "-v", "-v", |
| 809 | "-e", "ssh -o IdentityFile=" + privKeyPath + " -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -p " + srv.Port, |
| 810 | }, sourcesArgs(t)...), |
| 811 | filepath.Base(dest))...) |
| 812 | rsync.Dir = filepath.Dir(dest) |
| 813 | // Ensure SSH_* environment variables (like SSH_ASKPASS or |
| 814 | // SSH_AUTH_SOCK) do not leak into the test, otherwise tests |
| 815 | // might be interrupted by a text UI password prompt. |
| 816 | rsync.Env = []string{} // non-nil, but empty |
| 817 | rsync.Stdout = testlogger.New(t) |
| 818 | rsync.Stderr = testlogger.New(t) |
| 819 | if err := rsync.Run(); err != nil { |
| 820 | t.Fatalf("%v: %v", rsync.Args, err) |
| 821 | } |
| 822 | |
| 823 | if err := sourceFullySyncedTo(t, dest); err != nil { |
| 824 | t.Fatal(err) |
| 825 | } |
| 826 | }) |
| 827 | |
| 828 | t.Run("AuthorizedFail", func(t *testing.T) { |
| 829 | t.Parallel() |
| 830 | |
| 831 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 832 | |
| 833 | tmp, source, dest := createSourceFiles(t) |
| 834 | |
| 835 | authorizedKeysPath := filepath.Join(tmp, "authorized_keys") |
| 836 | if err := os.WriteFile(authorizedKeysPath, []byte("# no keys authorized"), 0644); err != nil { |
| 837 | t.Fatal(err) |
| 838 | } |
| 839 | |
| 840 | // start a server to sync from |
| 841 | srv := rsynctest.New(t, |
| 842 | rsynctest.InteropModule(source), |
| 843 | rsynctest.Listeners([]rsyncdconfig.Listener{ |
| 844 | { |
| 845 | AuthorizedSSH: rsyncdconfig.SSHListener{ |
| 846 | Address: "localhost:0", |
| 847 | AuthorizedKeys: authorizedKeysPath, |
| 848 | }, |
| 849 | }, |
| 850 | })) |
| 851 | |
| 852 | // sync into dest dir |
| 853 | rsync := exec.Command(rsyncBin, |
| 854 | append( |
| 855 | append([]string{ |
| 856 | // "--debug=all4", |
| 857 | "--archive", |
| 858 | "-v", "-v", "-v", "-v", |
| 859 | "-e", "ssh -o IdentityFile=" + privKeyPath + " -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -p " + srv.Port, |
| 860 | }, sourcesArgs(t)...), |
| 861 | filepath.Base(dest))...) |
| 862 | rsync.Dir = filepath.Dir(dest) |
| 863 | rsync.Stdout = testlogger.New(t) |
| 864 | rsync.Stderr = testlogger.New(t) |
| 865 | if err := rsync.Run(); err == nil { |
| 866 | t.Fatalf("rsync unexpectedly succeeded") |
| 867 | } |
| 868 | }) |
| 869 | |
| 870 | t.Run("AuthorizedPass", func(t *testing.T) { |
| 871 | t.Parallel() |
| 872 | |
| 873 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 874 | |
| 875 | tmp, source, dest := createSourceFiles(t) |
| 876 | |
| 877 | authorizedKeysPath := filepath.Join(tmp, "authorized_keys") |
| 878 | pubKey, err := os.ReadFile(privKeyPath + ".pub") |
| 879 | if err != nil { |
| 880 | t.Fatal(err) |
| 881 | } |
| 882 | if err := os.WriteFile(authorizedKeysPath, pubKey, 0644); err != nil { |
| 883 | t.Fatal(err) |
| 884 | } |
| 885 | |
| 886 | // start a server to sync from |
| 887 | srv := rsynctest.New(t, |
| 888 | rsynctest.InteropModule(source), |
| 889 | rsynctest.Listeners([]rsyncdconfig.Listener{ |
| 890 | { |
| 891 | AuthorizedSSH: rsyncdconfig.SSHListener{ |
| 892 | Address: "localhost:0", |
| 893 | AuthorizedKeys: authorizedKeysPath, |
| 894 | }, |
| 895 | }, |
| 896 | })) |
| 897 | |
| 898 | // sync into dest dir |
| 899 | rsync := exec.Command(rsyncBin, |
| 900 | append( |
| 901 | append([]string{ |
| 902 | // "--debug=all4", |
| 903 | "--archive", |
| 904 | "-v", "-v", "-v", "-v", |
| 905 | "-e", "ssh -o IdentityFile=" + privKeyPath + " -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -p " + srv.Port, |
| 906 | }, sourcesArgs(t)...), |
| 907 | filepath.Base(dest))...) |
| 908 | rsync.Dir = filepath.Dir(dest) |
| 909 | rsync.Stdout = testlogger.New(t) |
| 910 | rsync.Stderr = testlogger.New(t) |
| 911 | if err := rsync.Run(); err != nil { |
| 912 | t.Fatalf("%v: %v", rsync.Args, err) |
| 913 | } |
| 914 | |
| 915 | if err := sourceFullySyncedTo(t, dest); err != nil { |
| 916 | t.Fatal(err) |
| 917 | } |
| 918 | }) |
| 919 | } |