diff --git a/pkg/util/ip_test.go b/pkg/util/ip_test.go new file mode 100644 index 00000000..82976be8 --- /dev/null +++ b/pkg/util/ip_test.go @@ -0,0 +1,36 @@ +package util + +import "testing" + +func TestGetIPLoc(t *testing.T) { + type args struct { + ip string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "test1", + args: args{ + ip: "", + }, + want: "", + }, + { + name: "test2", + args: args{ + ip: "103.197.70.244", + }, + want: "香港", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetIPLoc(tt.args.ip); got != tt.want { + t.Errorf("GetIPLoc() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/util/iploc/iploc.go b/pkg/util/iploc/iploc.go index 0a103738..3ccfa1b7 100644 --- a/pkg/util/iploc/iploc.go +++ b/pkg/util/iploc/iploc.go @@ -4,6 +4,7 @@ import ( _ "embed" "encoding/binary" "net" + "strings" "github.com/yinheli/mahonia" ) @@ -21,6 +22,9 @@ const ( // Find get country and city base ip func Find(ip string) (string, string) { + if strings.Trim(ip, " ") == "" { + return "", "" + } offset := searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4())) if offset <= 0 { return "", "" diff --git a/pkg/util/md5_test.go b/pkg/util/md5_test.go new file mode 100644 index 00000000..0bab593f --- /dev/null +++ b/pkg/util/md5_test.go @@ -0,0 +1,43 @@ +package util + +import "testing" + +func TestEncodeMD5(t *testing.T) { + type args struct { + value string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "test1", + args: args{ + value: "123456", + }, + want: "e10adc3949ba59abbe56e057f20f883e", + }, + { + name: "test2", + args: args{ + value: "", + }, + want: "d41d8cd98f00b204e9800998ecf8427e", // really odd, why? + }, + { + name: "test3", + args: args{ + value: "paopaocestr", + }, + want: "8a5033dda1a8919224c66e68d846a289", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := EncodeMD5(tt.args.value); got != tt.want { + t.Errorf("EncodeMD5() = %v, want %v", got, tt.want) + } + }) + } +}