package pluginproto import ( "encoding/json" "errors" "sort" "strings" "testing" ) func TestEveryMethodIsFullyDescribed(t *testing.T) { for method, spec := range HostMethods { if !strings.HasPrefix(method, "host/") { t.Errorf("%s is not a host/* method", method) } if spec.Status != StatusStable && spec.Status != StatusExperimental { t.Errorf("%s has status %q", method, spec.Status) } if strings.TrimSpace(spec.Summary) == "" { t.Errorf("%s has no summary", method) } if Feature(method) == "" || !strings.HasPrefix(Feature(method), "host.") { t.Errorf("%s advertises feature %q", method, Feature(method)) } for _, perm := range spec.Permissions { if strings.TrimSpace(perm) == "" { t.Errorf("%s lists an empty permission", method) } } } } func TestFeaturesAreSortedUniqueAndStableOnly(t *testing.T) { features := Features() if !sort.StringsAreSorted(features) { t.Fatalf("features are not sorted: %v", features) } seen := map[string]bool{} for _, feature := range features { if seen[feature] { t.Fatalf("feature %q is advertised twice", feature) } seen[feature] = true } stable := map[string]bool{} for method, spec := range HostMethods { if spec.Status == StatusStable { stable[Feature(method)] = true } } if len(stable) != len(features) { t.Fatalf("features = %v, want one per stable method (%d)", features, len(stable)) } for feature := range stable { if !seen[feature] { t.Fatalf("stable feature %q is not advertised", feature) } } } func TestConsoleMethodsNeedTheWritePermission(t *testing.T) { for _, method := range []string{ "host/console.screenshot", "host/console.pointer", "host/console.key", "host/console.type", "host/console.typeSecret", "host/console.release", } { spec, ok := Lookup(method) if !ok { t.Fatalf("%s is missing from the catalog", method) } want := "vm:screen" if method != "host/console.screenshot" { want = "qmp:write" } if len(spec.Permissions) != 1 || spec.Permissions[0] != want { t.Errorf("%s requires %v, want %s", method, spec.Permissions, want) } if !spec.StrictParams { t.Errorf("%s does not decode its params strictly", method) } } } func TestVMAndConsoleMethodsDecodeStrictly(t *testing.T) { // The driver-side methods the supervisor serves keep tolerant decoding; the // VM and console methods the app serves are strict. strict := []string{ "host/vm.list", "host/vm.get", "host/vm.start", "host/vm.shutdown", "host/vm.suspend", "host/vm.resume", "host/vm.clone", "host/vm.delete", "host/vm.snapshot.list", "host/vm.snapshot.create", "host/vm.snapshot.restore", "host/console.screenshot", "host/console.pointer", "host/console.key", "host/console.type", "host/console.typeSecret", "host/console.release", } for _, method := range strict { spec, ok := Lookup(method) if !ok { t.Errorf("%s is missing from the catalog", method) continue } if !spec.StrictParams { t.Errorf("%s does not decode its params strictly", method) } } } func TestDecodeParamsTolerantKeepsUnknownFields(t *testing.T) { out, err := DecodeParams[ScreenshotParams]("host/console.screenshot", json.RawMessage(`{"vm":"a","nope":1}`), false) if err != nil { t.Fatalf("tolerant decode failed: %v", err) } if out.VM != "a" { t.Fatalf("vm = %q", out.VM) } } func TestDecodeParamsStrictRejectsUnknownFields(t *testing.T) { _, err := DecodeParams[ScreenshotParams]("host/console.screenshot", json.RawMessage(`{"vm":"a","nope":1}`), true) var invalid *InvalidParamsError if !errors.As(err, &invalid) { t.Fatalf("err = %v, want InvalidParamsError", err) } if invalid.Method != "host/console.screenshot" { t.Fatalf("method = %q", invalid.Method) } } func TestDecodeParamsStrictRejectsTrailingData(t *testing.T) { _, err := DecodeParams[KeyParams]("host/console.key", json.RawMessage(`{"key":"a"}{"key":"b"}`), true) var invalid *InvalidParamsError if !errors.As(err, &invalid) { t.Fatalf("err = %v, want InvalidParamsError", err) } } func TestDecodeParamsAcceptsMissingAndNullParams(t *testing.T) { for _, raw := range []json.RawMessage{nil, json.RawMessage("null"), json.RawMessage(" ")} { out, err := DecodeParams[VMParams]("host/vm.list", raw, true) if err != nil { t.Fatalf("decode %q failed: %v", raw, err) } if out != (VMParams{}) { t.Fatalf("decode %q = %+v, want the zero value", raw, out) } } } func TestDecodeParamsReportsATypeErrorAsInvalidParams(t *testing.T) { _, err := DecodeParams[ScreenshotParams]("host/console.screenshot", json.RawMessage(`{"max_width":"wide"}`), false) var invalid *InvalidParamsError if !errors.As(err, &invalid) { t.Fatalf("err = %v, want InvalidParamsError", err) } } func TestMethodsAreSorted(t *testing.T) { methods := Methods() if !sort.StringsAreSorted(methods) { t.Fatalf("methods are not sorted: %v", methods) } if len(methods) != len(HostMethods) { t.Fatalf("methods = %d, catalog = %d", len(methods), len(HostMethods)) } }