123 lines
3.7 KiB
Go
123 lines
3.7 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>.
|
|
|
|
package clipboard
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
func TestHistoryLimitUnicodeWrappingAndClear(t *testing.T) {
|
|
history := New_history(2)
|
|
for _, text := range []string{"old", "é界\nnext", "new"} {
|
|
if err := history.Add(text); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
history.Move(-1)
|
|
if text, ok := history.Current(); !ok || text != "é界\nnext" || !history.At_last() {
|
|
t.Fatal(text, ok)
|
|
}
|
|
history.Move(1)
|
|
if text, _ := history.Current(); text != "new" || !history.At_first() {
|
|
t.Fatal(text)
|
|
}
|
|
history.Last()
|
|
history.First()
|
|
if text, _ := history.Current(); text != "new" {
|
|
t.Fatal(text)
|
|
}
|
|
for _, text := range []string{string([]byte{0xff}), strings.Repeat("x", MaxBytes+1)} {
|
|
if err := history.Add(text); err == nil {
|
|
t.Fatal("unbounded or invalid text accepted")
|
|
}
|
|
}
|
|
history.Clear()
|
|
if _, ok := history.Current(); ok {
|
|
t.Fatal("clear retained history")
|
|
}
|
|
}
|
|
|
|
func TestDisplaySelectionDoesNotFallThroughAndDiscoversHigherNumbers(t *testing.T) {
|
|
root := t.TempDir()
|
|
for _, fixture := range []struct{ configured, current, want string }{{":8", ":7", ":8"}, {"", ":7", ":7"}} {
|
|
targets, err := Display_candidates(fixture.configured, fixture.current, "/test/auth", root, "/does-not-exist")
|
|
if err != nil || len(targets) != 1 || targets[0] != (Target{fixture.want, "/test/auth"}) {
|
|
t.Fatal(targets, err)
|
|
}
|
|
}
|
|
listener, err := net.Listen("unix", filepath.Join(root, "X42"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
if err := os.WriteFile(filepath.Join(root, "X3"), nil, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
targets, err := Display_candidates("", "", "", root, root)
|
|
if err != nil || len(targets) != 1 || targets[0].Display != ":42" {
|
|
t.Fatal(targets, err)
|
|
}
|
|
other, err := net.Listen("unix", filepath.Join(root, "X43"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer other.Close()
|
|
if _, err := Display_candidates("", "", "", root, root); err == nil {
|
|
t.Fatal("silently chose among multiple desktops")
|
|
}
|
|
}
|
|
|
|
func TestFileRoundTripBoundsPrivacyAndNonregularInput(t *testing.T) {
|
|
root := t.TempDir()
|
|
path := filepath.Join(root, "clipboard")
|
|
text := "é界\nsecond line\n"
|
|
if err := write_file(path, text); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := read_file(path); err != nil || got != text {
|
|
t.Fatal(got, err)
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.Mode().Perm() != 0600 {
|
|
t.Fatal(info, err)
|
|
}
|
|
if err := write_file(path, strings.Repeat("x", MaxBytes+1)); err == nil {
|
|
t.Fatal("oversize export accepted")
|
|
}
|
|
if got, _ := read_file(path); got != text {
|
|
t.Fatal("failed export changed original")
|
|
}
|
|
if err := syscall.Mkfifo(filepath.Join(root, "fifo"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := read_file(filepath.Join(root, "fifo")); err == nil {
|
|
t.Fatal("FIFO accepted")
|
|
}
|
|
if err := os.WriteFile(path, []byte{0xff}, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := read_file(path); err == nil {
|
|
t.Fatal("invalid UTF-8 imported")
|
|
}
|
|
}
|