2021-05-27 08:48:12 -04:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2024-11-22 21:21:01 -05:00
|
|
|
|
"reflect"
|
2021-05-27 08:48:12 -04:00
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type testSt struct {
|
|
|
|
|
input string
|
|
|
|
|
output string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNotification(t *testing.T) {
|
|
|
|
|
cases := []testSt{
|
|
|
|
|
{
|
2021-09-29 08:14:25 -04:00
|
|
|
|
input: "103.80.236.249/d5ce:d811:cdb8:067a:a873:2076:9521:9d2d",
|
|
|
|
|
output: "103.****.249/d5ce:d811:****:9521:9d2d",
|
2021-05-27 08:48:12 -04:00
|
|
|
|
},
|
|
|
|
|
{
|
2021-09-29 08:14:25 -04:00
|
|
|
|
input: "3.80.236.29/d5ce::cdb8:067a:a873:2076:9521:9d2d",
|
|
|
|
|
output: "3.****.29/d5ce::****:9521:9d2d",
|
2021-05-27 08:48:12 -04:00
|
|
|
|
},
|
|
|
|
|
{
|
2021-09-29 08:14:25 -04:00
|
|
|
|
input: "3.80.236.29/d5ce::cdb8:067a:a873:2076::9d2d",
|
|
|
|
|
output: "3.****.29/d5ce::****::9d2d",
|
2021-05-27 08:48:12 -04:00
|
|
|
|
},
|
|
|
|
|
{
|
2021-09-29 08:14:25 -04:00
|
|
|
|
input: "3.80.236.9/d5ce::cdb8:067a:a873:2076::9d2d",
|
|
|
|
|
output: "3.****.9/d5ce::****::9d2d",
|
2021-05-27 08:48:12 -04:00
|
|
|
|
},
|
|
|
|
|
{
|
2021-09-29 08:14:25 -04:00
|
|
|
|
input: "3.80.236.9/d5ce::cdb8:067a:a873:2076::9d2d",
|
|
|
|
|
output: "3.****.9/d5ce::****::9d2d",
|
2021-05-27 08:48:12 -04:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2024-03-23 01:00:47 -04:00
|
|
|
|
if c.output != IPDesensitize(c.input) {
|
|
|
|
|
t.Fatalf("Expected %s, but got %s", c.output, IPDesensitize(c.input))
|
|
|
|
|
}
|
2021-05-27 08:48:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-16 10:34:14 -05:00
|
|
|
|
|
|
|
|
|
func TestGenerGenerateRandomString(t *testing.T) {
|
|
|
|
|
generatedString := make(map[string]bool)
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
|
str, err := GenerateRandomString(32)
|
2024-03-23 01:00:47 -04:00
|
|
|
|
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)
|
|
|
|
|
}
|
2022-12-16 10:34:14 -05:00
|
|
|
|
generatedString[str] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-22 21:21:01 -05:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-22 04:23:55 -05:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|