MCPcopy
hub / github.com/restic/restic / testRewindReader

Function testRewindReader

internal/backend/rewind_reader_test.go:71–175  ·  view source on GitHub ↗
(t *testing.T, fn func() RewindReader, data []byte)

Source from the content-addressed store, hash-verified

69}
70
71func 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 },

Callers 2

TestByteReaderFunction · 0.85
TestFileReaderFunction · 0.85

Calls 13

testFunction · 0.85
NowMethod · 0.80
FatalfMethod · 0.80
FatalMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.80
LogfMethod · 0.65
LengthMethod · 0.65
ReadFullMethod · 0.65
RewindMethod · 0.65
HashMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected