aboutsummaryrefslogtreecommitdiff
path: root/ber/ber_test.go
blob: c1beb3249872d0ffea41379ceaa5232919de7713 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package ber

import (
	"bytes"
	"testing"
)

var boolTestData = map[bool][]byte{
	true:  {0x01, 0xff},
	false: {0x01, 0x00},
}

func TestBool(t *testing.T) {
	for val, out := range boolTestData {
		s := state{}
		s.marshalBool(val)
		o := s.Bytes()
		if !bytes.Equal(o, out) {
			t.Error(val, "expeced", out, "got", o)
		}
		v := s.unmarshalBool()
		if v != val {
			t.Error(out, "expected", val, "got", v)
		}
	}
}

var intTestData = map[int][]byte{
	0:         {0x01, 0x00},
	127:       {0x01, 0x7f},
	128:       {0x02, 0x00, 0x80},
	256:       {0x02, 0x01, 0x00},
	-128:      {0x01, 0x80},
	-129:      {0x02, 0xff, 0x7f},
	8388607:   {0x03, 0x7f, 0xff, 0xff},
	-8388607:  {0x03, 0x80, 0x00, 0x01},
	-136:      {0x02, 0xff, 0x78},
	0x1310009: {0x04, 0x01, 0x31, 0x00, 0x09},
}

func TestInt(t *testing.T) {
	for val, out := range intTestData {
		s := state{}
		s.marshalInt(val)
		o := s.Bytes()
		if !bytes.Equal(o, out) {
			t.Error(val, "expected", out, "got", o)
		}
		v := s.unmarshalInt()
		if v != val {
			t.Error(out, "expected", val, "got", v)
		}
	}
}

var stringTestData = map[string][]byte{
	"111":  {0x03, 0x031, 0x031, 0x031},
	"0A16": {0x04, 0x30, 0x41, 0x31, 0x36},
}

func TestString(t *testing.T) {
	for val, out := range stringTestData {
		s := state{}
		s.marshalString(val)
		o := s.Bytes()
		if !bytes.Equal(o, out) {
			t.Error(val, "expected", out, "got", o)
		}
		v := s.unmarshalString()
		if v != val {
			t.Error(out, "expected", val, "got", v)
		}
	}
}

type oidTest struct {
	val   OID
	out   []byte
	valid bool
}

var oidTestData = []oidTest{
	{
		val:   OID{1, 3, 12, 0, 218},
		out:   []byte{0x05, 0x2B, 0x0C, 0x00, 0x81, 0x5A},
		valid: true,
	},
	{
		val:   OID{1, 3, 12, 0, 285, 200},
		out:   []byte{0x07, 0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48},
		valid: true,
	},
	{
		val:   OID{0, 39},
		out:   []byte{0x01, 0x27},
		valid: true,
	},
	{
		val:   OID{1, 39},
		out:   []byte{0x01, 0x4f},
		valid: true,
	},
	{
		val:   OID{2, 40},
		out:   []byte{0x01, 0x78},
		valid: true,
	},
	{
		val:   OID{},
		out:   []byte{},
		valid: true,
	},
	{
		val:   OID{1, 40},
		out:   []byte{},
		valid: false,
	},
}

func TestOID(t *testing.T) {
	for _, test := range oidTestData {
		s := state{}
		s.marshalOID(test.val)
		o := s.Bytes()
		if !bytes.Equal(o, test.out) {
			t.Error(test.val, "expected", test.out, "got", o)
		}
		v := s.unmarshalOID()
		if !v.Equal(test.val) && test.valid {
			t.Error(test.out, "expected", test.val, "got", v)
		}
	}
}

type bitStringTest struct {
	val BitString
	out []byte
}

var bitStringTestData = []bitStringTest{
	{
		val: BitString{
			false, false, false, false,
			true, false, false, false,
			false, false, false, false,
			false, false, false, false,
		},
		out: []byte{0x03, 0x00, 0x08, 0x00},
	},
	{
		val: BitString{true},
		out: []byte{0x02, 0x07, 0x80},
	},
	{
		val: BitString{false, false, true, false},
		out: []byte{0x02, 0x04, 0x20},
	},
}

func TestBitString(t *testing.T) {
	for _, test := range bitStringTestData {
		s := state{}
		s.marshalBitString(test.val)
		o := s.Bytes()
		if !bytes.Equal(o, test.out) {
			t.Error(test.val, "expected", test.out, "got", o)
		}
		v := s.unmarshalBitString()
		if !v.Equal(test.val) {
			t.Error(test.out, "expected", test.val, "got", v)
		}
	}
}

var lenTestData = map[int][]byte{
	0:       {0x00},
	127:     {0x7f},
	128:     {0x81, 0x80},
	256:     {0x82, 0x01, 0x00},
	8388607: {0x83, 0x7f, 0xff, 0xff},
	//129:     {0x80, 0x81, 0x00, 0x00}, // indefinite length
}

func TestLen(t *testing.T) {
	for val, out := range lenTestData {
		s := state{}
		s.marshalLen(val)
		o := s.Bytes()
		if !bytes.Equal(o, out) {
			t.Error(val, "expected", out, "got", o)
		}
		v := s.unmarshalLen()
		if v != val {
			t.Error(out, "expected", val, "got", v)
		}
	}
}

var classTestData = map[Header][]byte{
	{classUniversal, kindPrimitive, tagInteger}:    {0x02},
	{classUniversal, kindConstructed, tagSequence}: {0x30},
	{classApplication, kindPrimitive, 2}:           {0x42},
	{classApplication, kindConstructed, 2}:         {0x62},
	{classContextSpecific, kindPrimitive, 1}:       {0x81},
	{classContextSpecific, kindConstructed, 1}:     {0xa1},
	{classPrivate, kindPrimitive, 32}:              {0xdf, 0x20},
	{classPrivate, kindConstructed, 256}:           {0xff, 0x82, 0x00},
}

func TestClass(t *testing.T) {
	for val, out := range classTestData {
		s := state{}
		s.marshalClass(val)
		o := s.Bytes()
		if !bytes.Equal(o, out) {
			t.Error(val, "expected", out, "got", o)
		}
		v := s.unmarshalClass()
		if v != val {
			t.Error(out, "expected", val, "got", v)
		}
	}
}