Skip to content

Commit effdc98

Browse files
Add RFC4511-compliant boolean type (#25)
1 parent 29be175 commit effdc98

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ber.go

+16
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,22 @@ func NewBoolean(ClassType Class, TagType Type, Tag Tag, Value bool, Description
472472
return p
473473
}
474474

475+
// NewLDAPBoolean returns a RFC 4511-compliant Boolean packet
476+
func NewLDAPBoolean(Value bool, Description string) *Packet {
477+
intValue := int64(0)
478+
479+
if Value {
480+
intValue = 255
481+
}
482+
483+
p := Encode(ClassUniversal, TypePrimitive, TagBoolean, nil, Description)
484+
485+
p.Value = Value
486+
p.Data.Write(encodeInteger(intValue))
487+
488+
return p
489+
}
490+
475491
func NewInteger(ClassType Class, TagType Type, Tag Tag, Value interface{}, Description string) *Packet {
476492
p := Encode(ClassType, TagType, Tag, nil, Description)
477493

ber_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ func TestBoolean(t *testing.T) {
4242

4343
}
4444

45+
func TestLDAPBoolean(t *testing.T) {
46+
var value bool = true
47+
48+
packet := NewLDAPBoolean(value, "first Packet, True")
49+
50+
newBoolean, ok := packet.Value.(bool)
51+
if !ok || newBoolean != value {
52+
t.Error("error during creating packet")
53+
}
54+
55+
encodedPacket := packet.Bytes()
56+
57+
newPacket := DecodePacket(encodedPacket)
58+
59+
newBoolean, ok = newPacket.Value.(bool)
60+
if !ok || newBoolean != value {
61+
t.Error("error during decoding packet")
62+
}
63+
64+
}
65+
4566
func TestInteger(t *testing.T) {
4667
var value int64 = 10
4768

0 commit comments

Comments
 (0)