File: xip_test.go

package info (click to toggle)
golang-github-likexian-gokit 0.24.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 720 kB
  • sloc: makefile: 5
file content (311 lines) | stat: -rw-r--r-- 6,873 bytes parent folder | download
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright 2012-2021 Li Kexian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * A toolkit for Golang development
 * https://www.likexian.com/
 */

package xip

import (
	"testing"

	"github.com/likexian/gokit/assert"
)

func TestVersion(t *testing.T) {
	assert.Contains(t, Version(), ".")
	assert.Contains(t, Author(), "likexian")
	assert.Contains(t, License(), "Apache License")
}

func TestIsIP(t *testing.T) {
	tests := []struct {
		in  string
		out bool
	}{
		{"", false},
		{"1.1.1.256", false},
		{"1.1.1.1:80", false},
		{"1.1.1.s", false},
		{"i.a.m.s", false},
		{"0.0.0.0", true},
		{"1.1.1.1", true},
		{"127.0.0.1", true},
		{"255.255.255.255", true},
		{"::1", true},
		{"2404:6800:4005:806::2004", true},
		{"2001:db8:0:1:1:1:1:1", true},
		{"::FFFF:1:1", true},
		{"::FFFF:1.1.1.1", true},
		{"2001:db8:0:0:0:0:2:1", true},
		{"2001:db8::2:1", true},
		{"2001:db8::2:1:12345", false},
		{"2001:db8::2:1::1", false},
		{"2001:db8::2:1:ss", false},
		{"1:1:1:1:1:1:1:1:80", false},
	}

	for _, v := range tests {
		assert.Equal(t, IsIP(v.in), v.out)
	}
}

func TestIsIPv4(t *testing.T) {
	tests := []struct {
		in  string
		out bool
	}{
		{"", false},
		{"1.1.1.256", false},
		{"1.1.1.1:80", false},
		{"1.1.1.s", false},
		{"i.a.m.s", false},
		{"0.0.0.0", true},
		{"1.1.1.1", true},
		{"127.0.0.1", true},
		{"255.255.255.255", true},
		{"::1", false},
		{"2404:6800:4005:806::2004", false},
		{"2001:db8:0:1:1:1:1:1", false},
		{"::FFFF:1:1", false},
		{"::FFFF:1.1.1.1", true},
		{"2001:db8:0:0:0:0:2:1", false},
		{"2001:db8::2:1", false},
		{"2001:db8::2:1:12345", false},
		{"2001:db8::2:1::1", false},
		{"2001:db8::2:1:ss", false},
		{"1:1:1:1:1:1:1:1:80", false},
	}

	for _, v := range tests {
		assert.Equal(t, IsIPv4(v.in), v.out)
	}
}

func TestIsIPv6(t *testing.T) {
	tests := []struct {
		in  string
		out bool
	}{
		{"", false},
		{"1.1.1.256", false},
		{"1.1.1.1:80", false},
		{"1.1.1.s", false},
		{"i.a.m.s", false},
		{"0.0.0.0", false},
		{"1.1.1.1", false},
		{"127.0.0.1", false},
		{"255.255.255.255", false},
		{"::1", true},
		{"2404:6800:4005:806::2004", true},
		{"2001:db8:0:1:1:1:1:1", true},
		{"::FFFF:1:1", true},
		{"::FFFF:1.1.1.1", true},
		{"2001:db8:0:0:0:0:2:1", true},
		{"2001:db8::2:1", true},
		{"2001:db8::2:1:12345", false},
		{"2001:db8::2:1::1", false},
		{"2001:db8::2:1:ss", false},
		{"1:1:1:1:1:1:1:1:80", false},
	}

	for _, v := range tests {
		assert.Equal(t, IsIPv6(v.in), v.out)
	}
}

func TestIPv4ToLong(t *testing.T) {
	tests := []struct {
		in  string
		out uint32
		err error
	}{
		{"", uint32(0), ErrInvalidIP},
		{"1.1.1.256", uint32(0), ErrInvalidIP},
		{"1.1.1.1:80", uint32(0), ErrInvalidIP},
		{"1.1.1.s", uint32(0), ErrInvalidIP},
		{"i.a.m.s", uint32(0), ErrInvalidIP},
		{"0.0.0.0", 0, nil},
		{"1.1.1.1", 16843009, nil},
		{"127.0.0.1", 2130706433, nil},
		{"255.255.255.255", 4294967295, nil},
		{"::1", uint32(0), ErrInvalidIP},
		{"2404:6800:4005:806::2004", uint32(0), ErrInvalidIP},
	}

	for _, v := range tests {
		vv, err := IPv4ToLong(v.in)
		assert.Equal(t, err, v.err)
		assert.Equal(t, vv, v.out)
	}
}

func TestLongToIPv4(t *testing.T) {
	tests := []struct {
		in  uint32
		out string
	}{
		{0, "0.0.0.0"},
		{16843009, "1.1.1.1"},
		{2130706433, "127.0.0.1"},
		{4294967295, "255.255.255.255"},
	}

	for _, v := range tests {
		vv := LongToIPv4(v.in)
		assert.Equal(t, vv, v.out)
	}
}

func TestUint32ToHex(t *testing.T) {
	tests := []struct {
		in  uint32
		out string
	}{
		{0, "00000000"},
		{16843009, "01010101"},
		{2130706433, "7f000001"},
		{4294967295, "ffffffff"},
	}

	for _, v := range tests {
		vv := Uint32ToHex(v.in)
		assert.Equal(t, vv, v.out)
	}
}

func TestHexToUint32(t *testing.T) {
	tests := []struct {
		in  string
		out uint32
		err error
	}{
		{"00000000", 0, nil},
		{"01010101", 16843009, nil},
		{"7f000001", 2130706433, nil},
		{"ffffffff", 4294967295, nil},
		{"s", 0, ErrInvalidHex},
	}

	for _, v := range tests {
		vv, err := HexToUint32(v.in)
		assert.Equal(t, err, v.err)
		assert.Equal(t, vv, v.out)
	}
}

func TestGetEthIPv4(t *testing.T) {
	ips, err := GetEthIPv4()
	assert.Nil(t, err)
	assert.Gt(t, len(ips), 0)
}

func TestGetEthIPv4ByInterface(t *testing.T) {
	ips, err := GetEthIPv4ByInterface("lo")
	if err != nil {
		ips, err = GetEthIPv4ByInterface("lo0")
	}
	assert.Nil(t, err)
	assert.Gt(t, len(ips), 0)
}

func TestGetEthIPv6(t *testing.T) {
	_, err := GetEthIPv6()
	assert.Nil(t, err)
	// assert.Gt(t, len(ips), 0)
}

func TestGetEthIPv6ByInterface(t *testing.T) {
	_, err := GetEthIPv6ByInterface("lo")
	if err != nil {
		_, err = GetEthIPv6ByInterface("lo0")
	}
	assert.Nil(t, err)
	// assert.Gt(t, len(ips), 0)
}

func TestIsContains(t *testing.T) {
	tests := []struct {
		cidr string
		ip   string
		out  bool
	}{
		{"1", "1", false},
		{"1.1.1.1", "1", false},
		{"1.1.1.1", "1.1.1.1", false},
		{"1.1.1.0/24", "1.1.1", false},
		{"1.1.1.0/24", "1.1.1.1", true},
		{"1.1.1.0/24", "1.1.2.1", false},
		{"2404:6800:4005:806::0", "2404:6800:4005:806::0", false},
		{"2404:6800:4005:806::0/64", "2404:6800:4005:806::0", true},
		{"2404:6800:4005:806::0/64", "2404:6800:4005:807::0", false},
	}

	for _, v := range tests {
		assert.Equal(t, IsContains(v.cidr, v.ip), v.out)
	}
}

func TestIsPrivate(t *testing.T) {
	tests := []struct {
		ip  string
		out bool
	}{
		{"1", false},
		{"127.0.0.1", true},
		{"10.0.0.0", true},
		{"192.168.1.1", true},
		{"100.64.1.1", true},
		{"0.0.0.0", false},
		{"1.1.1.1", false},
		{"fc00::1", true},
		{"2404:6800:4005:806::0", false},
	}

	for _, v := range tests {
		assert.Equal(t, IsPrivate(v.ip), v.out, v)
	}
}

func TestFixSubnet(t *testing.T) {
	tests := []struct {
		ip  string
		out string
		err error
	}{
		{"", "", ErrInvalidIP},
		{"1.1.1", "", ErrInvalidIP},
		{"1.1.1.1", "1.1.1.1/24", nil},
		{"1.1.1.1/", "1.1.1.1/24", nil},
		{"1.1.1.1/25", "1.1.1.1/25", nil},
		{"1.1.1.1/-1", "1.1.1.1/24", nil},
		{"1.1.1.1/33", "", ErrInvalidMask},
		{"1.1.1.1/x", "", ErrInvalidMask},
		{"fc00", "", ErrInvalidIP},
		{"fc00::1", "fc00::1/56", nil},
		{"fc00::1/", "fc00::1/56", nil},
		{"fc00::1/57", "fc00::1/57", nil},
		{"fc00::1/-1", "fc00::1/56", nil},
		{"fc00::1/129", "", ErrInvalidMask},
		{"fc00::1/x", "", ErrInvalidMask},
	}

	for _, v := range tests {
		vv, err := FixSubnet(v.ip)
		assert.Equal(t, err, v.err)
		assert.Equal(t, vv, v.out)
	}
}