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) } } }