-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathutil_internal_test.go
280 lines (240 loc) · 5.72 KB
/
util_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2017
package instana
import (
"errors"
"io/ioutil"
"os"
"testing"
"github.com/opentracing/opentracing-go/ext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Trace IDs (and Span IDs) are based on Java Signed Long datatype
const (
MinInt64 int64 = -9223372036854775808
MaxInt64 int64 = 9223372036854775807
)
func TestGeneratedIDRange(t *testing.T) {
for index := 0; index < 10000; index++ {
id := randomID()
assert.LessOrEqual(t, id, MaxInt64)
assert.GreaterOrEqual(t, id, MinInt64)
}
}
func TestParseID(t *testing.T) {
maxHex := "7fffffffffffffff"
// maxID (int64) -> header -> int64
header := FormatID(MaxInt64)
assert.Equal(t, maxHex, header)
id, err := ParseID(header)
require.NoError(t, err)
assert.Equal(t, MaxInt64, id)
}
func TestFormatID(t *testing.T) {
minID := int64(MinInt64)
minHex := "8000000000000000"
// minHex (unsigned 64bit hex string) -> signed 64bit int -> unsigned 64bit hex string
id, err := ParseID(minHex)
require.NoError(t, err)
assert.Equal(t, minID, id)
header := FormatID(id)
assert.Equal(t, minHex, header)
}
func TestParseLongID(t *testing.T) {
examples := map[string]struct {
Value string
ExpectedHi, ExpectedLo int64
}{
"64-bit short": {"1234ab", 0x0, 0x1234ab},
"64-bit padded": {"00000000001234ab", 0x0, 0x1234ab},
"64-bit padded to 128-bit": {"000000000000000000000000001234ab", 0x0, 0x1234ab},
"128-bit short": {"1c00000000001234ab", 0x1c, 0x1234ab},
"128-bit padded": {"000000000000001c00000000001234ab", 0x1c, 0x1234ab},
}
for name, example := range examples {
t.Run(name, func(t *testing.T) {
hi, lo, err := ParseLongID(example.Value)
require.NoError(t, err)
assert.Equal(t, example.ExpectedHi, hi)
assert.Equal(t, example.ExpectedLo, lo)
})
}
}
func TestFormatLongID(t *testing.T) {
examples := map[string]struct {
Hi, Lo int64
Expected string
}{
"64-bit": {0x0, 0x1234ab, "000000000000000000000000001234ab"},
"128-bit": {0x1c, 0x1234ab, "000000000000001c00000000001234ab"},
}
for name, example := range examples {
t.Run(name, func(t *testing.T) {
assert.Equal(t, example.Expected, FormatLongID(example.Hi, example.Lo))
})
}
}
func TestBogusValues(t *testing.T) {
var id int64
// ParseID with random strings should return 0
id, err := ParseID("this shouldnt work")
assert.Equal(t, int64(0), id, "Bad input should return 0")
assert.NotNil(t, err, "An error should be returned")
}
func TestHexGatewayToAddr(t *testing.T) {
tests := []struct {
in string
expected string
expectedErr error
}{
{
in: "0101FEA9",
expected: "169.254.1.1",
expectedErr: nil,
},
{
in: "0101FEAC",
expected: "172.254.1.1",
expectedErr: nil,
},
{
in: "0101FEA",
expected: "",
expectedErr: errors.New("invalid gateway length"),
},
}
for _, test := range tests {
gateway, err := hexGatewayToAddr(test.in)
assert.Equal(t, test.expectedErr, err)
assert.Equal(t, test.expected, gateway)
}
}
func TestGetDefaultGateway(t *testing.T) {
tests := []struct {
in string
expected string
expectError bool
}{
{
in: `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00000000 0101FEA9 0003 0 0 0 00000000 0 0 0
eth0 0101FEA9 00000000 0005 0 0 0 FFFFFFFF 0 0 0
`,
expected: "169.254.1.1",
},
{
in: `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 000011AC 00000000 0001 0 0 0 0000FFFF 0 0 0
eth0 00000000 010011AC 0003 0 0 0 00000000 0 0 0
`,
expected: "172.17.0.1",
},
}
for _, test := range tests {
func() {
tmpFile, err := ioutil.TempFile("", "getdefaultgateway")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())
_, err = tmpFile.WriteString(test.in)
if err != nil {
t.Fatal(err)
}
gateway, err := getDefaultGateway(tmpFile.Name())
require.NoError(t, err)
assert.Equal(t, test.expected, gateway)
}()
}
}
func BenchmarkFormatID(b *testing.B) {
for i := 0; i < b.N; i++ {
FormatID(int64(i))
}
}
func Test_isRootExitSpan(t *testing.T) {
type args struct {
kind interface{}
isRootSpan bool
}
tests := []struct {
name string
args args
want bool
}{
{
name: "spanType_exit_rootSpan_true",
args: args{
kind: ext.SpanKindRPCClientEnum,
isRootSpan: true,
},
want: true,
},
{
name: "spanType_exit_rootSpan_false",
args: args{
kind: ext.SpanKindRPCServerEnum,
},
want: false,
},
{
name: "spanType_entry_rootSpan_true",
args: args{
kind: nil,
},
want: false,
},
{
name: "spanType_entry_rootSpan_false",
args: args{
kind: nil,
},
want: false,
},
{
name: "spanType_nil_rootSpan_true",
args: args{
kind: nil,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isRootExitSpan(tt.args.kind, tt.args.isRootSpan)
assert.Equal(t, tt.want, got)
})
}
}
func Test_allowRootExitSpan(t *testing.T) {
tests := []struct {
name string
exportEnv bool
want bool
}{
{
name: "env_set",
exportEnv: true,
want: true,
},
{
name: "env_unset",
exportEnv: false,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.exportEnv {
os.Setenv(allowRootExitSpanEnv, "1")
defer func() {
os.Unsetenv(allowRootExitSpanEnv)
}()
}
if got := allowRootExitSpan(); got != tt.want {
t.Errorf("allowRootExitSpan() = %v, want %v", got, tt.want)
}
})
}
}