You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/gateway/console/config_test.go

94 lines
4.0 KiB
Go

package console
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestLoadConfigDoesNotAuditOwnershipOrMode(t *testing.T) {
path := filepath.Join(t.TempDir(), "agent-gateway.json")
raw := `{"listen_address":"127.0.0.1:8001","panel_introspection_url":"http://127.0.0.1/api/internal/cloud/session-introspection","panel_agent_auth_url":"http://127.0.0.1/api/internal/cloud/agent-auth","panel_shared_secret":"0123456789abcdef0123456789abcdef","connect_timeout_seconds":10,"agent_request_timeout_seconds":870}`
if err := os.WriteFile(path, []byte(raw), 0o644); err != nil {
t.Fatal(err)
}
if _, err := LoadConfig(path); err != nil {
t.Fatalf("readable 0644 configuration rejected: %v", err)
}
}
func TestLoadConfigRejectsDataPastSizeLimit(t *testing.T) {
path := filepath.Join(t.TempDir(), "agent-gateway.json")
raw := []byte(`{}` + strings.Repeat(" ", maxConfigBytes))
if err := os.WriteFile(path, raw, 0o644); err != nil {
t.Fatal(err)
}
if _, err := LoadConfig(path); err == nil || !strings.Contains(err.Error(), "exceeds") {
t.Fatalf("oversized Agent gateway configuration error = %v", err)
}
}
func TestConfigRejectsRemoteIntrospectionAndUnknownFields(t *testing.T) {
config := Config{ListenAddress: RequiredListenAddress,
PanelIntrospectionURL: "https://198.51.100.2" + RequiredIntrospectionPath,
PanelAgentAuthURL: "http://127.0.0.1" + RequiredAgentAuthPath,
PanelSharedSecret: strings.Repeat("s", 32), ConnectTimeoutSeconds: 10,
AgentRequestTimeoutSeconds: 870}
if err := config.Validate(); err == nil {
t.Fatal("remote introspection endpoint accepted")
}
path := filepath.Join(t.TempDir(), "agent-gateway.json")
raw := `{"listen_address":"127.0.0.1:8001","panel_introspection_url":"http://127.0.0.1/api/internal/cloud/session-introspection","panel_agent_auth_url":"http://127.0.0.1/api/internal/cloud/agent-auth","panel_shared_secret":"0123456789abcdef0123456789abcdef","connect_timeout_seconds":10,"agent_request_timeout_seconds":870,"unexpected":true}`
if err := os.WriteFile(path, []byte(raw), 0o600); err != nil {
t.Fatal(err)
}
if _, err := LoadConfig(path); err == nil {
t.Fatal("unknown configuration field accepted")
}
}
func TestConfigRejectsExplicitEmptyPanelPorts(t *testing.T) {
config := Config{ListenAddress: RequiredListenAddress,
PanelIntrospectionURL: "http://127.0.0.1:" + RequiredIntrospectionPath,
PanelAgentAuthURL: "http://127.0.0.1" + RequiredAgentAuthPath,
PanelSharedSecret: strings.Repeat("s", 32), ConnectTimeoutSeconds: 10,
AgentRequestTimeoutSeconds: 870}
if err := config.Validate(); err == nil {
t.Fatal("explicit empty introspection port was accepted")
}
config.PanelIntrospectionURL = "http://127.0.0.1" + RequiredIntrospectionPath
config.PanelAgentAuthURL = "http://127.0.0.1:" + RequiredAgentAuthPath
if err := config.Validate(); err == nil {
t.Fatal("explicit empty Agent authentication port was accepted")
}
for _, endpoint := range []string{
"http://127.0.0.1:0" + RequiredAgentAuthPath,
"http://127.0.0.1:65536" + RequiredAgentAuthPath,
"http://127.0.0.1" + RequiredAgentAuthPath + "?",
} {
config.PanelAgentAuthURL = endpoint
if err := config.Validate(); err == nil {
t.Fatalf("invalid Agent authentication endpoint accepted: %s", endpoint)
}
}
}
func TestConfigRequiresLayeredAgentRequestTimeout(t *testing.T) {
base := Config{ListenAddress: RequiredListenAddress,
PanelIntrospectionURL: "http://127.0.0.1" + RequiredIntrospectionPath,
PanelAgentAuthURL: "http://127.0.0.1" + RequiredAgentAuthPath,
PanelSharedSecret: strings.Repeat("s", 32), ConnectTimeoutSeconds: 10}
for _, timeout := range []int{1, 840, 869, 871, 900} {
config := base
config.AgentRequestTimeoutSeconds = timeout
if err := config.Validate(); err == nil || !strings.Contains(err.Error(), "must be 870") {
t.Fatalf("non-layered Agent request timeout %d error = %v", timeout, err)
}
}
base.AgentRequestTimeoutSeconds = RequiredAgentRequestTimeoutSeconds
if err := base.Validate(); err != nil {
t.Fatalf("required Agent request timeout rejected: %v", err)
}
}