2018-08-13 07:48:32 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"math/rand"
|
|
|
|
|
2018-08-15 07:17:18 +00:00
|
|
|
osm "git.loafle.net/overflow/service_matcher-go"
|
2018-08-13 07:48:32 +00:00
|
|
|
"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()
|
|
|
|
|
2018-09-03 13:36:57 +00:00
|
|
|
matchCtx := osm.NewMatchCtx("192.168.1.10", 10389)
|
|
|
|
|
|
|
|
for i := 0; i < m.PacketCount(matchCtx); i++ {
|
|
|
|
_, err := conn.Write(m.Packet(matchCtx, i).Buffer)
|
2018-08-13 07:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
bytes := make([]byte, 1024)
|
|
|
|
n, _ := conn.Read(bytes)
|
2018-08-15 07:17:18 +00:00
|
|
|
p := osm.NewPacket(bytes, n)
|
2018-08-13 07:48:32 +00:00
|
|
|
|
2018-09-18 03:42:15 +00:00
|
|
|
if err := m.Match(matchCtx, i, p); err != nil {
|
2018-08-13 07:48:32 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 13:36:57 +00:00
|
|
|
t.Log(m.Name(matchCtx))
|
|
|
|
t.Log(matchCtx)
|
2018-08-13 07:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|