forked from gabstv/go-bsdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsdiff_test.go
126 lines (120 loc) · 3.1 KB
/
bsdiff_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
package bsdiff
import (
"bytes"
"encoding/binary"
"io/ioutil"
"math/rand"
"os"
"testing"
"time"
"github.com/gabstv/go-bsdiff/pkg/util"
)
func TestDiff(t *testing.T) {
oldbs := []byte{0xFF, 0xFA, 0xB7, 0xDD}
newbs := []byte{0xFF, 0xFA, 0x90, 0xB7, 0xDD, 0xFE}
var diffbs []byte
var err error
if diffbs, err = Bytes(oldbs, newbs); err != nil {
t.Fatal(err)
}
z := []byte{
0x42, 0x53, 0x44, 0x49, 0x46, 0x46, 0x34, 0x30, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xC9, 0x9D, 0x1D, 0x33, 0x00, 0x00,
0x06, 0xC0, 0x40, 0x5C, 0x00, 0x40, 0x00, 0x20, 0x00, 0x21, 0x8C, 0xA0, 0x60, 0x6C, 0xE2, 0xC8,
0xF1, 0x47, 0xC5, 0xDC, 0x91, 0x4E, 0x14, 0x24, 0x32, 0x67, 0x47, 0x4C, 0xC0, 0x42, 0x5A, 0x68,
0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xFF, 0x48, 0x9B, 0x82, 0x00, 0x00, 0x00, 0xC0, 0x00,
0x40, 0x00, 0x20, 0x00, 0x21, 0x18, 0x46, 0xC2, 0xEE, 0x48, 0xA7, 0x0A, 0x12, 0x1F, 0xE9, 0x13,
0x70, 0x40, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xDD, 0x13, 0xBF, 0x5C,
0x00, 0x00, 0x00, 0x00, 0x2A, 0xC0, 0x00, 0x00, 0x80, 0x00, 0x02, 0x00, 0x01, 0x20, 0x00, 0x22,
0x1B, 0x03, 0x0C, 0x70, 0xC2, 0xEE, 0x48, 0xA7, 0x0A, 0x12, 0x1B, 0xA2, 0x77, 0xEB, 0x80,
}
if !bytes.Equal(diffbs[:len(z)], z) {
t.Fatal(diffbs[:len(z)], "!=", z)
}
}
func TestOfftout(t *testing.T) {
buf := make([]byte, 8)
offtout(9001, buf)
n := binary.LittleEndian.Uint64(buf)
if n != 9001 {
t.Fatal(n, "!=", 9001)
}
//
offtout(9002, buf)
n = binary.LittleEndian.Uint64(buf)
if n != 9002 {
t.Fatal(n, "!=", 9002)
}
}
func TestReader(t *testing.T) {
rand.Seed(time.Now().UnixNano())
file1 := make([]byte, 512)
file2 := make([]byte, 1024)
rand.Read(file1)
copy(file2, file1)
rand.Read(file2[512:])
rold := bytes.NewReader(file1)
rnew := bytes.NewReader(file2)
rpatch := new(util.BufWriter)
if err := Reader(rold, rnew, rpatch); err != nil {
t.Fatal(err)
}
b := make([]byte, 8)
copy(b, rpatch.Bytes())
if !bytes.Equal(b, []byte("BSDIFF40")) {
t.Fail()
}
}
func TestFile(t *testing.T) {
rand.Seed(time.Now().UnixNano())
file1 := make([]byte, 1024*32)
file2 := make([]byte, 1024*33)
rand.Read(file1)
copy(file2, file1)
rand.Read(file2[1024*32:])
rand.Read(file2[100:1024])
tf0, err := ioutil.TempFile(os.TempDir(), "")
if err != nil {
t.Fatal(err)
}
t0n := tf0.Name()
tf1, err := ioutil.TempFile(os.TempDir(), "")
if err != nil {
t.Fatal(err)
}
t1n := tf1.Name()
if _, err = tf0.Write(file1); err != nil {
tf0.Close()
tf1.Close()
os.Remove(t0n)
os.Remove(t1n)
t.Fatal(err)
}
if _, err = tf1.Write(file2); err != nil {
tf0.Close()
tf1.Close()
os.Remove(t0n)
os.Remove(t1n)
t.Fatal(err)
}
tf0.Close()
tf1.Close()
tp, err := ioutil.TempFile(os.TempDir(), "")
if err != nil {
os.Remove(t0n)
os.Remove(t1n)
t.Fatal(err)
}
tpp := tp.Name()
tp.Close()
if err := File(t0n, t1n, tpp); err != nil {
os.Remove(t0n)
os.Remove(t1n)
os.Remove(tpp)
t.Fatal(err)
}
os.Remove(t0n)
os.Remove(t1n)
os.Remove(tpp)
}