service_matcher-go/ldap/ldap_test.go

81 lines
1.9 KiB
Go
Raw Normal View History

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()
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)
2018-08-15 07:17:18 +00:00
p := osm.NewPacket(bytes, n)
2018-08-13 07:48:32 +00:00
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")
}
}