github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/libnetwork/drivers/bridge/port_mapping_test.go (about) 1 package bridge 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/docker/docker/pkg/reexec" 8 "github.com/docker/libnetwork/netlabel" 9 "github.com/docker/libnetwork/testutils" 10 "github.com/docker/libnetwork/types" 11 ) 12 13 func TestMain(m *testing.M) { 14 if reexec.Init() { 15 return 16 } 17 os.Exit(m.Run()) 18 } 19 20 func TestPortMappingConfig(t *testing.T) { 21 defer testutils.SetupTestOSContext(t)() 22 d := newDriver() 23 24 config := &configuration{ 25 EnableIPTables: true, 26 } 27 genericOption := make(map[string]interface{}) 28 genericOption[netlabel.GenericData] = config 29 30 if err := d.configure(genericOption); err != nil { 31 t.Fatalf("Failed to setup driver config: %v", err) 32 } 33 34 binding1 := types.PortBinding{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)} 35 binding2 := types.PortBinding{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)} 36 binding3 := types.PortBinding{Proto: types.SCTP, Port: uint16(300), HostPort: uint16(65000)} 37 portBindings := []types.PortBinding{binding1, binding2, binding3} 38 39 sbOptions := make(map[string]interface{}) 40 sbOptions[netlabel.PortMap] = portBindings 41 42 netConfig := &networkConfiguration{ 43 BridgeName: DefaultBridgeName, 44 } 45 netOptions := make(map[string]interface{}) 46 netOptions[netlabel.GenericData] = netConfig 47 48 ipdList := getIPv4Data(t, "") 49 err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil) 50 if err != nil { 51 t.Fatalf("Failed to create bridge: %v", err) 52 } 53 54 te := newTestEndpoint(ipdList[0].Pool, 11) 55 err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil) 56 if err != nil { 57 t.Fatalf("Failed to create the endpoint: %s", err.Error()) 58 } 59 60 if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil { 61 t.Fatalf("Failed to join the endpoint: %v", err) 62 } 63 64 if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil { 65 t.Fatalf("Failed to program external connectivity: %v", err) 66 } 67 68 network, ok := d.networks["dummy"] 69 if !ok { 70 t.Fatalf("Cannot find network %s inside driver", "dummy") 71 } 72 ep, _ := network.endpoints["ep1"] 73 if len(ep.portMapping) != 3 { 74 t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping) 75 } 76 if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port || 77 ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port || 78 ep.portMapping[2].Proto != binding3.Proto || ep.portMapping[2].Port != binding3.Port { 79 t.Fatal("bridgeEndpoint has incorrect port mapping values") 80 } 81 if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 || 82 ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 || 83 ep.portMapping[2].HostIP == nil || ep.portMapping[2].HostPort == 0 { 84 t.Fatal("operational port mapping data not found on bridgeEndpoint") 85 } 86 87 // release host mapped ports 88 err = d.Leave("dummy", "ep1") 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 err = d.RevokeExternalConnectivity("dummy", "ep1") 94 if err != nil { 95 t.Fatal(err) 96 } 97 } 98 99 func TestPortMappingV6Config(t *testing.T) { 100 defer testutils.SetupTestOSContext(t)() 101 d := newDriver() 102 103 config := &configuration{ 104 EnableIPTables: true, 105 EnableIP6Tables: true, 106 } 107 genericOption := make(map[string]interface{}) 108 genericOption[netlabel.GenericData] = config 109 110 if err := d.configure(genericOption); err != nil { 111 t.Fatalf("Failed to setup driver config: %v", err) 112 } 113 114 portBindings := []types.PortBinding{ 115 {Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)}, 116 {Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)}, 117 {Proto: types.SCTP, Port: uint16(500), HostPort: uint16(65000)}, 118 } 119 120 sbOptions := make(map[string]interface{}) 121 sbOptions[netlabel.PortMap] = portBindings 122 netConfig := &networkConfiguration{ 123 BridgeName: DefaultBridgeName, 124 EnableIPv6: true, 125 } 126 netOptions := make(map[string]interface{}) 127 netOptions[netlabel.GenericData] = netConfig 128 129 ipdList := getIPv4Data(t, "") 130 err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil) 131 if err != nil { 132 t.Fatalf("Failed to create bridge: %v", err) 133 } 134 135 te := newTestEndpoint(ipdList[0].Pool, 11) 136 err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil) 137 if err != nil { 138 t.Fatalf("Failed to create the endpoint: %s", err.Error()) 139 } 140 141 if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil { 142 t.Fatalf("Failed to join the endpoint: %v", err) 143 } 144 145 if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil { 146 t.Fatalf("Failed to program external connectivity: %v", err) 147 } 148 149 network, ok := d.networks["dummy"] 150 if !ok { 151 t.Fatalf("Cannot find network %s inside driver", "dummy") 152 } 153 ep, _ := network.endpoints["ep1"] 154 if len(ep.portMapping) != 6 { 155 t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping) 156 } 157 158 // release host mapped ports 159 err = d.Leave("dummy", "ep1") 160 if err != nil { 161 t.Fatal(err) 162 } 163 164 err = d.RevokeExternalConnectivity("dummy", "ep1") 165 if err != nil { 166 t.Fatal(err) 167 } 168 }