conf_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package wireguard
  15. import (
  16. "testing"
  17. )
  18. func TestCompareConf(t *testing.T) {
  19. for _, tc := range []struct {
  20. name string
  21. a []byte
  22. b []byte
  23. out bool
  24. }{
  25. {
  26. name: "empty",
  27. a: []byte{},
  28. b: []byte{},
  29. out: true,
  30. },
  31. {
  32. name: "key and value order",
  33. a: []byte(`[Interface]
  34. PrivateKey = private
  35. ListenPort = 51820
  36. [Peer]
  37. Endpoint = 10.1.0.2:51820
  38. PresharedKey = psk
  39. PublicKey = key
  40. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  41. `),
  42. b: []byte(`[Interface]
  43. ListenPort = 51820
  44. PrivateKey = private
  45. [Peer]
  46. PublicKey = key
  47. AllowedIPs = 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32, 10.2.2.0/24
  48. PresharedKey = psk
  49. Endpoint = 10.1.0.2:51820
  50. `),
  51. out: true,
  52. },
  53. {
  54. name: "whitespace",
  55. a: []byte(`[Interface]
  56. PrivateKey = private
  57. ListenPort = 51820
  58. [Peer]
  59. Endpoint = 10.1.0.2:51820
  60. PresharedKey = psk
  61. PublicKey = key
  62. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  63. `),
  64. b: []byte(`[Interface]
  65. PrivateKey=private
  66. ListenPort=51820
  67. [Peer]
  68. Endpoint=10.1.0.2:51820
  69. PresharedKey = psk
  70. PublicKey=key
  71. AllowedIPs=10.2.2.0/24,192.168.0.1/32,10.2.3.0/24,192.168.0.2/32,10.4.0.2/32
  72. `),
  73. out: true,
  74. },
  75. {
  76. name: "missing key",
  77. a: []byte(`[Interface]
  78. PrivateKey = private
  79. ListenPort = 51820
  80. [Peer]
  81. Endpoint = 10.1.0.2:51820
  82. PublicKey = key
  83. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  84. `),
  85. b: []byte(`[Interface]
  86. PrivateKey = private
  87. ListenPort = 51820
  88. [Peer]
  89. PublicKey = key
  90. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  91. `),
  92. out: false,
  93. },
  94. {
  95. name: "different value",
  96. a: []byte(`[Interface]
  97. PrivateKey = private
  98. ListenPort = 51820
  99. [Peer]
  100. Endpoint = 10.1.0.2:51820
  101. PublicKey = key
  102. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  103. `),
  104. b: []byte(`[Interface]
  105. PrivateKey = private
  106. ListenPort = 51820
  107. [Peer]
  108. Endpoint = 10.1.0.2:51820
  109. PublicKey = key2
  110. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  111. `),
  112. out: false,
  113. },
  114. {
  115. name: "section order",
  116. a: []byte(`[Interface]
  117. PrivateKey = private
  118. ListenPort = 51820
  119. [Peer]
  120. Endpoint = 10.1.0.2:51820
  121. PresharedKey = psk
  122. PublicKey = key
  123. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  124. `),
  125. b: []byte(`[Peer]
  126. Endpoint = 10.1.0.2:51820
  127. PresharedKey = psk
  128. PublicKey = key
  129. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  130. [Interface]
  131. PrivateKey = private
  132. ListenPort = 51820
  133. `),
  134. out: true,
  135. },
  136. {
  137. name: "out of order peers",
  138. a: []byte(`[Interface]
  139. PrivateKey = private
  140. ListenPort = 51820
  141. [Peer]
  142. Endpoint = 10.1.0.2:51820
  143. PresharedKey = psk2
  144. PublicKey = key2
  145. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  146. [Peer]
  147. Endpoint = 10.1.0.2:51820
  148. PresharedKey = psk1
  149. PublicKey = key1
  150. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  151. `),
  152. b: []byte(`[Interface]
  153. PrivateKey = private
  154. ListenPort = 51820
  155. [Peer]
  156. Endpoint = 10.1.0.2:51820
  157. PresharedKey = psk1
  158. PublicKey = key1
  159. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  160. [Peer]
  161. Endpoint = 10.1.0.2:51820
  162. PresharedKey = psk2
  163. PublicKey = key2
  164. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  165. `),
  166. out: true,
  167. },
  168. {
  169. name: "one empty",
  170. a: []byte(`[Interface]
  171. PrivateKey = private
  172. ListenPort = 51820
  173. [Peer]
  174. Endpoint = 10.1.0.2:51820
  175. PresharedKey = psk
  176. PublicKey = key
  177. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  178. `),
  179. b: []byte(``),
  180. out: false,
  181. },
  182. } {
  183. equal := Parse(tc.a).Equal(Parse(tc.b))
  184. if equal != tc.out {
  185. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, equal)
  186. }
  187. }
  188. }