args_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Shamelessly stolen from goptlib's args_test.go.
  2. package socks5
  3. import (
  4. "testing"
  5. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib"
  6. )
  7. func stringSlicesEqual(a, b []string) bool {
  8. if len(a) != len(b) {
  9. return false
  10. }
  11. for i := range a {
  12. if a[i] != b[i] {
  13. return false
  14. }
  15. }
  16. return true
  17. }
  18. func argsEqual(a, b pt.Args) bool {
  19. for k, av := range a {
  20. bv := b[k]
  21. if !stringSlicesEqual(av, bv) {
  22. return false
  23. }
  24. }
  25. for k, bv := range b {
  26. av := a[k]
  27. if !stringSlicesEqual(av, bv) {
  28. return false
  29. }
  30. }
  31. return true
  32. }
  33. func TestParseClientParameters(t *testing.T) {
  34. badTests := [...]string{
  35. "key",
  36. "key\\",
  37. "=value",
  38. "==value",
  39. "==key=value",
  40. "key=value\\",
  41. "a=b;key=value\\",
  42. "a;b=c",
  43. ";",
  44. "key=value;",
  45. ";key=value",
  46. "key\\=value",
  47. }
  48. goodTests := [...]struct {
  49. input string
  50. expected pt.Args
  51. }{
  52. {
  53. "",
  54. pt.Args{},
  55. },
  56. {
  57. "key=",
  58. pt.Args{"key": []string{""}},
  59. },
  60. {
  61. "key==",
  62. pt.Args{"key": []string{"="}},
  63. },
  64. {
  65. "key=value",
  66. pt.Args{"key": []string{"value"}},
  67. },
  68. {
  69. "a=b=c",
  70. pt.Args{"a": []string{"b=c"}},
  71. },
  72. {
  73. "key=a\nb",
  74. pt.Args{"key": []string{"a\nb"}},
  75. },
  76. {
  77. "key=value\\;",
  78. pt.Args{"key": []string{"value;"}},
  79. },
  80. {
  81. "key=\"value\"",
  82. pt.Args{"key": []string{"\"value\""}},
  83. },
  84. {
  85. "key=\"\"value\"\"",
  86. pt.Args{"key": []string{"\"\"value\"\""}},
  87. },
  88. {
  89. "\"key=value\"",
  90. pt.Args{"\"key": []string{"value\""}},
  91. },
  92. {
  93. "key=value;key=value",
  94. pt.Args{"key": []string{"value", "value"}},
  95. },
  96. {
  97. "key=value1;key=value2",
  98. pt.Args{"key": []string{"value1", "value2"}},
  99. },
  100. {
  101. "key1=value1;key2=value2;key1=value3",
  102. pt.Args{"key1": []string{"value1", "value3"}, "key2": []string{"value2"}},
  103. },
  104. {
  105. "\\;=\\;;\\\\=\\;",
  106. pt.Args{";": []string{";"}, "\\": []string{";"}},
  107. },
  108. {
  109. "a\\=b=c",
  110. pt.Args{"a=b": []string{"c"}},
  111. },
  112. {
  113. "shared-secret=rahasia;secrets-file=/tmp/blob",
  114. pt.Args{"shared-secret": []string{"rahasia"}, "secrets-file": []string{"/tmp/blob"}},
  115. },
  116. {
  117. "rocks=20;height=5.6",
  118. pt.Args{"rocks": []string{"20"}, "height": []string{"5.6"}},
  119. },
  120. }
  121. for _, input := range badTests {
  122. _, err := parseClientParameters(input)
  123. if err == nil {
  124. t.Errorf("%q unexpectedly succeeded", input)
  125. }
  126. }
  127. for _, test := range goodTests {
  128. args, err := parseClientParameters(test.input)
  129. if err != nil {
  130. t.Errorf("%q unexpectedly returned an error: %s", test.input, err)
  131. }
  132. if !argsEqual(args, test.expected) {
  133. t.Errorf("%q → %q (expected %q)", test.input, args, test.expected)
  134. }
  135. }
  136. }