Example #1
0
func TestLocalSearchPagingWithHandler(t *testing.T) {
	fmt.Printf("TestLocalSearchPagingWithHandler: starting...\n")

	l := ldap.NewLDAPConnection(server, port)
	err := l.Connect()

	// l.Debug = true
	if err != nil {
		t.Error(err)
		return
	}
	defer l.Close()

	err = l.Bind(binddn, passwd)
	if err != nil {
		t.Error(err)
		return
	}
	search_request := ldap.NewSimpleSearchRequest(
		base_dn,
		ldap.ScopeWholeSubtree,
		filters[0],
		attributes,
	)

	l.Debug = false
	pagingControl := ldap.NewControlPaging(2)
	search_request.Controls = append(search_request.Controls, pagingControl)

	for {
		sr := new(ldap.SearchResult)
		err = l.SearchWithHandler(search_request, sr, nil)
		if err != nil {
			t.Error(err)
			return
		}
		_, pagingResponsePacket := ldap.FindControl(sr.Controls, ldap.ControlTypePaging)
		if pagingResponsePacket == nil {
			t.Errorf("Expected Paging Control.")
		}
		pagingControl.Cookie = pagingResponsePacket.(*ldap.ControlPaging).Cookie
		ldap.ReplaceControl(search_request.Controls, pagingControl)
		fmt.Printf("TestLocalSearchPagingWithHandler: %s entries = %d, Referrals = %d\n",
			search_request.Filter, len(sr.Entries), len(sr.Referrals))
		if len(pagingControl.Cookie) == 0 {
			return
		}
	}
}
Example #2
0
func TestLocalOrderedSearch(t *testing.T) {
	fmt.Printf("TestLocalOrderedSearch: starting...\n")
	l := ldap.NewLDAPConnection(server, port)
	err := l.Connect()

	// l.Debug = true
	if err != nil {
		t.Error(err)
		return
	}
	defer l.Close()

	err = l.Bind(binddn, passwd)
	if err != nil {
		t.Error(err)
		return
	}
	search_request := ldap.NewSimpleSearchRequest(
		base_dn,
		ldap.ScopeWholeSubtree,
		filters[3],
		attributes,
	)

	serverSideSortAttrRuleOrder := ldap.ServerSideSortAttrRuleOrder{
		AttributeName: "cn",
		OrderingRule:  "",
		ReverseOrder:  false,
	}
	sortKeyList := make([]ldap.ServerSideSortAttrRuleOrder, 0, 1)
	sortKeyList = append(sortKeyList, serverSideSortAttrRuleOrder)
	sortControl := ldap.NewControlServerSideSortRequest(sortKeyList, true)
	fmt.Println(sortControl.String())
	search_request.AddControl(sortControl)
	l.Debug = false
	sr, err := l.Search(search_request)
	if err != nil {
		t.Error(err)
		return
	}
	_, sssResponse := ldap.FindControl(sr.Controls, ldap.ControlTypeServerSideSortResponse)
	if sssResponse != nil {
		fmt.Println(sssResponse.String())
	}
	fmt.Printf("TestLocalSearch: %s -> num of entries = %d\n", search_request.Filter, len(sr.Entries))
}
Example #3
0
func TestLocalVlvSearch(t *testing.T) {
	fmt.Printf("TestLocalVlvSearch: starting...\n")
	l := ldap.NewLDAPConnection(server, port)
	err := l.Connect()

	// l.Debug = true
	if err != nil {
		t.Error(err)
		return
	}
	defer l.Close()

	err = l.Bind(binddn, passwd)
	if err != nil {
		t.Error(err)
		return
	}
	search_request := ldap.NewSimpleSearchRequest(
		base_dn,
		ldap.ScopeWholeSubtree,
		"(cn=*)",
		attributes,
	)
	vlvControl := new(ldap.ControlVlvRequest)
	vlvControl.BeforeCount = 0
	vlvControl.AfterCount = 3

	offset := new(ldap.VlvOffSet)
	offset.Offset = 1
	offset.ContentCount = 3

	vlvControl.ByOffset = offset

	//pack, _ := vlvControl.Encode()
	//fmt.Println(hex.Dump(pack.Bytes()))

	search_request.AddControl(vlvControl)

	serverSideSortAttrRuleOrder := ldap.ServerSideSortAttrRuleOrder{
		AttributeName: "cn",
		OrderingRule:  "",
		ReverseOrder:  false,
	}
	sortKeyList := make([]ldap.ServerSideSortAttrRuleOrder, 0, 1)
	sortKeyList = append(sortKeyList, serverSideSortAttrRuleOrder)
	sortControl := ldap.NewControlServerSideSortRequest(sortKeyList, true)
	search_request.AddControl(sortControl)

	l.Debug = false
	sr, err := l.Search(search_request)
	if err != nil {
		t.Error(err)
		return
	}
	_, vlvResp := ldap.FindControl(sr.Controls, ldap.ControlTypeVlvResponse)
	if vlvResp != nil {
		fmt.Println(vlvResp.String())
	}
	for _, entry := range sr.Entries {
		fmt.Println(entry.GetAttributeValues("cn")[0])
	}
	fmt.Printf("TestLocalVlvSearch (byOffSet): %s -> num of entries = %d\n", search_request.Filter, len(sr.Entries))

	search_request = ldap.NewSimpleSearchRequest(
		base_dn,
		ldap.ScopeWholeSubtree,
		"(cn=*)",
		attributes,
	)

	vlvControl = new(ldap.ControlVlvRequest)
	vlvControl.BeforeCount = 0
	vlvControl.AfterCount = 3
	vlvControl.GreaterThanOrEqual = "Aaren Amar"

	//pack, _ := vlvControl.Encode()
	//fmt.Println(hex.Dump(pack.Bytes()))

	search_request.AddControl(vlvControl)
	search_request.AddControl(sortControl)

	sr, err = l.Search(search_request)
	if err != nil {
		t.Error(err)
		return
	}
	_, vlvResp = ldap.FindControl(sr.Controls, ldap.ControlTypeVlvResponse)
	if vlvResp != nil {
		fmt.Println(vlvResp.String())
	}
	for _, entry := range sr.Entries {
		fmt.Println(entry.GetAttributeValues("cn")[0])
	}
	fmt.Printf("TestLocalVlvSearch (value): %s -> num of entries = %d\n", search_request.Filter, len(sr.Entries))
	fmt.Printf("TestLocalVlvSearch: Finished.\n")
}