| 1 | package syntax |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestEncodeErrors(t *testing.T) { |
| 9 | errorCases := map[string]interface{}{ |
| 10 | "unsupported": float64(0), |
| 11 | |
| 12 | "varint-too-big": struct { |
| 13 | V uint64 `tls:"varint"` |
| 14 | }{V: uint64(1) << 63}, |
| 15 | |
| 16 | "no-head": struct { |
| 17 | V []byte |
| 18 | }{V: buffer(0x20)}, |
| 19 | |
| 20 | "head-too-short": struct { |
| 21 | V []byte `tls:"head=1"` |
| 22 | }{V: buffer(0x100)}, |
| 23 | |
| 24 | "overflow": struct { |
| 25 | V []byte `tls:"head=1,max=31"` |
| 26 | }{V: buffer(0x20)}, |
| 27 | |
| 28 | "underflow": struct { |
| 29 | V []byte `tls:"head=1,min=33"` |
| 30 | }{V: buffer(0x20)}, |
| 31 | |
| 32 | "nil": struct{ V *uint8 }{V: nil}, |
| 33 | |
| 34 | "invalid-head-tag": struct { |
| 35 | V int `tls:"head=2"` |
| 36 | }{V: 0}, |
| 37 | |
| 38 | "invalid-varint-tag": struct { |
| 39 | V struct{} `tls:"varint"` |
| 40 | }{V: struct{}{}}, |
| 41 | |
| 42 | "invalid-optional-tag": struct { |
| 43 | V int `tls:"optional"` |
| 44 | }{V: 0}, |
| 45 | |
| 46 | "invalid-validator": CrypticString(strings.Repeat("A", 257)), |
| 47 | } |
| 48 | |
| 49 | for label, badValue := range errorCases { |
| 50 | encoded, err := Marshal(badValue) |
| 51 | t.Logf("[%s] -> [%v]", label, err) |
| 52 | if err == nil { |
| 53 | t.Fatalf("Incorrectly allowed marshal [%s]: [%x]", label, encoded) |
| 54 | } |
| 55 | } |
| 56 | } |