| 1 | package sync_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "regexp" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/gokrazy/rsync/internal/rsynctest" |
| 14 | "github.com/gokrazy/rsync/internal/testlogger" |
| 15 | ) |
| 16 | |
| 17 | func TestMain(m *testing.M) { |
| 18 | rsynctest.CommandMain(m) |
| 19 | } |
| 20 | |
| 21 | var statsTransferRe = regexp.MustCompile(`^sent ([0-9,]+) bytes received ([0-9,]+) bytes ([0-9,.]+) bytes/sec$`) |
| 22 | |
| 23 | func TestSyncExtended(t *testing.T) { |
| 24 | t.Parallel() |
| 25 | |
| 26 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 27 | |
| 28 | tmp := t.TempDir() |
| 29 | source := filepath.Join(tmp, "source") |
| 30 | dest := filepath.Join(tmp, "dest") |
| 31 | destLarge := filepath.Join(dest, "large-data-file") |
| 32 | |
| 33 | headPattern := []byte{0x11} |
| 34 | bodyPattern := []byte{0xbb} |
| 35 | endPattern := []byte{0xee} |
| 36 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 37 | |
| 38 | // start a server to sync from |
| 39 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 40 | |
| 41 | sync := func() int64 { |
| 42 | rsync := exec.Command(rsyncBin, |
| 43 | // "--debug=all4", |
| 44 | "--archive", |
| 45 | // A verbosity level of 3 is enough, any higher than that and rsync |
| 46 | // will start listing individual chunk matches. |
| 47 | "-v", "-v", "-v", // "-v", |
| 48 | "--checksum", |
| 49 | "--port="+srv.Port, |
| 50 | "rsync://localhost/interop/", // copy contents of source |
| 51 | filepath.Base(dest)) |
| 52 | rsync.Dir = filepath.Dir(dest) |
| 53 | rsync.Env = append(os.Environ(), |
| 54 | // Ensure rsync does not localize decimal separators and fractional |
| 55 | // points based on the current locale: |
| 56 | "LANG=C.UTF-8") |
| 57 | var buf bytes.Buffer |
| 58 | rsync.Stdout = &buf |
| 59 | rsync.Stderr = testlogger.New(t) |
| 60 | if err := rsync.Run(); err != nil { |
| 61 | t.Fatalf("%v: %v", rsync.Args, err) |
| 62 | } |
| 63 | |
| 64 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | |
| 68 | totalRead := int64(-1) |
| 69 | for _, line := range strings.Split(buf.String(), "\n") { |
| 70 | // log.Printf("rsync output line: %q", line) |
| 71 | if strings.HasPrefix(line, "sent ") { |
| 72 | // e.g.: |
| 73 | // sent 1,590 bytes received 18 bytes 3,216.00 bytes/sec |
| 74 | // total size is 1,188,046 speedup is 738.83 |
| 75 | matches := statsTransferRe.FindStringSubmatch(line) |
| 76 | if len(matches) == 0 { |
| 77 | t.Fatalf("could not parse rsync 'sent' line") |
| 78 | } |
| 79 | |
| 80 | var err error |
| 81 | totalRead, err = strconv.ParseInt(strings.ReplaceAll(matches[2], ",", ""), 0, 64) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return totalRead |
| 88 | } |
| 89 | |
| 90 | { |
| 91 | // initial sync into dest dir |
| 92 | totalRead := sync() |
| 93 | if got, want := totalRead, int64(3*1024); got < want { |
| 94 | t.Fatalf("rsync unexpectedly did not read the whole file over the network: got %d, want >= %d", got, want) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | { |
| 99 | // second sync (unmodified) into dest dir |
| 100 | totalRead := sync() |
| 101 | if got, want := totalRead, int64(512*1024); got >= want { |
| 102 | t.Fatalf("rsync unexpectedly transferred more data than needed: got %d, want < %d", got, want) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Change the middle of the large data file: |
| 107 | bodyPattern = []byte{0x66} |
| 108 | { |
| 109 | // modify the large data file |
| 110 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 111 | |
| 112 | // sync modifications into dest dir |
| 113 | totalRead := sync() |
| 114 | |
| 115 | // TODO: verify speedup value, compare to rsync and openrsync |
| 116 | |
| 117 | if got, want := totalRead, int64(2*1024*1024); got >= want { |
| 118 | t.Fatalf("rsync unexpectedly transferred more data than needed: got %d, want < %d", got, want) |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestSyncMultipleSources(t *testing.T) { |
| 124 | tmp := t.TempDir() |
| 125 | src1 := filepath.Join(tmp, "src1") |
| 126 | src2 := filepath.Join(tmp, "src2") |
| 127 | dest := filepath.Join(tmp, "dest") |
| 128 | if err := os.MkdirAll(src1, 0755); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | const hello = "world" |
| 132 | if err := os.WriteFile(filepath.Join(src1, "hello"), []byte(hello), 0644); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | if err := os.MkdirAll(src2, 0755); err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | const bye = "moon" |
| 139 | if err := os.WriteFile(filepath.Join(src2, "bye"), []byte(bye), 0644); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | |
| 143 | rsynctest.Run(t, "gokr-rsync", |
| 144 | "-av", |
| 145 | src1, |
| 146 | src2, |
| 147 | dest) |
| 148 | |
| 149 | hellob, err := os.ReadFile(filepath.Join(dest, "src1", "hello")) |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | if !bytes.Equal(hellob, []byte(hello)) { |
| 154 | t.Errorf("file 'hello' has unexpected contents: got %q, want %q", string(hellob), hello) |
| 155 | } |
| 156 | |
| 157 | byeb, err := os.ReadFile(filepath.Join(dest, "src2", "bye")) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | if !bytes.Equal(byeb, []byte(bye)) { |
| 162 | t.Errorf("file 'bye' has unexpected contents: got %q, want %q", string(byeb), bye) |
| 163 | } |
| 164 | } |