service_matcher-go/ldap/ldap_test.go
crusader bce329e44e ing
2018-10-23 13:31:25 +09:00

81 lines
2.0 KiB
Go

package ldap
import (
"math/rand"
"net"
"testing"
"time"
osm "git.loafle.net/overflow/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()
matchCtx := osm.NewMatchCtx("192.168.1.10", 10389)
for i := 0; i < m.PacketCount(matchCtx); i++ {
_, err := conn.Write(m.Packet(matchCtx, i).Buffer)
if err != nil {
t.Error(err)
}
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := osm.NewPacket(bytes, n)
if err := m.Match(matchCtx, i, p); err != nil {
t.Error(err)
}
}
t.Log(m.Name(matchCtx))
t.Log(matchCtx)
}
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")
}
}