github.com/erriapo/docker@v1.6.0-rc2/daemon/networkdriver/portmapper/mapper_test.go (about) 1 package portmapper 2 3 import ( 4 "net" 5 "testing" 6 7 "github.com/docker/docker/daemon/networkdriver/portallocator" 8 "github.com/docker/docker/pkg/iptables" 9 ) 10 11 func init() { 12 // override this func to mock out the proxy server 13 NewProxy = NewMockProxyCommand 14 } 15 16 func reset() { 17 chain = nil 18 currentMappings = make(map[string]*mapping) 19 } 20 21 func TestSetIptablesChain(t *testing.T) { 22 defer reset() 23 24 c := &iptables.Chain{ 25 Name: "TEST", 26 Bridge: "192.168.1.1", 27 } 28 29 if chain != nil { 30 t.Fatal("chain should be nil at init") 31 } 32 33 SetIptablesChain(c) 34 if chain == nil { 35 t.Fatal("chain should not be nil after set") 36 } 37 } 38 39 func TestMapPorts(t *testing.T) { 40 dstIp1 := net.ParseIP("192.168.0.1") 41 dstIp2 := net.ParseIP("192.168.0.2") 42 dstAddr1 := &net.TCPAddr{IP: dstIp1, Port: 80} 43 dstAddr2 := &net.TCPAddr{IP: dstIp2, Port: 80} 44 45 srcAddr1 := &net.TCPAddr{Port: 1080, IP: net.ParseIP("172.16.0.1")} 46 srcAddr2 := &net.TCPAddr{Port: 1080, IP: net.ParseIP("172.16.0.2")} 47 48 addrEqual := func(addr1, addr2 net.Addr) bool { 49 return (addr1.Network() == addr2.Network()) && (addr1.String() == addr2.String()) 50 } 51 52 if host, err := Map(srcAddr1, dstIp1, 80); err != nil { 53 t.Fatalf("Failed to allocate port: %s", err) 54 } else if !addrEqual(dstAddr1, host) { 55 t.Fatalf("Incorrect mapping result: expected %s:%s, got %s:%s", 56 dstAddr1.String(), dstAddr1.Network(), host.String(), host.Network()) 57 } 58 59 if _, err := Map(srcAddr1, dstIp1, 80); err == nil { 60 t.Fatalf("Port is in use - mapping should have failed") 61 } 62 63 if _, err := Map(srcAddr2, dstIp1, 80); err == nil { 64 t.Fatalf("Port is in use - mapping should have failed") 65 } 66 67 if _, err := Map(srcAddr2, dstIp2, 80); err != nil { 68 t.Fatalf("Failed to allocate port: %s", err) 69 } 70 71 if Unmap(dstAddr1) != nil { 72 t.Fatalf("Failed to release port") 73 } 74 75 if Unmap(dstAddr2) != nil { 76 t.Fatalf("Failed to release port") 77 } 78 79 if Unmap(dstAddr2) == nil { 80 t.Fatalf("Port already released, but no error reported") 81 } 82 } 83 84 func TestGetUDPKey(t *testing.T) { 85 addr := &net.UDPAddr{IP: net.ParseIP("192.168.1.5"), Port: 53} 86 87 key := getKey(addr) 88 89 if expected := "192.168.1.5:53/udp"; key != expected { 90 t.Fatalf("expected key %s got %s", expected, key) 91 } 92 } 93 94 func TestGetTCPKey(t *testing.T) { 95 addr := &net.TCPAddr{IP: net.ParseIP("192.168.1.5"), Port: 80} 96 97 key := getKey(addr) 98 99 if expected := "192.168.1.5:80/tcp"; key != expected { 100 t.Fatalf("expected key %s got %s", expected, key) 101 } 102 } 103 104 func TestGetUDPIPAndPort(t *testing.T) { 105 addr := &net.UDPAddr{IP: net.ParseIP("192.168.1.5"), Port: 53} 106 107 ip, port := getIPAndPort(addr) 108 if expected := "192.168.1.5"; ip.String() != expected { 109 t.Fatalf("expected ip %s got %s", expected, ip) 110 } 111 112 if ep := 53; port != ep { 113 t.Fatalf("expected port %d got %d", ep, port) 114 } 115 } 116 117 func TestMapAllPortsSingleInterface(t *testing.T) { 118 dstIp1 := net.ParseIP("0.0.0.0") 119 srcAddr1 := &net.TCPAddr{Port: 1080, IP: net.ParseIP("172.16.0.1")} 120 121 hosts := []net.Addr{} 122 var host net.Addr 123 var err error 124 125 defer func() { 126 for _, val := range hosts { 127 Unmap(val) 128 } 129 }() 130 131 for i := 0; i < 10; i++ { 132 start, end := portallocator.PortRange() 133 for i := start; i < end; i++ { 134 if host, err = Map(srcAddr1, dstIp1, 0); err != nil { 135 t.Fatal(err) 136 } 137 138 hosts = append(hosts, host) 139 } 140 141 if _, err := Map(srcAddr1, dstIp1, start); err == nil { 142 t.Fatalf("Port %d should be bound but is not", start) 143 } 144 145 for _, val := range hosts { 146 if err := Unmap(val); err != nil { 147 t.Fatal(err) 148 } 149 } 150 151 hosts = []net.Addr{} 152 } 153 }