-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip_test.go
143 lines (106 loc) · 2.67 KB
/
ip_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
package utils_test
import (
"net"
"testing"
"github.com/valyala/fasthttp"
"github.com/nano-interactive/go-utils/v2"
"github.com/stretchr/testify/require"
)
func TestGetLocalIP(t *testing.T) {
t.Parallel()
assert := require.New(t)
addrs, err := net.InterfaceAddrs()
assert.NoError(err)
var ip string
for _, address := range addrs {
ipnet, ok := address.(*net.IPNet)
// check the address type and if it is not a loopback the display it
if ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
ip = ipnet.IP.String()
break
}
}
localIp := utils.GetLocalIP()
assert.Equal(ip, localIp)
// Testing the cached version
localIp = utils.GetLocalIP()
assert.Equal(ip, localIp)
}
func TestGetLocalIPs(t *testing.T) {
t.Parallel()
assert := require.New(t)
addrs, err := net.InterfaceAddrs()
assert.NoError(err)
var ips []string
for _, address := range addrs {
ipnet, ok := address.(*net.IPNet)
// check the address type and if it is not a loopback the display it
if ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
localIps := utils.GetLocalIPs()
assert.EqualValues(ips, localIps)
// Testing the cached version
localIps = utils.GetLocalIPs()
assert.EqualValues(ips, localIps)
}
func TestAnonymizeIpSuccess(t *testing.T) {
// Arrange
t.Parallel()
assert := require.New(t)
ip := []byte("192.172.90.70")
expectedIp := []byte("192.172.90.0")
// Act
anonymizedIp := utils.AnonymizeIp(ip)
// Assert
assert.NotEqual(ip, anonymizedIp)
assert.Equal(expectedIp, anonymizedIp)
}
func TestAnonymizeIpInvalidIp(t *testing.T) {
// Arrange
t.Parallel()
assert := require.New(t)
ip := []byte("invalid_ip")
// Act
anonymizedIp := utils.AnonymizeIp(ip)
// Assert
assert.Equal([]byte(utils.UnknownIp), anonymizedIp)
}
func TestRealIpReturnNilEmptyHeader(t *testing.T) {
// Arrange
t.Parallel()
assert := require.New(t)
header := fasthttp.ResponseHeader{}
// Act
ip := utils.RealIp(&header)
// Assert
assert.Nil(ip)
}
func TestRealIpReturnIpNormalIp(t *testing.T) {
// Arrange
t.Parallel()
assert := require.New(t)
header := fasthttp.ResponseHeader{}
ipString := "10.20.30.40"
header.Set(utils.HeaderXForwardedFor, ipString)
// Act
ip := utils.RealIp(&header)
// Assert
assert.NotNil(ip)
assert.Equal([]byte(ipString), ip)
}
func TestRealIpReturnIpCommaSeperated(t *testing.T) {
// Arrange
t.Parallel()
assert := require.New(t)
header := fasthttp.ResponseHeader{}
expected := "10.20.30.40"
ipString := expected + ",50.60.70.80"
header.Set(utils.HeaderXForwardedFor, ipString)
// Act
ip := utils.RealIp(&header)
// Assert
assert.NotNil(ip)
assert.Equal([]byte(expected), ip)
}