ldap_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package ldap
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. var ldapServer = "ldap.itd.umich.edu"
  7. var ldapPort = uint16(389)
  8. var baseDN = "dc=umich,dc=edu"
  9. var filter = []string{
  10. "(cn=cis-fac)",
  11. "(&(objectclass=rfc822mailgroup)(cn=*Computer*))",
  12. "(&(objectclass=rfc822mailgroup)(cn=*Mathematics*))"}
  13. var attributes = []string{
  14. "cn",
  15. "description"}
  16. func TestConnect(t *testing.T) {
  17. fmt.Printf("TestConnect: starting...\n")
  18. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  19. if err != nil {
  20. t.Errorf(err.Error())
  21. return
  22. }
  23. defer l.Close()
  24. fmt.Printf("TestConnect: finished...\n")
  25. }
  26. func TestSearch(t *testing.T) {
  27. fmt.Printf("TestSearch: starting...\n")
  28. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  29. if err != nil {
  30. t.Errorf(err.Error())
  31. return
  32. }
  33. defer l.Close()
  34. searchRequest := NewSearchRequest(
  35. baseDN,
  36. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  37. filter[0],
  38. attributes,
  39. nil)
  40. sr, err := l.Search(searchRequest)
  41. if err != nil {
  42. t.Errorf(err.Error())
  43. return
  44. }
  45. fmt.Printf("TestSearch: %s -> num of entries = %d\n", searchRequest.Filter, len(sr.Entries))
  46. }
  47. func TestSearchWithPaging(t *testing.T) {
  48. fmt.Printf("TestSearchWithPaging: starting...\n")
  49. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  50. if err != nil {
  51. t.Errorf(err.Error())
  52. return
  53. }
  54. defer l.Close()
  55. err = l.Bind("", "")
  56. if err != nil {
  57. t.Errorf(err.Error())
  58. return
  59. }
  60. searchRequest := NewSearchRequest(
  61. baseDN,
  62. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  63. filter[1],
  64. attributes,
  65. nil)
  66. sr, err := l.SearchWithPaging(searchRequest, 5)
  67. if err != nil {
  68. t.Errorf(err.Error())
  69. return
  70. }
  71. fmt.Printf("TestSearchWithPaging: %s -> num of entries = %d\n", searchRequest.Filter, len(sr.Entries))
  72. }
  73. func testMultiGoroutineSearch(t *testing.T, l *Conn, results chan *SearchResult, i int) {
  74. searchRequest := NewSearchRequest(
  75. baseDN,
  76. ScopeWholeSubtree, DerefAlways, 0, 0, false,
  77. filter[i],
  78. attributes,
  79. nil)
  80. sr, err := l.Search(searchRequest)
  81. if err != nil {
  82. t.Errorf(err.Error())
  83. results <- nil
  84. return
  85. }
  86. results <- sr
  87. }
  88. func TestMultiGoroutineSearch(t *testing.T) {
  89. fmt.Printf("TestMultiGoroutineSearch: starting...\n")
  90. l, err := Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
  91. if err != nil {
  92. t.Errorf(err.Error())
  93. return
  94. }
  95. defer l.Close()
  96. results := make([]chan *SearchResult, len(filter))
  97. for i := range filter {
  98. results[i] = make(chan *SearchResult)
  99. go testMultiGoroutineSearch(t, l, results[i], i)
  100. }
  101. for i := range filter {
  102. sr := <-results[i]
  103. if sr == nil {
  104. t.Errorf("Did not receive results from goroutine for %q", filter[i])
  105. } else {
  106. fmt.Printf("TestMultiGoroutineSearch(%d): %s -> num of entries = %d\n", i, filter[i], len(sr.Entries))
  107. }
  108. }
  109. }