-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathip_test.go
79 lines (75 loc) · 1.7 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
package go_utils
import (
"log"
"math/big"
"reflect"
"testing"
)
func TestGetOutboundIP(t *testing.T) {
//oI := *GetFromIplocation() // *GetFromIpapi() // GetIp()
oI := GetPublicIp()
log.Println(oI)
}
func TestIp2Int(t *testing.T) {
type args struct {
ip string
}
type Test struct {
name string
args args
want *big.Int
}
var tests = []Test{
Test{name: "Ipv4 to Int",
args: args{ip: "20.36.77.12"},
want: big.NewInt(281471019666700),
},
Test{name: "Ip v6 to Int",
args: args{ip: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
want: Str2BigInt("42540766452641154071740215577757643572", 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := StrIp2Int(tt.args.ip)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Ip2Int() = %v, want %v", got, tt.want)
} else {
s1 := IntToIpv6Str(got)
if !reflect.DeepEqual(s1, tt.args.ip) {
t.Errorf("IntToIpv6Str() = %v, want %v", s1, tt.args.ip)
}
}
})
}
}
func TestAny2Hex(t *testing.T) {
type args struct {
n interface{}
}
tests1 := []struct {
name string
args args
want string
}{
struct {
name string
args args
want string
}{name: "int to hex", args: args{
n: Str2BigInt("42540766452641154071740215577757643572", 0),
}, want: "3432353430373636343532363431313534303731373430323135353737373537363433353732"}}
for _, tt := range tests1 {
t.Run(tt.name, func(t *testing.T) {
if got := Any2Hex(tt.args.n); got != tt.want {
t.Errorf("Int2Hex() = %v, want %v", got, tt.want)
} else {
x1 := BigInt2Hex(tt.args.n.(*big.Int), 16)
x2 := "20010db885a3000000008a2e03707334"
if x1 != x2 {
t.Errorf("BigInt2Hex() = %v, want %v", x1, x2)
}
}
})
}
}