this repo has no description
0
fork

Configure Feed

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

Add error case tests for binary chunk parsing

+54 -19
+1 -1
gitdiff/base85.go
··· 29 29 v = 85*v + uint32(b) 30 30 n++ 31 31 } 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]) 33 33 } 34 34 if n == 5 { 35 35 rem := len(dst) - ndst
+53 -18
gitdiff/parser_binary_test.go
··· 4 4 "encoding/binary" 5 5 "io" 6 6 "reflect" 7 + "strings" 7 8 "testing" 8 9 ) 9 10 ··· 115 116 Input string 116 117 Fragment BinaryFragment 117 118 Output []byte 118 - Err bool 119 + Err string 119 120 }{ 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", 122 123 Fragment: BinaryFragment{ 123 - Size: 40, 124 + Size: 20, 124 125 }, 125 - Output: fib(10), 126 + Output: fib(5, binary.BigEndian), 126 127 }, 127 - "newFileMultiline": { 128 + "multiline": { 128 129 Input: "zcmZQzU|?i`U?w2V48*KJ%mKu_Kr9NxN<eH5#F0Qe0f=7$l~*z_FeL$%-)3N7vt?l5\n" + 129 130 "zl3-vE2xVZ9%4J~CI>f->s?WfX|B-=Vs{#X~svra7Ekg#T|4s}nH;WnAZ)|1Y*`&cB\n" + 130 131 "s(sh?X(Uz6L^!Ou&aF*u`J!eibJifSrv0z>$Q%Hd(^HIJ<Y?5`S0gT5UE&u=k\n\n", 131 132 Fragment: BinaryFragment{ 132 133 Size: 160, 133 134 }, 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", 135 171 }, 136 172 } 137 173 ··· 141 177 142 178 frag := test.Fragment 143 179 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) 147 183 } 148 184 return 149 185 } ··· 157 193 } 158 194 } 159 195 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 { 166 197 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 + } 169 204 } 170 205 return buf 171 206 }