| 1 | package rsyncchecksum_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/gokrazy/rsync/internal/rsyncchecksum" |
| 10 | ) |
| 11 | |
| 12 | func constructLargeDataFile(headPattern, bodyPattern, endPattern []byte) []byte { |
| 13 | // create large data file in source directory to be copied |
| 14 | head := bytes.Repeat(headPattern, 1*1024*1024) |
| 15 | body := bytes.Repeat(bodyPattern, 1*1024*1024) |
| 16 | end := bytes.Repeat(endPattern, 1*1024*1024) |
| 17 | return append(append(head, body...), end...) |
| 18 | } |
| 19 | |
| 20 | func writeLargeDataFile(t *testing.T, source string, headPattern, bodyPattern, endPattern []byte) { |
| 21 | // create large data file in source directory to be copied |
| 22 | content := constructLargeDataFile(headPattern, bodyPattern, endPattern) |
| 23 | large := filepath.Join(source, "large-data-file") |
| 24 | if err := os.MkdirAll(filepath.Dir(large), 0755); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := os.WriteFile(large, content, 0644); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestSyncExtended(t *testing.T) { |
| 33 | tmp := t.TempDir() |
| 34 | source := filepath.Join(tmp, "source") |
| 35 | |
| 36 | writeLargeDataFile(t, source, []byte{0x11}, []byte{0xbb}, []byte{0xee}) |
| 37 | |
| 38 | // These values are taken from the rsync debug output: |
| 39 | const k = 1768 |
| 40 | want := make([]uint32, 1780) |
| 41 | for i := 0; i <= 592; i++ { |
| 42 | want[i] = 0xa5d47568 |
| 43 | } |
| 44 | want[593] = 0x23645688 |
| 45 | for i := 594; i <= 1185; i++ { |
| 46 | want[i] = 0x8c1c2378 |
| 47 | } |
| 48 | want[1186] = 0x12504720 |
| 49 | for i := 1187; i <= 1778; i++ { |
| 50 | want[i] = 0x7d9883b0 |
| 51 | } |
| 52 | want[1779] = 0x61b8dff0 |
| 53 | |
| 54 | sourceLarge := filepath.Join(source, "large-data-file") |
| 55 | f, err := os.Open(sourceLarge) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | defer f.Close() |
| 60 | buf := make([]byte, k) |
| 61 | for idx, wantChecksum := range want { |
| 62 | n, err := f.Read(buf) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | chunk := buf[:n] |
| 68 | sum := rsyncchecksum.Checksum1(chunk) |
| 69 | if sum != wantChecksum { |
| 70 | t.Fatalf("checksum calculation error: got %08x, want %08x (idx %d), chunk: %#v", sum, wantChecksum, idx, chunk) |
| 71 | } |
| 72 | } |
| 73 | } |