diff options
Diffstat (limited to 'ber/new/ber_test.go')
-rw-r--r-- | ber/new/ber_test.go | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/ber/new/ber_test.go b/ber/new/ber_test.go new file mode 100644 index 0000000..47974c5 --- /dev/null +++ b/ber/new/ber_test.go @@ -0,0 +1,164 @@ +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}, +} + +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 +} + +var oidTestData = []oidTest{ + { + val: OID{1, 3, 12, 0, 218}, + out: []byte{0x05, 0x2B, 0x0C, 0x00, 0x81, 0x5A}, + }, + { + val: OID{1, 3, 12, 0, 285, 200}, + out: []byte{0x07, 0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48}, + }, + { + val: OID{0, 39}, + out: []byte{0x01, 0x27}, + }, + { + val: OID{1, 39}, + out: []byte{0x01, 0x4f}, + }, + { + val: OID{2, 40}, + out: []byte{0x01, 0x78}, + }, + { + val: OID{}, + out: []byte{}, + }, + { + val: OID{1, 40}, + out: []byte{}, + }, +} + +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) { + 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) + } + } +} |