-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk_test.go
182 lines (168 loc) · 3.74 KB
/
chunk_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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package bakemono
import (
"os"
"reflect"
"testing"
)
func TestChunk_SetVerityGet(t *testing.T) {
chunk := &Chunk{}
err := chunk.Set([]byte("key"), []byte("value"))
if err != nil {
t.Fatal(err)
}
err = chunk.Verify()
if err != nil {
t.Fatal(err)
}
key, data := chunk.GetKeyData()
if string(key) != "key" {
t.Fatal("key is not equal to \"key\"")
}
if string(data) != "value" {
t.Fatal("data is not equal to \"value\"")
}
}
func TestChunk_BadSet(t *testing.T) {
chunk := &Chunk{}
badKey := make([]byte, ChunkKeyMaxSize+1)
err := chunk.Set(badKey, []byte("value"))
if err == nil {
t.Fatal(err)
}
badData := make([]byte, ChunkDataSize+1)
err = chunk.Set([]byte("key"), badData)
if err == nil {
t.Fatal(err)
}
err = chunk.Set(badKey, badData)
if err == nil {
t.Fatal(err)
}
}
func TestChunk_Marshal_Unmarshal_Binary(t *testing.T) {
chunk := &Chunk{}
err := chunk.Set([]byte("key"), []byte("value114514"))
if err != nil {
t.Fatal(err)
}
bin, err := chunk.MarshalBinary()
if err != nil {
t.Fatal(err)
}
chunk2 := &Chunk{}
err = chunk2.UnmarshalBinary(bin)
if err != nil {
t.Fatal(err)
}
err = chunk2.Verify()
if err != nil {
t.Fatal(err)
}
equal := reflect.DeepEqual(chunk, chunk2)
if !equal {
t.Fatal("chunk2 is not equal to chunk1")
}
k, v := chunk2.GetKeyData()
if string(k) != "key" {
t.Fatal("key is not equal to \"key\"")
}
if string(v) != "value114514" {
t.Fatal("data is not equal to \"value\"")
}
}
func TestChunk_BadRead(t *testing.T) {
chunk := &Chunk{}
err := chunk.Set([]byte("key"), []byte("value114514"))
if err != nil {
t.Fatal(err)
}
bin, err := chunk.MarshalBinary()
if err != nil {
t.Fatal(err)
}
// bad body length
chunk2 := &Chunk{}
err = chunk2.UnmarshalBinary(bin[:len(bin)-1])
if err == nil {
t.Fatal("chunk2.UnmarshalBinary should return an error")
}
// bad body content
chunk3 := &Chunk{}
bin2 := make([]byte, len(bin))
copy(bin2, bin)
bin2[0] = bin2[0] + 1
err = chunk3.UnmarshalBinary(bin2)
if err == nil {
t.Fatal("chunk3.UnmarshalBinary should return an error")
}
}
func TestChunk_WriteAt(t *testing.T) {
chunk := &Chunk{}
err := chunk.Set([]byte("key"), []byte("value"))
if err != nil {
t.Fatal(err)
}
// open file for writing
path := os.TempDir() + "/test_chunk_writeat"
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
t.Fatal(err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
t.Fatal(err)
}
}(f)
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(path)
err = chunk.WriteAt(f, 0)
if err != nil {
t.Fatal(err)
}
}
func TestChunkHeader_Marshal_UnmarshalBinary(t *testing.T) {
// create a chunk header struct
// marshal it to binary
// check if the binary is equal to the expected binary
// if not, then the test fails
ch := ChunkHeader{
Checksum: 0xb0cc1000,
Key: [ChunkKeyMaxSize]byte{0x12, 0x34, 0x56, 0x78},
DataLength: 0x11451419,
HeaderSize: 0xa928b2ef,
}
b, err := ch.MarshalBinary()
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
ch2 := ChunkHeader{}
err = ch2.UnmarshalBinary(b)
if err != nil {
t.Fatal(err)
}
t.Log("ChunkHeader2 is equal to ChunkHeader1")
}
func TestMaxChunkHeaderSize(t *testing.T) {
// calculate the chunk header struct sizeInternal using reflect
// and compare it with the constant ChunkHeaderSizeFixed
// if bigger than ChunkHeaderSizeFixed, then the test fails
ch := ChunkHeader{
Checksum: 0xb0cc1000,
Key: [ChunkKeyMaxSize]byte{0x12, 0x34, 0x56, 0x78},
DataLength: 0x11451419,
HeaderSize: 0xa928b2ef,
}
b, err := ch.MarshalBinary()
if err != nil {
t.Fatal(err)
}
if len(b) > ChunkHeaderSizeFixed {
t.Fatal("ChunkHeaderSizeFixed is not equal to the calculated sizeInternal")
}
}