Files
fenrir/internal/console/paste_linux_test.go

77 lines
2.8 KiB
Go

// Fenrir console screen reader.
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
//go:build linux && cgo
package console
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
func TestConsolePasteRejectsChangedTargetAndFailedInjection(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "sys/devices/virtual/tty/tty0"), 0700); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(root, "dev"), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "dev/tty2"), nil, 0600); err != nil {
t.Fatal(err)
}
activePath := filepath.Join(root, "sys/devices/virtual/tty/tty0/active")
source := &Source{root: root, state: func(string) (bool, bool, bool, error) { return true, false, false, nil }}
if err := os.WriteFile(activePath, []byte("tty3\n"), 0600); err != nil {
t.Fatal(err)
}
if err := source.Paste(context.Background(), "text", 2); err == nil || !strings.Contains(err.Error(), "active VT changed") {
t.Fatal(err)
}
if err := os.WriteFile(activePath, []byte("tty2\n"), 0600); err != nil {
t.Fatal(err)
}
if err := source.Paste(context.Background(), "text", 2); err == nil {
t.Fatal("failed TIOCSTI reported success")
}
source.Configure_ignored_vts([]int{2})
if err := source.Paste(context.Background(), "text", 2); err == nil || !strings.Contains(err.Error(), "active VT changed") {
t.Fatal("paste attempted injection into ignored VT", err)
}
data, err := os.ReadFile(filepath.Join(root, "dev/tty2"))
if err != nil || len(data) != 0 {
t.Fatal("paste wrote to terminal output", data, err)
}
source.Configure_ignored_vts(nil)
source.usage = func(string) (uint16, error) { return 3, nil }
source.state = func(string) (bool, bool, bool, error) {
t.Fatal("paste queried unused target")
return true, false, false, nil
}
// Missing target distinguishes a pre-open rejection from an open failure.
if err := os.Remove(filepath.Join(root, "dev/tty2")); err != nil {
t.Fatal(err)
}
if err := source.Paste(context.Background(), "text", 2); err == nil || !strings.Contains(err.Error(), "active VT changed") {
t.Fatal("paste opened unused console before checking usage", err)
}
}