Files
cthulhu/internal/tentaclewire/remote_interaction_test.go

79 lines
3.5 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package tentaclewire
import (
"encoding/json"
"strings"
"testing"
)
func TestRemoteConsentInteractionValidation(t *testing.T) {
valid := Interaction{Remote: &RemoteRequest{Role: "controller", Grants: []string{"remote-present"}}, Next: "approved"}
data, _ := json.Marshal(valid)
if _, err := DecodeInteraction(data); err != nil {
t.Fatal(err)
}
for _, raw := range []string{
`{"remote":null,"next":"approved"}`,
`{"remote":{"role":"controller","grants":["remote-present"]}}`,
`{"remote":{"role":"controller","grants":["keyboard-inject"]},"next":"approved"}`,
`{"remote":{"role":"controlled","grants":[]},"next":"approved"}`,
`{"remote":{"role":"controlled","grants":["output-observe","output-observe"]},"next":"approved"}`,
`{"remote":{"role":"controlled","grants":["output-observe"],"session":"other"},"next":"approved"}`,
} {
if _, err := DecodeInteraction([]byte(raw)); err == nil {
t.Fatal("invalid remote interaction accepted", raw)
}
}
for _, field := range []string{`"clipboard":"text"`, `"source":"clipboard"`, `"done":true`, `"poll":"poll"`, `"reader":{"snapshot":true}`} {
raw := `{"remote":{"role":"controller","grants":["remote-present"]},"next":"approved",` + field + `}`
if _, err := DecodeInteraction([]byte(raw)); err == nil {
t.Fatal("remote consent mixed with another effect", field)
}
}
}
func TestRemoteBackgroundInteractionValidation(t *testing.T) {
lease := RemoteLease{Lease: strings.Repeat("a", 32), Epoch: 1}
valid := Interaction{RemoteBackground: &lease, Done: true, Presentation: Presentation{Speech: "Connection controls closed"}}
data, _ := json.Marshal(valid)
if _, err := DecodeInteraction(data); err != nil {
t.Fatal(err)
}
for _, raw := range []string{
`{"remote_background":null,"done":true}`,
`{"remote_background":{"lease":"short","epoch":1},"done":true}`,
`{"remote_background":{"lease":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","epoch":0},"done":true}`,
`{"remote_background":{"lease":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","epoch":1,"session":"other"},"done":true}`,
`{"remote_background":{"lease":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","epoch":1}}`,
} {
if _, err := DecodeInteraction([]byte(raw)); err == nil {
t.Fatal("invalid background interaction accepted", raw)
}
}
for _, field := range []string{`"form":{"title":"Menu","elements":[]}`, `"next":"submit"`, `"poll":"poll"`, `"source":"focus"`, `"clipboard":"text"`, `"reader":{"snapshot":true}`, `"remote":{"role":"controller","grants":["remote-present"]}`, `"uninterruptible":true`} {
raw := `{"remote_background":{"lease":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","epoch":1},"done":true,"speech":"Closed",` + field + `}`
if _, err := DecodeInteraction([]byte(raw)); err == nil {
t.Fatal("background interaction mixed with another effect", field)
}
}
}
func TestRemoteOptionalGrantsValidation(t *testing.T) {
for _, test := range []struct {
optional []string
valid bool
}{
{[]string{"keyboard-capture", "clipboard-exchange", "braille-stream"}, true},
{[]string{"keyboard-inject"}, false}, {[]string{"remote-present"}, false},
{[]string{"braille-stream", "braille-stream"}, false}, {[]string{"unknown"}, false},
} {
data, _ := json.Marshal(Interaction{Remote: &RemoteRequest{Role: "controller", Grants: []string{"remote-present"}, OptionalGrants: test.optional}, Next: "approve"})
_, err := DecodeInteraction(data)
if (err == nil) != test.valid {
t.Fatalf("optional grants %v: %v", test.optional, err)
}
}
}