Skip to content

Commit d104a6c

Browse files
committed
Add error case tests for binary chunk parsing
1 parent 62569e0 commit d104a6c

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

gitdiff/base85.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func base85Decode(dst, src []byte) error {
2929
v = 85*v + uint32(b)
3030
n++
3131
} else {
32-
return fmt.Errorf("invalid base85 byte at index %d: 0x%x", i, b)
32+
return fmt.Errorf("invalid base85 byte at index %d: 0x%X", i, src[i])
3333
}
3434
if n == 5 {
3535
rem := len(dst) - ndst

gitdiff/parser_binary_test.go

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"io"
66
"reflect"
7+
"strings"
78
"testing"
89
)
910

@@ -115,23 +116,58 @@ func TestParseBinaryChunk(t *testing.T) {
115116
Input string
116117
Fragment BinaryFragment
117118
Output []byte
118-
Err bool
119+
Err string
119120
}{
120-
"newFile": {
121-
Input: "gcmZQzU|?i`U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx\n\n",
121+
"singleline": {
122+
Input: "TcmZQzU|?i`U?w2V48*Je09XJG\n\n",
122123
Fragment: BinaryFragment{
123-
Size: 40,
124+
Size: 20,
124125
},
125-
Output: fib(10),
126+
Output: fib(5, binary.BigEndian),
126127
},
127-
"newFileMultiline": {
128+
"multiline": {
128129
Input: "zcmZQzU|?i`U?w2V48*KJ%mKu_Kr9NxN<eH5#F0Qe0f=7$l~*z_FeL$%-)3N7vt?l5\n" +
129130
"zl3-vE2xVZ9%4J~CI>f->s?WfX|B-=Vs{#X~svra7Ekg#T|4s}nH;WnAZ)|1Y*`&cB\n" +
130131
"s(sh?X(Uz6L^!Ou&aF*u`J!eibJifSrv0z>$Q%Hd(^HIJ<Y?5`S0gT5UE&u=k\n\n",
131132
Fragment: BinaryFragment{
132133
Size: 160,
133134
},
134-
Output: fib(40),
135+
Output: fib(40, binary.BigEndian),
136+
},
137+
"shortLine": {
138+
Input: "A00\n\n",
139+
Err: "corrupt data line",
140+
},
141+
"underpaddedLine": {
142+
Input: "H00000000\n\n",
143+
Err: "corrupt data line",
144+
},
145+
"invalidLengthByte": {
146+
Input: "!00000\n\n",
147+
Err: "invalid length byte",
148+
},
149+
"miscountedLine": {
150+
Input: "H00000\n\n",
151+
Err: "incorrect byte count",
152+
},
153+
"invalidEncoding": {
154+
Input: "TcmZQzU|?i'U?w2V48*Je09XJG\n",
155+
Err: "invalid base85 byte",
156+
},
157+
"noTrailingEmptyLine": {
158+
Input: "TcmZQzU|?i`U?w2V48*Je09XJG\n",
159+
Err: "unexpected EOF",
160+
},
161+
"invalidCompression": {
162+
Input: "F007GV%KiWV\n\n",
163+
Err: "zlib",
164+
},
165+
"incorrectSize": {
166+
Input: "TcmZQzU|?i`U?w2V48*Je09XJG\n\n",
167+
Fragment: BinaryFragment{
168+
Size: 16,
169+
},
170+
Err: "16 byte fragment inflated to 20",
135171
},
136172
}
137173

@@ -141,9 +177,9 @@ func TestParseBinaryChunk(t *testing.T) {
141177

142178
frag := test.Fragment
143179
err := p.ParseBinaryChunk(&frag)
144-
if test.Err {
145-
if err == nil || err == io.EOF {
146-
t.Fatalf("expected error parsing binary chunk, but got %v", err)
180+
if test.Err != "" {
181+
if err == nil || !strings.Contains(err.Error(), test.Err) {
182+
t.Fatalf("expected error containing %q parsing binary chunk, but got %v", test.Err, err)
147183
}
148184
return
149185
}
@@ -157,15 +193,14 @@ func TestParseBinaryChunk(t *testing.T) {
157193
}
158194
}
159195

160-
func fib(n int) []byte {
161-
seq := []uint32{1, 1}
162-
for i := 2; i < n; i++ {
163-
seq = append(seq, seq[i-1]+seq[i-2])
164-
}
165-
196+
func fib(n int, ord binary.ByteOrder) []byte {
166197
buf := make([]byte, 4*n)
167-
for i, v := range seq[:n] {
168-
binary.BigEndian.PutUint32(buf[i*4:], v)
198+
for i := 0; i < len(buf); i += 4 {
199+
if i < 8 {
200+
ord.PutUint32(buf[i:], 1)
201+
} else {
202+
ord.PutUint32(buf[i:], ord.Uint32(buf[i-4:])+ord.Uint32(buf[i-8:]))
203+
}
169204
}
170205
return buf
171206
}

0 commit comments

Comments
 (0)