github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/config/utils_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package config
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  type formatTestCase struct {
    13  	input  []byte
    14  	output string
    15  }
    16  
    17  func TestDefineIPv6(t *testing.T) {
    18  	tests := []formatTestCase{
    19  		{
    20  			input:  nil,
    21  			output: "/* BUG: bad ip define foo  */\n",
    22  		},
    23  		{
    24  			input:  []byte{},
    25  			output: "/* BUG: bad ip define foo  */\n",
    26  		},
    27  		{
    28  			input:  []byte{1, 2, 3},
    29  			output: "/* BUG: bad ip define foo 0x1, 0x2, 0x3 */\n",
    30  		},
    31  		{
    32  			input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
    33  			output: "DEFINE_IPV6(foo, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10);\n" +
    34  				"#define foo_V\n",
    35  		},
    36  	}
    37  
    38  	for _, test := range tests {
    39  		require.Equal(t, test.output, defineIPv6("foo", test.input))
    40  	}
    41  }
    42  
    43  func TestDefineMAC(t *testing.T) {
    44  	tests := []formatTestCase{
    45  		{
    46  			input:  nil,
    47  			output: "/* BUG: bad mac define foo  */\n",
    48  		},
    49  		{
    50  			input:  []byte{},
    51  			output: "/* BUG: bad mac define foo  */\n",
    52  		},
    53  		{
    54  			input:  []byte{1, 2, 3},
    55  			output: "/* BUG: bad mac define foo 0x1, 0x2, 0x3 */\n",
    56  		},
    57  		{
    58  			input: []byte{1, 2, 3, 4, 5, 6},
    59  			output: "DEFINE_MAC(foo, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6);\n" +
    60  				"#define foo fetch_mac(foo)\n",
    61  		},
    62  	}
    63  	for _, test := range tests {
    64  		require.Equal(t, test.output, defineMAC("foo", test.input))
    65  	}
    66  }