summaryrefslogtreecommitdiffhomepage
path: root/pkg/hashio/hashio_test.go
blob: 41dbdf860c8eab2166c051a6debb725bfd85e137 (plain)
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
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hashio

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha256"
	"fmt"
	"io"
	"math/rand"
	"testing"
)

var testKey = []byte("01234567890123456789012345678901")

func runTest(c []byte, fn func(enc *bytes.Buffer), iters int) error {
	// Encoding happens via a buffer.
	var (
		enc bytes.Buffer
		dec bytes.Buffer
	)

	for i := 0; i < iters; i++ {
		enc.Reset()
		w := NewWriter(&enc, hmac.New(sha256.New, testKey))
		if _, err := io.Copy(w, bytes.NewBuffer(c)); err != nil {
			return err
		}
		if err := w.Close(); err != nil {
			return err
		}
	}

	fn(&enc)

	for i := 0; i < iters; i++ {
		dec.Reset()
		r := NewReader(bytes.NewReader(enc.Bytes()), hmac.New(sha256.New, testKey))
		if _, err := io.Copy(&dec, r); err != nil {
			return err
		}
	}

	// Check that the data matches; this should never fail.
	if !bytes.Equal(c, dec.Bytes()) {
		panic(fmt.Sprintf("data didn't match: got %v, expected %v", dec.Bytes(), c))
	}

	return nil
}

func TestTable(t *testing.T) {
	cases := [][]byte{
		// Various data sizes.
		nil,
		[]byte(""),
		[]byte("_"),
		[]byte("0"),
		[]byte("01"),
		[]byte("012"),
		[]byte("0123"),
		[]byte("01234"),
		[]byte("012356"),
		[]byte("0123567"),
		[]byte("01235678"),

		// Make sure we have one longer than the hash length.
		[]byte("012356asdjflkasjlk3jlk23j4lkjaso0d789f0aujw3lkjlkxsdf78asdful2kj3ljka78"),

		// Make sure we have one longer than the segment size.
		make([]byte, 3*SegmentSize),
		make([]byte, 3*SegmentSize-1),
		make([]byte, 3*SegmentSize+1),
		make([]byte, 3*SegmentSize-32),
		make([]byte, 3*SegmentSize+32),
		make([]byte, 30*SegmentSize),
	}

	for _, c := range cases {
		for _, flip := range []bool{false, true} {
			if len(c) == 0 && flip == true {
				continue
			}

			// Log the case.
			t.Logf("case: len=%d flip=%v", len(c), flip)

			if err := runTest(c, func(enc *bytes.Buffer) {
				if flip {
					corrupted := rand.Intn(enc.Len())
					enc.Bytes()[corrupted]++
				}
			}, 1); err != nil {
				if !flip || err != ErrHashMismatch {
					t.Errorf("error during read: got %v, expected nil", err)
				}
				continue
			} else if flip {
				t.Errorf("failed to detect ErrHashMismatch on corrupted data!")
				continue
			}
		}
	}
}

const benchBytes = 10 * 1024 * 1024 // 10 MB.

func BenchmarkWrite(b *testing.B) {
	b.StopTimer()
	x := make([]byte, benchBytes)
	b.SetBytes(benchBytes)
	b.StartTimer()
	if err := runTest(x, func(enc *bytes.Buffer) {
		b.StopTimer()
	}, b.N); err != nil {
		b.Errorf("benchmark failed: %v", err)
	}
}

func BenchmarkRead(b *testing.B) {
	b.StopTimer()
	x := make([]byte, benchBytes)
	b.SetBytes(benchBytes)
	if err := runTest(x, func(enc *bytes.Buffer) {
		b.StartTimer()
	}, b.N); err != nil {
		b.Errorf("benchmark failed: %v", err)
	}
}