github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/libnetwork/drivers/windows/windows_test.go (about)

     1  // +build windows
     2  
     3  package windows
     4  
     5  import (
     6  	"net"
     7  	"testing"
     8  
     9  	"github.com/docker/libnetwork/driverapi"
    10  	"github.com/docker/libnetwork/netlabel"
    11  	"github.com/docker/libnetwork/types"
    12  )
    13  
    14  func testNetwork(networkType string, t *testing.T) {
    15  	d := newDriver(networkType)
    16  	bnw, _ := types.ParseCIDR("172.16.0.0/24")
    17  	br, _ := types.ParseCIDR("172.16.0.1/16")
    18  
    19  	netOption := make(map[string]interface{})
    20  	networkOptions := map[string]string{
    21  		NetworkName: "TestNetwork",
    22  	}
    23  
    24  	netOption[netlabel.GenericData] = networkOptions
    25  	ipdList := []driverapi.IPAMData{
    26  		{
    27  			Pool:    bnw,
    28  			Gateway: br,
    29  		},
    30  	}
    31  
    32  	err := d.CreateNetwork("dummy", netOption, nil, ipdList, nil)
    33  	if err != nil {
    34  		t.Fatalf("Failed to create bridge: %v", err)
    35  	}
    36  	defer func() {
    37  		err = d.DeleteNetwork("dummy")
    38  		if err != nil {
    39  			t.Fatalf("Failed to create bridge: %v", err)
    40  		}
    41  	}()
    42  
    43  	epOptions := make(map[string]interface{})
    44  	te := &testEndpoint{}
    45  	err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions)
    46  	if err != nil {
    47  		t.Fatalf("Failed to create an endpoint : %s", err.Error())
    48  	}
    49  
    50  	err = d.DeleteEndpoint("dummy", "ep1")
    51  	if err != nil {
    52  		t.Fatalf("Failed to delete an endpoint : %s", err.Error())
    53  	}
    54  }
    55  
    56  func TestNAT(t *testing.T) {
    57  	testNetwork("nat", t)
    58  }
    59  
    60  func TestTransparent(t *testing.T) {
    61  	testNetwork("transparent", t)
    62  }
    63  
    64  type testEndpoint struct {
    65  	t                     *testing.T
    66  	src                   string
    67  	dst                   string
    68  	address               string
    69  	macAddress            string
    70  	gateway               string
    71  	disableGatewayService bool
    72  }
    73  
    74  func (test *testEndpoint) Interface() driverapi.InterfaceInfo {
    75  	return test
    76  }
    77  
    78  func (test *testEndpoint) Address() *net.IPNet {
    79  	if test.address == "" {
    80  		return nil
    81  	}
    82  	nw, _ := types.ParseCIDR(test.address)
    83  	return nw
    84  }
    85  
    86  func (test *testEndpoint) AddressIPv6() *net.IPNet {
    87  	return nil
    88  }
    89  
    90  func (test *testEndpoint) MacAddress() net.HardwareAddr {
    91  	if test.macAddress == "" {
    92  		return nil
    93  	}
    94  	mac, _ := net.ParseMAC(test.macAddress)
    95  	return mac
    96  }
    97  
    98  func (test *testEndpoint) SetMacAddress(mac net.HardwareAddr) error {
    99  	if test.macAddress != "" {
   100  		return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", test.macAddress, mac)
   101  	}
   102  
   103  	if mac == nil {
   104  		return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
   105  	}
   106  	test.macAddress = mac.String()
   107  	return nil
   108  }
   109  
   110  func (test *testEndpoint) SetIPAddress(address *net.IPNet) error {
   111  	if address.IP == nil {
   112  		return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
   113  	}
   114  
   115  	test.address = address.String()
   116  	return nil
   117  }
   118  
   119  func (test *testEndpoint) InterfaceName() driverapi.InterfaceNameInfo {
   120  	return test
   121  }
   122  
   123  func (test *testEndpoint) SetGateway(ipv4 net.IP) error {
   124  	return nil
   125  }
   126  
   127  func (test *testEndpoint) SetGatewayIPv6(ipv6 net.IP) error {
   128  	return nil
   129  }
   130  
   131  func (test *testEndpoint) SetNames(src string, dst string) error {
   132  	return nil
   133  }
   134  
   135  func (test *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
   136  	return nil
   137  }
   138  
   139  func (test *testEndpoint) DisableGatewayService() {
   140  	test.disableGatewayService = true
   141  }