tsaddr_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package tsaddr
  4. import (
  5. "net/netip"
  6. "testing"
  7. "github.com/google/go-cmp/cmp"
  8. "github.com/google/go-cmp/cmp/cmpopts"
  9. "tailscale.com/net/netaddr"
  10. "tailscale.com/types/views"
  11. )
  12. func TestInCrostiniRange(t *testing.T) {
  13. tests := []struct {
  14. ip netip.Addr
  15. want bool
  16. }{
  17. {netaddr.IPv4(192, 168, 0, 1), false},
  18. {netaddr.IPv4(100, 101, 102, 103), false},
  19. {netaddr.IPv4(100, 115, 92, 0), true},
  20. {netaddr.IPv4(100, 115, 92, 5), true},
  21. {netaddr.IPv4(100, 115, 92, 255), true},
  22. {netaddr.IPv4(100, 115, 93, 40), true},
  23. {netaddr.IPv4(100, 115, 94, 1), false},
  24. }
  25. for _, test := range tests {
  26. if got := ChromeOSVMRange().Contains(test.ip); got != test.want {
  27. t.Errorf("inCrostiniRange(%q) = %v, want %v", test.ip, got, test.want)
  28. }
  29. }
  30. }
  31. func TestTailscaleServiceIP(t *testing.T) {
  32. got := TailscaleServiceIP().String()
  33. want := "100.100.100.100"
  34. if got != want {
  35. t.Errorf("got %q; want %q", got, want)
  36. }
  37. if TailscaleServiceIPString != want {
  38. t.Error("TailscaleServiceIPString is not consistent")
  39. }
  40. }
  41. func TestTailscaleServiceIPv6(t *testing.T) {
  42. got := TailscaleServiceIPv6().String()
  43. want := "fd7a:115c:a1e0::53"
  44. if got != want {
  45. t.Errorf("got %q; want %q", got, want)
  46. }
  47. if TailscaleServiceIPv6String != want {
  48. t.Error("TailscaleServiceIPv6String is not consistent")
  49. }
  50. }
  51. func TestChromeOSVMRange(t *testing.T) {
  52. if got, want := ChromeOSVMRange().String(), "100.115.92.0/23"; got != want {
  53. t.Errorf("got %q; want %q", got, want)
  54. }
  55. }
  56. func TestCGNATRange(t *testing.T) {
  57. if got, want := CGNATRange().String(), "100.64.0.0/10"; got != want {
  58. t.Errorf("got %q; want %q", got, want)
  59. }
  60. }
  61. var sinkIP netip.Addr
  62. func BenchmarkTailscaleServiceAddr(b *testing.B) {
  63. b.ReportAllocs()
  64. for range b.N {
  65. sinkIP = TailscaleServiceIP()
  66. }
  67. }
  68. func TestUnmapVia(t *testing.T) {
  69. tests := []struct {
  70. ip string
  71. want string
  72. }{
  73. {"1.2.3.4", "1.2.3.4"}, // unchanged v4
  74. {"fd7a:115c:a1e0:b1a::bb:10.2.1.3", "10.2.1.3"},
  75. {"fd7a:115c:a1e0:b1b::bb:10.2.1.4", "fd7a:115c:a1e0:b1b:0:bb:a02:104"}, // "b1b",not "bia"
  76. }
  77. for _, tt := range tests {
  78. if got := UnmapVia(netip.MustParseAddr(tt.ip)).String(); got != tt.want {
  79. t.Errorf("for %q: got %q, want %q", tt.ip, got, tt.want)
  80. }
  81. }
  82. }
  83. func TestIsExitNodeRoute(t *testing.T) {
  84. tests := []struct {
  85. pref netip.Prefix
  86. want bool
  87. }{
  88. {
  89. pref: AllIPv4(),
  90. want: true,
  91. },
  92. {
  93. pref: AllIPv6(),
  94. want: true,
  95. },
  96. {
  97. pref: netip.MustParsePrefix("1.1.1.1/0"),
  98. want: false,
  99. },
  100. {
  101. pref: netip.MustParsePrefix("1.1.1.1/1"),
  102. want: false,
  103. },
  104. {
  105. pref: netip.MustParsePrefix("192.168.0.0/24"),
  106. want: false,
  107. },
  108. }
  109. for _, tt := range tests {
  110. if got := IsExitRoute(tt.pref); got != tt.want {
  111. t.Errorf("for %q: got %v, want %v", tt.pref, got, tt.want)
  112. }
  113. }
  114. }
  115. func TestWithoutExitRoutes(t *testing.T) {
  116. tests := []struct {
  117. prefs []netip.Prefix
  118. want []netip.Prefix
  119. }{
  120. {
  121. prefs: []netip.Prefix{AllIPv4(), AllIPv6()},
  122. want: []netip.Prefix{},
  123. },
  124. {
  125. prefs: []netip.Prefix{AllIPv4()},
  126. want: []netip.Prefix{AllIPv4()},
  127. },
  128. {
  129. prefs: []netip.Prefix{AllIPv4(), AllIPv6(), netip.MustParsePrefix("10.0.0.0/10")},
  130. want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/10")},
  131. },
  132. {
  133. prefs: []netip.Prefix{AllIPv6(), netip.MustParsePrefix("10.0.0.0/10")},
  134. want: []netip.Prefix{AllIPv6(), netip.MustParsePrefix("10.0.0.0/10")},
  135. },
  136. }
  137. for _, tt := range tests {
  138. got := WithoutExitRoutes(views.SliceOf(tt.prefs))
  139. if diff := cmp.Diff(tt.want, got.AsSlice(), cmpopts.EquateEmpty(), cmp.Comparer(func(a, b netip.Prefix) bool { return a == b })); diff != "" {
  140. t.Errorf("unexpected route difference (-want +got):\n%s", diff)
  141. }
  142. }
  143. }
  144. func TestWithoutExitRoute(t *testing.T) {
  145. tests := []struct {
  146. prefs []netip.Prefix
  147. want []netip.Prefix
  148. }{
  149. {
  150. prefs: []netip.Prefix{AllIPv4(), AllIPv6()},
  151. want: []netip.Prefix{},
  152. },
  153. {
  154. prefs: []netip.Prefix{AllIPv4()},
  155. want: []netip.Prefix{},
  156. },
  157. {
  158. prefs: []netip.Prefix{AllIPv4(), AllIPv6(), netip.MustParsePrefix("10.0.0.0/10")},
  159. want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/10")},
  160. },
  161. {
  162. prefs: []netip.Prefix{AllIPv6(), netip.MustParsePrefix("10.0.0.0/10")},
  163. want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/10")},
  164. },
  165. }
  166. for _, tt := range tests {
  167. got := WithoutExitRoute(views.SliceOf(tt.prefs))
  168. if diff := cmp.Diff(tt.want, got.AsSlice(), cmpopts.EquateEmpty(), cmp.Comparer(func(a, b netip.Prefix) bool { return a == b })); diff != "" {
  169. t.Errorf("unexpected route difference (-want +got):\n%s", diff)
  170. }
  171. }
  172. }
  173. func TestContainsExitRoute(t *testing.T) {
  174. tests := []struct {
  175. prefs []netip.Prefix
  176. want bool
  177. }{
  178. {
  179. prefs: []netip.Prefix{AllIPv4(), AllIPv6()},
  180. want: true,
  181. },
  182. {
  183. prefs: []netip.Prefix{AllIPv4()},
  184. want: true,
  185. },
  186. {
  187. prefs: []netip.Prefix{AllIPv4(), AllIPv6(), netip.MustParsePrefix("10.0.0.0/10")},
  188. want: true,
  189. },
  190. {
  191. prefs: []netip.Prefix{AllIPv6(), netip.MustParsePrefix("10.0.0.0/10")},
  192. want: true,
  193. },
  194. {
  195. prefs: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/10")},
  196. want: false,
  197. },
  198. }
  199. for _, tt := range tests {
  200. if got := ContainsExitRoute(views.SliceOf(tt.prefs)); got != tt.want {
  201. t.Errorf("for %q: got %v, want %v", tt.prefs, got, tt.want)
  202. }
  203. }
  204. }
  205. func TestIsTailscaleIPv4(t *testing.T) {
  206. tests := []struct {
  207. in netip.Addr
  208. want bool
  209. }{
  210. {
  211. in: netip.MustParseAddr("100.67.19.57"),
  212. want: true,
  213. },
  214. {
  215. in: netip.MustParseAddr("10.10.10.10"),
  216. want: false,
  217. },
  218. {
  219. in: netip.MustParseAddr("fd7a:115c:a1e0:3f2b:7a1d:4e88:9c2b:7f01"),
  220. want: false,
  221. },
  222. {
  223. in: netip.MustParseAddr("bc9d:0aa0:1f0a:69ab:eb5c:28e0:5456:a518"),
  224. want: false,
  225. },
  226. {
  227. in: netip.MustParseAddr("100.115.92.157"),
  228. want: false,
  229. },
  230. }
  231. for _, tt := range tests {
  232. if got := IsTailscaleIPv4(tt.in); got != tt.want {
  233. t.Errorf("IsTailscaleIPv4(%v) = %v, want %v", tt.in, got, tt.want)
  234. }
  235. }
  236. }
  237. func TestIsTailscaleIP(t *testing.T) {
  238. tests := []struct {
  239. in netip.Addr
  240. want bool
  241. }{
  242. {
  243. in: netip.MustParseAddr("100.67.19.57"),
  244. want: true,
  245. },
  246. {
  247. in: netip.MustParseAddr("10.10.10.10"),
  248. want: false,
  249. },
  250. {
  251. in: netip.MustParseAddr("fd7a:115c:a1e0:3f2b:7a1d:4e88:9c2b:7f01"),
  252. want: true,
  253. },
  254. {
  255. in: netip.MustParseAddr("bc9d:0aa0:1f0a:69ab:eb5c:28e0:5456:a518"),
  256. want: false,
  257. },
  258. {
  259. in: netip.MustParseAddr("100.115.92.157"),
  260. want: false,
  261. },
  262. }
  263. for _, tt := range tests {
  264. if got := IsTailscaleIP(tt.in); got != tt.want {
  265. t.Errorf("IsTailscaleIP(%v) = %v, want %v", tt.in, got, tt.want)
  266. }
  267. }
  268. }