Debian Codegraph @

md5_test.go

raw
1 // Copyright (c) 2020 MinIO Inc. All rights reserved.
2 // Use of this source code is governed by a license that can be
3 // found in the LICENSE file.
4
5 package md5simd
6
7 import (
8 "bytes"
9 "crypto/md5"
10 "encoding/hex"
11 "fmt"
12 "hash"
13 "io"
14 "math/rand"
15 "runtime"
16 "sync"
17 "testing"
18 )
19
20 type md5Test struct {
21 in string
22 want string
23 }
24
25 var golden = []md5Test{
26 {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "014842d480b571495a4a0363793f7367"},
27 {"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "0b649bcb5a82868817fec9a6e709d233"},
28 {"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", "bcd5708ed79b18f0f0aaa27fd0056d86"},
29 {"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", "e987c862fbd2f2f0ca859cb8d7806bf3"},
30 {"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", "982731671f0cd82cafce8d96a98e7a48"},
31 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "baf13e8b16d8c06324d7c9ab32cb7ff0"},
32 {"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", "8ea3109cbd951bba1ace2f401a784ae4"},
33 {"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", "d141045bfb385cad357e7c39c60e5da0"},
34 {"", "d41d8cd98f00b204e9800998ecf8427e"},
35 {"a", "0cc175b9c0f1b6a831c399e269772661"},
36 {"ab", "187ef4436122d1cc2f40dc2b92f0eba0"},
37 {"abc", "900150983cd24fb0d6963f7d28e17f72"},
38 {"abcd", "e2fc714c4727ee9395f324cd2e7f331f"},
39 {"abcde", "ab56b4d92b40713acc5af89985d4b786"},
40 {"abcdef", "e80b5017098950fc58aad83c8c14978e"},
41 {"abcdefg", "7ac66c0f148de9519b8bd264312c4d64"},
42 {"abcdefgh", "e8dc4081b13434b45189a720b77b6818"},
43 {"abcdefghi", "8aa99b1f439ff71293e95357bac6fd94"},
44 {"abcdefghij", "a925576942e94b2ef57a066101b48876"},
45 {"Discard medicine more than two years old.", "d747fc1719c7eacb84058196cfe56d57"},
46 {"He who has a shady past knows that nice guys finish last.", "bff2dcb37ef3a44ba43ab144768ca837"},
47 {"I wouldn't marry him with a ten foot pole.", "0441015ecb54a7342d017ed1bcfdbea5"},
48 {"Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave", "9e3cac8e9e9757a60c3ea391130d3689"},
49 {"The days of the digital watch are numbered. -Tom Stoppard", "a0f04459b031f916a59a35cc482dc039"},
50 {"Nepal premier won't resign.", "e7a48e0fe884faf31475d2a04b1362cc"},
51 {"For every action there is an equal and opposite government program.", "637d2fe925c07c113800509964fb0e06"},
52 {"His money is twice tainted: 'taint yours and 'taint mine.", "834a8d18d5c6562119cf4c7f5086cb71"},
53 {"There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977", "de3a4d2fd6c73ec2db2abad23b444281"},
54 {"It's a tiny change to the code and not completely disgusting. - Bob Manchek", "acf203f997e2cf74ea3aff86985aefaf"},
55 {"size: a.out: bad magic", "e1c1384cb4d2221dfdd7c795a4222c9a"},
56 {"The major problem is with sendmail. -Mark Horton", "c90f3ddecc54f34228c063d7525bf644"},
57 {"Give me a rock, paper and scissors and I will move the world. CCFestoon", "cdf7ab6c1fd49bd9933c43f3ea5af185"},
58 {"If the enemy is within range, then so are you.", "83bc85234942fc883c063cbd7f0ad5d0"},
59 {"It's well we cannot hear the screams/That we create in others' dreams.", "277cbe255686b48dd7e8f389394d9299"},
60 {"You remind me of a TV show, but that's all right: I watch it anyway.", "fd3fb0a7ffb8af16603f3d3af98f8e1f"},
61 {"C is as portable as Stonehedge!!", "469b13a78ebf297ecda64d4723655154"},
62 {"Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley", "63eb3a2f466410104731c4b037600110"},
63 {"The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule", "72c2ed7592debca1c90fc0100f931a2f"},
64 {"How can you write a big system without C++? -Paul Glick", "132f7619d33b523b1d9e5bd8e0928355"},
65 {"", "d41d8cd98f00b204e9800998ecf8427e"},
66 }
67
68 func testGolden16(t *testing.T, megabyte int) {
69
70 server := NewServer()
71 h16 := [16]hash.Hash{}
72 input := [16][]byte{}
73 for i := range h16 {
74 h16[i] = server.NewHash()
75 input[i] = bytes.Repeat([]byte{0x61 + byte(i)}, megabyte*1024*1024)
76 }
77
78 for i := range h16 {
79 h16[i].Write(input[i])
80 }
81
82 for i := range h16 {
83 digest := h16[i].Sum([]byte{})
84 got := fmt.Sprintf("%x\n", digest)
85
86 h := md5.New()
87 h.Write(input[i])
88 want := fmt.Sprintf("%x\n", h.Sum(nil))
89
90 if got != want {
91 t.Errorf("TestGolden16[%d], got %v, want %v", i, got, want)
92 }
93 }
94 }
95
96 func TestGolden16(t *testing.T) {
97 t.Run("1MB", func(t *testing.T) {
98 testGolden16(t, 1)
99 })
100 t.Run("2MB", func(t *testing.T) {
101 testGolden16(t, 2)
102 })
103 }
104
105 func TestGolangGolden16(t *testing.T) {
106 server := NewServer()
107 defer server.Close()
108 h16 := [16]Hasher{}
109 for i := range h16 {
110 h16[i] = server.NewHash()
111 defer h16[i].Close()
112 }
113
114 // Skip first 8, so we even 2 rounds of 16 test vectors
115 golden16 := golden[8:]
116
117 for tc := 0; tc < len(golden16); tc += 16 {
118 for i := range h16 {
119 h16[i].Reset()
120 h16[i].Write([]byte(golden16[tc+i].in))
121 }
122
123 for i := range h16 {
124 digest := h16[i].Sum([]byte{})
125 if fmt.Sprintf("%x", digest) != golden16[tc+i].want {
126 t.Errorf("TestGolangGolden[%d], got %v, want %v, uid:%+v", tc+i, fmt.Sprintf("%x", digest), golden16[tc+i].want, h16[i])
127 }
128 }
129 }
130 }
131
132 func testMultipleSums(t *testing.T, incr, incr2 int) {
133 server := NewServer()
134 defer server.Close()
135
136 h := server.NewHash()
137 var tmp [Size]byte
138
139 h.Write(bytes.Repeat([]byte{0x61}, 64+incr))
140 digestMiddle1 := fmt.Sprintf("%x", h.Sum(tmp[:0]))
141 digestMiddle1b := fmt.Sprintf("%x", h.Sum(tmp[:0]))
142 if digestMiddle1 != digestMiddle1b {
143 t.Errorf("TestMultipleSums<Middle1/1b>, got %s, want %s", digestMiddle1, digestMiddle1b)
144 }
145 h.Write(bytes.Repeat([]byte{0x62}, 64+incr2))
146 digestMiddle2 := fmt.Sprintf("%x", h.Sum(tmp[:0]))
147 digestMiddle2b := fmt.Sprintf("%x", h.Sum(tmp[:0]))
148 if digestMiddle2 != digestMiddle2b {
149 t.Errorf("TestMultipleSums<Middle2/2b>, got %s, want %s", digestMiddle2, digestMiddle2b)
150 }
151 h.Write(bytes.Repeat([]byte{0x63}, 64))
152 digestFinal := fmt.Sprintf("%x", h.Sum(tmp[:0]))
153
154 h2 := md5.New()
155 h2.Write(bytes.Repeat([]byte{0x61}, 64+incr))
156 digestCryptoMiddle1 := fmt.Sprintf("%x", h2.Sum(tmp[:0]))
157
158 if digestMiddle1 != digestCryptoMiddle1 {
159 t.Errorf("TestMultipleSums<Middle1>, got %s, want %s", digestMiddle1, digestCryptoMiddle1)
160 }
161
162 h2.Write(bytes.Repeat([]byte{0x62}, 64+incr2))
163 digestCryptoMiddle2 := fmt.Sprintf("%x", h2.Sum(tmp[:0]))
164
165 if digestMiddle2 != digestCryptoMiddle2 {
166 t.Errorf("TestMultipleSums<Middle2>, got %s, want %s", digestMiddle2, digestCryptoMiddle2)
167 }
168
169 h2.Write(bytes.Repeat([]byte{0x63}, 64))
170 digestCryptoFinal := fmt.Sprintf("%x", h2.Sum(tmp[:0]))
171
172 if digestFinal != digestCryptoFinal {
173 t.Errorf("TestMultipleSums<Final>, got %s, want %s", digestFinal, digestCryptoFinal)
174 }
175 }
176
177 func TestMultipleSums(t *testing.T) {
178 t.Run("", func(t *testing.T) {
179 for i := 0; i < 64*2; i++ {
180 for j := 0; j < 64; j++ {
181 testMultipleSums(t, i, j)
182 }
183 }
184 })
185 }
186
187 func testMd5Simulator(t *testing.T, concurrency, iterations, maxSize int, server Server) {
188
189 // Use deterministic RNG.
190 rng := rand.New(rand.NewSource(0xabad1dea))
191
192 for i := 0; i < iterations; i++ {
193 var wg sync.WaitGroup
194 wg.Add(concurrency)
195 for j := 0; j < concurrency; j++ {
196 size := 1 + rng.Intn(maxSize)
197 go func(j int) {
198 defer wg.Done()
199 h := server.NewHash()
200 defer h.Close()
201 input := bytes.Repeat([]byte{0x61 + byte(i^j)}, size)
202
203 // Copy using odd-sized buffer
204 n, err := io.CopyBuffer(h, bytes.NewBuffer(input), make([]byte, 13773))
205 if int(n) != size || err != nil {
206 panic(fmt.Errorf("wrote %d of %d, err: %v", n, size, err))
207 }
208 got := h.Sum([]byte{})
209
210 // Calculate reference
211 want := md5.Sum(input)
212 if !bytes.Equal(got, want[:]) {
213 panic(fmt.Errorf("got %s, want %s", hex.EncodeToString(got), hex.EncodeToString(want[:])))
214 }
215 }(j)
216 }
217 wg.Wait()
218 }
219 }
220
221 func TestMd5Simulator(t *testing.T) {
222 iterations := 400
223 if testing.Short() {
224 iterations = 40
225 }
226
227 t.Run("c16", func(t *testing.T) {
228 server := NewServer()
229 t.Cleanup(server.Close)
230 t.Parallel()
231 testMd5Simulator(t, 16, iterations/10, 20<<20, server)
232 })
233 t.Run("c1", func(t *testing.T) {
234 server := NewServer()
235 t.Cleanup(server.Close)
236 t.Parallel()
237 testMd5Simulator(t, 1, iterations, 5<<20, server)
238 })
239 t.Run("c19", func(t *testing.T) {
240 server := NewServer()
241 t.Cleanup(server.Close)
242 t.Parallel()
243 testMd5Simulator(t, 19, iterations*2, 100<<10, server)
244 })
245 }
246
247 // TestRandomInput tests a number of random inputs.
248 func TestRandomInput(t *testing.T) {
249 n := 500
250 if testing.Short() {
251 n = 100
252 }
253 conc := runtime.GOMAXPROCS(0)
254 for c := 0; c < conc; c++ {
255 t.Run(fmt.Sprint("routine-", c), func(t *testing.T) {
256 server := NewServer()
257 t.Cleanup(server.Close)
258 for i := 0; i < n; i++ {
259 rng := rand.New(rand.NewSource(0xabad1dea + int64(c*n+i)))
260 // Up to 1 MB
261 length := rng.Intn(1 << 20)
262 baseBuf := make([]byte, length)
263
264 t.Run(fmt.Sprint("hash-", i), func(t *testing.T) {
265 t.Parallel()
266 testBuffer := baseBuf
267 rng.Read(testBuffer)
268 wantMD5 := md5.Sum(testBuffer)
269 h := server.NewHash()
270 for len(testBuffer) > 0 {
271 wrLen := rng.Intn(len(testBuffer) + 1)
272 n, err := h.Write(testBuffer[:wrLen])
273 if err != nil {
274 t.Fatal(err)
275 }
276 if n != wrLen {
277 t.Fatalf("write mismatch, want %d, got %d", wrLen, n)
278 }
279 testBuffer = testBuffer[n:]
280 if len(testBuffer) == 0 {
281 // Test if we can use the buffer without races.
282 rng.Read(baseBuf)
283 }
284 }
285 got := h.Sum(nil)
286 if !bytes.Equal(wantMD5[:], got) {
287 t.Fatalf("mismatch, want %v, got %v", wantMD5[:], got)
288 }
289 h.Close()
290 })
291 }
292 })
293 }
294 }
295
296 func benchmarkCryptoMd5(b *testing.B, blockSize int) {
297
298 input := bytes.Repeat([]byte{0x61}, blockSize)
299
300 b.SetBytes(int64(blockSize))
301 b.ReportAllocs()
302 b.ResetTimer()
303
304 h := md5.New()
305 var tmp [Size]byte
306
307 for j := 0; j < b.N; j++ {
308 h.Write(input)
309 h.Sum(tmp[:0])
310 }
311 }
312
313 func benchmarkCryptoMd5P(b *testing.B, blockSize int) {
314 b.SetBytes(int64(blockSize))
315 b.ReportAllocs()
316 b.ResetTimer()
317 var tmp [Size]byte
318
319 b.RunParallel(func(pb *testing.PB) {
320 input := bytes.Repeat([]byte{0x61}, blockSize)
321 h := md5.New()
322 for pb.Next() {
323 h.Write(input)
324 h.Sum(tmp[:0])
325 }
326 })
327 }
328
329 func BenchmarkCryptoMd5(b *testing.B) {
330 b.Run("32KB", func(b *testing.B) {
331 benchmarkCryptoMd5(b, 32*1024)
332 })
333 b.Run("64KB", func(b *testing.B) {
334 benchmarkCryptoMd5(b, 64*1024)
335 })
336 b.Run("128KB", func(b *testing.B) {
337 benchmarkCryptoMd5(b, 128*1024)
338 })
339 b.Run("256KB", func(b *testing.B) {
340 benchmarkCryptoMd5(b, 256*1024)
341 })
342 b.Run("512KB", func(b *testing.B) {
343 benchmarkCryptoMd5(b, 512*1024)
344 })
345 b.Run("1MB", func(b *testing.B) {
346 benchmarkCryptoMd5(b, 1024*1024)
347 })
348 b.Run("2MB", func(b *testing.B) {
349 benchmarkCryptoMd5(b, 2*1024*1024)
350 })
351 }
352
353 func BenchmarkCryptoMd5Parallel(b *testing.B) {
354 b.Run("32KB", func(b *testing.B) {
355 benchmarkCryptoMd5P(b, 32*1024)
356 })
357 b.Run("64KB", func(b *testing.B) {
358 benchmarkCryptoMd5P(b, 64*1024)
359 })
360 b.Run("128KB", func(b *testing.B) {
361 benchmarkCryptoMd5P(b, 128*1024)
362 })
363 b.Run("256KB", func(b *testing.B) {
364 benchmarkCryptoMd5P(b, 256*1024)
365 })
366 b.Run("512KB", func(b *testing.B) {
367 benchmarkCryptoMd5P(b, 512*1024)
368 })
369 b.Run("1MB", func(b *testing.B) {
370 benchmarkCryptoMd5P(b, 1024*1024)
371 })
372 b.Run("2MB", func(b *testing.B) {
373 benchmarkCryptoMd5P(b, 2*1024*1024)
374 })
375 b.Run("4MB", func(b *testing.B) {
376 benchmarkCryptoMd5P(b, 4*1024*1024)
377 })
378 b.Run("8MB", func(b *testing.B) {
379 benchmarkCryptoMd5P(b, 8*1024*1024)
380 })
381 }