File size: 4,050 Bytes
5c24871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package utils

import (
	"reflect"
	"testing"
)

type testSt struct {
	input  string
	output string
}

func TestNotification(t *testing.T) {
	cases := []testSt{
		{
			input:  "103.80.236.249/d5ce:d811:cdb8:067a:a873:2076:9521:9d2d",
			output: "103.****.249/d5ce:d811:****:9521:9d2d",
		},
		{
			input:  "3.80.236.29/d5ce::cdb8:067a:a873:2076:9521:9d2d",
			output: "3.****.29/d5ce::****:9521:9d2d",
		},
		{
			input:  "3.80.236.29/d5ce::cdb8:067a:a873:2076::9d2d",
			output: "3.****.29/d5ce::****::9d2d",
		},
		{
			input:  "3.80.236.9/d5ce::cdb8:067a:a873:2076::9d2d",
			output: "3.****.9/d5ce::****::9d2d",
		},
		{
			input:  "3.80.236.9/d5ce::cdb8:067a:a873:2076::9d2d",
			output: "3.****.9/d5ce::****::9d2d",
		},
	}

	for _, c := range cases {
		if c.output != IPDesensitize(c.input) {
			t.Fatalf("Expected %s, but got %s", c.output, IPDesensitize(c.input))
		}
	}
}

func TestGenerGenerateRandomString(t *testing.T) {
	generatedString := make(map[string]bool)
	for range 100 {
		str, err := GenerateRandomString(32)
		if err != nil {
			t.Fatalf("Error: %s", err)
		}
		if len(str) != 32 {
			t.Fatalf("Expected 32, but got %d", len(str))
		}
		if generatedString[str] {
			t.Fatalf("Duplicated string: %s", str)
		}
		generatedString[str] = true
	}
}

func TestIPStringToBinary(t *testing.T) {
	cases := []struct {
		ip          string
		want        []byte
		expectError bool
	}{
		// ζœ‰ζ•ˆηš„ IPv4 εœ°ε€
		{
			ip: "192.168.1.1",
			want: []byte{
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 192, 168, 1, 1,
			},
			expectError: false,
		},
		// ζœ‰ζ•ˆηš„ IPv6 εœ°ε€
		{
			ip: "2001:db8::68",
			want: []byte{
				32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104,
			},
			expectError: false,
		},
		// ζ— ζ•ˆηš„ IP εœ°ε€
		{
			ip:          "invalid_ip",
			want:        []byte{},
			expectError: true,
		},
	}

	for _, c := range cases {
		got, err := IPStringToBinary(c.ip)
		if (err != nil) != c.expectError {
			t.Errorf("IPStringToBinary(%q) error = %v, expect error = %v", c.ip, err, c.expectError)
			continue
		}
		if err == nil && !reflect.DeepEqual(got, c.want) {
			t.Errorf("IPStringToBinary(%q) = %v, want %v", c.ip, got, c.want)
		}
	}
}

func TestBinaryToIPString(t *testing.T) {
	cases := []struct {
		binary []byte
		want   string
	}{
		// IPv4 εœ°ε€οΌˆIPv4 ζ˜ ε°„ηš„ IPv6 εœ°ε€ζ ΌεΌοΌ‰
		{
			binary: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 192, 168, 1, 1},
			want:   "192.168.1.1",
		},
		// 兢他桋试用例
		{
			binary: []byte{32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104},
			want:   "2001:db8::68",
		},
		// ε…¨ι›Άε€Ό
		{
			binary: []byte{},
			want:   "::",
		},
		// IPv4 ζ˜ ε°„ηš„ IPv6 εœ°ε€
		{
			binary: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1},
			want:   "127.0.0.1",
		},
	}

	for _, c := range cases {
		got := BinaryToIPString(c.binary)
		if got != c.want {
			t.Errorf("BinaryToIPString(%v) = %q, ζœŸζœ› %q", c.binary, got, c.want)
		}
	}
}

func TestUnique(t *testing.T) {
	cases := []struct {
		input  []string
		output []string
	}{
		{
			input:  []string{"a", "b", "c", "a", "b", "c"},
			output: []string{"a", "b", "c"},
		},
		{
			input:  []string{"a", "b", "c"},
			output: []string{"a", "b", "c"},
		},
		{
			input:  []string{"a", "a", "a"},
			output: []string{"a"},
		},
		{
			input:  []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
			output: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
		},
		{
			input:  []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "a"},
			output: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"},
		},
		{
			input:  []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "a", "b", "c", "d", "e", "f", "g", "h", "i"},
			output: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"},
		},
		{
			input:  []string{},
			output: []string{},
		},
		{
			input:  []string{"a"},
			output: []string{"a"},
		},
	}

	for _, c := range cases {
		if !reflect.DeepEqual(Unique(c.input), c.output) {
			t.Fatalf("Expected %v, but got %v", c.output, Unique(c.input))
		}
	}
}