File: encode_test.go

package info (click to toggle)
golang-github-bifurcation-mint 0.0~git20200214.93c820e-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 632 kB
  • sloc: makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package syntax

import (
	"strings"
	"testing"
)

func TestEncodeErrors(t *testing.T) {
	errorCases := map[string]interface{}{
		"unsupported": float64(0),

		"varint-too-big": struct {
			V uint64 `tls:"varint"`
		}{V: uint64(1) << 63},

		"no-head": struct {
			V []byte
		}{V: buffer(0x20)},

		"head-too-short": struct {
			V []byte `tls:"head=1"`
		}{V: buffer(0x100)},

		"overflow": struct {
			V []byte `tls:"head=1,max=31"`
		}{V: buffer(0x20)},

		"underflow": struct {
			V []byte `tls:"head=1,min=33"`
		}{V: buffer(0x20)},

		"nil": struct{ V *uint8 }{V: nil},

		"invalid-head-tag": struct {
			V int `tls:"head=2"`
		}{V: 0},

		"invalid-varint-tag": struct {
			V struct{} `tls:"varint"`
		}{V: struct{}{}},

		"invalid-optional-tag": struct {
			V int `tls:"optional"`
		}{V: 0},

		"invalid-validator": CrypticString(strings.Repeat("A", 257)),
	}

	for label, badValue := range errorCases {
		encoded, err := Marshal(badValue)
		t.Logf("[%s] -> [%v]", label, err)
		if err == nil {
			t.Fatalf("Incorrectly allowed marshal [%s]: [%x]", label, encoded)
		}
	}
}