server_modify_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package ldap
  2. import (
  3. "net"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. //
  10. func TestAdd(t *testing.T) {
  11. quit := make(chan bool)
  12. done := make(chan bool)
  13. go func() {
  14. s := NewServer()
  15. s.QuitChannel(quit)
  16. s.BindFunc("", modifyTestHandler{})
  17. s.AddFunc("", modifyTestHandler{})
  18. if err := s.ListenAndServe(listenString); err != nil {
  19. t.Errorf("s.ListenAndServe failed: %s", err.Error())
  20. }
  21. }()
  22. go func() {
  23. cmd := exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add.ldif")
  24. out, _ := cmd.CombinedOutput()
  25. if !strings.Contains(string(out), "modify complete") {
  26. t.Errorf("ldapadd failed: %v", string(out))
  27. }
  28. cmd = exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add2.ldif")
  29. out, _ = cmd.CombinedOutput()
  30. if !strings.Contains(string(out), "ldap_add: Insufficient access") {
  31. t.Errorf("ldapadd should have failed: %v", string(out))
  32. }
  33. if strings.Contains(string(out), "modify complete") {
  34. t.Errorf("ldapadd should have failed: %v", string(out))
  35. }
  36. done <- true
  37. }()
  38. select {
  39. case <-done:
  40. case <-time.After(timeout):
  41. t.Errorf("ldapadd command timed out")
  42. }
  43. quit <- true
  44. }
  45. //
  46. func TestDelete(t *testing.T) {
  47. quit := make(chan bool)
  48. done := make(chan bool)
  49. go func() {
  50. s := NewServer()
  51. s.QuitChannel(quit)
  52. s.BindFunc("", modifyTestHandler{})
  53. s.DeleteFunc("", modifyTestHandler{})
  54. if err := s.ListenAndServe(listenString); err != nil {
  55. t.Errorf("s.ListenAndServe failed: %s", err.Error())
  56. }
  57. }()
  58. go func() {
  59. cmd := exec.Command("ldapdelete", "-v", "-H", ldapURL, "-x", "cn=Delete Me,dc=example,dc=com")
  60. out, _ := cmd.CombinedOutput()
  61. if !strings.Contains(string(out), "Delete Result: Success (0)") || !strings.Contains(string(out), "Additional info: Success") {
  62. t.Errorf("ldapdelete failed: %v", string(out))
  63. }
  64. cmd = exec.Command("ldapdelete", "-v", "-H", ldapURL, "-x", "cn=Bob,dc=example,dc=com")
  65. out, _ = cmd.CombinedOutput()
  66. if strings.Contains(string(out), "Success") || !strings.Contains(string(out), "ldap_delete: Insufficient access") {
  67. t.Errorf("ldapdelete should have failed: %v", string(out))
  68. }
  69. done <- true
  70. }()
  71. select {
  72. case <-done:
  73. case <-time.After(timeout):
  74. t.Errorf("ldapdelete command timed out")
  75. }
  76. quit <- true
  77. }
  78. func TestModify(t *testing.T) {
  79. quit := make(chan bool)
  80. done := make(chan bool)
  81. go func() {
  82. s := NewServer()
  83. s.QuitChannel(quit)
  84. s.BindFunc("", modifyTestHandler{})
  85. s.ModifyFunc("", modifyTestHandler{})
  86. if err := s.ListenAndServe(listenString); err != nil {
  87. t.Errorf("s.ListenAndServe failed: %s", err.Error())
  88. }
  89. }()
  90. go func() {
  91. cmd := exec.Command("ldapmodify", "-v", "-H", ldapURL, "-x", "-f", "tests/modify.ldif")
  92. out, _ := cmd.CombinedOutput()
  93. if !strings.Contains(string(out), "modify complete") {
  94. t.Errorf("ldapmodify failed: %v", string(out))
  95. }
  96. cmd = exec.Command("ldapmodify", "-v", "-H", ldapURL, "-x", "-f", "tests/modify2.ldif")
  97. out, _ = cmd.CombinedOutput()
  98. if !strings.Contains(string(out), "ldap_modify: Insufficient access") || strings.Contains(string(out), "modify complete") {
  99. t.Errorf("ldapmodify should have failed: %v", string(out))
  100. }
  101. done <- true
  102. }()
  103. select {
  104. case <-done:
  105. case <-time.After(timeout):
  106. t.Errorf("ldapadd command timed out")
  107. }
  108. quit <- true
  109. }
  110. /*
  111. func TestModifyDN(t *testing.T) {
  112. quit := make(chan bool)
  113. done := make(chan bool)
  114. go func() {
  115. s := NewServer()
  116. s.QuitChannel(quit)
  117. s.BindFunc("", modifyTestHandler{})
  118. s.AddFunc("", modifyTestHandler{})
  119. if err := s.ListenAndServe(listenString); err != nil {
  120. t.Errorf("s.ListenAndServe failed: %s", err.Error())
  121. }
  122. }()
  123. go func() {
  124. cmd := exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add.ldif")
  125. //ldapmodrdn -H ldap://localhost:3389 -x "uid=babs,dc=example,dc=com" "uid=babsy,dc=example,dc=com"
  126. out, _ := cmd.CombinedOutput()
  127. if !strings.Contains(string(out), "modify complete") {
  128. t.Errorf("ldapadd failed: %v", string(out))
  129. }
  130. cmd = exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add2.ldif")
  131. out, _ = cmd.CombinedOutput()
  132. if !strings.Contains(string(out), "ldap_add: Insufficient access") {
  133. t.Errorf("ldapadd should have failed: %v", string(out))
  134. }
  135. if strings.Contains(string(out), "modify complete") {
  136. t.Errorf("ldapadd should have failed: %v", string(out))
  137. }
  138. done <- true
  139. }()
  140. select {
  141. case <-done:
  142. case <-time.After(timeout):
  143. t.Errorf("ldapadd command timed out")
  144. }
  145. quit <- true
  146. }
  147. */
  148. //
  149. type modifyTestHandler struct {
  150. }
  151. func (h modifyTestHandler) Bind(bindDN, bindSimplePw string, conn net.Conn) (LDAPResultCode, error) {
  152. if bindDN == "" && bindSimplePw == "" {
  153. return LDAPResultSuccess, nil
  154. }
  155. return LDAPResultInvalidCredentials, nil
  156. }
  157. func (h modifyTestHandler) Add(boundDN string, req AddRequest, conn net.Conn) (LDAPResultCode, error) {
  158. // only succeed on expected contents of add.ldif:
  159. if len(req.attributes) == 5 && req.dn == "cn=Barbara Jensen,dc=example,dc=com" &&
  160. req.attributes[2].attrType == "sn" && len(req.attributes[2].attrVals) == 1 &&
  161. req.attributes[2].attrVals[0] == "Jensen" {
  162. return LDAPResultSuccess, nil
  163. }
  164. return LDAPResultInsufficientAccessRights, nil
  165. }
  166. func (h modifyTestHandler) Delete(boundDN, deleteDN string, conn net.Conn) (LDAPResultCode, error) {
  167. // only succeed on expected deleteDN
  168. if deleteDN == "cn=Delete Me,dc=example,dc=com" {
  169. return LDAPResultSuccess, nil
  170. }
  171. return LDAPResultInsufficientAccessRights, nil
  172. }
  173. func (h modifyTestHandler) Modify(boundDN string, req ModifyRequest, conn net.Conn) (LDAPResultCode, error) {
  174. // only succeed on expected contents of modify.ldif:
  175. if req.Dn == "cn=testy,dc=example,dc=com" && len(req.AddAttributes) == 1 &&
  176. len(req.DeleteAttributes) == 3 && len(req.ReplaceAttributes) == 2 &&
  177. req.DeleteAttributes[2].AttrType == "details" && len(req.DeleteAttributes[2].AttrVals) == 0 {
  178. return LDAPResultSuccess, nil
  179. }
  180. return LDAPResultInsufficientAccessRights, nil
  181. }
  182. func (h modifyTestHandler) ModifyDN(boundDN string, req ModifyDNRequest, conn net.Conn) (LDAPResultCode, error) {
  183. return LDAPResultInsufficientAccessRights, nil
  184. }