-
Notifications
You must be signed in to change notification settings - Fork 57
/
io_test.go
102 lines (84 loc) · 1.56 KB
/
io_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
package multihash
import (
"bytes"
"io"
"testing"
)
type evilReader struct {
buffer []byte
}
func (er *evilReader) Read(buf []byte) (int, error) {
n := copy(buf, er.buffer)
er.buffer = er.buffer[n:]
var err error
if len(er.buffer) == 0 {
err = io.EOF
}
return n, err
}
func TestEvilReader(t *testing.T) {
emptyHash, err := Sum(nil, IDENTITY, 0)
if err != nil {
t.Fatal(err)
}
r := NewReader(&evilReader{emptyHash})
h, err := r.ReadMultihash()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(h, []byte(emptyHash)) {
t.Fatal(err)
}
h, err = r.ReadMultihash()
if len([]byte(h)) > 0 || err != io.EOF {
t.Fatal("expected end of file")
}
}
func TestReader(t *testing.T) {
var buf bytes.Buffer
for _, tc := range testCases {
m, err := tc.Multihash()
if err != nil {
t.Fatal(err)
}
buf.Write([]byte(m))
}
r := NewReader(&buf)
for _, tc := range testCases {
h, err := tc.Multihash()
if err != nil {
t.Fatal(err)
}
h2, err := r.ReadMultihash()
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(h, h2) {
t.Error("h and h2 should be equal")
}
}
}
func TestWriter(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(&buf)
for _, tc := range testCases {
m, err := tc.Multihash()
if err != nil {
t.Error(err)
continue
}
if err := w.WriteMultihash(m); err != nil {
t.Error(err)
continue
}
buf2 := make([]byte, len(m))
if _, err := io.ReadFull(&buf, buf2); err != nil {
t.Error(err)
continue
}
if !bytes.Equal(m, buf2) {
t.Error("m and buf2 should be equal")
}
}
}