74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package webdevice
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestIsPrivateIPv4Literal(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := map[string]bool{
|
|
"192.168.1.10": true,
|
|
"10.0.0.8": true,
|
|
"172.16.5.2": true,
|
|
"127.0.0.1": false,
|
|
"8.8.8.8": false,
|
|
"0.0.0.0": false,
|
|
"::1": false,
|
|
"bad-ip": false,
|
|
}
|
|
|
|
for input, want := range cases {
|
|
if got := IsPrivateIPv4Literal(input); got != want {
|
|
t.Fatalf("IsPrivateIPv4Literal(%q) = %v, want %v", input, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWebDeviceForwardPort(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
port, ok := WebDeviceForwardPort("192.168.1.124")
|
|
if !ok {
|
|
t.Fatal("WebDeviceForwardPort() ok = false")
|
|
}
|
|
if port != 31124 {
|
|
t.Fatalf("port = %d, want 31124", port)
|
|
}
|
|
}
|
|
|
|
func TestScanBuildsDirectURLAndAllowList(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
svc := NewService()
|
|
svc.interfaceGetter = func() ([]InterfaceInfo, error) {
|
|
return []InterfaceInfo{{
|
|
Name: "eth0",
|
|
IP: "10.8.0.14",
|
|
Netmask: "255.255.255.0",
|
|
}}, nil
|
|
}
|
|
svc.tcpScanner = func(ip, netmask string, port int, excludeIPs map[string]bool) ([]TCPDevice, error) {
|
|
return []TCPDevice{{IP: "192.168.1.124", Port: 80}}, nil
|
|
}
|
|
svc.newForwarder = func(ip string, port int, listenAddress, targetAddress string) (*webDeviceForwarder, error) {
|
|
return &webDeviceForwarder{ip: ip, port: port, targetAddress: targetAddress}, nil
|
|
}
|
|
|
|
req := httptest.NewRequest("GET", "http://10.8.0.14:13000/api/web-devices/scan", nil)
|
|
result, err := svc.Scan(req)
|
|
if err != nil {
|
|
t.Fatalf("Scan() error = %v", err)
|
|
}
|
|
if result.Count != 1 {
|
|
t.Fatalf("result.Count = %d", result.Count)
|
|
}
|
|
if !svc.IsAllowed("192.168.1.124") {
|
|
t.Fatal("expected IP to be allowed after scan")
|
|
}
|
|
if result.Devices[0].DirectURL != "http://10.8.0.14:31124/" {
|
|
t.Fatalf("DirectURL = %q", result.Devices[0].DirectURL)
|
|
}
|
|
}
|