2
0

ip_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 mesh
  15. import (
  16. "net"
  17. "testing"
  18. )
  19. func TestNewAllocator(t *testing.T) {
  20. _, c1, err := net.ParseCIDR("10.1.0.0/16")
  21. if err != nil {
  22. t.Fatalf("failed to parse CIDR: %v", err)
  23. }
  24. a1 := newAllocator(*c1)
  25. _, c2, err := net.ParseCIDR("10.1.0.0/32")
  26. if err != nil {
  27. t.Fatalf("failed to parse CIDR: %v", err)
  28. }
  29. a2 := newAllocator(*c2)
  30. _, c3, err := net.ParseCIDR("10.1.0.0/31")
  31. if err != nil {
  32. t.Fatalf("failed to parse CIDR: %v", err)
  33. }
  34. a3 := newAllocator(*c3)
  35. for _, tc := range []struct {
  36. name string
  37. a *allocator
  38. next string
  39. }{
  40. {
  41. name: "10.1.0.0/16 first",
  42. a: a1,
  43. next: "10.1.0.1/16",
  44. },
  45. {
  46. name: "10.1.0.0/16 second",
  47. a: a1,
  48. next: "10.1.0.2/16",
  49. },
  50. {
  51. name: "10.1.0.0/32",
  52. a: a2,
  53. next: "<nil>",
  54. },
  55. {
  56. name: "10.1.0.0/31 first",
  57. a: a3,
  58. next: "10.1.0.1/31",
  59. },
  60. {
  61. name: "10.1.0.0/31 second",
  62. a: a3,
  63. next: "<nil>",
  64. },
  65. } {
  66. next := tc.a.next()
  67. if next.String() != tc.next {
  68. t.Errorf("test case %q: expected %s, got %s", tc.name, tc.next, next.String())
  69. }
  70. }
  71. }
  72. func TestSortIPs(t *testing.T) {
  73. ip1 := oneAddressCIDR(net.ParseIP("10.0.0.1"))
  74. ip2 := oneAddressCIDR(net.ParseIP("10.0.0.2"))
  75. ip3 := oneAddressCIDR(net.ParseIP("192.168.0.1"))
  76. ip4 := oneAddressCIDR(net.ParseIP("2001::7"))
  77. ip5 := oneAddressCIDR(net.ParseIP("fd68:da49:09da:b27f::"))
  78. for _, tc := range []struct {
  79. name string
  80. ips []*net.IPNet
  81. out []*net.IPNet
  82. }{
  83. {
  84. name: "single",
  85. ips: []*net.IPNet{ip1},
  86. out: []*net.IPNet{ip1},
  87. },
  88. {
  89. name: "IPv4s",
  90. ips: []*net.IPNet{ip2, ip3, ip1},
  91. out: []*net.IPNet{ip1, ip2, ip3},
  92. },
  93. {
  94. name: "IPv4 and IPv6",
  95. ips: []*net.IPNet{ip4, ip1},
  96. out: []*net.IPNet{ip1, ip4},
  97. },
  98. {
  99. name: "IPv6s",
  100. ips: []*net.IPNet{ip5, ip4},
  101. out: []*net.IPNet{ip4, ip5},
  102. },
  103. {
  104. name: "all",
  105. ips: []*net.IPNet{ip3, ip4, ip2, ip5, ip1},
  106. out: []*net.IPNet{ip1, ip2, ip3, ip4, ip5},
  107. },
  108. } {
  109. sortIPs(tc.ips)
  110. equal := true
  111. if len(tc.ips) != len(tc.out) {
  112. equal = false
  113. } else {
  114. for i := range tc.ips {
  115. if !ipNetsEqual(tc.ips[i], tc.out[i]) {
  116. equal = false
  117. break
  118. }
  119. }
  120. }
  121. if !equal {
  122. t.Errorf("test case %q: expected %s, got %s", tc.name, tc.out, tc.ips)
  123. }
  124. }
  125. }
  126. func TestIsPublic(t *testing.T) {
  127. for _, tc := range []struct {
  128. name string
  129. ip net.IP
  130. out bool
  131. }{
  132. {
  133. name: "10/8",
  134. ip: net.ParseIP("10.0.0.1"),
  135. out: false,
  136. },
  137. {
  138. name: "172.16/12",
  139. ip: net.ParseIP("172.16.0.0"),
  140. out: false,
  141. },
  142. {
  143. name: "172.16/12 random",
  144. ip: net.ParseIP("172.24.135.46"),
  145. out: false,
  146. },
  147. {
  148. name: "below 172.16/12",
  149. ip: net.ParseIP("172.15.255.255"),
  150. out: true,
  151. },
  152. {
  153. name: "above 172.16/12",
  154. ip: net.ParseIP("172.160.255.255"),
  155. out: true,
  156. },
  157. {
  158. name: "192.168/16",
  159. ip: net.ParseIP("192.168.0.0"),
  160. out: false,
  161. },
  162. } {
  163. if isPublic(tc.ip) != tc.out {
  164. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, !tc.out)
  165. }
  166. }
  167. }