package plugin import ( "net/url" "testing" ) func TestNetPermissionMatchesOriginsAndHosts(t *testing.T) { parse := func(raw string) *url.URL { u, err := url.Parse(raw) if err != nil { t.Fatal(err) } return u } cases := []struct { permission, target string want bool }{ {"net:https://cdn.example.com", "https://cdn.example.com/a/b.iso", true}, {"net:https://cdn.example.com", "https://CDN.example.com:443/a", true}, {"net:https://cdn.example.com", "http://cdn.example.com/a", false}, {"net:https://cdn.example.com", "https://cdn.example.com:8443/a", false}, {"net:https://cdn.example.com", "https://evil.example.com/a", false}, {"net:https://cdn.example.com", "https://cdn.example.com.evil.net/a", false}, {"net:cdn.example.com", "https://cdn.example.com/a", true}, {"net:cdn.example.com", "https://sub.cdn.example.com/a", false}, {"net:cdn.example.com:8443", "https://cdn.example.com:8443/a", true}, {"net:cdn.example.com:8443", "https://cdn.example.com/a", false}, {"net:http://127.0.0.1:9000", "http://127.0.0.1:9000/f", true}, {"net:127.0.0.1", "http://127.0.0.1:9000/f", true}, {"net:", "https://cdn.example.com/a", false}, {"vm:read", "https://cdn.example.com/a", false}, {"net:https://cdn.example.com/path", "https://cdn.example.com/a", false}, } for _, tc := range cases { if got := netPermissionMatches(tc.permission, parse(tc.target)); got != tc.want { t.Errorf("netPermissionMatches(%q, %q) = %v, want %v", tc.permission, tc.target, got, tc.want) } } } func TestIsLoopbackHost(t *testing.T) { for host, want := range map[string]bool{ "localhost": true, "LOCALHOST": true, "127.0.0.1": true, "127.5.5.5": true, "::1": true, "example.com": false, "10.0.0.1": false, "": false, } { if got := IsLoopbackHost(host); got != want { t.Errorf("IsLoopbackHost(%q) = %v, want %v", host, got, want) } } }