github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/util_internal_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2017
     3  
     4  package instana
     5  
     6  import (
     7  	"errors"
     8  	"io/ioutil"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/instana/testify/assert"
    13  	"github.com/instana/testify/require"
    14  )
    15  
    16  // Trace IDs (and Span IDs) are based on Java Signed Long datatype
    17  const (
    18  	MinInt64 int64 = -9223372036854775808
    19  	MaxInt64 int64 = 9223372036854775807
    20  )
    21  
    22  func TestGeneratedIDRange(t *testing.T) {
    23  	for index := 0; index < 10000; index++ {
    24  		id := randomID()
    25  		assert.LessOrEqual(t, id, MaxInt64)
    26  		assert.GreaterOrEqual(t, id, MinInt64)
    27  	}
    28  }
    29  
    30  func TestParseID(t *testing.T) {
    31  	maxHex := "7fffffffffffffff"
    32  
    33  	// maxID (int64) -> header -> int64
    34  	header := FormatID(MaxInt64)
    35  	assert.Equal(t, maxHex, header)
    36  
    37  	id, err := ParseID(header)
    38  	require.NoError(t, err)
    39  	assert.Equal(t, MaxInt64, id)
    40  }
    41  
    42  func TestFormatID(t *testing.T) {
    43  	minID := int64(MinInt64)
    44  	minHex := "8000000000000000"
    45  
    46  	// minHex (unsigned 64bit hex string) -> signed 64bit int -> unsigned 64bit hex string
    47  	id, err := ParseID(minHex)
    48  	require.NoError(t, err)
    49  	assert.Equal(t, minID, id)
    50  
    51  	header := FormatID(id)
    52  	assert.Equal(t, minHex, header)
    53  }
    54  
    55  func TestParseLongID(t *testing.T) {
    56  	examples := map[string]struct {
    57  		Value                  string
    58  		ExpectedHi, ExpectedLo int64
    59  	}{
    60  		"64-bit short":             {"1234ab", 0x0, 0x1234ab},
    61  		"64-bit padded":            {"00000000001234ab", 0x0, 0x1234ab},
    62  		"64-bit padded to 128-bit": {"000000000000000000000000001234ab", 0x0, 0x1234ab},
    63  		"128-bit short":            {"1c00000000001234ab", 0x1c, 0x1234ab},
    64  		"128-bit padded":           {"000000000000001c00000000001234ab", 0x1c, 0x1234ab},
    65  	}
    66  
    67  	for name, example := range examples {
    68  		t.Run(name, func(t *testing.T) {
    69  			hi, lo, err := ParseLongID(example.Value)
    70  			require.NoError(t, err)
    71  
    72  			assert.Equal(t, example.ExpectedHi, hi)
    73  			assert.Equal(t, example.ExpectedLo, lo)
    74  		})
    75  	}
    76  }
    77  
    78  func TestFormatLongID(t *testing.T) {
    79  	examples := map[string]struct {
    80  		Hi, Lo   int64
    81  		Expected string
    82  	}{
    83  		"64-bit":  {0x0, 0x1234ab, "000000000000000000000000001234ab"},
    84  		"128-bit": {0x1c, 0x1234ab, "000000000000001c00000000001234ab"},
    85  	}
    86  
    87  	for name, example := range examples {
    88  		t.Run(name, func(t *testing.T) {
    89  			assert.Equal(t, example.Expected, FormatLongID(example.Hi, example.Lo))
    90  		})
    91  	}
    92  }
    93  
    94  func TestBogusValues(t *testing.T) {
    95  	var id int64
    96  
    97  	// ParseID with random strings should return 0
    98  	id, err := ParseID("this shouldnt work")
    99  	assert.Equal(t, int64(0), id, "Bad input should return 0")
   100  	assert.NotNil(t, err, "An error should be returned")
   101  }
   102  
   103  func TestHexGatewayToAddr(t *testing.T) {
   104  	tests := []struct {
   105  		in          string
   106  		expected    string
   107  		expectedErr error
   108  	}{
   109  		{
   110  			in:          "0101FEA9",
   111  			expected:    "169.254.1.1",
   112  			expectedErr: nil,
   113  		},
   114  		{
   115  			in:          "0101FEAC",
   116  			expected:    "172.254.1.1",
   117  			expectedErr: nil,
   118  		},
   119  		{
   120  			in:          "0101FEA",
   121  			expected:    "",
   122  			expectedErr: errors.New("invalid gateway length"),
   123  		},
   124  	}
   125  
   126  	for _, test := range tests {
   127  		gatewayHex := []rune(test.in)
   128  		gateway, err := hexGatewayToAddr(gatewayHex)
   129  		assert.Equal(t, test.expectedErr, err)
   130  		assert.Equal(t, test.expected, gateway)
   131  	}
   132  }
   133  
   134  func TestGetDefaultGateway(t *testing.T) {
   135  
   136  	tests := []struct {
   137  		in          string
   138  		expected    string
   139  		expectError bool
   140  	}{
   141  		{
   142  			in: `Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask		MTU	Window	IRTT
   143  
   144  eth0	00000000	0101FEA9	0003	0	0	0	00000000	0	0	0
   145  
   146  eth0	0101FEA9	00000000	0005	0	0	0	FFFFFFFF	0	0	0
   147  
   148  `,
   149  			expected: "169.254.1.1",
   150  		},
   151  		{
   152  			in: `Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask		MTU	Window	IRTT
   153  										 
   154  eth0	000011AC	00000000	0001	0	0	0	0000FFFF	0	0	0
   155  
   156  eth0	00000000	010011AC	0003	0	0	0	00000000	0	0	0
   157                                                                                 
   158  `,
   159  			expected: "172.17.0.1",
   160  		},
   161  	}
   162  
   163  	for _, test := range tests {
   164  		func() {
   165  			tmpFile, err := ioutil.TempFile("", "getdefaultgateway")
   166  			if err != nil {
   167  				t.Fatal(err)
   168  			}
   169  
   170  			defer os.Remove(tmpFile.Name())
   171  
   172  			_, err = tmpFile.WriteString(test.in)
   173  
   174  			if err != nil {
   175  				t.Fatal(err)
   176  			}
   177  
   178  			gateway, err := getDefaultGateway(tmpFile.Name())
   179  			require.NoError(t, err)
   180  			assert.Equal(t, test.expected, gateway)
   181  		}()
   182  	}
   183  }
   184  
   185  func BenchmarkFormatID(b *testing.B) {
   186  	for i := 0; i < b.N; i++ {
   187  		FormatID(int64(i))
   188  	}
   189  }