summaryrefslogtreecommitdiffstats
path: root/ldap_test.go
blob: 708dde6a6c9f5b4885f44f04ae6ffd162b04ae1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ldap

import (
   "fmt"
   "testing"
)

var ldap_server string = "ldap.itd.umich.edu"
var ldap_port uint16 = 389
var base_dn string = "dc=umich,dc=edu"
var filter []string = []string {
   "(cn=cis-fac)",
   "(&(objectclass=rfc822mailgroup)(cn=*Computer*))",
   "(&(objectclass=rfc822mailgroup)(cn=*Mathematics*))" }
var attributes []string = []string {
   "cn",
   "description" }

func TestConnect( t *testing.T ) {
   fmt.Printf( "TestConnect: starting...\n" )
   l, err := Dial( "tcp", fmt.Sprintf( "%s:%d", ldap_server, ldap_port ) )
   if err != nil {
      t.Errorf( err.String() )
      return
   }
   defer l.Close()
   fmt.Printf( "TestConnect: finished...\n" )
}

func TestSearch( t *testing.T ) {
   fmt.Printf( "TestSearch: starting...\n" )
   l, err := Dial( "tcp", fmt.Sprintf( "%s:%d", ldap_server, ldap_port ) )
   if err != nil {
      t.Errorf( err.String() )
      return
   }
   defer l.Close()

   search_request := NewSearchRequest(
      base_dn,
      ScopeWholeSubtree, DerefAlways, 0, 0, false,
      filter[0],
      attributes,
      nil )

   sr, err := l.Search( search_request )
   if err != nil {
      t.Errorf( err.String() )
      return
   }

   fmt.Printf( "TestSearch: %s -> num of entries = %d\n", search_request.Filter, len( sr.Entries ) )
}

func TestSearchWithPaging( t *testing.T ) {
   fmt.Printf( "TestSearchWithPaging: starting...\n" )
   l, err := Dial( "tcp", fmt.Sprintf( "%s:%d", ldap_server, ldap_port ) )
   if err != nil {
      t.Errorf( err.String() )
      return
   }
   defer l.Close()

   err = l.Bind( "", "" )
   if err != nil {
      t.Errorf( err.String() )
      return
   }

   search_request := NewSearchRequest(
      base_dn,
      ScopeWholeSubtree, DerefAlways, 0, 0, false,
      filter[1],
      attributes,
      nil )
   sr, err := l.SearchWithPaging( search_request, 5 )
   if err != nil {
      t.Errorf( err.String() )
      return
   }

   fmt.Printf( "TestSearchWithPaging: %s -> num of entries = %d\n", search_request.Filter, len( sr.Entries ) )
}

func testMultiGoroutineSearch( t *testing.T, l *Conn, results chan *SearchResult, i int ) {
   search_request := NewSearchRequest(
      base_dn,
      ScopeWholeSubtree, DerefAlways, 0, 0, false,
      filter[i],
      attributes,
      nil )
   sr, err := l.Search( search_request )

   if err != nil {
      t.Errorf( err.String() )
      results <- nil
      return
   }

   results <- sr
}

func TestMultiGoroutineSearch( t *testing.T ) {
   fmt.Printf( "TestMultiGoroutineSearch: starting...\n" )
   l, err := Dial( "tcp", fmt.Sprintf( "%s:%d", ldap_server, ldap_port ) )
   if err != nil {
      t.Errorf( err.String() )
      return
   }
   defer l.Close()

   results := make( []chan *SearchResult, len( filter ) )
   for i := range filter {
      results[ i ] = make( chan *SearchResult )
      go testMultiGoroutineSearch( t, l, results[ i ], i )
   }
   for i := range filter {
      sr := <-results[ i ]
      if sr == nil {
         t.Errorf( "Did not receive results from goroutine for %q", filter[ i ] )
      } else {
         fmt.Printf( "TestMultiGoroutineSearch(%d): %s -> num of entries = %d\n", i, filter[ i ], len( sr.Entries ) )
      }
   }
}