-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator_test.go
89 lines (83 loc) · 2.15 KB
/
indicator_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
package uniquefile_test
import (
"bytes"
"context"
"encoding/binary"
"io"
"strings"
"testing"
"github.com/skillian/uniquefile"
)
type indicatorTest struct {
name string
ir uniquefile.Indicator
source string
expect uniquefile.Indication
}
var (
byteOrder = binary.BigEndian
indicatorTests = []indicatorTest{
func() (t indicatorTest) {
var buf [8]byte
t.name = "helloWorldLen"
t.ir = uniquefile.LengthIndicator
t.source = "hello, world!"
byteOrder.PutUint64(buf[:], uint64(len(t.source)))
t.expect.Write([]byte("length"), buf[:])
return
}(),
func() (t indicatorTest) {
var buf [8]byte
t.name = "helloWorldCRC32"
t.ir = uniquefile.CRC32Indicator
t.source = "hello, world!"
byteOrder.PutUint64(buf[:], uint64(len(t.source)))
t.expect.Write([]byte("length"), buf[:])
t.expect.Write([]byte("crc32"), []byte{88, 152, 141, 19})
return
}(),
func() (t indicatorTest) {
var buf [8]byte
t.name = "helloWorldCRC32SHA256"
t.ir = uniquefile.NewIndicators(
uniquefile.CRC32Indicator,
uniquefile.SHA256Indicator,
)
t.source = "hello, world!"
byteOrder.PutUint64(buf[:], uint64(len(t.source)))
t.expect.Write([]byte("length"), buf[:])
t.expect.Write([]byte("crc32"), []byte{88, 152, 141, 19})
t.expect.Write([]byte("sha256"), []byte{
104, 230, 86, 178, 81, 230, 126, 131,
88, 190, 248, 72, 58, 176, 213, 28, 102,
25, 243, 231, 161, 169, 240, 231, 88,
56, 212, 31, 243, 104, 247, 40,
})
return
}(),
}
)
func TestIndicator(t *testing.T) {
for _, tc := range indicatorTests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
r := strings.NewReader(tc.source)
res := uniquefile.NewIndication()
if err := tc.ir.Indicate(ctx, r, res); err != nil {
t.Fatal(err)
}
if !bytes.Equal(res.Bytes(), tc.expect.Bytes()) {
t.Fatalf(
"result does not match expected:\n\t%v\n\t%v",
res.Bytes(), tc.expect.Bytes(),
)
}
if cr, ok := tc.ir.(io.Closer); ok {
if err := cr.Close(); err != nil {
t.Fatal(err)
}
}
})
}
}