forked from nine-lives-later/go-xdelta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_test.go
247 lines (198 loc) · 5.74 KB
/
full_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package xdelta
import (
"bytes"
"context"
"crypto/sha1"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
"github.com/dustin/go-humanize"
)
type testFullRoundtrip_Context struct {
FromFilePath string
ToFilePath string
PatchFilePath string
AppliedFilePath string
Header []byte // set during seeding
ToFileHash []byte
}
func TestFullRoundtrip(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
// get temporary directory
tempDir, err := ioutil.TempDir("", "go-xdelta")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
ctx := &testFullRoundtrip_Context{
FromFilePath: filepath.Join(tempDir, "from"),
ToFilePath: filepath.Join(tempDir, "to"),
PatchFilePath: filepath.Join(tempDir, "patch"),
AppliedFilePath: filepath.Join(tempDir, "to_applied"),
}
t.Run("Seed", func(t *testing.T) { testFullRoundtrip_Seed(t, ctx) })
t.Run("CreatePatch", func(t *testing.T) { testFullRoundtrip_CreatePatch(t, ctx) })
t.Run("DumpPatchInfo", func(t *testing.T) { testFullRoundtrip_DumpPatchInfo(t, ctx) })
t.Run("ApplyPatch", func(t *testing.T) { testFullRoundtrip_ApplyPatch(t, ctx) })
t.Run("CompareHash", func(t *testing.T) { testFullRoundtrip_CompareHash(t, ctx) })
}
func testFullRoundtrip_Seed(t *testing.T, ctx *testFullRoundtrip_Context) {
// open the files
fromFile, err := os.Create(ctx.FromFilePath)
if err != nil {
t.Fatalf("Failed to create FROM file: %v", err)
}
defer fromFile.Close()
toFile, err := os.Create(ctx.ToFilePath)
if err != nil {
t.Fatalf("Failed to create TO file: %v", err)
}
defer toFile.Close()
// determine file sizes
rand.Seed(time.Now().UnixNano())
buf := make([]byte, 64*1024)
fromBlocks := int(1024 + rand.Int31n(1024))
toBlocks := int(1024 + rand.Int31n(1024))
t.Logf("FROM file size: %v (%v)", fromBlocks*len(buf), humanize.Bytes(uint64(fromBlocks*len(buf))))
t.Logf("TO file size: %v (%v)", toBlocks*len(buf), humanize.Bytes(uint64(toBlocks*len(buf))))
fromSkipMod := int(3 + rand.Int31n(10))
toSkipMod := int(3 + rand.Int31n(10))
// start seeding
maxBlocks := fromBlocks
if toBlocks > maxBlocks {
maxBlocks = toBlocks
}
toHash := sha1.New()
for block := 0; block < maxBlocks; block++ {
_, err := rand.Read(buf)
if err != nil {
t.Fatalf("Failed to seed random data: %v", err)
}
if (block%fromSkipMod != 0) && (block < fromBlocks) {
fromFile.Write(buf)
}
if (block%toSkipMod != 0) && (block < toBlocks) {
toFile.Write(buf)
toHash.Write(buf)
}
}
// seed header
ctx.Header = make([]byte, 7000+rand.Int31n(20000))
rand.Read(ctx.Header)
// done
ctx.ToFileHash = toHash.Sum(nil)
t.Logf("TO file hash: %x", ctx.ToFileHash)
}
func testFullRoundtrip_CreatePatch(t *testing.T, ctx *testFullRoundtrip_Context) {
// open the files
fromFile, err := os.Open(ctx.FromFilePath)
if err != nil {
t.Fatalf("Failed to open FROM file: %v", err)
}
defer fromFile.Close()
toFile, err := os.Open(ctx.ToFilePath)
if err != nil {
t.Fatalf("Failed to open TO file: %v", err)
}
defer toFile.Close()
patchFile, err := os.Create(ctx.PatchFilePath)
if err != nil {
t.Fatalf("Failed to open PATCH file: %v", err)
}
defer patchFile.Close()
// prepare encoder
options := EncoderOptions{
FileID: "TestFullRoundtrip",
FromFile: fromFile,
ToFile: toFile,
PatchFile: patchFile,
Header: ctx.Header,
}
enc, err := NewEncoder(options)
if err != nil {
t.Fatalf("Failed to create encoder: %v", err)
}
defer enc.Close()
// create the patch
err = enc.Process(context.TODO())
if err != nil {
t.Fatalf("Failed to create patch: %v", err)
}
}
func testFullRoundtrip_DumpPatchInfo(t *testing.T, ctx *testFullRoundtrip_Context) {
patchFileStat, err := os.Stat(ctx.PatchFilePath)
if err != nil {
t.Fatalf("Failed to get patch filesize: %v", err)
}
t.Logf("PATCH file size: %v (%v)", patchFileStat.Size(), humanize.Bytes(uint64(patchFileStat.Size())))
}
func testFullRoundtrip_ApplyPatch(t *testing.T, ctx *testFullRoundtrip_Context) {
// open the files
fromFile, err := os.Open(ctx.FromFilePath)
if err != nil {
t.Fatalf("Failed to open FROM file: %v", err)
}
defer fromFile.Close()
appliedFile, err := os.Create(ctx.AppliedFilePath)
if err != nil {
t.Fatalf("Failed to open APPLIED file: %v", err)
}
defer appliedFile.Close()
patchFile, err := os.Open(ctx.PatchFilePath)
if err != nil {
t.Fatalf("Failed to open PATCH file: %v", err)
}
defer patchFile.Close()
// prepare decoder
options := DecoderOptions{
FileID: "TestFullRoundtrip",
FromFile: fromFile,
ToFile: appliedFile,
PatchFile: patchFile,
}
dec, err := NewDecoder(options)
if err != nil {
t.Fatalf("Failed to apply encoder: %v", err)
}
defer dec.Close()
// retrieve header
headerChannel := make(chan []byte, 1)
dec.Header = headerChannel
// apply the patch
err = dec.Process(context.TODO())
if err != nil {
t.Fatalf("Failed to apply patch: %v", err)
}
// compare the header
readHeader := <-headerChannel
if !bytes.Equal(ctx.Header, readHeader) {
t.Fatalf("Header of PATCH file does not match")
}
}
func testFullRoundtrip_CompareHash(t *testing.T, ctx *testFullRoundtrip_Context) {
// open the files
appliedFile, err := os.Open(ctx.AppliedFilePath)
if err != nil {
t.Fatalf("Failed to open APPLIED file: %v", err)
}
defer appliedFile.Close()
// calculate hash
appliedHash := sha1.New()
_, err = io.Copy(appliedHash, appliedFile)
if err != nil {
t.Fatalf("Failed to hash APPLIED file: %v", err)
}
appliedFile.Close()
appliedHashResult := appliedHash.Sum(nil)
// compare
t.Logf("APPLIED file hash: %x", appliedHashResult)
if !bytes.Equal(ctx.ToFileHash, appliedHashResult) {
t.Fatalf("File hash of TO and APPLIED file are different")
}
}