github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/network/linklayer_test.go (about)

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package network
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  )
    13  
    14  type linkLayerSuite struct {
    15  	testing.IsolationSuite
    16  }
    17  
    18  var _ = gc.Suite(&linkLayerSuite{})
    19  
    20  func (s *linkLayerSuite) TestIsValidLinkLayerDeviceTypeValid(c *gc.C) {
    21  	validTypes := []LinkLayerDeviceType{
    22  		LoopbackDevice,
    23  		EthernetDevice,
    24  		VLAN8021QDevice,
    25  		BondDevice,
    26  		BridgeDevice,
    27  		VXLANDevice,
    28  	}
    29  
    30  	for _, value := range validTypes {
    31  		result := IsValidLinkLayerDeviceType(string(value))
    32  		c.Check(result, jc.IsTrue)
    33  	}
    34  }
    35  
    36  func (s *linkLayerSuite) TestIsValidLinkLayerDeviceTypeInvalid(c *gc.C) {
    37  	result := IsValidLinkLayerDeviceType("")
    38  	c.Check(result, jc.IsFalse)
    39  
    40  	result = IsValidLinkLayerDeviceType("anything")
    41  	c.Check(result, jc.IsFalse)
    42  
    43  	result = IsValidLinkLayerDeviceType(" ")
    44  	c.Check(result, jc.IsFalse)
    45  
    46  	result = IsValidLinkLayerDeviceType("unknown")
    47  	c.Check(result, jc.IsFalse)
    48  }
    49  
    50  var validUnixDeviceNames = []string{"eth0", "eno1", "br-eth0.123", "tun:1", "bond0.42"}
    51  
    52  func (s *linkLayerSuite) TestIsValidLinkLayerDeviceNameValidNamesLinux(c *gc.C) {
    53  	for i, name := range validUnixDeviceNames {
    54  		c.Logf("test #%d: %q -> valid", i, name)
    55  		result := isValidLinkLayerDeviceName(name, "linux")
    56  		c.Check(result, jc.IsTrue)
    57  	}
    58  }
    59  
    60  func (s *linkLayerSuite) TestIsValidLinkLayerDeviceNameInvalidNamesLinux(c *gc.C) {
    61  	for i, name := range []string{
    62  		"",
    63  		strings.Repeat("x", 16),
    64  		"with-hash#",
    65  		"has spaces",
    66  		"has\tabs",
    67  		"has\newline",
    68  		"has\r",
    69  		"has\vtab",
    70  		".",
    71  		"..",
    72  	} {
    73  		c.Logf("test #%d: %q -> invalid", i, name)
    74  		result := isValidLinkLayerDeviceName(name, "linux")
    75  		c.Check(result, jc.IsFalse)
    76  	}
    77  }
    78  
    79  func (s *linkLayerSuite) TestIsValidLinkLayerDeviceNameValidNamesNonLinux(c *gc.C) {
    80  	validDeviceNames := append(validUnixDeviceNames,
    81  		// Windows network device as friendly name and as underlying UUID.
    82  		"Local Area Connection", "{4a62b748-43d0-4136-92e4-22ce7ee31938}",
    83  	)
    84  
    85  	for i, name := range validDeviceNames {
    86  		c.Logf("test #%d: %q -> valid", i, name)
    87  		result := isValidLinkLayerDeviceName(name, "non-linux")
    88  		c.Check(result, jc.IsTrue)
    89  	}
    90  }
    91  
    92  func (s *linkLayerSuite) TestIsValidLinkLayerDeviceNameInvalidNamesNonLinux(c *gc.C) {
    93  	os := "non-linux"
    94  
    95  	result := isValidLinkLayerDeviceName("", os)
    96  	c.Check(result, jc.IsFalse)
    97  
    98  	const wayTooLongLength = 1024
    99  	result = isValidLinkLayerDeviceName(strings.Repeat("x", wayTooLongLength), os)
   100  	c.Check(result, jc.IsFalse)
   101  
   102  	result = isValidLinkLayerDeviceName("hash# not allowed", os)
   103  	c.Check(result, jc.IsFalse)
   104  }
   105  
   106  func (s *linkLayerSuite) TestStringLengthBetweenWhenTooShort(c *gc.C) {
   107  	result := stringLengthBetween("", 1, 2)
   108  	c.Check(result, jc.IsFalse)
   109  
   110  	result = stringLengthBetween("", 1, 1)
   111  	c.Check(result, jc.IsFalse)
   112  
   113  	result = stringLengthBetween("1", 2, 3)
   114  	c.Check(result, jc.IsFalse)
   115  
   116  	result = stringLengthBetween("12", 3, 3)
   117  	c.Check(result, jc.IsFalse)
   118  }
   119  
   120  func (s *linkLayerSuite) TestStringLengthBetweenWhenTooLong(c *gc.C) {
   121  	result := stringLengthBetween("1", 0, 0)
   122  	c.Check(result, jc.IsFalse)
   123  
   124  	result = stringLengthBetween("12", 1, 1)
   125  	c.Check(result, jc.IsFalse)
   126  
   127  	result = stringLengthBetween("123", 1, 2)
   128  	c.Check(result, jc.IsFalse)
   129  
   130  	result = stringLengthBetween("123", 0, 1)
   131  	c.Check(result, jc.IsFalse)
   132  }
   133  
   134  func (s *linkLayerSuite) TestStringLengthBetweenWhenWithinLimit(c *gc.C) {
   135  	const (
   136  		minLength = 1
   137  		maxLength = 255
   138  	)
   139  	for i := minLength; i <= maxLength; i++ {
   140  		input := strings.Repeat("x", i)
   141  		result := stringLengthBetween(input, minLength, maxLength)
   142  		c.Check(result, jc.IsTrue)
   143  	}
   144  }