From 9c744184399e6684882081b0469b6a708b770ac2 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 29 Jun 2015 17:22:48 +0200 Subject: Adjust OID test --- ber/marshal.go | 2 +- ber/obj.go | 13 +++++++++---- ber/obj_test.go | 60 +++++++++++++++++++++++++++++++++++---------------------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/ber/marshal.go b/ber/marshal.go index b5096ec..c73c122 100644 --- a/ber/marshal.go +++ b/ber/marshal.go @@ -84,6 +84,6 @@ func intEncoder(e *encodeState, v reflect.Value) { } func objEncoder(e *encodeState, v reflect.Value) { - b := marshalObj(v.Interface().(Obj)) + b := marshalOID(v.Interface().(OID)) e.Write(b) } diff --git a/ber/obj.go b/ber/obj.go index 5482cd1..a03d198 100644 --- a/ber/obj.go +++ b/ber/obj.go @@ -1,6 +1,6 @@ package ber -type Obj []int +type OID []int func base128(n int) (b []byte) { if n == 0 { @@ -25,7 +25,7 @@ func debase128(b []byte) int { return 0 } -func marshalObj(obj Obj) (b []byte) { +func marshalOID(obj OID) (b []byte) { if len(obj) < 2 || obj[0] > 2 { return []byte{} } @@ -40,6 +40,11 @@ func marshalObj(obj Obj) (b []byte) { return b } -func unmarshalObj(b []byte) Obj { - return Obj{} +func unmarshalOID(b []byte) OID { + return OID{} +} + +func (o OID) Marshal() ([]byte, error) { + b := marshalOID(o) + return b, nil } diff --git a/ber/obj_test.go b/ber/obj_test.go index abe6832..baf5c7c 100644 --- a/ber/obj_test.go +++ b/ber/obj_test.go @@ -5,34 +5,48 @@ import ( "testing" ) -func testBase128(t *testing.T, n int, e []byte) { - a := base128(n) - if !bytes.Equal(a, e) { - t.Error("Base128", n, "expexted", e, "got", a) - } +type base128Test struct { + in int + out []byte } -func TestBase128(t *testing.T) { - testBase128(t, 643, []byte{0x85, 0x03}) - testBase128(t, 113549, []byte{0x86, 0xF7, 0x0D}) - testBase128(t, 49152, []byte{0x83, 0x80, 0x00}) +var base128TestData = []base128Test{ + {643, []byte{0x85, 0x03}}, + {113549, []byte{0x86, 0xF7, 0x0D}}, + {49152, []byte{0x83, 0x80, 0x00}}, } -func testObj(t *testing.T, o []int, e []byte) { - a := marshalObj(o) - if !bytes.Equal(a, e) { - t.Error("Obj", o, "expexted", e, "got", a) +func TestBase128(t *testing.T) { + for _, b := range base128TestData { + a := base128(b.in) + if !bytes.Equal(a, b.out) { + t.Error("Base128", b.in, "expected", b.out, "got", a) + } } } -func TestObj(t *testing.T) { - testObj(t, []int{1, 3, 12, 0, 218}, - []byte{0x2B, 0x0C, 0x00, 0x81, 0x5A}) - testObj(t, []int{1, 3, 12, 0, 285, 200}, - []byte{0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48}) - testObj(t, []int{}, []byte{}) - testObj(t, []int{0, 39}, []byte{0x27}) - testObj(t, []int{1, 39}, []byte{0x4f}) - testObj(t, []int{1, 40}, []byte{}) - testObj(t, []int{2, 40}, []byte{0x78}) +type oidTest struct { + oid OID + out []byte +} + +var oidTestData = []oidTest{ + {OID{1, 3, 12, 0, 218}, + []byte{0x2B, 0x0C, 0x00, 0x81, 0x5A}}, + {OID{1, 3, 12, 0, 285, 200}, + []byte{0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48}}, + {OID{}, []byte{}}, + {OID{0, 39}, []byte{0x27}}, + {OID{1, 39}, []byte{0x4f}}, + {OID{1, 40}, []byte{}}, + {OID{2, 40}, []byte{0x78}}, +} + +func TestOID(t *testing.T) { + for _, o := range oidTestData { + a := marshalOID(o.oid) + if !bytes.Equal(a, o.out) { + t.Error("OID", o.oid, "expected", o.out, "got", a) + } + } } -- cgit v1.2.3