Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch int64 encoding #7

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/lor00x/goldap

go 1.16
28 changes: 21 additions & 7 deletions message/asn1.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,32 @@ func parseInt64(bytes []byte) (ret int64, err error) {
}

func sizeInt64(i int64) (size int) {
for ; i != 0 || size == 0; i >>= 8 {
size++
n := 1

for i > 127 {
n++
i >>= 8
}
return

for i < -128 {
n++
i >>= 8
}

return n
}

func writeInt64(bytes *Bytes, i int64) (size int) {
for ; i != 0 || size == 0; i >>= 8 { // Write at least one byte even if the value is 0
bytes.writeBytes([]byte{byte(i)})
size++
n := sizeInt64(i)
buf := [8]byte{}

for j := 0; j < n; j++ {
b := i >> uint((n-1-j)*8)
buf[j] = byte(b)
}
return
bytes.writeBytes(buf[:n])

return n
}

// parseInt treats the given bytes as a big-endian, signed integer and returns
Expand Down
54 changes: 54 additions & 0 deletions message/asn1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package message

import (
"testing"
"bytes"
)

func TestSizeInt64(t *testing.T) {
s := sizeInt64(0)
if s != 1 {
t.Errorf("computed size is %d, expected 1", s)
}

s = sizeInt64(127)
if s != 1 {
t.Errorf("computed size is %d, expected 1", s)
}

s = sizeInt64(128)
if s != 2 {
t.Errorf("computed size is %d, expected 2", s)
}

s = sizeInt64(50000)
if s != 3 {
t.Errorf("computed size is %d, expected 3", s)
}

s = sizeInt64(-12345)
if s != 2 {
t.Errorf("computed size is %d, expected 2", s)
}
}

func TestWriteInt64(t *testing.T) {
vtests := []int64{0, 127, 128, 50000, -12345}
expsize := []int{1, 1, 2, 3, 2}
expresult := [][]byte{{0x00}, {0x7F}, {0x00, 0x80}, {0x00, 0xc3, 0x50}, {0xcf, 0xc7}}

for idx, v := range vtests {
fs := sizeInt64(v)
b := NewBytes(fs, make([]byte, fs))
t.Log("computing", v)
s := writeInt64(b, v)
if s != expsize[idx] {
t.Errorf("computed size is %d, expected %d", s, expsize[idx])
}
if !bytes.Equal(b.Bytes(), expresult[idx]) {
t.Errorf("wrong computed bytes, got %v, expected %v", b.Bytes(), expresult[idx])
}
a, e := parseInt64(b.Bytes())
t.Log("parse", a, e)
}
}
36 changes: 36 additions & 0 deletions message/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package message

import (
"testing"
"fmt"
)

func toHex(b []byte) (r string) {
r = "[ "
for _, e := range b {
r += fmt.Sprintf("0x%x ", e)
}
return r + "]"
}

func TestMessageID(t *testing.T) {
m := NewLDAPMessageWithProtocolOp(UnbindRequest{})
m.SetMessageID(128)
buf, err := m.Write()
if err != nil {
t.Errorf("marshalling failed with %v", err)
}
t.Logf("%v", toHex(buf.Bytes()))

ret, err := ReadLDAPMessage(NewBytes(0, buf.Bytes()))
if err != nil {
t.Errorf("unmarshalling failed with %v", err)
}
if _, ok := ret.ProtocolOp().(UnbindRequest); !ok {
t.Errorf("should be an unbind request")
}
if ret.MessageID() != 128 {
t.Errorf("Expect message id 128, got %d", ret.MessageID())
}
t.Log("Done, marshal/unmarshall worked")
}