81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package ldap
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"math/rand"
|
|
|
|
csm "git.loafle.net/commons/service_matcher-go"
|
|
"gopkg.in/asn1-ber.v1"
|
|
)
|
|
|
|
func TestLdap(t *testing.T) {
|
|
m := NewMatcher()
|
|
|
|
conn, err := net.Dial("tcp", "192.168.1.10:10389")
|
|
if err != nil {
|
|
t.Errorf("ERR %s", err)
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
for i := 0; i < m.PacketCount(); i++ {
|
|
_, err := conn.Write(m.Packet(i).Buffer)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
bytes := make([]byte, 1024)
|
|
n, _ := conn.Read(bytes)
|
|
p := csm.NewPacket(bytes, n)
|
|
|
|
if err := m.Match(nil, i, p); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
t.Log(m.Name())
|
|
t.Log(m.Meta())
|
|
}
|
|
|
|
func TestBer(t *testing.T) {
|
|
const (
|
|
ApplicationBindRequest = 0
|
|
ApplicationBindResponse = 1
|
|
)
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
reqID := rand.Int63n(1000)
|
|
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
|
|
|
|
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, reqID, "MessageID"))
|
|
bindRequest := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
|
|
bindRequest.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 2, "Version"))
|
|
bindRequest.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "LOAFLEOVERFLOW", "User Name"))
|
|
bindRequest.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, 0, "LOAFLEOVERFLOW", "Password"))
|
|
packet.AppendChild(bindRequest)
|
|
|
|
conn, err := net.Dial("tcp", "192.168.1.10:10389")
|
|
if err != nil {
|
|
t.Errorf("ERR %s", err)
|
|
}
|
|
conn.Write(packet.Bytes())
|
|
|
|
p, err := ber.ReadPacket(conn)
|
|
if err != nil {
|
|
t.Errorf("ERR %s", err)
|
|
}
|
|
|
|
respID, ok := p.Children[0].Value.(int64)
|
|
if !ok {
|
|
t.Errorf("%s", "cannot cast response ID")
|
|
}
|
|
if respID != reqID {
|
|
t.Error("not matched message ID")
|
|
}
|
|
|
|
if p.Children[1].Tag != ApplicationBindResponse {
|
|
t.Error("Not match")
|
|
}
|
|
}
|