-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
51 lines (44 loc) · 1.22 KB
/
main_test.go
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
package main
import (
// "sync"
"testing"
)
func TestScanPort(t *testing.T) {
tests := []struct {
hostname string
port int
expected bool
}{
{"example.com", 80, true},
{"localhost", 12345, false},
{"invalid-hostname", 80, false},
}
for _, tt := range tests {
resultChan := make(chan PortStatus)
sem := make(chan struct{}, 2)
go scanPort(tt.hostname, tt.port, sem, resultChan)
got := <-resultChan
if got.Port != tt.port || got.Open != tt.expected {
t.Errorf("scanPort(%s, %d) = %v, expected %v", tt.hostname, tt.port, got, tt.expected)
}
close(resultChan)
close(sem)
}
}
func TestIsValidHostname(t *testing.T) {
tests := []struct {
hostname string
expected bool
}{
{"localhost", true},
{"127.0.0.1", true},
{"example.com", true},
{"invalid-hostname", false},
}
for _, tt := range tests {
got := isValidHostname(tt.hostname)
if got != tt.expected {
t.Errorf("isValidHostname(%s) = %t, expected %t", tt.hostname, got, tt.expected)
}
}
}