Skip to content

Commit

Permalink
Add RFC4511-compliant boolean type (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbrowning authored Feb 3, 2020
1 parent 29be175 commit effdc98
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ber.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,22 @@ func NewBoolean(ClassType Class, TagType Type, Tag Tag, Value bool, Description
return p
}

// NewLDAPBoolean returns a RFC 4511-compliant Boolean packet
func NewLDAPBoolean(Value bool, Description string) *Packet {
intValue := int64(0)

if Value {
intValue = 255
}

p := Encode(ClassUniversal, TypePrimitive, TagBoolean, nil, Description)

p.Value = Value
p.Data.Write(encodeInteger(intValue))

return p
}

func NewInteger(ClassType Class, TagType Type, Tag Tag, Value interface{}, Description string) *Packet {
p := Encode(ClassType, TagType, Tag, nil, Description)

Expand Down
21 changes: 21 additions & 0 deletions ber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ func TestBoolean(t *testing.T) {

}

func TestLDAPBoolean(t *testing.T) {
var value bool = true

packet := NewLDAPBoolean(value, "first Packet, True")

newBoolean, ok := packet.Value.(bool)
if !ok || newBoolean != value {
t.Error("error during creating packet")
}

encodedPacket := packet.Bytes()

newPacket := DecodePacket(encodedPacket)

newBoolean, ok = newPacket.Value.(bool)
if !ok || newBoolean != value {
t.Error("error during decoding packet")
}

}

func TestInteger(t *testing.T) {
var value int64 = 10

Expand Down

0 comments on commit effdc98

Please sign in to comment.