forked from CorentinB/warc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_test.go
304 lines (261 loc) · 7.3 KB
/
read_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package warc
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"go.uber.org/goleak"
)
func testFileHash(t *testing.T, path string) {
t.Logf("checking 'WARC-Block-Digest' on %q", path)
file, err := os.Open(path)
if err != nil {
t.Fatalf("failed to open %q: %v", path, err)
}
defer file.Close()
reader, err := NewReader(file)
if err != nil {
t.Fatalf("warc.NewReader failed for %q: %v", path, err)
}
for {
record, eol, err := reader.ReadRecord()
if eol {
break
}
if err != nil {
t.Fatalf("failed to read all record content: %v", err)
break
}
hash := fmt.Sprintf("sha1:%s", GetSHA1(record.Content))
if hash != record.Header["WARC-Block-Digest"] {
record.Content.Close()
t.Fatalf("expected %s, got %s", record.Header.Get("WARC-Block-Digest"), hash)
}
record.Content.Close()
}
}
func testFileScan(t *testing.T, path string) {
file, err := os.Open(path)
if err != nil {
t.Fatalf("failed to open %q: %v", path, err)
}
defer file.Close()
reader, err := NewReader(file)
if err != nil {
t.Fatalf("warc.NewReader failed for %q: %v", path, err)
}
total := 0
for {
_, eol, err := reader.ReadRecord()
if eol {
break
}
if err != nil {
t.Fatalf("failed to read all record content: %v", err)
break
}
total++
}
if total != 3 {
t.Fatalf("expected 3 records, got %v", total)
}
}
func testFileSingleHashCheck(t *testing.T, path string, hash string, expectedContentLength []string, expectedTotal int) int {
// The below function validates the Block-Digest per record while the function we are in checks for a specific Payload-Digest in records :)
testFileHash(t, path)
file, err := os.Open(path)
if err != nil {
t.Fatalf("failed to open %q: %v", path, err)
}
defer file.Close()
t.Logf("checking 'WARC-Payload-Digest' on %q", path)
reader, err := NewReader(file)
if err != nil {
t.Fatalf("warc.NewReader failed for %q: %v", path, err)
}
totalRead := 0
for {
record, eol, err := reader.ReadRecord()
if eol {
if expectedTotal == -1 {
// This is expected for multiple file WARCs as we need to count the total count outside of this function.
return totalRead
}
if totalRead == expectedTotal {
// We've read the expected amount and reached the end of the WARC file. Time to break out.
break
} else {
t.Fatalf("unexpected number of records read, read: %d but expected: %d", totalRead, expectedTotal)
return -1
}
}
if err != nil {
record.Content.Close()
t.Fatalf("warc.ReadRecord failed: %v", err)
break
}
if record.Header.Get("WARC-Type") != "response" && record.Header.Get("WARC-Type") != "revisit" {
// We're not currently interesting in anything but response and revisit records at the moment.
record.Content.Close()
continue
}
if record.Header.Get("WARC-Payload-Digest") != hash {
record.Content.Close()
t.Fatalf("WARC-Payload-Digest doesn't match intended result %s != %s", record.Header.Get("WARC-Payload-Digest"), hash)
}
badContentLength := false
for i := 0; i < len(expectedContentLength); i++ {
if record.Header.Get("Content-Length") != expectedContentLength[i] {
badContentLength = true
} else {
badContentLength = false
break
}
}
if badContentLength {
record.Content.Close()
t.Fatalf("Content-Length doesn't match intended result %s != %s", record.Header.Get("Content-Length"), expectedContentLength)
}
record.Content.Close()
totalRead++
}
return -1
}
func testFileRevisitVailidity(t *testing.T, path string, originalTime string, originalDigest string, shouldBeEmpty bool) {
var revisitRecordsFound = false
file, err := os.Open(path)
if err != nil {
t.Fatalf("failed to open %q: %v", path, err)
}
defer file.Close()
t.Logf("checking 'WARC-Refers-To-Date' and 'WARC-Payload-Digest' for revisits on %q", path)
reader, err := NewReader(file)
if err != nil {
t.Fatalf("warc.NewReader failed for %q: %v", path, err)
}
for {
record, eol, err := reader.ReadRecord()
if eol {
if revisitRecordsFound {
return
}
if shouldBeEmpty {
t.Logf("No revisit records found. That's expected for this test.")
break
}
t.Fatalf("No revisit records found.")
break
}
if err != nil {
record.Content.Close()
t.Fatalf("warc.ReadRecord failed: %v", err)
break
}
if record.Header.Get("WARC-Type") != "response" && record.Header.Get("WARC-Type") != "revisit" {
// We're not currently interesting in anything but response and revisit records at the moment.
record.Content.Close()
continue
}
if record.Header.Get("WARC-Type") == "response" {
originalDigest = record.Header.Get("WARC-Payload-Digest")
originalTime = record.Header.Get("WARC-Date")
record.Content.Close()
continue
}
if record.Header.Get("WARC-Type") == "revisit" {
revisitRecordsFound = true
if record.Header.Get("WARC-Payload-Digest") == originalDigest && record.Header.Get("WARC-Refers-To-Date") == originalTime {
record.Content.Close()
continue
} else {
record.Content.Close()
t.Fatalf("Revisit digest or date does not match doesn't match intended result %s != %s (or %s != %s)", record.Header.Get("WARC-Payload-Digest"), originalDigest, record.Header.Get("WARC-Refers-To-Date"), originalTime)
}
}
}
}
func testFileEarlyEOF(t *testing.T, path string) {
file, err := os.Open(path)
if err != nil {
t.Fatalf("failed to open %q: %v", path, err)
}
reader, err := NewReader(file)
if err != nil {
t.Fatalf("warc.NewReader failed for %q: %v", path, err)
}
// read the file into memory
data, err := io.ReadAll(reader.bufReader)
if err != nil {
t.Fatalf("failed to read %q: %v", path, err)
}
// delete the last two bytes (\r\n)
if data[len(data)-2] != '\r' || data[len(data)-1] != '\n' {
t.Fatalf("expected \\r\\n, got %q", data[len(data)-2:])
}
data = data[:len(data)-2]
// new reader
reader, err = NewReader(io.NopCloser(bytes.NewReader(data)))
if err != nil {
t.Fatalf("warc.NewReader failed for %q: %v", path, err)
}
// read the records
for {
_, eol, err := reader.ReadRecord()
if eol {
break
}
if err != nil {
if strings.Contains(err.Error(), "early EOF record boundary") {
return // ok
} else {
t.Fatalf("expected early EOF record boundary, got %v", err)
}
}
}
t.Fatalf("expected early EOF record boundary, got none")
}
func TestReader(t *testing.T) {
defer goleak.VerifyNone(t)
var paths = []string{
"testdata/test.warc.gz",
}
for _, path := range paths {
testFileHash(t, path)
testFileScan(t, path)
testFileEarlyEOF(t, path)
}
}
func BenchmarkBasicRead(b *testing.B) {
// default test warc location
path := "testdata/test.warc.gz"
for n := 0; n < b.N; n++ {
b.Logf("checking 'WARC-Block-Digest' on %q", path)
file, err := os.Open(path)
if err != nil {
b.Fatalf("failed to open %q: %v", path, err)
}
defer file.Close()
reader, err := NewReader(file)
if err != nil {
b.Fatalf("warc.NewReader failed for %q: %v", path, err)
}
for {
record, eol, err := reader.ReadRecord()
if eol {
break
}
if err != nil {
b.Fatalf("failed to read all record content: %v", err)
break
}
hash := fmt.Sprintf("sha1:%s", GetSHA1(record.Content))
if hash != record.Header["WARC-Block-Digest"] {
record.Content.Close()
b.Fatalf("expected %s, got %s", record.Header.Get("WARC-Block-Digest"), hash)
}
record.Content.Close()
}
}
}