Adopt common v0.8.0 and preserve canceled hardware settings
This commit is contained in:
1 parent
b21fceb311
commit
5dfa038706
410 files changed
+5353
-4803
No files matched your search
@@ -0,0 +1,199 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package tentaclewire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
ui "git.stormux.org/storm/screenreader-common/internalui"
|
||||
)
|
||||
|
||||
// Kind is a Tentacle form control. Its numeric JSON values are wire format.
|
||||
type Kind uint8
|
||||
|
||||
const (
|
||||
List Kind = iota + 1
|
||||
Entry
|
||||
CheckBox
|
||||
Button
|
||||
)
|
||||
|
||||
type FormItem struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
}
|
||||
|
||||
type Element struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Kind Kind `json:"kind"`
|
||||
Items []FormItem `json:"items,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Checked bool `json:"checked,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Required bool `json:"required,omitempty"`
|
||||
MaxLength int `json:"max_length,omitempty"`
|
||||
// Secret is supported only on transient entries, never preference pages.
|
||||
Secret bool `json:"secret,omitempty"`
|
||||
}
|
||||
|
||||
// Form is the flat form a Tentacle sends. The reader runs it as an
|
||||
// internalui flat form whose single page is FormTab.
|
||||
type Form struct {
|
||||
Title string `json:"title"`
|
||||
Elements []Element `json:"elements"`
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
Text string `json:"text,omitempty"`
|
||||
Selected string `json:"selected,omitempty"`
|
||||
Checked bool `json:"checked,omitempty"`
|
||||
}
|
||||
|
||||
// FormTab is the page ID of every converted Tentacle form; submitted
|
||||
// internalui values are keyed FormTab + "." + element ID.
|
||||
const FormTab = "form"
|
||||
|
||||
const maxFormText = 8192
|
||||
|
||||
// UnmarshalJSON keeps a malformed secret flag from silently becoming an
|
||||
// ordinary entry (encoding/json otherwise accepts null for a boolean). The
|
||||
// wire decoder separately rejects duplicate keys and bounds nesting/framing.
|
||||
func (element *Element) UnmarshalJSON(data []byte) error {
|
||||
type plain Element
|
||||
var value plain
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&value); err != nil {
|
||||
return err
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &fields); err != nil {
|
||||
return err
|
||||
}
|
||||
for name, raw := range fields {
|
||||
if strings.EqualFold(name, "secret") {
|
||||
flag := string(bytes.TrimSpace(raw))
|
||||
if name != "secret" || flag != "true" && flag != "false" {
|
||||
return errors.New("secret must be a boolean")
|
||||
}
|
||||
}
|
||||
}
|
||||
*element = Element(value)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewSession validates a Tentacle form against the external protocol limits
|
||||
// and opens it. Its text is Tentacle-owned, so it is never translated.
|
||||
func NewSession(form Form) (*ui.Session, error) {
|
||||
converted, err := uiForm(form, 256, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ui.NewSession(converted, ui.Messages{})
|
||||
}
|
||||
|
||||
// ValidatePreferences shares control validation with Tentacle forms, while
|
||||
// allowing a native, scrolling page to contain several full voice editors.
|
||||
func ValidatePreferences(form Form) error {
|
||||
for _, element := range form.Elements {
|
||||
if element.Secret {
|
||||
return errors.New("secret entries are not supported in preferences")
|
||||
}
|
||||
}
|
||||
converted, err := uiForm(form, 256, 256)
|
||||
if err == nil {
|
||||
_, err = ui.NewSession(converted, ui.Messages{})
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func uiForm(form Form, maxItems, maxElements int) (ui.Form, error) {
|
||||
if form.Title == "" || len(form.Title) > 1024 || len(form.Elements) == 0 || len(form.Elements) > maxElements {
|
||||
return ui.Form{}, fmt.Errorf("a titled form with 1 to %d controls is required", maxElements)
|
||||
}
|
||||
controls := make([]ui.Control, 0, len(form.Elements))
|
||||
ids := map[string]bool{}
|
||||
for _, element := range form.Elements {
|
||||
if element.ID == "" || len(element.ID) > 1024 || element.Label == "" || len(element.Label) > 1024 || ids[element.ID] || element.Kind < List || element.Kind > Button {
|
||||
return ui.Form{}, errors.New("controls need unique IDs, labels and supported kinds")
|
||||
}
|
||||
ids[element.ID] = true
|
||||
if element.Secret && element.Kind != Entry {
|
||||
return ui.Form{}, errors.New("only entries may be secret")
|
||||
}
|
||||
if element.MaxLength == 0 {
|
||||
element.MaxLength = maxFormText
|
||||
}
|
||||
if element.MaxLength < 1 || element.MaxLength > maxFormText || len([]rune(element.Text)) > element.MaxLength {
|
||||
return ui.Form{}, errors.New("invalid entry length")
|
||||
}
|
||||
if !utf8.ValidString(element.Text) || strings.ContainsFunc(element.Text, unicode.IsControl) {
|
||||
return ui.Form{}, errors.New("entries must contain single-line text")
|
||||
}
|
||||
if len(element.Items) > maxItems {
|
||||
return ui.Form{}, errors.New("too many list items")
|
||||
}
|
||||
control := ui.Control{ID: element.ID, Label: element.Label, Text: element.Text, Checked: element.Checked, Disabled: element.Disabled, Required: element.Required, Secret: element.Secret, MaxLength: element.MaxLength, Selected: -1, Action: element.ID}
|
||||
itemIDs := map[string]bool{}
|
||||
var choices []ui.Choice
|
||||
for index, item := range element.Items {
|
||||
if item.ID == "" || len(item.ID) > 1024 || item.Label == "" || len(item.Label) > 1024 || itemIDs[item.ID] {
|
||||
return ui.Form{}, errors.New("list items need unique IDs and labels")
|
||||
}
|
||||
itemIDs[item.ID] = true
|
||||
if control.Selected < 0 && !item.Disabled {
|
||||
control.Selected = index
|
||||
}
|
||||
choices = append(choices, ui.Choice{ID: item.ID, Label: item.Label, Literal: true, Disabled: item.Disabled})
|
||||
}
|
||||
switch element.Kind {
|
||||
case List:
|
||||
control.Kind, control.SelectAction, control.Choices = ui.List, true, choices
|
||||
if control.Selected < 0 {
|
||||
control.Disabled = true
|
||||
}
|
||||
case Entry:
|
||||
control.Kind = ui.Entry
|
||||
case CheckBox:
|
||||
control.Kind = ui.CheckBox
|
||||
case Button:
|
||||
control.Kind = ui.Button
|
||||
}
|
||||
controls = append(controls, control)
|
||||
}
|
||||
return ui.Form{Type: ui.FlatForm, Title: form.Title, Detailed: true, Tabs: []ui.Tab{{ID: FormTab, Label: form.Title, Controls: controls}}}, nil
|
||||
}
|
||||
|
||||
// SubmissionValues converts a converted form's internalui values back to the
|
||||
// Tentacle wire values, keyed by element ID. Entry values may hold secrets.
|
||||
func SubmissionValues(controls map[string]ui.Control) map[string]Value {
|
||||
if controls == nil {
|
||||
return nil
|
||||
}
|
||||
values := make(map[string]Value, len(controls))
|
||||
for key, control := range controls {
|
||||
id, found := strings.CutPrefix(key, FormTab+".")
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
value := Value{Text: control.Text, Checked: control.Checked}
|
||||
if control.Kind == ui.List {
|
||||
value.Text = ""
|
||||
if control.Selected >= 0 && control.Selected < len(control.Choices) {
|
||||
value.Selected = control.Choices[control.Selected].ID
|
||||
}
|
||||
}
|
||||
values[id] = value
|
||||
}
|
||||
return values
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package tentaclewire
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
ui "git.stormux.org/storm/screenreader-common/internalui"
|
||||
)
|
||||
|
||||
func TestFormKeepsExternalListLimit(t *testing.T) {
|
||||
items := make([]FormItem, 300)
|
||||
for index := range items {
|
||||
items[index] = FormItem{ID: strconv.Itoa(index), Label: "Example item"}
|
||||
}
|
||||
if _, err := NewSession(Form{Title: "External form", Elements: []Element{{ID: "items", Label: "Items", Kind: List, Items: items}}}); err == nil {
|
||||
t.Fatal("external form exceeded its protocol limit")
|
||||
}
|
||||
if _, err := NewSession(Form{Title: "External form", Elements: []Element{{ID: "items", Label: "Items", Kind: List, Items: items[:256]}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormListSelectionAndIndependentSessions(t *testing.T) {
|
||||
form := Form{Title: "Actions", Elements: []Element{{ID: "actions", Label: "Actions", Kind: List, Items: []FormItem{{ID: "skip", Label: "Unavailable", Disabled: true}, {ID: "click", Label: "Click"}, {ID: "press", Label: "Press"}}}}}
|
||||
first, err := NewSession(form)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, _ := NewSession(form)
|
||||
if opening := first.Open(); opening != "Actions. Click" {
|
||||
t.Fatalf("opening speech = %q", opening)
|
||||
}
|
||||
if moved := first.Handle(ui.Key{Name: "down"}).Speech; moved != "Press" {
|
||||
t.Fatalf("option speech = %q", moved)
|
||||
}
|
||||
if effect := first.Handle(ui.Key{Name: "enter"}); effect.Action != "press" {
|
||||
t.Fatalf("selection = %+v", effect)
|
||||
}
|
||||
if second.Handle(ui.Key{Name: "enter"}).Action != "click" {
|
||||
t.Fatal("sessions share state")
|
||||
}
|
||||
form.Elements[0].Items[1].Label = "Changed"
|
||||
if !strings.Contains(second.Presentation().Speech, "Click") {
|
||||
t.Fatal("caller mutation changed session")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormListLabelOnlyBesideOtherControls(t *testing.T) {
|
||||
menu, err := NewSession(Form{Title: "Wormhole Menu", Elements: []Element{{ID: "choices", Label: "Options", Kind: List, Items: []FormItem{{ID: "send", Label: "Send focused item"}, {ID: "receive", Label: "Receive file"}}}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if opening := menu.Open(); opening != "Wormhole Menu. Send focused item" {
|
||||
t.Fatalf("custom title = %q", opening)
|
||||
}
|
||||
menu.Handle(ui.Key{Name: "down"})
|
||||
if focus := menu.Handle(ui.Key{Name: "tab"}).Speech; focus != "Receive file" {
|
||||
t.Fatalf("menu repeated its label on Tab: %q", focus)
|
||||
}
|
||||
form, _ := NewSession(Form{Title: "Example", Elements: []Element{{ID: "code", Label: "Code", Kind: Entry}, {ID: "method", Label: "Method", Kind: List, Items: []FormItem{{ID: "send", Label: "Send"}}}}})
|
||||
if focus := form.Handle(ui.Key{Name: "tab"}).Speech; focus != "Method. Send" {
|
||||
t.Fatalf("form list lost its field label: %q", focus)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidForms(t *testing.T) {
|
||||
for name, form := range map[string]Form{
|
||||
"empty": {},
|
||||
"list of nothing": {Title: "Empty", Elements: []Element{{ID: "list", Label: "List", Kind: List}}},
|
||||
"duplicate IDs": {Title: "Duplicate", Elements: []Element{{ID: "same", Label: "First", Kind: Button}, {ID: "same", Label: "Second", Kind: Entry}}},
|
||||
"unknown kind": {Title: "Kind", Elements: []Element{{ID: "x", Label: "X", Kind: Button + 1}}},
|
||||
"multiline text": {Title: "Text", Elements: []Element{{ID: "x", Label: "X", Kind: Entry, Text: "a\nb"}}},
|
||||
"text over limit": {Title: "Text", Elements: []Element{{ID: "x", Label: "X", Kind: Entry, Text: "toolong", MaxLength: 3}}},
|
||||
"duplicate items": {Title: "Items", Elements: []Element{{ID: "x", Label: "X", Kind: List, Items: []FormItem{{ID: "a", Label: "A"}, {ID: "a", Label: "B"}}}}},
|
||||
"secret checkbox": {Title: "Secret", Elements: []Element{{ID: "x", Label: "X", Kind: CheckBox, Secret: true}}},
|
||||
"nothing enabled": {Title: "Disabled", Elements: []Element{{ID: "x", Label: "X", Kind: Button, Disabled: true}}},
|
||||
"too many controls": func() Form {
|
||||
form := Form{Title: "Many"}
|
||||
for index := range 65 {
|
||||
form.Elements = append(form.Elements, Element{ID: strconv.Itoa(index), Label: "Button", Kind: Button})
|
||||
}
|
||||
return form
|
||||
}(),
|
||||
} {
|
||||
if _, err := NewSession(form); err == nil {
|
||||
t.Fatalf("accepted invalid form: %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferencesRejectSecretsAndAllowLargerPages(t *testing.T) {
|
||||
secret := Form{Title: "Connection", Elements: []Element{{ID: "key", Label: "Channel key", Kind: Entry, Secret: true}}}
|
||||
if _, err := NewSession(secret); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ValidatePreferences(secret); err == nil {
|
||||
t.Fatal("secret accepted in persistent preferences")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubmissionValuesFollowWireShape(t *testing.T) {
|
||||
session, err := NewSession(Form{Title: "Receive", Elements: []Element{
|
||||
{ID: "code", Label: "Code", Kind: Entry, Required: true},
|
||||
{ID: "method", Label: "Method", Kind: List, Items: []FormItem{{ID: "send", Label: "Send"}, {ID: "receive", Label: "Receive"}}},
|
||||
{ID: "keep", Label: "Keep", Kind: CheckBox},
|
||||
{ID: "submit", Label: "Receive", Kind: Button},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if effect := session.Handle(ui.Key{Name: "tab"}); effect.Speech != "Method. Send" {
|
||||
t.Fatalf("list arrival = %q", effect.Speech)
|
||||
}
|
||||
session.Handle(ui.Key{Name: "tab", Shift: true})
|
||||
session.Insert("7-alpha")
|
||||
session.Handle(ui.Key{Name: "tab"})
|
||||
session.Handle(ui.Key{Text: "r"})
|
||||
session.Handle(ui.Key{Name: "tab"})
|
||||
session.Handle(ui.Key{Name: "space", Text: " "})
|
||||
session.Handle(ui.Key{Name: "tab"})
|
||||
if effect := session.Handle(ui.Key{Name: "enter"}); effect.Action != "submit" {
|
||||
t.Fatalf("submit = %+v", effect)
|
||||
}
|
||||
got := SubmissionValues(session.Values())
|
||||
want := map[string]Value{"code": {Text: "7-alpha"}, "method": {Selected: "receive"}, "keep": {Checked: true}, "submit": {}}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("values = %+v", got)
|
||||
}
|
||||
for id, value := range want {
|
||||
if got[id] != value {
|
||||
t.Fatalf("%s = %+v, want %+v", id, got[id], value)
|
||||
}
|
||||
}
|
||||
if SubmissionValues(nil) != nil {
|
||||
t.Fatal("missing controls became an empty submission")
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,6 @@ import (
|
||||
"slices"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/interaction"
|
||||
)
|
||||
|
||||
// Interaction is an opt-in command result. The host runs forms outside the
|
||||
@@ -20,14 +18,14 @@ import (
|
||||
// reader-owned consent, or finish with a verified background lease.
|
||||
type Interaction struct {
|
||||
Presentation
|
||||
Form *interaction.Form `json:"form,omitempty"`
|
||||
Next string `json:"next,omitempty"`
|
||||
Poll string `json:"poll,omitempty"`
|
||||
Source string `json:"source,omitempty"` // "focus" or "clipboard", supplied to Next.
|
||||
Clipboard *string `json:"clipboard,omitempty"`
|
||||
Done bool `json:"done,omitempty"`
|
||||
Reader *ReaderRequest `json:"reader,omitempty"`
|
||||
Remote *RemoteRequest `json:"remote,omitempty"`
|
||||
Form *Form `json:"form,omitempty"`
|
||||
Next string `json:"next,omitempty"`
|
||||
Poll string `json:"poll,omitempty"`
|
||||
Source string `json:"source,omitempty"` // "focus" or "clipboard", supplied to Next.
|
||||
Clipboard *string `json:"clipboard,omitempty"`
|
||||
Done bool `json:"done,omitempty"`
|
||||
Reader *ReaderRequest `json:"reader,omitempty"`
|
||||
Remote *RemoteRequest `json:"remote,omitempty"`
|
||||
// RemoteBackground ends this interaction while retaining an already-open
|
||||
// lease owned by this process session. It grants no new access or lifetime.
|
||||
RemoteBackground *RemoteLease `json:"remote_background,omitempty"`
|
||||
@@ -42,13 +40,13 @@ type RemoteRequest struct {
|
||||
}
|
||||
|
||||
type Submission struct {
|
||||
Activated string `json:"activated,omitempty"`
|
||||
Values map[string]interaction.Value `json:"values,omitempty"`
|
||||
Cancelled bool `json:"cancelled,omitempty"`
|
||||
Item *Item `json:"item,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Reader *ReaderState `json:"reader,omitempty"`
|
||||
Remote *SessionEvent `json:"remote,omitempty"`
|
||||
Activated string `json:"activated,omitempty"`
|
||||
Values map[string]Value `json:"values,omitempty"`
|
||||
Cancelled bool `json:"cancelled,omitempty"`
|
||||
Item *Item `json:"item,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Reader *ReaderState `json:"reader,omitempty"`
|
||||
Remote *SessionEvent `json:"remote,omitempty"`
|
||||
}
|
||||
|
||||
// Item is a single copied value, without accessible identities or native handles.
|
||||
@@ -112,7 +110,7 @@ func DecodeInteraction(data []byte) (Interaction, error) {
|
||||
if !validFormText(value.Form.Title) {
|
||||
return value, ErrProtocol
|
||||
}
|
||||
if _, err := interaction.NewSession(*value.Form); err != nil {
|
||||
if _, err := NewSession(*value.Form); err != nil {
|
||||
return value, ErrProtocol
|
||||
}
|
||||
if value.Next == "" || value.Done || value.Source != "" {
|
||||
|
||||
@@ -2,13 +2,12 @@ package tentaclewire
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.stormux.org/storm/cthulhu/internal/interaction"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInteractionContract(t *testing.T) {
|
||||
form := &interaction.Form{Title: "Wormhole menu", Elements: []interaction.Element{{ID: "choices", Label: "Options", Kind: interaction.List, Items: []interaction.Item{{ID: "send", Label: "Send focused item"}}}}}
|
||||
form := &Form{Title: "Wormhole menu", Elements: []Element{{ID: "choices", Label: "Options", Kind: List, Items: []FormItem{{ID: "send", Label: "Send focused item"}}}}}
|
||||
for _, value := range []Interaction{
|
||||
{Form: form, Next: "choice"}, {Form: form, Next: "choice", Poll: "status"}, {Source: "focus", Next: "send"}, {Source: "clipboard", Next: "send"},
|
||||
{Presentation: Presentation{Speech: "Ready"}, Done: true}, {Done: true}, {},
|
||||
@@ -70,8 +69,8 @@ func TestSecretInteractionContract(t *testing.T) {
|
||||
}
|
||||
}
|
||||
page := PreferencePage{ID: "connection", Title: "Connection", Command: "connect",
|
||||
Elements: []interaction.Element{{ID: "key", Label: "Channel key", Kind: interaction.Entry, Secret: true}},
|
||||
Values: map[string]interaction.Value{"key": {Text: "fixture"}}}
|
||||
Elements: []Element{{ID: "key", Label: "Channel key", Kind: Entry, Secret: true}},
|
||||
Values: map[string]Value{"key": {Text: "fixture"}}}
|
||||
if ValidateExtensions(Extensions{Pages: []PreferencePage{page}}) == nil || ValidatePageValues(page, page.Values) == nil {
|
||||
t.Fatal("secret permitted in persistent page values")
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/config"
|
||||
"git.stormux.org/storm/cthulhu/internal/interaction"
|
||||
"git.stormux.org/storm/cthulhu/internal/speech"
|
||||
)
|
||||
|
||||
@@ -70,20 +69,20 @@ func (value SpeechConfiguration) Apply(settings config.Settings) config.Settings
|
||||
}
|
||||
|
||||
type PreferencePage struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Command string `json:"command"`
|
||||
Load bool `json:"load,omitempty"`
|
||||
Elements []interaction.Element `json:"elements"`
|
||||
Values map[string]interaction.Value `json:"values"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Command string `json:"command"`
|
||||
Load bool `json:"load,omitempty"`
|
||||
Elements []Element `json:"elements"`
|
||||
Values map[string]Value `json:"values"`
|
||||
}
|
||||
|
||||
type QuickSetting struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Command string `json:"command"`
|
||||
Options []interaction.Item `json:"options"`
|
||||
Selected string `json:"selected"`
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Command string `json:"command"`
|
||||
Options []FormItem `json:"options"`
|
||||
Selected string `json:"selected"`
|
||||
}
|
||||
|
||||
// Extensions are copied at registration/update. IDs are local to one session.
|
||||
@@ -115,14 +114,14 @@ type ReaderState struct {
|
||||
// ExtensionEvent handlers propose effects only. The host commits page values
|
||||
// and quick selections after validating all proposals and applying core settings.
|
||||
type ExtensionEvent struct {
|
||||
Event string `json:"event"`
|
||||
ID string `json:"id"`
|
||||
Values map[string]interaction.Value `json:"values,omitempty"`
|
||||
Selected string `json:"selected,omitempty"`
|
||||
Reader ReaderState `json:"reader"`
|
||||
Event string `json:"event"`
|
||||
ID string `json:"id"`
|
||||
Values map[string]Value `json:"values,omitempty"`
|
||||
Selected string `json:"selected,omitempty"`
|
||||
Reader ReaderState `json:"reader"`
|
||||
}
|
||||
|
||||
func valid_label(value string) bool {
|
||||
func validLabel(value string) bool {
|
||||
return strings.TrimSpace(value) != "" && len(value) <= 256 && validFormText(value)
|
||||
}
|
||||
|
||||
@@ -144,19 +143,19 @@ func ValidateExtensions(value Extensions) error {
|
||||
}
|
||||
ids := map[string]bool{}
|
||||
for _, page := range value.Pages {
|
||||
if !ValidExtensionID(page.ID) || ids[page.ID] || !valid_label(page.Title) || !ValidExtensionID(page.Command) {
|
||||
if !ValidExtensionID(page.ID) || ids[page.ID] || !validLabel(page.Title) || !ValidExtensionID(page.Command) {
|
||||
return ErrProtocol
|
||||
}
|
||||
ids[page.ID] = true
|
||||
if err := interaction.ValidatePreferences(interaction.Form{Title: page.Title, Elements: page.Elements}); err != nil {
|
||||
if err := ValidatePreferences(Form{Title: page.Title, Elements: page.Elements}); err != nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
for _, element := range page.Elements {
|
||||
if !ValidExtensionID(element.ID) || !valid_label(element.Label) || element.Kind == interaction.Button {
|
||||
if !ValidExtensionID(element.ID) || !validLabel(element.Label) || element.Kind == Button {
|
||||
return ErrProtocol
|
||||
}
|
||||
for _, item := range element.Items {
|
||||
if !ValidExtensionID(item.ID) || !valid_label(item.Label) {
|
||||
if !ValidExtensionID(item.ID) || !validLabel(item.Label) {
|
||||
return ErrProtocol
|
||||
}
|
||||
}
|
||||
@@ -167,14 +166,14 @@ func ValidateExtensions(value Extensions) error {
|
||||
}
|
||||
clear(ids)
|
||||
for _, setting := range value.QuickSettings {
|
||||
if !ValidExtensionID(setting.ID) || ids[setting.ID] || !valid_label(setting.Label) || !ValidExtensionID(setting.Command) || len(setting.Options) == 0 || len(setting.Options) > 64 {
|
||||
if !ValidExtensionID(setting.ID) || ids[setting.ID] || !validLabel(setting.Label) || !ValidExtensionID(setting.Command) || len(setting.Options) == 0 || len(setting.Options) > 64 {
|
||||
return ErrProtocol
|
||||
}
|
||||
ids[setting.ID] = true
|
||||
options := map[string]bool{}
|
||||
selected := false
|
||||
for _, option := range setting.Options {
|
||||
if !ValidExtensionID(option.ID) || options[option.ID] || !valid_label(option.Label) {
|
||||
if !ValidExtensionID(option.ID) || options[option.ID] || !validLabel(option.Label) {
|
||||
return ErrProtocol
|
||||
}
|
||||
options[option.ID] = true
|
||||
@@ -191,7 +190,7 @@ func ValidateExtensions(value Extensions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidatePageValues(page PreferencePage, values map[string]interaction.Value) error {
|
||||
func ValidatePageValues(page PreferencePage, values map[string]Value) error {
|
||||
if len(values) != len(page.Elements) {
|
||||
return errors.New("page values do not match its controls")
|
||||
}
|
||||
@@ -204,7 +203,7 @@ func ValidatePageValues(page PreferencePage, values map[string]interaction.Value
|
||||
return ErrProtocol
|
||||
}
|
||||
switch element.Kind {
|
||||
case interaction.Entry:
|
||||
case Entry:
|
||||
limit := element.MaxLength
|
||||
if limit == 0 {
|
||||
limit = 8192
|
||||
@@ -212,11 +211,11 @@ func ValidatePageValues(page PreferencePage, values map[string]interaction.Value
|
||||
if !validFormText(value.Text) || len([]rune(value.Text)) > limit || value.Selected != "" || value.Checked || element.Required && strings.TrimSpace(value.Text) == "" {
|
||||
return ErrProtocol
|
||||
}
|
||||
case interaction.CheckBox:
|
||||
case CheckBox:
|
||||
if value.Text != "" || value.Selected != "" {
|
||||
return ErrProtocol
|
||||
}
|
||||
case interaction.List:
|
||||
case List:
|
||||
valid := false
|
||||
for _, item := range element.Items {
|
||||
valid = valid || item.ID == value.Selected && !item.Disabled
|
||||
|
||||
@@ -6,36 +6,35 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.stormux.org/storm/cthulhu/internal/config"
|
||||
"git.stormux.org/storm/cthulhu/internal/interaction"
|
||||
"math"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func reader_test_extensions() Extensions {
|
||||
func readerTestExtensions() Extensions {
|
||||
return Extensions{
|
||||
Pages: []PreferencePage{{ID: "options", Title: "Reader probe", Command: "apply", Elements: []interaction.Element{
|
||||
{ID: "text", Label: "Text", Kind: interaction.Entry, Required: true, MaxLength: 32},
|
||||
{ID: "enabled", Label: "Enabled", Kind: interaction.CheckBox},
|
||||
{ID: "choice", Label: "Choice", Kind: interaction.List, Items: []interaction.Item{{ID: "first", Label: "First"}, {ID: "second", Label: "Second", Disabled: true}}},
|
||||
}, Values: map[string]interaction.Value{"text": {Text: "Initial"}, "enabled": {}, "choice": {Selected: "first"}}}},
|
||||
QuickSettings: []QuickSetting{{ID: "choice", Label: "Probe choice", Command: "apply", Options: []interaction.Item{{ID: "default", Label: "Default"}, {ID: "custom", Label: "Custom"}}, Selected: "default"}},
|
||||
Pages: []PreferencePage{{ID: "options", Title: "Reader probe", Command: "apply", Elements: []Element{
|
||||
{ID: "text", Label: "Text", Kind: Entry, Required: true, MaxLength: 32},
|
||||
{ID: "enabled", Label: "Enabled", Kind: CheckBox},
|
||||
{ID: "choice", Label: "Choice", Kind: List, Items: []FormItem{{ID: "first", Label: "First"}, {ID: "second", Label: "Second", Disabled: true}}},
|
||||
}, Values: map[string]Value{"text": {Text: "Initial"}, "enabled": {}, "choice": {Selected: "first"}}}},
|
||||
QuickSettings: []QuickSetting{{ID: "choice", Label: "Probe choice", Command: "apply", Options: []FormItem{{ID: "default", Label: "Default"}, {ID: "custom", Label: "Custom"}}, Selected: "default"}},
|
||||
}
|
||||
}
|
||||
|
||||
func TestReaderPageCanDescribeSixCompleteVoiceConfigurations(t *testing.T) {
|
||||
page := PreferencePage{ID: "voices", Title: "Voice configurations", Command: "apply", Values: map[string]interaction.Value{}}
|
||||
page := PreferencePage{ID: "voices", Title: "Voice configurations", Command: "apply", Values: map[string]Value{}}
|
||||
for profile := 1; profile <= 6; profile++ {
|
||||
for field := 1; field <= 24; field++ {
|
||||
id := fmt.Sprintf("profile%d.field%d", profile, field)
|
||||
page.Elements = append(page.Elements, interaction.Element{ID: id, Label: fmt.Sprintf("Profile %d setting %d", profile, field), Kind: interaction.Entry, MaxLength: 64})
|
||||
page.Values[id] = interaction.Value{Text: "Example"}
|
||||
page.Elements = append(page.Elements, Element{ID: id, Label: fmt.Sprintf("Profile %d setting %d", profile, field), Kind: Entry, MaxLength: 64})
|
||||
page.Values[id] = Value{Text: "Example"}
|
||||
}
|
||||
}
|
||||
if err := ValidateExtensions(Extensions{Pages: []PreferencePage{page}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := interaction.NewSession(interaction.Form{Title: page.Title, Elements: page.Elements}); err == nil {
|
||||
if _, err := NewSession(Form{Title: page.Title, Elements: page.Elements}); err == nil {
|
||||
t.Fatal("native page limit leaked into internal forms")
|
||||
}
|
||||
state := ReaderState{Extensions: Extensions{Pages: []PreferencePage{page}}, Speech: SpeechFromSettings(config.DefaultSettings()), DefaultSpeech: SpeechFromSettings(config.DefaultSettings())}
|
||||
@@ -49,27 +48,27 @@ func TestReaderExtensionsRejectInvalidSchemasAndValues(t *testing.T) {
|
||||
"duplicate page": func(v *Extensions) { v.Pages = append(v.Pages, v.Pages[0]) },
|
||||
"duplicate setting": func(v *Extensions) { v.QuickSettings = append(v.QuickSettings, v.QuickSettings[0]) },
|
||||
"duplicate control": func(v *Extensions) { v.Pages[0].Elements = append(v.Pages[0].Elements, v.Pages[0].Elements[0]) },
|
||||
"button": func(v *Extensions) { v.Pages[0].Elements[0].Kind = interaction.Button },
|
||||
"button": func(v *Extensions) { v.Pages[0].Elements[0].Kind = Button },
|
||||
"bad label": func(v *Extensions) { v.Pages[0].Title = "Probe\npage" },
|
||||
"bad identifier": func(v *Extensions) { v.Pages[0].ID = "../page" },
|
||||
"missing value": func(v *Extensions) { delete(v.Pages[0].Values, "text") },
|
||||
"extra value": func(v *Extensions) { v.Pages[0].Values["other"] = interaction.Value{} },
|
||||
"wrong value type": func(v *Extensions) { v.Pages[0].Values["enabled"] = interaction.Value{Text: "yes"} },
|
||||
"required": func(v *Extensions) { v.Pages[0].Values["text"] = interaction.Value{} },
|
||||
"extra value": func(v *Extensions) { v.Pages[0].Values["other"] = Value{} },
|
||||
"wrong value type": func(v *Extensions) { v.Pages[0].Values["enabled"] = Value{Text: "yes"} },
|
||||
"required": func(v *Extensions) { v.Pages[0].Values["text"] = Value{} },
|
||||
"oversized": func(v *Extensions) {
|
||||
v.Pages[0].Values["text"] = interaction.Value{Text: "This string is longer than thirty-two characters"}
|
||||
v.Pages[0].Values["text"] = Value{Text: "This string is longer than thirty-two characters"}
|
||||
},
|
||||
"control character": func(v *Extensions) { v.Pages[0].Values["text"] = interaction.Value{Text: "bad\x1bvalue"} },
|
||||
"disabled choice": func(v *Extensions) { v.Pages[0].Values["choice"] = interaction.Value{Selected: "second"} },
|
||||
"control character": func(v *Extensions) { v.Pages[0].Values["text"] = Value{Text: "bad\x1bvalue"} },
|
||||
"disabled choice": func(v *Extensions) { v.Pages[0].Values["choice"] = Value{Selected: "second"} },
|
||||
"unknown quick choice": func(v *Extensions) { v.QuickSettings[0].Selected = "missing" },
|
||||
"disabled quick choice": func(v *Extensions) { v.QuickSettings[0].Options[0].Disabled = true },
|
||||
}
|
||||
if err := ValidateExtensions(reader_test_extensions()); err != nil {
|
||||
if err := ValidateExtensions(readerTestExtensions()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, mutate := range mutations {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
value := reader_test_extensions()
|
||||
value := readerTestExtensions()
|
||||
mutate(&value)
|
||||
if ValidateExtensions(value) == nil {
|
||||
t.Fatal("accepted invalid extension")
|
||||
@@ -126,7 +125,7 @@ func TestReaderInteractionValidation(t *testing.T) {
|
||||
t.Fatal("accepted", raw)
|
||||
}
|
||||
}
|
||||
value := reader_test_extensions()
|
||||
value := readerTestExtensions()
|
||||
data, _ := json.Marshal(Interaction{Reader: &ReaderRequest{Extensions: &value}})
|
||||
if _, err := DecodeInteraction(data); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -49,14 +49,14 @@ func ValidRemotePresentation(request RemotePresentation) bool {
|
||||
func (request *RemotePresentation) UnmarshalJSON(data []byte) error {
|
||||
type plain RemotePresentation
|
||||
var value plain
|
||||
if reject_speech_nulls(data) != nil || DecodeStrict(data, &value) != nil || !ValidRemotePresentation(RemotePresentation(value)) {
|
||||
if rejectSpeechNulls(data) != nil || DecodeStrict(data, &value) != nil || !ValidRemotePresentation(RemotePresentation(value)) {
|
||||
return ErrProtocol
|
||||
}
|
||||
*request = RemotePresentation(value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func reject_speech_nulls(data []byte) error {
|
||||
func rejectSpeechNulls(data []byte) error {
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(data, &fields) != nil || fields == nil {
|
||||
return ErrProtocol
|
||||
@@ -72,7 +72,7 @@ func reject_speech_nulls(data []byte) error {
|
||||
func (part *RemoteSpeechPart) UnmarshalJSON(data []byte) error {
|
||||
type plain RemoteSpeechPart
|
||||
var value plain
|
||||
if reject_speech_nulls(data) != nil || DecodeStrict(data, &value) != nil {
|
||||
if rejectSpeechNulls(data) != nil || DecodeStrict(data, &value) != nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
*part = RemoteSpeechPart(value)
|
||||
@@ -82,7 +82,7 @@ func (part *RemoteSpeechPart) UnmarshalJSON(data []byte) error {
|
||||
func (speech *RemoteSpeech) UnmarshalJSON(data []byte) error {
|
||||
type plain RemoteSpeech
|
||||
var value plain
|
||||
if reject_speech_nulls(data) != nil || DecodeStrict(data, &value) != nil || !ValidRemoteSpeech(RemoteSpeech(value)) {
|
||||
if rejectSpeechNulls(data) != nil || DecodeStrict(data, &value) != nil || !ValidRemoteSpeech(RemoteSpeech(value)) {
|
||||
return ErrProtocol
|
||||
}
|
||||
*speech = RemoteSpeech(value)
|
||||
|
||||
@@ -48,7 +48,7 @@ func ValidateJSON(data []byte) error {
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
if err := validate_value(decoder, 0); err != nil {
|
||||
if err := validateValue(decoder, 0); err != nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
if _, err := decoder.Token(); err != io.EOF {
|
||||
@@ -57,7 +57,7 @@ func ValidateJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validate_value(decoder *json.Decoder, depth int) error {
|
||||
func validateValue(decoder *json.Decoder, depth int) error {
|
||||
if depth > 32 {
|
||||
return ErrLimit
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func validate_value(decoder *json.Decoder, depth int) error {
|
||||
}
|
||||
keys[key] = true
|
||||
}
|
||||
if err := validate_value(decoder, depth+1); err != nil {
|
||||
if err := validateValue(decoder, depth+1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user