| 1 |
package syntax |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestDecodeUnsupported(t *testing.T) { |
| 9 |
dummyBuffer := []byte(nil) |
| 10 |
|
| 11 |
var yi int |
| 12 |
read, err := Unmarshal(dummyBuffer, yi) |
| 13 |
if err == nil || read != 0 { |
| 14 |
t.Fatalf("Agreed to unmarshal to a non-pointer") |
| 15 |
} |
| 16 |
|
| 17 |
read, err = Unmarshal(dummyBuffer, nil) |
| 18 |
if err == nil || read != 0 { |
| 19 |
t.Fatalf("Agreed to unmarshal to a nil pointer") |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func TestDecodeErrors(t *testing.T) { |
| 24 |
vector0x20 := append([]byte{0x20}, buffer(0x20)...) |
| 25 |
errorCases := map[string]struct { |
| 26 |
template interface{} |
| 27 |
encoding []byte |
| 28 |
}{ |
| 29 |
"unsupported": { |
| 30 |
template: float64(0), |
| 31 |
encoding: buffer(0), |
| 32 |
}, |
| 33 |
|
| 34 |
"uint-too-small": { |
| 35 |
template: uint32(0), |
| 36 |
encoding: unhex("7fff"), |
| 37 |
}, |
| 38 |
|
| 39 |
"varint-too-big": { |
| 40 |
template: struct { |
| 41 |
V uint8 `tls:"varint"` |
| 42 |
}{}, |
| 43 |
encoding: unhex("7fff"), |
| 44 |
}, |
| 45 |
|
| 46 |
|
| 47 |
"no-head": { |
| 48 |
template: struct{ V []byte }{}, |
| 49 |
encoding: buffer(0), |
| 50 |
}, |
| 51 |
|
| 52 |
"overflow": { |
| 53 |
template: struct { |
| 54 |
V []byte `tls:"head=1,max=31"` |
| 55 |
}{}, |
| 56 |
encoding: vector0x20, |
| 57 |
}, |
| 58 |
|
| 59 |
"overflow-no-head": { |
| 60 |
template: struct { |
| 61 |
V []byte `tls:"head=none,max=31"` |
| 62 |
}{}, |
| 63 |
encoding: buffer(32), |
| 64 |
}, |
| 65 |
|
| 66 |
"underflow": { |
| 67 |
template: struct { |
| 68 |
V []byte `tls:"head=1,min=33"` |
| 69 |
}{}, |
| 70 |
encoding: vector0x20, |
| 71 |
}, |
| 72 |
|
| 73 |
"underflow-no-head": { |
| 74 |
template: struct { |
| 75 |
V []byte `tls:"head=none,min=33"` |
| 76 |
}{}, |
| 77 |
encoding: buffer(32), |
| 78 |
}, |
| 79 |
|
| 80 |
"too-short-for-head": { |
| 81 |
template: struct { |
| 82 |
V []byte `tls:"head=3"` |
| 83 |
}{}, |
| 84 |
encoding: vector0x20[:2], |
| 85 |
}, |
| 86 |
|
| 87 |
"too-short-for-value": { |
| 88 |
template: struct { |
| 89 |
V []byte `tls:"head=3"` |
| 90 |
}{}, |
| 91 |
encoding: vector0x20, |
| 92 |
}, |
| 93 |
|
| 94 |
"too-short-for-varint-head-length": { |
| 95 |
template: struct { |
| 96 |
V []byte `tls:"head=varint"` |
| 97 |
}{}, |
| 98 |
encoding: vector0x20[:0], |
| 99 |
}, |
| 100 |
|
| 101 |
"too-short-for-varint-head": { |
| 102 |
template: struct { |
| 103 |
V []byte `tls:"head=varint"` |
| 104 |
}{}, |
| 105 |
encoding: unhex("40"), |
| 106 |
}, |
| 107 |
|
| 108 |
"invalid-head-tag": { |
| 109 |
template: struct { |
| 110 |
V int `tls:"head=2"` |
| 111 |
}{V: 0}, |
| 112 |
encoding: unhex(""), |
| 113 |
}, |
| 114 |
|
| 115 |
"invalid-varint-tag": { |
| 116 |
template: struct { |
| 117 |
V struct{} `tls:"varint"` |
| 118 |
}{V: struct{}{}}, |
| 119 |
encoding: unhex(""), |
| 120 |
}, |
| 121 |
|
| 122 |
"invalid-optional-tag": { |
| 123 |
template: struct { |
| 124 |
V int `tls:"optional"` |
| 125 |
}{V: 0}, |
| 126 |
encoding: unhex(""), |
| 127 |
}, |
| 128 |
|
| 129 |
|
| 130 |
"invalid-optional-flag": { |
| 131 |
template: struct { |
| 132 |
V *uint8 `tls:"optional"` |
| 133 |
}{}, |
| 134 |
encoding: unhex("0203"), |
| 135 |
}, |
| 136 |
|
| 137 |
|
| 138 |
"invalid-validator": { |
| 139 |
template: CrypticString(""), |
| 140 |
encoding: unhex("056069677b6e"), |
| 141 |
}, |
| 142 |
} |
| 143 |
|
| 144 |
for label, testCase := range errorCases { |
| 145 |
decodedPointer := reflect.New(reflect.TypeOf(testCase.template)) |
| 146 |
read, err := Unmarshal(testCase.encoding, decodedPointer.Interface()) |
| 147 |
t.Logf("[%s] -> [%v]", label, err) |
| 148 |
if err == nil || read > 0 { |
| 149 |
t.Fatalf("Incorrectly allowed unmarshal [%s]: %v", label, err) |
| 150 |
} |
| 151 |
} |
| 152 |
} |