wireguard_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. PublicKey = key
  39. 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
  40. `),
  41. b: []byte(`[Interface]
  42. ListenPort = 51820
  43. PrivateKey = private
  44. [Peer]
  45. PublicKey = key
  46. 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
  47. Endpoint = 10.1.0.2:51820
  48. `),
  49. out: true,
  50. },
  51. {
  52. name: "whitespace",
  53. a: []byte(`[Interface]
  54. PrivateKey = private
  55. ListenPort = 51820
  56. [Peer]
  57. Endpoint = 10.1.0.2:51820
  58. PublicKey = key
  59. 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
  60. `),
  61. b: []byte(`[Interface]
  62. PrivateKey=private
  63. ListenPort=51820
  64. [Peer]
  65. Endpoint=10.1.0.2:51820
  66. PublicKey=key
  67. 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
  68. `),
  69. out: true,
  70. },
  71. {
  72. name: "missing key",
  73. a: []byte(`[Interface]
  74. PrivateKey = private
  75. ListenPort = 51820
  76. [Peer]
  77. Endpoint = 10.1.0.2:51820
  78. PublicKey = key
  79. 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
  80. `),
  81. b: []byte(`[Interface]
  82. PrivateKey = private
  83. ListenPort = 51820
  84. [Peer]
  85. PublicKey = key
  86. 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
  87. `),
  88. out: false,
  89. },
  90. {
  91. name: "section order",
  92. a: []byte(`[Interface]
  93. PrivateKey = private
  94. ListenPort = 51820
  95. [Peer]
  96. Endpoint = 10.1.0.2:51820
  97. PublicKey = key
  98. 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
  99. `),
  100. b: []byte(`[Peer]
  101. Endpoint = 10.1.0.2:51820
  102. PublicKey = key
  103. 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
  104. [Interface]
  105. PrivateKey = private
  106. ListenPort = 51820
  107. `),
  108. out: true,
  109. },
  110. {
  111. name: "one empty",
  112. a: []byte(`[Interface]
  113. PrivateKey = private
  114. ListenPort = 51820
  115. [Peer]
  116. Endpoint = 10.1.0.2:51820
  117. PublicKey = key
  118. 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
  119. `),
  120. b: []byte(``),
  121. out: false,
  122. },
  123. } {
  124. equal, err := CompareConf(tc.a, tc.b)
  125. if err != nil {
  126. t.Errorf("test case %q: got unexpected error: %v", tc.name, err)
  127. }
  128. if equal != tc.out {
  129. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, equal)
  130. }
  131. }
  132. }