control_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package ldap
  2. import (
  3. "bytes"
  4. "fmt"
  5. "reflect"
  6. "runtime"
  7. "testing"
  8. "gopkg.in/asn1-ber.v1"
  9. )
  10. func TestControlPaging(t *testing.T) {
  11. runControlTest(t, NewControlPaging(0))
  12. runControlTest(t, NewControlPaging(100))
  13. }
  14. func TestControlManageDsaIT(t *testing.T) {
  15. runControlTest(t, NewControlManageDsaIT(true))
  16. runControlTest(t, NewControlManageDsaIT(false))
  17. }
  18. func TestControlString(t *testing.T) {
  19. runControlTest(t, NewControlString("x", true, "y"))
  20. runControlTest(t, NewControlString("x", true, ""))
  21. runControlTest(t, NewControlString("x", false, "y"))
  22. runControlTest(t, NewControlString("x", false, ""))
  23. }
  24. func runControlTest(t *testing.T, originalControl Control) {
  25. header := ""
  26. if callerpc, _, line, ok := runtime.Caller(1); ok {
  27. if caller := runtime.FuncForPC(callerpc); caller != nil {
  28. header = fmt.Sprintf("%s:%d: ", caller.Name(), line)
  29. }
  30. }
  31. encodedPacket := originalControl.Encode()
  32. encodedBytes := encodedPacket.Bytes()
  33. // Decode directly from the encoded packet (ensures Value is correct)
  34. fromPacket, err := DecodeControl(encodedPacket)
  35. if err != nil {
  36. t.Errorf("%sdecoding encoded bytes control failed: %s", header, err)
  37. }
  38. if !bytes.Equal(encodedBytes, fromPacket.Encode().Bytes()) {
  39. t.Errorf("%sround-trip from encoded packet failed", header)
  40. }
  41. if reflect.TypeOf(originalControl) != reflect.TypeOf(fromPacket) {
  42. t.Errorf("%sgot different type decoding from encoded packet: %T vs %T", header, fromPacket, originalControl)
  43. }
  44. // Decode from the wire bytes (ensures ber-encoding is correct)
  45. pkt, err := ber.DecodePacketErr(encodedBytes)
  46. if err != nil {
  47. t.Errorf("%sdecoding encoded bytes failed: %s", header, err)
  48. }
  49. fromBytes, err := DecodeControl(pkt)
  50. if err != nil {
  51. t.Errorf("%sdecoding control failed: %s", header, err)
  52. }
  53. if !bytes.Equal(encodedBytes, fromBytes.Encode().Bytes()) {
  54. t.Errorf("%sround-trip from encoded bytes failed", header)
  55. }
  56. if reflect.TypeOf(originalControl) != reflect.TypeOf(fromPacket) {
  57. t.Errorf("%sgot different type decoding from encoded bytes: %T vs %T", header, fromBytes, originalControl)
  58. }
  59. }
  60. func TestDescribeControlManageDsaIT(t *testing.T) {
  61. runAddControlDescriptions(t, NewControlManageDsaIT(false), "Control Type (Manage DSA IT)")
  62. runAddControlDescriptions(t, NewControlManageDsaIT(true), "Control Type (Manage DSA IT)", "Criticality")
  63. }
  64. func TestDescribeControlPaging(t *testing.T) {
  65. runAddControlDescriptions(t, NewControlPaging(100), "Control Type (Paging)", "Control Value (Paging)")
  66. runAddControlDescriptions(t, NewControlPaging(0), "Control Type (Paging)", "Control Value (Paging)")
  67. }
  68. func TestDescribeControlString(t *testing.T) {
  69. runAddControlDescriptions(t, NewControlString("x", true, "y"), "Control Type ()", "Criticality", "Control Value")
  70. runAddControlDescriptions(t, NewControlString("x", true, ""), "Control Type ()", "Criticality", "Control Value")
  71. runAddControlDescriptions(t, NewControlString("x", false, "y"), "Control Type ()", "Control Value")
  72. runAddControlDescriptions(t, NewControlString("x", false, ""), "Control Type ()", "Control Value")
  73. }
  74. func runAddControlDescriptions(t *testing.T, originalControl Control, childDescriptions ...string) {
  75. header := ""
  76. if callerpc, _, line, ok := runtime.Caller(1); ok {
  77. if caller := runtime.FuncForPC(callerpc); caller != nil {
  78. header = fmt.Sprintf("%s:%d: ", caller.Name(), line)
  79. }
  80. }
  81. encodedControls := encodeControls([]Control{originalControl})
  82. addControlDescriptions(encodedControls)
  83. encodedPacket := encodedControls.Children[0]
  84. if len(encodedPacket.Children) != len(childDescriptions) {
  85. t.Errorf("%sinvalid number of children: %d != %d", header, len(encodedPacket.Children), len(childDescriptions))
  86. }
  87. for i, desc := range childDescriptions {
  88. if encodedPacket.Children[i].Description != desc {
  89. t.Errorf("%sdescription not as expected: %s != %s", header, encodedPacket.Children[i].Description, desc)
  90. }
  91. }
  92. }