aboutsummaryrefslogtreecommitdiff
path: root/ber
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-09-26 00:29:02 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-09-26 00:29:02 +0200
commit108e965580cbbae0d5029b7cc2ca592bd955883e (patch)
treeb031c9ab06f9913f2bb74f5e463ca1f0eea28092 /ber
parent4083fc0161bcf1a669ad0af50145ed92c8b776ce (diff)
Add len test
Diffstat (limited to 'ber')
-rw-r--r--ber/new/ber_test.go22
-rw-r--r--ber/new/class.go2
-rw-r--r--ber/new/dump.go16
-rw-r--r--ber/new/marshal.go2
-rw-r--r--ber/new/unmarshal.go5
5 files changed, 32 insertions, 15 deletions
diff --git a/ber/new/ber_test.go b/ber/new/ber_test.go
index 04d01e6..cdbed5a 100644
--- a/ber/new/ber_test.go
+++ b/ber/new/ber_test.go
@@ -162,3 +162,25 @@ func TestBitString(t *testing.T) {
}
}
}
+
+var lenTestData = map[int][]byte{
+ 0: {0x00},
+ 127: {0x7f},
+ 128: {0x81, 0x80},
+ 256: {0x82, 0x01, 0x00},
+}
+
+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)
+ }
+ }
+}
diff --git a/ber/new/class.go b/ber/new/class.go
index f0bdff8..094c804 100644
--- a/ber/new/class.go
+++ b/ber/new/class.go
@@ -128,7 +128,7 @@ func (s *state) ident() Header {
func (h Header) String() string {
f := "%v %v %v"
- if h.Kind != kindPrimitive {
+ if h.Class != classUniversal {
f = "%v %v %d"
}
return fmt.Sprintf(f, h.Class, h.Kind, h.Tag)
diff --git a/ber/new/dump.go b/ber/new/dump.go
index 42ca123..787ba11 100644
--- a/ber/new/dump.go
+++ b/ber/new/dump.go
@@ -8,19 +8,13 @@ func Dump(b []byte) string {
return newState(b).dump(0)
}
-func (s *state) dump(ident int) string {
+func (s *state) dump(indent int) string {
if s.Len() == 0 {
return ""
}
- fmt.Printf("%x %x\n", s.Len(), s.Bytes())
+ fmt.Println(s.Len(), s.Bytes())
h := s.ident()
- fmt.Printf("%*s", 2*ident, "")
- fmt.Println(h)
- for s.Len() > 0 {
- if h.Kind == kindConstructed {
- return s.subState().dump(ident + 1)
- }
- s.next()
- }
- return ""
+ fmt.Printf("%*s%v\n", 2*indent, "", h)
+ s.subState().dump(indent + 1)
+ return s.dump(indent)
}
diff --git a/ber/new/marshal.go b/ber/new/marshal.go
index c45e61b..45841ba 100644
--- a/ber/new/marshal.go
+++ b/ber/new/marshal.go
@@ -16,7 +16,7 @@ func (s *state) marshalLen(val int) {
n := lenLen(val)
s.WriteByte(byte(n) | 0x80)
for ; n > 0; n-- {
- s.WriteByte(byte(val >> uint(n-1) * 8))
+ s.WriteByte(byte(val >> uint((n-1)*8)))
}
}
diff --git a/ber/new/unmarshal.go b/ber/new/unmarshal.go
index 9fd27cd..b29eba5 100644
--- a/ber/new/unmarshal.go
+++ b/ber/new/unmarshal.go
@@ -5,9 +5,10 @@ func (s *state) unmarshalLen() int {
if b&0x80 == 0 {
return int(b)
}
+ l := int(b) & 0x7f
var n int
- for i, v := range s.Next(int(b)) {
- n |= int(v) << uint(i*8)
+ for i, v := range s.Next(l) {
+ n |= int(v) << uint((l-i-1)*8)
}
return n
}