ldap_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package ldap
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "testing"
  6. )
  7. var ldapServer = "ldap.itd.umich.edu"
  8. var ldapPort = uint16(389)
  9. var ldapTLSPort = uint16(636)
  10. var baseDN = "dc=umich,dc=edu"
  11. var filter = []string{
  12. "(cn=cis-fac)",
  13. "(&(owner=*)(cn=cis-fac))",
  14. "(&(objectclass=rfc822mailgroup)(cn=*Computer*))",
  15. "(&(objectclass=rfc822mailgroup)(cn=*Mathematics*))"}
  16. var attributes = []string{
  17. "cn",
  18. "description"}
  19. func TestDial(t *testing.T) {
  20. fmt.Printf("TestDial: starting...\n")
  21. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  22. if err != nil {
  23. t.Errorf(err.Error())
  24. return
  25. }
  26. defer l.Close()
  27. fmt.Printf("TestDial: finished...\n")
  28. }
  29. func TestDialTLS(t *testing.T) {
  30. fmt.Printf("TestDialTLS: starting...\n")
  31. l, err := DialTLS("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapTLSPort), &tls.Config{InsecureSkipVerify: true})
  32. if err != nil {
  33. t.Errorf(err.Error())
  34. return
  35. }
  36. defer l.Close()
  37. fmt.Printf("TestDialTLS: finished...\n")
  38. }
  39. func TestStartTLS(t *testing.T) {
  40. fmt.Printf("TestStartTLS: starting...\n")
  41. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  42. if err != nil {
  43. t.Errorf(err.Error())
  44. return
  45. }
  46. err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
  47. if err != nil {
  48. t.Errorf(err.Error())
  49. return
  50. }
  51. fmt.Printf("TestStartTLS: finished...\n")
  52. }
  53. func TestSearch(t *testing.T) {
  54. fmt.Printf("TestSearch: starting...\n")
  55. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  56. if err != nil {
  57. t.Errorf(err.Error())
  58. return
  59. }
  60. defer l.Close()
  61. searchRequest := NewSearchRequest(
  62. baseDN,
  63. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  64. filter[0],
  65. attributes,
  66. nil)
  67. sr, err := l.Search(searchRequest)
  68. if err != nil {
  69. t.Errorf(err.Error())
  70. return
  71. }
  72. fmt.Printf("TestSearch: %s -> num of entries = %d\n", searchRequest.Filter, len(sr.Entries))
  73. }
  74. func TestSearchStartTLS(t *testing.T) {
  75. fmt.Printf("TestSearchStartTLS: starting...\n")
  76. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  77. if err != nil {
  78. t.Errorf(err.Error())
  79. return
  80. }
  81. defer l.Close()
  82. searchRequest := NewSearchRequest(
  83. baseDN,
  84. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  85. filter[0],
  86. attributes,
  87. nil)
  88. sr, err := l.Search(searchRequest)
  89. if err != nil {
  90. t.Errorf(err.Error())
  91. return
  92. }
  93. fmt.Printf("TestSearchStartTLS: %s -> num of entries = %d\n", searchRequest.Filter, len(sr.Entries))
  94. fmt.Printf("TestSearchStartTLS: upgrading with startTLS\n")
  95. err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
  96. if err != nil {
  97. t.Errorf(err.Error())
  98. return
  99. }
  100. sr, err = l.Search(searchRequest)
  101. if err != nil {
  102. t.Errorf(err.Error())
  103. return
  104. }
  105. fmt.Printf("TestSearchStartTLS: %s -> num of entries = %d\n", searchRequest.Filter, len(sr.Entries))
  106. }
  107. func TestSearchWithPaging(t *testing.T) {
  108. fmt.Printf("TestSearchWithPaging: starting...\n")
  109. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  110. if err != nil {
  111. t.Errorf(err.Error())
  112. return
  113. }
  114. defer l.Close()
  115. err = l.UnauthenticatedBind("")
  116. if err != nil {
  117. t.Errorf(err.Error())
  118. return
  119. }
  120. searchRequest := NewSearchRequest(
  121. baseDN,
  122. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  123. filter[2],
  124. attributes,
  125. nil)
  126. sr, err := l.SearchWithPaging(searchRequest, 5)
  127. if err != nil {
  128. t.Errorf(err.Error())
  129. return
  130. }
  131. fmt.Printf("TestSearchWithPaging: %s -> num of entries = %d\n", searchRequest.Filter, len(sr.Entries))
  132. searchRequest = NewSearchRequest(
  133. baseDN,
  134. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  135. filter[2],
  136. attributes,
  137. []Control{NewControlPaging(5)})
  138. sr, err = l.SearchWithPaging(searchRequest, 5)
  139. if err != nil {
  140. t.Errorf(err.Error())
  141. return
  142. }
  143. fmt.Printf("TestSearchWithPaging: %s -> num of entries = %d\n", searchRequest.Filter, len(sr.Entries))
  144. searchRequest = NewSearchRequest(
  145. baseDN,
  146. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  147. filter[2],
  148. attributes,
  149. []Control{NewControlPaging(500)})
  150. sr, err = l.SearchWithPaging(searchRequest, 5)
  151. if err == nil {
  152. t.Errorf("expected an error when paging size in control in search request doesn't match size given in call, got none")
  153. return
  154. }
  155. }
  156. func searchGoroutine(t *testing.T, l *Conn, results chan *SearchResult, i int) {
  157. searchRequest := NewSearchRequest(
  158. baseDN,
  159. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  160. filter[i],
  161. attributes,
  162. nil)
  163. sr, err := l.Search(searchRequest)
  164. if err != nil {
  165. t.Errorf(err.Error())
  166. results <- nil
  167. return
  168. }
  169. results <- sr
  170. }
  171. func testMultiGoroutineSearch(t *testing.T, TLS bool, startTLS bool) {
  172. fmt.Printf("TestMultiGoroutineSearch: starting...\n")
  173. var l *Conn
  174. var err error
  175. if TLS {
  176. l, err = DialTLS("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapTLSPort), &tls.Config{InsecureSkipVerify: true})
  177. if err != nil {
  178. t.Errorf(err.Error())
  179. return
  180. }
  181. defer l.Close()
  182. } else {
  183. l, err = Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  184. if err != nil {
  185. t.Errorf(err.Error())
  186. return
  187. }
  188. if startTLS {
  189. fmt.Printf("TestMultiGoroutineSearch: using StartTLS...\n")
  190. err := l.StartTLS(&tls.Config{InsecureSkipVerify: true})
  191. if err != nil {
  192. t.Errorf(err.Error())
  193. return
  194. }
  195. }
  196. }
  197. results := make([]chan *SearchResult, len(filter))
  198. for i := range filter {
  199. results[i] = make(chan *SearchResult)
  200. go searchGoroutine(t, l, results[i], i)
  201. }
  202. for i := range filter {
  203. sr := <-results[i]
  204. if sr == nil {
  205. t.Errorf("Did not receive results from goroutine for %q", filter[i])
  206. } else {
  207. fmt.Printf("TestMultiGoroutineSearch(%d): %s -> num of entries = %d\n", i, filter[i], len(sr.Entries))
  208. }
  209. }
  210. }
  211. func TestMultiGoroutineSearch(t *testing.T) {
  212. testMultiGoroutineSearch(t, false, false)
  213. testMultiGoroutineSearch(t, true, true)
  214. testMultiGoroutineSearch(t, false, true)
  215. }
  216. func TestEscapeFilter(t *testing.T) {
  217. if got, want := EscapeFilter("a\x00b(c)d*e\\f"), `a\00b\28c\29d\2ae\5cf`; got != want {
  218. t.Errorf("Got %s, expected %s", want, got)
  219. }
  220. if got, want := EscapeFilter("Lučić"), `Lu\c4\8di\c4\87`; got != want {
  221. t.Errorf("Got %s, expected %s", want, got)
  222. }
  223. }
  224. func TestCompare(t *testing.T) {
  225. fmt.Printf("TestCompare: starting...\n")
  226. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  227. if err != nil {
  228. t.Fatal(err.Error())
  229. }
  230. defer l.Close()
  231. dn := "cn=math mich,ou=User Groups,ou=Groups,dc=umich,dc=edu"
  232. attribute := "cn"
  233. value := "math mich"
  234. sr, err := l.Compare(dn, attribute, value)
  235. if err != nil {
  236. t.Errorf(err.Error())
  237. return
  238. }
  239. fmt.Printf("TestCompare: -> %v\n", sr)
  240. }