this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

+69 -13
+9 -13
gitdiff/base85.go
··· 4 4 "fmt" 5 5 ) 6 6 7 - const ( 8 - base85Alphabet = "0123456789" + 9 - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + 10 - "abcdefghijklmnopqrstuvwxyz" + 11 - "!#$%&()*+-;<=>?@^_`{|}~" 12 - ) 13 - 14 7 var ( 15 - de85 map[byte]byte 8 + b85Table map[byte]byte 9 + b85Alpha = []byte( 10 + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "!#$%&()*+-;<=>?@^_`{|}~", 11 + ) 16 12 ) 17 13 18 14 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) 22 18 } 23 19 } 24 20 ··· 29 25 var v uint32 30 26 var n, ndst int 31 27 for i, b := range src { 32 - if b, ok := de85[b]; ok { 28 + if b, ok := b85Table[b]; ok { 33 29 v = 85*v + uint32(b) 34 30 n++ 35 31 } else { ··· 50 46 return fmt.Errorf("base85 data terminated by underpadded sequence") 51 47 } 52 48 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)) 54 50 } 55 51 return nil 56 52 }
+60
gitdiff/base85_test.go
··· 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 + }