network_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package docker
  2. import (
  3. "github.com/dotcloud/docker/pkg/iptables"
  4. "github.com/dotcloud/docker/proxy"
  5. "net"
  6. "testing"
  7. )
  8. func TestPortAllocation(t *testing.T) {
  9. ip := net.ParseIP("192.168.0.1")
  10. ip2 := net.ParseIP("192.168.0.2")
  11. allocator, err := newPortAllocator()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. if port, err := allocator.Acquire(ip, 80); err != nil {
  16. t.Fatal(err)
  17. } else if port != 80 {
  18. t.Fatalf("Acquire(80) should return 80, not %d", port)
  19. }
  20. port, err := allocator.Acquire(ip, 0)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if port <= 0 {
  25. t.Fatalf("Acquire(0) should return a non-zero port")
  26. }
  27. if _, err := allocator.Acquire(ip, port); err == nil {
  28. t.Fatalf("Acquiring a port already in use should return an error")
  29. }
  30. if newPort, err := allocator.Acquire(ip, 0); err != nil {
  31. t.Fatal(err)
  32. } else if newPort == port {
  33. t.Fatalf("Acquire(0) allocated the same port twice: %d", port)
  34. }
  35. if _, err := allocator.Acquire(ip, 80); err == nil {
  36. t.Fatalf("Acquiring a port already in use should return an error")
  37. }
  38. if _, err := allocator.Acquire(ip2, 80); err != nil {
  39. t.Fatalf("It should be possible to allocate the same port on a different interface")
  40. }
  41. if _, err := allocator.Acquire(ip2, 80); err == nil {
  42. t.Fatalf("Acquiring a port already in use should return an error")
  43. }
  44. if err := allocator.Release(ip, 80); err != nil {
  45. t.Fatal(err)
  46. }
  47. if _, err := allocator.Acquire(ip, 80); err != nil {
  48. t.Fatal(err)
  49. }
  50. }
  51. func TestNetworkRange(t *testing.T) {
  52. // Simple class C test
  53. _, network, _ := net.ParseCIDR("192.168.0.1/24")
  54. first, last := networkRange(network)
  55. if !first.Equal(net.ParseIP("192.168.0.0")) {
  56. t.Error(first.String())
  57. }
  58. if !last.Equal(net.ParseIP("192.168.0.255")) {
  59. t.Error(last.String())
  60. }
  61. if size := networkSize(network.Mask); size != 256 {
  62. t.Error(size)
  63. }
  64. // Class A test
  65. _, network, _ = net.ParseCIDR("10.0.0.1/8")
  66. first, last = networkRange(network)
  67. if !first.Equal(net.ParseIP("10.0.0.0")) {
  68. t.Error(first.String())
  69. }
  70. if !last.Equal(net.ParseIP("10.255.255.255")) {
  71. t.Error(last.String())
  72. }
  73. if size := networkSize(network.Mask); size != 16777216 {
  74. t.Error(size)
  75. }
  76. // Class A, random IP address
  77. _, network, _ = net.ParseCIDR("10.1.2.3/8")
  78. first, last = networkRange(network)
  79. if !first.Equal(net.ParseIP("10.0.0.0")) {
  80. t.Error(first.String())
  81. }
  82. if !last.Equal(net.ParseIP("10.255.255.255")) {
  83. t.Error(last.String())
  84. }
  85. // 32bit mask
  86. _, network, _ = net.ParseCIDR("10.1.2.3/32")
  87. first, last = networkRange(network)
  88. if !first.Equal(net.ParseIP("10.1.2.3")) {
  89. t.Error(first.String())
  90. }
  91. if !last.Equal(net.ParseIP("10.1.2.3")) {
  92. t.Error(last.String())
  93. }
  94. if size := networkSize(network.Mask); size != 1 {
  95. t.Error(size)
  96. }
  97. // 31bit mask
  98. _, network, _ = net.ParseCIDR("10.1.2.3/31")
  99. first, last = networkRange(network)
  100. if !first.Equal(net.ParseIP("10.1.2.2")) {
  101. t.Error(first.String())
  102. }
  103. if !last.Equal(net.ParseIP("10.1.2.3")) {
  104. t.Error(last.String())
  105. }
  106. if size := networkSize(network.Mask); size != 2 {
  107. t.Error(size)
  108. }
  109. // 26bit mask
  110. _, network, _ = net.ParseCIDR("10.1.2.3/26")
  111. first, last = networkRange(network)
  112. if !first.Equal(net.ParseIP("10.1.2.0")) {
  113. t.Error(first.String())
  114. }
  115. if !last.Equal(net.ParseIP("10.1.2.63")) {
  116. t.Error(last.String())
  117. }
  118. if size := networkSize(network.Mask); size != 64 {
  119. t.Error(size)
  120. }
  121. }
  122. func TestConversion(t *testing.T) {
  123. ip := net.ParseIP("127.0.0.1")
  124. i := ipToInt(ip)
  125. if i == 0 {
  126. t.Fatal("converted to zero")
  127. }
  128. conv := intToIP(i)
  129. if !ip.Equal(conv) {
  130. t.Error(conv.String())
  131. }
  132. }
  133. func TestIPAllocator(t *testing.T) {
  134. expectedIPs := []net.IP{
  135. 0: net.IPv4(127, 0, 0, 2),
  136. 1: net.IPv4(127, 0, 0, 3),
  137. 2: net.IPv4(127, 0, 0, 4),
  138. 3: net.IPv4(127, 0, 0, 5),
  139. 4: net.IPv4(127, 0, 0, 6),
  140. }
  141. gwIP, n, _ := net.ParseCIDR("127.0.0.1/29")
  142. alloc := newIPAllocator(&net.IPNet{IP: gwIP, Mask: n.Mask})
  143. // Pool after initialisation (f = free, u = used)
  144. // 2(f) - 3(f) - 4(f) - 5(f) - 6(f)
  145. // ↑
  146. // Check that we get 5 IPs, from 127.0.0.2–127.0.0.6, in that
  147. // order.
  148. for i := 0; i < 5; i++ {
  149. ip, err := alloc.Acquire()
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. assertIPEquals(t, expectedIPs[i], ip)
  154. }
  155. // Before loop begin
  156. // 2(f) - 3(f) - 4(f) - 5(f) - 6(f)
  157. // ↑
  158. // After i = 0
  159. // 2(u) - 3(f) - 4(f) - 5(f) - 6(f)
  160. // ↑
  161. // After i = 1
  162. // 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
  163. // ↑
  164. // After i = 2
  165. // 2(u) - 3(u) - 4(u) - 5(f) - 6(f)
  166. // ↑
  167. // After i = 3
  168. // 2(u) - 3(u) - 4(u) - 5(u) - 6(f)
  169. // ↑
  170. // After i = 4
  171. // 2(u) - 3(u) - 4(u) - 5(u) - 6(u)
  172. // ↑
  173. // Check that there are no more IPs
  174. _, err := alloc.Acquire()
  175. if err == nil {
  176. t.Fatal("There shouldn't be any IP addresses at this point")
  177. }
  178. // Release some IPs in non-sequential order
  179. alloc.Release(expectedIPs[3])
  180. // 2(u) - 3(u) - 4(u) - 5(f) - 6(u)
  181. // ↑
  182. alloc.Release(expectedIPs[2])
  183. // 2(u) - 3(u) - 4(f) - 5(f) - 6(u)
  184. // ↑
  185. alloc.Release(expectedIPs[4])
  186. // 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
  187. // ↑
  188. // Make sure that IPs are reused in sequential order, starting
  189. // with the first released IP
  190. newIPs := make([]net.IP, 3)
  191. for i := 0; i < 3; i++ {
  192. ip, err := alloc.Acquire()
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. newIPs[i] = ip
  197. }
  198. // Before loop begin
  199. // 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
  200. // ↑
  201. // After i = 0
  202. // 2(u) - 3(u) - 4(f) - 5(u) - 6(f)
  203. // ↑
  204. // After i = 1
  205. // 2(u) - 3(u) - 4(f) - 5(u) - 6(u)
  206. // ↑
  207. // After i = 2
  208. // 2(u) - 3(u) - 4(u) - 5(u) - 6(u)
  209. // ↑
  210. assertIPEquals(t, expectedIPs[3], newIPs[0])
  211. assertIPEquals(t, expectedIPs[4], newIPs[1])
  212. assertIPEquals(t, expectedIPs[2], newIPs[2])
  213. _, err = alloc.Acquire()
  214. if err == nil {
  215. t.Fatal("There shouldn't be any IP addresses at this point")
  216. }
  217. }
  218. func assertIPEquals(t *testing.T, ip1, ip2 net.IP) {
  219. if !ip1.Equal(ip2) {
  220. t.Fatalf("Expected IP %s, got %s", ip1, ip2)
  221. }
  222. }
  223. func AssertOverlap(CIDRx string, CIDRy string, t *testing.T) {
  224. _, netX, _ := net.ParseCIDR(CIDRx)
  225. _, netY, _ := net.ParseCIDR(CIDRy)
  226. if !networkOverlaps(netX, netY) {
  227. t.Errorf("%v and %v should overlap", netX, netY)
  228. }
  229. }
  230. func AssertNoOverlap(CIDRx string, CIDRy string, t *testing.T) {
  231. _, netX, _ := net.ParseCIDR(CIDRx)
  232. _, netY, _ := net.ParseCIDR(CIDRy)
  233. if networkOverlaps(netX, netY) {
  234. t.Errorf("%v and %v should not overlap", netX, netY)
  235. }
  236. }
  237. func TestNetworkOverlaps(t *testing.T) {
  238. //netY starts at same IP and ends within netX
  239. AssertOverlap("172.16.0.1/24", "172.16.0.1/25", t)
  240. //netY starts within netX and ends at same IP
  241. AssertOverlap("172.16.0.1/24", "172.16.0.128/25", t)
  242. //netY starts and ends within netX
  243. AssertOverlap("172.16.0.1/24", "172.16.0.64/25", t)
  244. //netY starts at same IP and ends outside of netX
  245. AssertOverlap("172.16.0.1/24", "172.16.0.1/23", t)
  246. //netY starts before and ends at same IP of netX
  247. AssertOverlap("172.16.1.1/24", "172.16.0.1/23", t)
  248. //netY starts before and ends outside of netX
  249. AssertOverlap("172.16.1.1/24", "172.16.0.1/23", t)
  250. //netY starts and ends before netX
  251. AssertNoOverlap("172.16.1.1/25", "172.16.0.1/24", t)
  252. //netX starts and ends before netY
  253. AssertNoOverlap("172.16.1.1/25", "172.16.2.1/24", t)
  254. }
  255. func TestCheckRouteOverlaps(t *testing.T) {
  256. routesData := []string{"10.0.2.0/32", "10.0.3.0/24", "10.0.42.0/24", "172.16.42.0/24", "192.168.142.0/24"}
  257. routes := []*net.IPNet{}
  258. for _, addr := range routesData {
  259. _, netX, _ := net.ParseCIDR(addr)
  260. routes = append(routes, netX)
  261. }
  262. _, netX, _ := net.ParseCIDR("172.16.0.1/24")
  263. if err := checkRouteOverlaps(routes, netX); err != nil {
  264. t.Fatal(err)
  265. }
  266. _, netX, _ = net.ParseCIDR("10.0.2.0/24")
  267. if err := checkRouteOverlaps(routes, netX); err == nil {
  268. t.Fatalf("10.0.2.0/24 and 10.0.2.0 should overlap but it doesn't")
  269. }
  270. }
  271. func TestCheckNameserverOverlaps(t *testing.T) {
  272. nameservers := []string{"10.0.2.3/32", "192.168.102.1/32"}
  273. _, netX, _ := net.ParseCIDR("10.0.2.3/32")
  274. if err := checkNameserverOverlaps(nameservers, netX); err == nil {
  275. t.Fatalf("%s should overlap 10.0.2.3/32 but doesn't", netX)
  276. }
  277. _, netX, _ = net.ParseCIDR("192.168.102.2/32")
  278. if err := checkNameserverOverlaps(nameservers, netX); err != nil {
  279. t.Fatalf("%s should not overlap %v but it does", netX, nameservers)
  280. }
  281. }
  282. type StubProxy struct {
  283. frontendAddr *net.Addr
  284. backendAddr *net.Addr
  285. }
  286. func (proxy *StubProxy) Run() {}
  287. func (proxy *StubProxy) Close() {}
  288. func (proxy *StubProxy) FrontendAddr() net.Addr { return *proxy.frontendAddr }
  289. func (proxy *StubProxy) BackendAddr() net.Addr { return *proxy.backendAddr }
  290. func NewStubProxy(frontendAddr, backendAddr net.Addr) (proxy.Proxy, error) {
  291. return &StubProxy{
  292. frontendAddr: &frontendAddr,
  293. backendAddr: &backendAddr,
  294. }, nil
  295. }
  296. func TestPortMapper(t *testing.T) {
  297. // FIXME: is this iptables chain still used anywhere?
  298. var chain *iptables.Chain
  299. mapper := &PortMapper{
  300. tcpMapping: make(map[string]*net.TCPAddr),
  301. tcpProxies: make(map[string]proxy.Proxy),
  302. udpMapping: make(map[string]*net.UDPAddr),
  303. udpProxies: make(map[string]proxy.Proxy),
  304. iptables: chain,
  305. defaultIp: net.IP("0.0.0.0"),
  306. proxyFactoryFunc: NewStubProxy,
  307. }
  308. dstIp1 := net.ParseIP("192.168.0.1")
  309. dstIp2 := net.ParseIP("192.168.0.2")
  310. srcAddr1 := &net.TCPAddr{Port: 1080, IP: net.ParseIP("172.16.0.1")}
  311. srcAddr2 := &net.TCPAddr{Port: 1080, IP: net.ParseIP("172.16.0.2")}
  312. if err := mapper.Map(dstIp1, 80, srcAddr1); err != nil {
  313. t.Fatalf("Failed to allocate port: %s", err)
  314. }
  315. if mapper.Map(dstIp1, 80, srcAddr1) == nil {
  316. t.Fatalf("Port is in use - mapping should have failed")
  317. }
  318. if mapper.Map(dstIp1, 80, srcAddr2) == nil {
  319. t.Fatalf("Port is in use - mapping should have failed")
  320. }
  321. if err := mapper.Map(dstIp2, 80, srcAddr2); err != nil {
  322. t.Fatalf("Failed to allocate port: %s", err)
  323. }
  324. if mapper.Unmap(dstIp1, 80, "tcp") != nil {
  325. t.Fatalf("Failed to release port")
  326. }
  327. if mapper.Unmap(dstIp2, 80, "tcp") != nil {
  328. t.Fatalf("Failed to release port")
  329. }
  330. if mapper.Unmap(dstIp2, 80, "tcp") == nil {
  331. t.Fatalf("Port already released, but no error reported")
  332. }
  333. }