112 lines
2.1 KiB
Go
112 lines
2.1 KiB
Go
package utils
|
|
|
|
import "testing"
|
|
|
|
func TestGenerateID(t *testing.T) {
|
|
id := GenerateID()
|
|
if len(id) != 32 {
|
|
t.Errorf("Expected ID length 32, got %d", len(id))
|
|
}
|
|
}
|
|
|
|
func TestFormatBytes(t *testing.T) {
|
|
tests := []struct {
|
|
bytes int64
|
|
want string
|
|
}{
|
|
{0, "0 B"},
|
|
{1023, "1023 B"},
|
|
{1024, "1.0 KiB"},
|
|
{1024 * 1024, "1.0 MiB"},
|
|
{1024 * 1024 * 1024, "1.0 GiB"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := FormatBytes(tt.bytes)
|
|
if got != tt.want {
|
|
t.Errorf("FormatBytes(%d) = %s, want %s", tt.bytes, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseBytes(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want int64
|
|
}{
|
|
{"1KB", 1024},
|
|
{"1MB", 1024 * 1024},
|
|
{"1GB", 1024 * 1024 * 1024},
|
|
{"100", 100},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got, err := ParseBytes(tt.input)
|
|
if err != nil {
|
|
t.Errorf("ParseBytes(%s) error: %v", tt.input, err)
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("ParseBytes(%s) = %d, want %d", tt.input, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsPrivateIP(t *testing.T) {
|
|
tests := []struct {
|
|
ip string
|
|
want bool
|
|
}{
|
|
{"10.0.0.1", true},
|
|
{"172.16.0.1", true},
|
|
{"192.168.1.1", true},
|
|
{"127.0.0.1", true},
|
|
{"8.8.8.8", false},
|
|
{"1.2.3.4", false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := IsPrivateIP(tt.ip)
|
|
if got != tt.want {
|
|
t.Errorf("IsPrivateIP(%s) = %v, want %v", tt.ip, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsValidPort(t *testing.T) {
|
|
tests := []struct {
|
|
port int
|
|
want bool
|
|
}{
|
|
{0, false},
|
|
{1, true},
|
|
{80, true},
|
|
{1080, true},
|
|
{65535, true},
|
|
{65536, false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := IsValidPort(tt.port)
|
|
if got != tt.want {
|
|
t.Errorf("IsValidPort(%d) = %v, want %v", tt.port, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestContains(t *testing.T) {
|
|
slice := []string{"a", "b", "c"}
|
|
if !Contains(slice, "a") {
|
|
t.Error("Expected Contains to return true for 'a'")
|
|
}
|
|
if Contains(slice, "d") {
|
|
t.Error("Expected Contains to return false for 'd'")
|
|
}
|
|
}
|
|
|
|
func TestUnique(t *testing.T) {
|
|
slice := []string{"a", "b", "a", "c", "b"}
|
|
result := Unique(slice)
|
|
if len(result) != 3 {
|
|
t.Errorf("Expected 3 unique items, got %d", len(result))
|
|
}
|
|
} |