forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsplitter_simple_test.go
170 lines (147 loc) · 5.37 KB
/
bsplitter_simple_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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"bytes"
"testing"
"github.com/keybase/kbfs/kbfscodec"
)
func TestBsplitterEmptyCopyAll(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
fblock := NewFileBlock().(*FileBlock)
data := []byte{1, 2, 3, 4, 5}
if n := bsplit.CopyUntilSplit(fblock, false, data, 0); n != 5 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents, data) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterNonemptyCopyAll(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
fblock := NewFileBlock().(*FileBlock)
fblock.Contents = []byte{10, 9}
data := []byte{1, 2, 3, 4, 5}
if n := bsplit.CopyUntilSplit(fblock, false, data, 0); n != 5 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents, data) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterAppendAll(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
fblock := NewFileBlock().(*FileBlock)
fblock.Contents = []byte{10, 9}
data := []byte{1, 2, 3, 4, 5}
if n := bsplit.CopyUntilSplit(fblock, false, data, 2); n != 5 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents, append([]byte{10, 9}, data...)) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterAppendExact(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
fblock := NewFileBlock().(*FileBlock)
fblock.Contents = []byte{10, 9, 8, 7, 6}
data := []byte{1, 2, 3, 4, 5}
if n := bsplit.CopyUntilSplit(fblock, false, data, 5); n != 5 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents,
append([]byte{10, 9, 8, 7, 6}, data...)) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterSplitOne(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
fblock := NewFileBlock().(*FileBlock)
fblock.Contents = []byte{10, 9, 8, 7, 6}
data := []byte{1, 2, 3, 4, 5, 6}
if n := bsplit.CopyUntilSplit(fblock, false, data, 5); n != 5 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents,
[]byte{10, 9, 8, 7, 6, 1, 2, 3, 4, 5}) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterOverwriteMaxSizeBlock(t *testing.T) {
bsplit := &BlockSplitterSimple{5, 5, 10}
fblock := NewFileBlock().(*FileBlock)
fblock.Contents = []byte{10, 9, 8, 7, 6}
data := []byte{1, 2, 3, 4, 5, 6, 7, 8}
if n := bsplit.CopyUntilSplit(fblock, false, data, 0); n != 5 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents, []byte{1, 2, 3, 4, 5}) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterBlockTooBig(t *testing.T) {
bsplit := &BlockSplitterSimple{3, 5, 10}
fblock := NewFileBlock().(*FileBlock)
fblock.Contents = []byte{10, 9, 8, 7, 6}
data := []byte{1, 2, 3, 4, 5, 6}
if n := bsplit.CopyUntilSplit(fblock, false, data, 5); n != 0 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents, []byte{10, 9, 8, 7, 6}) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterOffTooBig(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
fblock := NewFileBlock().(*FileBlock)
fblock.Contents = []byte{10, 9, 8, 7, 6}
data := []byte{1, 2, 3, 4, 5, 6}
if n := bsplit.CopyUntilSplit(fblock, false, data, 15); n != 0 {
t.Errorf("Did not copy expected number of bytes: %d", n)
} else if !bytes.Equal(fblock.Contents,
[]byte{10, 9, 8, 7, 6, 0, 0, 0, 0, 0}) {
t.Errorf("Wrong file contents after copy: %v", fblock.Contents)
}
}
func TestBsplitterShouldEmbed(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
bc := &BlockChanges{}
bc.sizeEstimate = 1
if !bsplit.ShouldEmbedBlockChanges(bc) {
t.Errorf("Not embedding a 1-byte block change")
}
bc.sizeEstimate = 10
if !bsplit.ShouldEmbedBlockChanges(bc) {
t.Errorf("Not embedding a 10-byte block change")
}
}
func TestBsplitterShouldNotEmbed(t *testing.T) {
bsplit := &BlockSplitterSimple{10, 5, 10}
bc := &BlockChanges{}
bc.sizeEstimate = 11
if bsplit.ShouldEmbedBlockChanges(bc) {
t.Errorf("Not embedding a 1-byte block change")
}
}
func TestBsplitterOverhead(t *testing.T) {
codec := kbfscodec.NewMsgpack()
desiredBlockSize := int64(64 * 1024)
bsplit, err := NewBlockSplitterSimple(desiredBlockSize, 8*1024, codec)
if err != nil {
t.Fatalf("Got error making block splitter with overhead: %v", err)
}
// Test that an encoded, padded block matches this desired block size
block := NewFileBlock().(*FileBlock)
block.Contents = make([]byte, bsplit.maxSize)
for i := range block.Contents {
block.Contents[i] = byte(i)
}
encodedBlock, err := codec.Encode(block)
if err != nil {
t.Fatalf("Encoding block failed: %v", err)
}
crypto := MakeCryptoCommon(codec)
paddedBlock, err := crypto.padBlock(encodedBlock)
if err != nil {
t.Fatalf("Padding block failed: %v", err)
}
// first 4 bytes of the padded block encodes the block size
if g, e := int64(len(paddedBlock)), desiredBlockSize+4; g != e {
t.Fatalf("Padded block size %d doesn't match desired block size %d",
g, e)
}
}