siprng_test.go (view raw)
1package captcha
2
3import (
4 "bytes"
5 "testing"
6)
7
8func TestSiphash(t *testing.T) {
9 good := uint64(0xe849e8bb6ffe2567)
10 cur := siphash(0, 0, 0)
11 if cur != good {
12 t.Fatalf("siphash: expected %x, got %x", good, cur)
13 }
14}
15
16func TestSiprng(t *testing.T) {
17 m := make(map[uint64]interface{})
18 var yes interface{}
19 r := siprng{}
20 r.Seed([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
21 for i := 0; i < 100000; i++ {
22 v := r.Uint64()
23 if _, ok := m[v]; ok {
24 t.Errorf("siphash: collision on %d: %x", i, v)
25 }
26 m[v] = yes
27 }
28}
29
30func TestSiprngBytes(t *testing.T) {
31 r := siprng{}
32 r.Seed([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
33 x := r.Bytes(32)
34 if len(x) != 32 {
35 t.Fatalf("siphash: wrong length: expected 32, got %d", len(x))
36 }
37 y := r.Bytes(32)
38 if bytes.Equal(x, y) {
39 t.Fatalf("siphash: stream repeats: %x = %x", x, y)
40 }
41 r.Seed([16]byte{})
42 z := r.Bytes(32)
43 if bytes.Equal(z, x) {
44 t.Fatalf("siphash: outputs under different keys repeat: %x = %x", z, x)
45 }
46}
47
48func BenchmarkSiprng(b *testing.B) {
49 b.SetBytes(8)
50 p := &siprng{}
51 for i := 0; i < b.N; i++ {
52 p.Uint64()
53 }
54}