forked from TotallyGamerJet/bsdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbspatch_test.go
129 lines (122 loc) · 3.29 KB
/
bspatch_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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2019 Gabriel Ochsenhofer
// SPDX-FileCopyrightText: 2025 TotallyGamerJet
package bsdiff
import (
"bytes"
"testing"
)
func TestPatch(t *testing.T) {
oldfile := []byte{
0x66, 0xFF, 0xD1, 0x55, 0x56, 0x10, 0x30, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD1,
}
newfilecomp := []byte{
0x66, 0xFF, 0xD1, 0x55, 0x56, 0x10, 0x30, 0x00,
0x44, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xD1, 0xFF, 0xD1,
}
patchfile := []byte{
0x42, 0x53, 0x44, 0x49, 0x46, 0x46, 0x34, 0x30,
0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26,
0x53, 0x59, 0xDA, 0xE4, 0x46, 0xF2, 0x00, 0x00,
0x05, 0xC0, 0x00, 0x4A, 0x09, 0x20, 0x00, 0x22,
0x34, 0xD9, 0x06, 0x06, 0x4B, 0x21, 0xEE, 0x17,
0x72, 0x45, 0x38, 0x50, 0x90, 0xDA, 0xE4, 0x46,
0xF2, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59,
0x26, 0x53, 0x59, 0x30, 0x88, 0x1C, 0x89, 0x00,
0x00, 0x02, 0xC4, 0x00, 0x44, 0x00, 0x06, 0x00,
0x20, 0x00, 0x21, 0x21, 0xA0, 0xC3, 0x1B, 0x03,
0x3C, 0x5D, 0xC9, 0x14, 0xE1, 0x42, 0x40, 0xC2,
0x20, 0x72, 0x24, 0x42, 0x5A, 0x68, 0x39, 0x31,
0x41, 0x59, 0x26, 0x53, 0x59, 0x65, 0x25, 0x30,
0x43, 0x00, 0x00, 0x00, 0x40, 0x02, 0xC0, 0x00,
0x20, 0x00, 0x00, 0x00, 0xA0, 0x00, 0x22, 0x1F,
0xA4, 0x19, 0x82, 0x58, 0x5D, 0xC9, 0x14, 0xE1,
0x42, 0x41, 0x94, 0x94, 0xC1, 0x0C,
}
newfile, err := Patch(oldfile, patchfile)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(newfile, newfilecomp) {
t.Fatal("expected:", newfilecomp, "got:", newfile)
}
// test invalid patch
_, err = Patch(oldfile, oldfile)
if err == nil {
t.Fail()
}
}
func TestCorruptHeader(t *testing.T) {
corruptPatch := []byte{
0x41, 0x53, 0x44, 0x49, 0x46, 0x46, 0x34, 0x30,
0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
_, err := Patch(corruptPatch, corruptPatch[:30])
if err == nil {
t.Fatal("header should be corrupt")
}
if err.Error()[:13] != "corrupt patch" {
t.Fatal("header should be corrupt (2)")
}
_, err = Patch(corruptPatch, corruptPatch)
if err == nil {
t.Fatal("header should be corrupt (3)")
}
if err.Error() != "corrupt patch (header BSDIFF40)" {
t.Fatal("header should be corrupt (4)")
}
corruptPatch[0] = 0x42
corruptLen := []byte{100, 0, 0, 0, 0, 0, 0, 128}
copy(corruptPatch[8:], corruptLen)
_, err = Patch(corruptPatch, corruptPatch)
if err == nil {
t.Fatal("header should be corrupt (5)")
}
if err.Error()[:15] != "corrupt patch (" {
t.Fatal("header should be corrupt (6)")
}
}
type lowcaprdr struct {
read []byte
n int
}
func (r *lowcaprdr) Read(b []byte) (int, error) {
if len(b) > 8 {
copy(r.read[r.n:], b[:8])
r.n += 8
return 8, nil
}
copy(r.read[r.n:], b)
r.n += len(b)
return len(b), nil
}
func TestZReadAll(t *testing.T) {
buf := []byte{
0x10, 0x10, 0x10, 0x10, 0x20, 0x20, 0x20, 0x20,
0x30, 0x30, 0x30, 0x30, 0x40, 0x40, 0x40, 0x40,
0x43,
}
rr := &lowcaprdr{
read: make([]byte, 1024),
}
nr, err := zreadall(rr, buf, len(buf))
if err != nil {
t.Fail()
}
if nr != len(buf) {
t.Fail()
}
if buf[16] != rr.read[16] {
t.Fail()
}
if buf[7] != rr.read[7] {
t.Fail()
}
}