ldap_test.go 6.5 KB

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