Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_sys/tests/pty.rs

3.5 KiB, 7 runs

created by r1870400018:79791, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Tests for `fe2o3_sys::pty` against real children, which report what the kernel tells them of
2//! their terminal through `stty` and `tty`, programs the module does not run.
3
4#![cfg(all(target_os = "linux", feature = "pty"))]
5
6use oxedyne_fe2o3_core::prelude::*;
7use oxedyne_fe2o3_sys::pty::{
8 self,
9 Pty,
10 Raw,
11};
12
13use std::{
14 fs::File,
15 io::{
16 ErrorKind,
17 Read,
18 },
19 process::{
20 Child,
21 Command,
22 },
23 time::{
24 Duration,
25 Instant,
26 },
27};
28
29const WAIT: Duration = Duration::from_secs(10); // fixed: the most a child's words are waited for
30
31fn spawn(pty: &Pty, script: &str) -> Outcome<Child> {
32 let [i, o, e] = res!(pty.stdio());
33 Ok(res!(Command::new("sh").arg("-c").arg(script).stdin(i).stdout(o).stderr(e).spawn()))
34}
35
36// What the master has to read until it holds `want`, within WAIT.
37fn read_until(master: &mut File, want: &str) -> Outcome<String> {
38 let (end, mut got, mut buf) = (Instant::now() + WAIT, Vec::new(), [0u8; 4096]);
39 while Instant::now() < end {
40 match master.read(&mut buf) {
41 Ok(0) => break,
42 Ok(n) => got.extend_from_slice(&buf[..n]),
43 Err(e) if e.kind() == ErrorKind::WouldBlock => std::thread::sleep(Duration::from_millis(20)),
44 Err(_) => break, // EIO: the last slave closed
45 }
46 if String::from_utf8_lossy(&got).contains(want) {
47 break;
48 }
49 }
50 Ok(String::from_utf8_lossy(&got).into_owned())
51}
52
53#[test]
54fn a_child_on_the_slave_has_a_terminal_of_the_size_given() -> Outcome<()> {
55 let p = res!(Pty::open(80, 24));
56 let mut kid = res!(spawn(&p, "stty size; tty"));
57 let mut m = File::from(p.into_master());
58 let out = res!(read_until(&mut m, "/dev/pts/"));
59 let _ = kid.wait();
60 assert!(out.contains("24 80"), "the size given: {:?}", out);
61 assert!(out.contains("/dev/pts/"), "a terminal: {:?}", out);
62 Ok(())
63}
64
65#[test]
66fn a_new_size_reaches_the_child_once_it_is_told() -> Outcome<()> {
67 let p = res!(Pty::open(80, 24));
68 let mut kid = res!(spawn(&p, "trap 'stty size; exit' WINCH; echo ready; while :; do sleep 0.05; done"));
69 let master = p.into_master();
70 res!(pty::nonblocking(&master));
71 let mut m = File::from(master);
72 assert!(res!(read_until(&mut m, "ready")).contains("ready"));
73 res!(pty::set_size(&m, 100, 30));
74 assert_eq!(res!(pty::size(&m)), (100, 30));
75 res!(pty::resized(kid.id()));
76 let out = res!(read_until(&mut m, "30 100"));
77 let _ = kid.kill();
78 let _ = kid.wait();
79 assert!(out.contains("30 100"), "the new size, told: {:?}", out);
80 Ok(())
81}
82
83#[test]
84fn raw_mode_holds_while_raw_lives_and_is_put_back_after() -> Outcome<()> {
85 let p = res!(Pty::open(80, 24));
86 res!(pty::nonblocking(p.master()));
87 let mut m = File::from(res!(p.master().try_clone_to_owned()));
88 let mode = |m: &mut File| -> Outcome<String> {
89 let mut kid = res!(spawn(&p, "stty -a; echo end"));
90 let out = res!(read_until(m, "end"));
91 let _ = kid.wait();
92 Ok(out)
93 };
94 let raw = res!(Raw::enter(p.slave()));
95 let during = res!(mode(&mut m));
96 drop(raw);
97 let after = res!(mode(&mut m));
98 assert!(during.contains("end") && during.contains("-icanon") && during.contains("-echo "), "raw: {:?}", during);
99 assert!(after.contains("end") && after.contains(" icanon") && !after.contains("-icanon") && !after.contains("-echo "),
100 "put back: {:?}", after);
101 Ok(())
102}