| 69 | } |
| 70 | |
| 71 | func testRewindReader(t *testing.T, fn func() RewindReader, data []byte) { |
| 72 | seed := time.Now().UnixNano() |
| 73 | t.Logf("seed is %d", seed) |
| 74 | rnd := rand.New(rand.NewSource(seed)) |
| 75 | |
| 76 | type ReaderTestFunc func(t testing.TB, r RewindReader, data []byte) |
| 77 | var tests = []ReaderTestFunc{ |
| 78 | func(t testing.TB, rd RewindReader, data []byte) { |
| 79 | if rd.Length() != int64(len(data)) { |
| 80 | t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length()) |
| 81 | } |
| 82 | |
| 83 | buf := make([]byte, len(data)) |
| 84 | _, err := io.ReadFull(rd, buf) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | |
| 89 | if !bytes.Equal(buf, data) { |
| 90 | t.Fatalf("wrong data returned") |
| 91 | } |
| 92 | |
| 93 | if rd.Length() != int64(len(data)) { |
| 94 | t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length()) |
| 95 | } |
| 96 | |
| 97 | err = rd.Rewind() |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | |
| 102 | if rd.Length() != int64(len(data)) { |
| 103 | t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length()) |
| 104 | } |
| 105 | |
| 106 | buf2 := make([]byte, int64(len(data))) |
| 107 | _, err = io.ReadFull(rd, buf2) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | |
| 112 | if !bytes.Equal(buf2, data) { |
| 113 | t.Fatalf("wrong data returned") |
| 114 | } |
| 115 | |
| 116 | if rd.Length() != int64(len(data)) { |
| 117 | t.Fatalf("wrong length returned, want %d, got %d", int64(len(data)), rd.Length()) |
| 118 | } |
| 119 | |
| 120 | if rd.Hash() != nil { |
| 121 | hasher := md5.New() |
| 122 | // must never fail according to interface |
| 123 | _, _ = hasher.Write(buf2) |
| 124 | if !bytes.Equal(rd.Hash(), hasher.Sum(nil)) { |
| 125 | t.Fatal("hash does not match data") |
| 126 | } |
| 127 | } |
| 128 | }, |