gunk-dev/the-valley

approvalsign_linux_test.go

sourceb919222bf28dmain's published tip history
2.9 KiB
1package main
2
3import (
4 "io"
5 "os"
6 "strconv"
7 "syscall"
8 "testing"
9 "time"
10 "unsafe"
11)
12
13func init() { extraFakePIV["echo-off"] = fakeEchoOff }
14
15// fakeEchoOff turns echo off on the terminal VALLEY_TEST_FAKE_PIV_TTY
16// names, as the helper does while it reads the PIN, says it is waiting,
17// and waits to be killed.
18func fakeEchoOff() int {
19 f, err := os.OpenFile(os.Getenv("VALLEY_TEST_FAKE_PIV_TTY"), os.O_RDWR|syscall.O_NOCTTY, 0)
20 if err != nil {
21 return 1
22 }
23 t, err := termios(f)
24 if err != nil {
25 return 1
26 }
27 t.Lflag &^= syscall.ECHO
28 if ioctl(f, syscall.TCSETS, unsafe.Pointer(&t)) != nil || !waitingFor(make(chan os.Signal, 1)) {
29 return 1
30 }
31 io.Copy(io.Discard, os.Stdin)
32 return 1
33}
34
35func ioctl(f *os.File, req uintptr, arg unsafe.Pointer) error {
36 if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), req, uintptr(arg)); errno != 0 {
37 return errno
38 }
39 return nil
40}
41
42func termios(f *os.File) (syscall.Termios, error) {
43 var t syscall.Termios
44 return t, ioctl(f, syscall.TCGETS, unsafe.Pointer(&t))
45}
46
47// A PIV signer's helper killed while echo is off, as it is while the
48// helper reads the PIN, leaves the terminal as it was once approve has
49// refused: approve puts back the settings it saved before the helper
50// started. The terminal is a pty of the test's own, so its settings
51// really change.
52func TestAKilledPIVSignerLeavesTheTerminalAsItWas(t *testing.T) {
53 saved, savedTTY := pivSigner, ttyPath
54 pivSigner = os.Args[0]
55 t.Cleanup(func() { pivSigner, ttyPath = saved, savedTTY })
56 master, err := os.OpenFile("/dev/ptmx", os.O_RDWR|syscall.O_NOCTTY, 0)
57 if err != nil {
58 t.Skipf("no pty here: %v", err)
59 }
60 defer master.Close()
61 var n uint32
62 var unlock int32
63 if ioctl(master, syscall.TIOCGPTN, unsafe.Pointer(&n)) != nil || ioctl(master, syscall.TIOCSPTLCK, unsafe.Pointer(&unlock)) != nil {
64 t.Fatal("the pty did not open")
65 }
66 ttyPath = "/dev/pts/" + strconv.Itoa(int(n))
67 pty, err := os.OpenFile(ttyPath, os.O_RDWR|syscall.O_NOCTTY, 0)
68 if err != nil {
69 t.Fatal(err)
70 }
71 defer pty.Close()
72 before, err := termios(pty)
73 if err != nil || before.Lflag&syscall.ECHO == 0 {
74 t.Fatalf("the pty does not echo to begin with: %v", err)
75 }
76
77 w := newSignWorld(t, nil)
78 t.Setenv("VALLEY_TEST_FAKE_PIV_TTY", ttyPath)
79 ended := make(chan int, 1)
80 pid := w.helperWaiting("echo-off", func() {
81 ended <- w.approve("--head", w.head, "--approval-piv", w.id)
82 }, ended)
83 if during, err := termios(pty); err != nil || during.Lflag&syscall.ECHO != 0 {
84 t.Fatalf("the stand-in did not turn echo off: %v", err)
85 }
86 if err := syscall.Kill(pid, syscall.SIGKILL); err != nil {
87 t.Fatal(err)
88 }
89 select {
90 case status := <-ended:
91 w.nothingPublished(status, "approval not signed")
92 case <-time.After(time.Minute):
93 t.Fatal("approve did not end once its helper was killed")
94 }
95 if after, err := termios(pty); err != nil || after != before {
96 t.Errorf("the terminal was left with other settings than it had: echo %v, %v", after.Lflag&syscall.ECHO != 0, err)
97 }
98}