Skip to content

Commit 62569e0

Browse files
committed
Add dedicated tests for base85 decoding
These are fairly basic as the decoder is also exercised by the fragment parsing tests, but they cover some errror cases that may not be covered otherwise.
1 parent 8ce8cba commit 62569e0

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

gitdiff/base85.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ import (
44
"fmt"
55
)
66

7-
const (
8-
base85Alphabet = "0123456789" +
9-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
10-
"abcdefghijklmnopqrstuvwxyz" +
11-
"!#$%&()*+-;<=>?@^_`{|}~"
12-
)
13-
147
var (
15-
de85 map[byte]byte
8+
b85Table map[byte]byte
9+
b85Alpha = []byte(
10+
"0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "!#$%&()*+-;<=>?@^_`{|}~",
11+
)
1612
)
1713

1814
func init() {
19-
de85 = make(map[byte]byte)
20-
for i, c := range base85Alphabet {
21-
de85[byte(c)] = byte(i)
15+
b85Table = make(map[byte]byte)
16+
for i, c := range b85Alpha {
17+
b85Table[c] = byte(i)
2218
}
2319
}
2420

@@ -29,7 +25,7 @@ func base85Decode(dst, src []byte) error {
2925
var v uint32
3026
var n, ndst int
3127
for i, b := range src {
32-
if b, ok := de85[b]; ok {
28+
if b, ok := b85Table[b]; ok {
3329
v = 85*v + uint32(b)
3430
n++
3531
} else {
@@ -50,7 +46,7 @@ func base85Decode(dst, src []byte) error {
5046
return fmt.Errorf("base85 data terminated by underpadded sequence")
5147
}
5248
if ndst < len(dst) {
53-
return fmt.Errorf("base85 data is too short: %d < %d", ndst, len(dst))
49+
return fmt.Errorf("base85 data underrun: %d < %d", ndst, len(dst))
5450
}
5551
return nil
5652
}

gitdiff/base85_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package gitdiff
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestBase85Decode(t *testing.T) {
8+
tests := map[string]struct {
9+
Input string
10+
Output []byte
11+
Err bool
12+
}{
13+
"twoBytes": {
14+
Input: "%KiWV",
15+
Output: []byte{0xCA, 0xFE},
16+
},
17+
"fourBytes": {
18+
Input: "007GV",
19+
Output: []byte{0x0, 0x0, 0xCA, 0xFE},
20+
},
21+
"sixBytes": {
22+
Input: "007GV%KiWV",
23+
Output: []byte{0x0, 0x0, 0xCA, 0xFE, 0xCA, 0xFE},
24+
},
25+
"invalidCharacter": {
26+
Input: "00'GV",
27+
Err: true,
28+
},
29+
"underpaddedSequence": {
30+
Input: "007G",
31+
Err: true,
32+
},
33+
"dataUnderrun": {
34+
Input: "007GV",
35+
Output: make([]byte, 8),
36+
Err: true,
37+
},
38+
}
39+
40+
for name, test := range tests {
41+
t.Run(name, func(t *testing.T) {
42+
dst := make([]byte, len(test.Output))
43+
err := base85Decode(dst, []byte(test.Input))
44+
if test.Err {
45+
if err == nil {
46+
t.Fatalf("expected error decoding base85 data, but got nil")
47+
}
48+
return
49+
}
50+
if err != nil {
51+
t.Fatalf("unexpected error decoding base85 data: %v", err)
52+
}
53+
for i, b := range test.Output {
54+
if dst[i] != b {
55+
t.Errorf("incorrect byte at index %d: expected 0x%X, actual 0x%X", i, b, dst[i])
56+
}
57+
}
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)