|
1
|
|
|
package main |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"net" |
|
5
|
|
|
"path/filepath" |
|
6
|
|
|
"testing" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
func TestFilterIpV4(t *testing.T) { |
|
10
|
|
|
// Create a mix of IPv4 and IPv6 addresses |
|
11
|
|
|
ips := []net.IP{ |
|
12
|
|
|
net.ParseIP("192.168.1.1"), |
|
13
|
|
|
net.ParseIP("fe80::1"), |
|
14
|
|
|
net.ParseIP("127.0.0.1"), |
|
15
|
|
|
net.ParseIP("::1"), |
|
16
|
|
|
net.ParseIP("10.0.0.1"), |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// Filter IPv4 addresses |
|
20
|
|
|
filtered := FilterIPV4(ips) |
|
21
|
|
|
|
|
22
|
|
|
// Check if the filtered list contains exactly the 3 IPv4 addresses |
|
23
|
|
|
if len(filtered) != 3 { |
|
24
|
|
|
t.Fatalf("Expected 3 IPv4 addresses, got %d", len(filtered)) |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// Check if all filtered addresses are IPv4 |
|
28
|
|
|
expectedAddrs := map[string]bool{ |
|
29
|
|
|
"192.168.1.1": true, |
|
30
|
|
|
"127.0.0.1": true, |
|
31
|
|
|
"10.0.0.1": true, |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
for _, ip := range filtered { |
|
35
|
|
|
if !expectedAddrs[ip] { |
|
36
|
|
|
t.Fatalf("Unexpected IPv4 address: %s", ip) |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
func TestFolderOfPanic1(t *testing.T) { |
|
42
|
|
|
defer func() { |
|
43
|
|
|
if r := recover(); r == nil { |
|
44
|
|
|
t.Errorf("The code did not panic") |
|
45
|
|
|
} |
|
46
|
|
|
}() |
|
47
|
|
|
url := "http://foo.bar/.." |
|
48
|
|
|
FolderOf(url) |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
func TestFolderOfPanic2(t *testing.T) { |
|
52
|
|
|
defer func() { |
|
53
|
|
|
if r := recover(); r == nil { |
|
54
|
|
|
t.Errorf("The code did not panic") |
|
55
|
|
|
} |
|
56
|
|
|
}() |
|
57
|
|
|
url := "http://foo.bar/../../../foobar" |
|
58
|
|
|
FolderOf(url) |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
func TestFolderOfNormal(t *testing.T) { |
|
62
|
|
|
url := "http://foo.bar/file" |
|
63
|
|
|
u := FolderOf(url) |
|
64
|
|
|
if filepath.Base(u) != "file" { |
|
65
|
|
|
t.Fatalf("url of return incorrect value") |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
func TestFolderWithoutParams(t *testing.T) { |
|
70
|
|
|
url := "http://foo.bar/file?param=value" |
|
71
|
|
|
u := FolderOf(url) |
|
72
|
|
|
if filepath.Base(u) != "file" { |
|
73
|
|
|
t.Fatalf("url of return incorrect value") |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
func TestTaskFromURL(t *testing.T) { |
|
78
|
|
|
testCases := []struct { |
|
79
|
|
|
url string |
|
80
|
|
|
expected string |
|
81
|
|
|
}{ |
|
82
|
|
|
{"http://example.com/path/to/file.zip", "file.zip"}, |
|
83
|
|
|
{"https://download.com/file.tar.gz?token=123", "file.tar.gz"}, |
|
84
|
|
|
{"http://domain.com/path/", "path"}, |
|
85
|
|
|
{"https://test.org/path/to/file.txt#fragment", "file.txt"}, |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
for _, tc := range testCases { |
|
89
|
|
|
result := TaskFromURL(tc.url) |
|
90
|
|
|
if result != tc.expected { |
|
91
|
|
|
t.Errorf("TaskFromURL(%s) = %s; want %s", tc.url, result, tc.expected) |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
func TestIsURL(t *testing.T) { |
|
97
|
|
|
validURLs := []string{ |
|
98
|
|
|
"http://example.com", |
|
99
|
|
|
"https://test.org/path", |
|
100
|
|
|
"ftp://files.org/file.zip", |
|
101
|
|
|
"http://localhost:8080", |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
invalidURLs := []string{ |
|
105
|
|
|
"not a url", |
|
106
|
|
|
"http:/missing-slash", |
|
107
|
|
|
"://no-scheme", |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
for _, url := range validURLs { |
|
111
|
|
|
if !IsURL(url) { |
|
112
|
|
|
t.Errorf("IsURL(%s) = false; want true", url) |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
for _, url := range invalidURLs { |
|
117
|
|
|
if IsURL(url) { |
|
118
|
|
|
t.Errorf("IsURL(%s) = true; want false", url) |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
func TestMkdirIfNotExist(t *testing.T) { |
|
124
|
|
|
// Test creating a temporary directory |
|
125
|
|
|
tempDir := filepath.Join(t.TempDir(), "test-dir") |
|
126
|
|
|
|
|
127
|
|
|
// Directory shouldn't exist yet |
|
128
|
|
|
if ExistDir(tempDir) { |
|
129
|
|
|
t.Fatalf("Directory %s should not exist yet", tempDir) |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// Create the directory |
|
133
|
|
|
err := MkdirIfNotExist(tempDir) |
|
134
|
|
|
if err != nil { |
|
135
|
|
|
t.Fatalf("MkdirIfNotExist failed: %v", err) |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// Directory should now exist |
|
139
|
|
|
if !ExistDir(tempDir) { |
|
140
|
|
|
t.Fatalf("Directory %s should exist after MkdirIfNotExist", tempDir) |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Running again on existing directory should not error |
|
144
|
|
|
err = MkdirIfNotExist(tempDir) |
|
145
|
|
|
if err != nil { |
|
146
|
|
|
t.Fatalf("MkdirIfNotExist on existing dir failed: %v", err) |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|