forked from ipfs/go-ipfs-chunker
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse_test.go
116 lines (95 loc) · 2.86 KB
/
parse_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package chunk
import (
"bytes"
"fmt"
"testing"
)
const (
testTwoThirdsOfChunkLimit = 2 * (float32(ChunkSizeLimit) / float32(3))
)
func TestParseRabin(t *testing.T) {
r := bytes.NewReader(randBuf(t, 1000))
_, err := FromString(r, "rabin-18-25-32")
if err != nil {
t.Errorf(err.Error())
}
_, err = FromString(r, "rabin-15-23-31")
if err != ErrRabinMin {
t.Fatalf("Expected an 'ErrRabinMin' error, got: %#v", err)
}
_, err = FromString(r, "rabin-20-20-21")
if err == nil || err.Error() != "incorrect format: rabin-min must be smaller than rabin-avg" {
t.Fatalf("Expected an arg-out-of-order error, got: %#v", err)
}
_, err = FromString(r, "rabin-19-21-21")
if err == nil || err.Error() != "incorrect format: rabin-avg must be smaller than rabin-max" {
t.Fatalf("Expected an arg-out-of-order error, got: %#v", err)
}
_, err = FromString(r, fmt.Sprintf("rabin-19-21-%d", ChunkSizeLimit))
if err != nil {
t.Fatalf("Expected success, got: %#v", err)
}
_, err = FromString(r, fmt.Sprintf("rabin-19-21-%d", 1+ChunkSizeLimit))
if err != ErrSizeMax {
t.Fatalf("Expected 'ErrSizeMax', got: %#v", err)
}
_, err = FromString(r, fmt.Sprintf("rabin-%.0f", testTwoThirdsOfChunkLimit))
if err != nil {
t.Fatalf("Expected success, got: %#v", err)
}
_, err = FromString(r, fmt.Sprintf("rabin-%.0f", 1+testTwoThirdsOfChunkLimit))
if err != ErrSizeMax {
t.Fatalf("Expected 'ErrSizeMax', got: %#v", err)
}
}
func TestParseSize(t *testing.T) {
r := bytes.NewReader(randBuf(t, 1000))
_, err := FromString(r, "size-0")
if err != ErrSize {
t.Fatalf("Expected an 'ErrSize' error, got: %#v", err)
}
_, err = FromString(r, "size-32")
if err != nil {
t.Fatalf("Expected success, got: %#v", err)
}
_, err = FromString(r, fmt.Sprintf("size-%d", ChunkSizeLimit))
if err != nil {
t.Fatalf("Expected success, got: %#v", err)
}
_, err = FromString(r, fmt.Sprintf("size-%d", 1+ChunkSizeLimit))
if err != ErrSizeMax {
t.Fatalf("Expected 'ErrSizeMax', got: %#v", err)
}
}
func TestParseReedSolomon(t *testing.T) {
rsCases := []struct {
chunker string
success bool
}{
{"reed-solomon", true},
{"reed-solomon-", false},
{"reed-s0lomon", false},
{"reed-solomon-10", false},
{"reed-solomon-10-20", false},
{"reed-solomon-10-20-0", false},
{"reed-solomon-10-20-100", true},
{"reed-solomon-0-20-100", false},
{"reed-solomon-10-0-100", false},
{"reed-solomon-150-150-100", false},
}
for _, rsc := range rsCases {
rsc := rsc // capture range variable
t.Run(fmt.Sprintf("test chunker %s", rsc.chunker), func(t *testing.T) {
// speed up with parallel test
t.Parallel()
max := 1000
r := bytes.NewReader(randBuf(t, max))
_, err := FromString(r, rsc.chunker)
if rsc.success && err != nil {
t.Fatal("failed to parse chunker", err)
} else if !rsc.success && err == nil {
t.Fatal("should fail to parse chunker but succeeded")
}
})
}
}