This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-10-quiet-head
in repository terminology.
View the commit online.
commit 2a26faac6df1959efd2fb124e2df73a63de898f5
Author: Cedric BAIL <[email protected]>
AuthorDate: Wed Nov 18 21:41:26 2026 -0600
tytest: drive a real tymuxd through the native client
Live tests start a tymuxd on a private XDG_RUNTIME_DIR and attach to it
with termtymux, so that later changes to the backlog transport can be
checked end to end without a second implementation of the protocol.
'tytest all' skips them; the tymux-live meson test names them.
The test body runs in a forked child: a failed assert aborts without
running atexit handlers, so the parent ends the daemons and removes the
directory instead. The first test pins how the daemon streams the
backlog to a client, wide lines included.
---
DESIGN.md | 1 +
src/bin/meson.build | 3 +-
src/bin/termsrv.c | 18 +-
src/bin/termsrv.h | 6 +
src/bin/tytest.c | 20 +++
src/bin/tytest_tymux.c | 463 +++++++++++++++++++++++++++++++++++++++++++++++++
src/bin/tytest_tymux.h | 47 +++++
tests/meson.build | 6 +
8 files changed, 559 insertions(+), 5 deletions(-)
diff --git a/DESIGN.md b/DESIGN.md
index 06855655..048175d2 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -49,6 +49,7 @@ A simple documentation to navigate through the C files:
* `src/bin/tymuxd_stubs.c`: stands in for the `termio` functions `tymuxd` has no widget for
* `src/bin/typop.c`: the `typop` tool
* `src/bin/tyq.c`: the `tyq` tool
+* `src/bin/tytest_tymux.c`: the live tests of `tytest`, which drive a real `tymuxd` through the native client
* `src/bin/utf8.c`: handles conversion between Eina_Unicode and char *
* `src/bin/utils.c`: small utilitarian functions
* `src/bin/win.c`: handles the windows, splits, tabs
diff --git a/src/bin/meson.build b/src/bin/meson.build
index f82e17e3..2b70b8ab 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -164,7 +164,8 @@ tybench_sources = [color_names,
'tybench.c']
if host_os == 'linux'
- tytest_sources += ['termsrv.c', 'termsrv.h', 'termtymux.c', 'termtymux.h']
+ tytest_sources += ['termsrv.c', 'termsrv.h', 'termtymux.c', 'termtymux.h',
+ 'tytest_tymux.c', 'tytest_tymux.h']
endif
executable('terminology',
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 550155d7..568a1dbd 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -205,8 +205,8 @@ _sigterm_pid_send(pid_t pid)
return kill(pid, SIGTERM);
}
-int
-termsrv_daemon_kill(int sock)
+pid_t
+termsrv_daemon_pid(int sock)
{
struct ucred cred;
socklen_t cred_len = sizeof(cred);
@@ -218,6 +218,16 @@ termsrv_daemon_kill(int sock)
errno = EPERM;
return -1;
}
+ return cred.pid;
+}
+
+int
+termsrv_daemon_kill(int sock)
+{
+ pid_t pid = termsrv_daemon_pid(sock);
+
+ if (pid < 0)
+ return -1;
#if defined(TERMSRV_PIDFD)
{
/* The kernel resolves the pidfd from the same record as SO_PEERCRED,
@@ -225,8 +235,8 @@ termsrv_daemon_kill(int sock)
* that does not know the option, or the syscall, falls back to the
* pid: any other failure is the caller's to see. */
int pidfd = -1, ret, err;
+ socklen_t cred_len = sizeof(pidfd);
- cred_len = sizeof(pidfd);
if (getsockopt(sock, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &cred_len) < 0)
{
if (errno != ENOPROTOOPT && errno != EOPNOTSUPP)
@@ -245,7 +255,7 @@ termsrv_daemon_kill(int sock)
}
}
#endif
- return _sigterm_pid_send(cred.pid);
+ return _sigterm_pid_send(pid);
}
static void
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index 491434c4..c132b656 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -255,6 +255,12 @@ int termsrv_ensure_tymux_dir(void);
* removed. Returns the connected socket, or -1 with errno set. */
int termsrv_connect(const char *name);
+/* The pid of the tymuxd listening on sock, a socket termsrv_connect()
+ * returned, as the kernel recorded it when the daemon started listening.
+ * Returns -1 with errno set on error, EPERM if the daemon belongs to
+ * another user. */
+pid_t termsrv_daemon_pid(int sock);
+
/* Ask the tymuxd listening on sock, a socket termsrv_connect() returned,
* to end its session by sending it SIGTERM. Returns 0 on success, -1 on
* error with errno set, EPERM if the daemon belongs to another user. */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 87c30eff..9d47a448 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -23,6 +23,9 @@
#include "tytest_common.h"
#include "md5.h"
+#if defined(HAVE_TYMUX)
+#include "tytest_tymux.h"
+#endif
int _log_domain = -1;
@@ -188,6 +191,18 @@ static struct {
{ NULL, NULL},
};
+/* Tests that start processes and take seconds: 'tytest all' skips them,
+ * the tymux-live meson test names them. */
+static const struct {
+ const char *name;
+ tytest_func func;
+} _tytests_live[] = {
+#if defined(HAVE_TYMUX)
+ { "tymux_live_backlog", tytest_tymux_live_backlog},
+#endif
+ { NULL, NULL},
+};
+
static int
_run_this_tytest(const char *name, tytest_func func)
{
@@ -209,6 +224,11 @@ _find_tytest(const char *name)
if (strcmp(name, _tytests[i].name) == 0)
return _tytests[i].func;
}
+ for (i = 0; _tytests_live[i].name; i++)
+ {
+ if (strcmp(name, _tytests_live[i].name) == 0)
+ return _tytests_live[i].func;
+ }
return NULL;
}
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
new file mode 100644
index 00000000..e500b1d8
--- /dev/null
+++ b/src/bin/tytest_tymux.c
@@ -0,0 +1,463 @@
+#include "private.h"
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <Elementary.h>
+#include "config.h"
+#include "tytest_tymux.h"
+#include "backlog.h"
+#include "utf8.h"
+
+#define LIVE_TIMEOUT_SECONDS 100
+#define DAEMON_EXIT_TIMEOUT_SECONDS 5.0
+
+#define RUNTIME_DIR_TEMPLATE "/tmp/tytest-tymux-XXXXXX"
+static char _runtime_dir[] = RUNTIME_DIR_TEMPLATE;
+static Config *_config = NULL;
+
+static double
+_now(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec + ts.tv_nsec / 1e9;
+}
+
+static Eina_Bool
+_pid_gone(pid_t pid)
+{
+ return kill(pid, 0) < 0 && errno == ESRCH;
+}
+
+static void
+_rc_file_write(void)
+{
+ char path[sizeof(_runtime_dir) + sizeof("/shellrc")];
+ FILE *f;
+
+ snprintf(path, sizeof(path), "%s/shellrc", _runtime_dir);
+ f = fopen(path, "w");
+ assert(f);
+ /* interactive POSIX shells read $ENV: switch line editing off so that
+ * readline does not eat the control characters typed in the tests */
+ fputs("set +o emacs 2>/dev/null; set +o vi 2>/dev/null\n", f);
+ fclose(f);
+ setenv("ENV", path, 1);
+}
+
+static void
+_environment_setup(void)
+{
+ /* mkdtemp() overwrites the template, restore it for each run */
+ memcpy(_runtime_dir, RUNTIME_DIR_TEMPLATE, sizeof(RUNTIME_DIR_TEMPLATE));
+ assert(mkdtemp(_runtime_dir));
+ unsetenv("DISPLAY");
+ unsetenv("WAYLAND_DISPLAY");
+ unsetenv("TYMUX_NAME");
+ unsetenv("TERMINOLOGY");
+ unsetenv("TMUX");
+ unsetenv("STY");
+ unsetenv("TYMUX_NO_NATIVE");
+ setenv("XDG_RUNTIME_DIR", _runtime_dir, 1);
+ setenv("SHELL", "/bin/sh", 1);
+ setenv("PS1", "$ ", 1);
+ setenv("TERM", "xterm-256color", 1);
+ _rc_file_write();
+}
+
+/* The tymuxd processes that have our XDG_RUNTIME_DIR in their environment,
+ * whatever session they were started for. */
+static void
+_daemons_end(void)
+{
+ char needle[sizeof("XDG_RUNTIME_DIR=") + sizeof(_runtime_dir)];
+ size_t needle_len;
+ DIR *proc = opendir("/proc");
+ struct dirent *entry;
+ int pass;
+
+ snprintf(needle, sizeof(needle), "XDG_RUNTIME_DIR=%s", _runtime_dir);
+ needle_len = strlen(needle) + 1;
+ if (!proc) return;
+ for (pass = 0; pass < 2; pass++)
+ {
+ rewinddir(proc);
+ while ((entry = readdir(proc)))
+ {
+ char path[64], comm[32], *environ_buf, *p;
+ size_t len, got;
+ FILE *f;
+ pid_t pid = atoi(entry->d_name);
+
+ if (pid <= 1) continue;
+ snprintf(path, sizeof(path), "/proc/%d/comm", pid);
+ f = fopen(path, "r");
+ if (!f) continue;
+ got = fread(comm, 1, sizeof(comm) - 1, f);
+ fclose(f);
+ comm[got] = '\0';
+ if (strncmp(comm, "tymuxd", strlen("tymuxd"))) continue;
+
+ snprintf(path, sizeof(path), "/proc/%d/environ", pid);
+ f = fopen(path, "r");
+ if (!f) continue;
+ environ_buf = malloc(1 << 20);
+ if (!environ_buf)
+ {
+ fclose(f);
+ continue;
+ }
+ len = fread(environ_buf, 1, 1 << 20, f);
+ fclose(f);
+ for (p = environ_buf; p < environ_buf + len;
+ p += strlen(p) + 1)
+ {
+ if ((size_t)(environ_buf + len - p) >= needle_len &&
+ !memcmp(p, needle, needle_len))
+ {
+ kill(pid, pass == 0 ? SIGTERM : SIGKILL);
+ break;
+ }
+ }
+ free(environ_buf);
+ }
+ if (pass == 0) usleep(300 * 1000);
+ }
+ closedir(proc);
+}
+
+static void
+_dir_remove(const char *dir)
+{
+ DIR *d = opendir(dir);
+ struct dirent *entry;
+
+ if (!d) return;
+ while ((entry = readdir(d)))
+ {
+ char path[4096];
+ struct stat st;
+
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+ snprintf(path, sizeof(path), "%s/%s", dir, entry->d_name);
+ if (lstat(path, &st) < 0) continue;
+ if (S_ISDIR(st.st_mode)) _dir_remove(path);
+ else unlink(path);
+ }
+ closedir(d);
+ rmdir(dir);
+}
+
+int
+tytest_tymux_live_run(int (*body)(void))
+{
+ int status = 0, res = -1;
+ pid_t child;
+
+ _environment_setup();
+ /* Asserts abort(), which runs no cleanup: the parent, which never
+ * touches a session, is the one that cleans up after the child. */
+ child = fork();
+ assert(child >= 0);
+ if (child == 0)
+ {
+ int ret;
+
+ alarm(LIVE_TIMEOUT_SECONDS);
+ ecore_init();
+ ret = body();
+ if (_config) config_del(_config);
+ ecore_shutdown();
+ _exit(ret);
+ }
+ if (waitpid(child, &status, 0) == child && WIFEXITED(status))
+ res = WEXITSTATUS(status);
+ _daemons_end();
+ _dir_remove(_runtime_dir);
+ return res;
+}
+
+Eina_Bool
+tytest_tymux_start(const char *name, int cols, int rows,
+ TermTymux **ts, Termpty **ty)
+{
+ if (!_config)
+ {
+ _config = config_new();
+ assert(_config);
+ _config->scrollback = 10000;
+ }
+ *ts = termtymux_new(name, cols, rows, _config, TSRV_ATTACH_HIJACK, ty);
+ return *ts != NULL;
+}
+
+Eina_Bool
+tytest_tymux_wait(Eina_Bool (*done)(void *data), void *data, double timeout)
+{
+ double end = _now() + timeout;
+
+ do
+ {
+ ecore_main_loop_iterate();
+ if (done(data)) return EINA_TRUE;
+ usleep(1000);
+ }
+ while (_now() < end);
+ return EINA_FALSE;
+}
+
+Eina_Bool
+tytest_tymux_type(TermTymux *ts, Termpty *ty, const char *text)
+{
+ if (!ts || !ty) return EINA_FALSE;
+ termpty_write(ty, text, strlen(text));
+ return EINA_TRUE;
+}
+
+/* BACKLOG_ROW_GET() does not parenthesize its arguments */
+static const Termsave *
+_backlog_row(Termpty *ty, int backlog_y)
+{
+ return BACKLOG_ROW_GET(ty, backlog_y);
+}
+
+char *
+tytest_tymux_backlog_line(Termpty *ty, int backlog_y)
+{
+ const Termsave *save;
+ char *text;
+ size_t len = 0;
+ unsigned int i;
+
+ if (backlog_y < 1 || (size_t)backlog_y >= ty->backsize) return NULL;
+ save = _backlog_row(ty, backlog_y);
+ if (!save->cells) return NULL;
+
+ text = malloc((size_t)save->w * 4 + 1);
+ if (!text) return NULL;
+ for (i = 0; i < save->w; i++)
+ {
+ Eina_Unicode codepoint = save->cells[i].codepoint;
+
+ /* a cell that stands for a block or a link has no text */
+ if (codepoint == 0 || (codepoint & 0x80000000)) codepoint = ' ';
+ len += codepoint_to_utf8(codepoint, text + len);
+ }
+ while (len > 0 && text[len - 1] == ' ') len--;
+ text[len] = '\0';
+ return text;
+}
+
+void
+tytest_tymux_stop(TermTymux *ts)
+{
+ pid_t pid;
+
+ if (!ts) return;
+ pid = termsrv_daemon_pid(ts->sock_fd);
+ if (pid > 1) termsrv_daemon_kill(ts->sock_fd);
+ termtymux_free(ts);
+ if (pid > 1)
+ {
+ double end = _now() + DAEMON_EXIT_TIMEOUT_SECONDS;
+
+ while (!_pid_gone(pid) && _now() < end) usleep(5000);
+ assert(_pid_gone(pid));
+ }
+}
+
+int
+tytest_tymux_daemon_memfds(TermTymux *ts, const char *prefix)
+{
+ char dir[64], want[256];
+ DIR *fds;
+ struct dirent *entry;
+ int count = 0;
+ pid_t pid = termsrv_daemon_pid(ts->sock_fd);
+
+ if (pid <= 1) return -1;
+ snprintf(dir, sizeof(dir), "/proc/%d/fd", pid);
+ snprintf(want, sizeof(want), "/memfd:%s", prefix);
+ fds = opendir(dir);
+ if (!fds) return -1;
+ while ((entry = readdir(fds)))
+ {
+ char path[320], target[256];
+ ssize_t n;
+
+ snprintf(path, sizeof(path), "%s/%s", dir, entry->d_name);
+ n = readlink(path, target, sizeof(target) - 1);
+ if (n < 0) continue;
+ target[n] = '\0';
+ if (!strncmp(target, want, strlen(want))) count++;
+ }
+ closedir(fds);
+ return count;
+}
+
+#define SESSION "ty-live"
+#define SEQ_CHECKED 2990
+/* termd_tymux.c sends at most TERMD_TYMUX_ATTACH_BACKLOG_MAX rows on
+ * attach, and the rows after the checked run take some of them */
+#define REATTACH_RUN_MIN 1900
+#define WIDE_MIN 5000
+#define SCREEN_ROWS 10
+
+static int
+_backlog_entries(Termpty *ty)
+{
+ int n = 0;
+
+ while ((size_t)(n + 1) < ty->backsize && _backlog_row(ty, n + 1)->cells)
+ n++;
+ return n;
+}
+
+static int
+_backlog_find(Termpty *ty, const char *text)
+{
+ int y, n = _backlog_entries(ty);
+
+ for (y = 1; y <= n; y++)
+ {
+ char *line = tytest_tymux_backlog_line(ty, y);
+ Eina_Bool found = line && !strcmp(line, text);
+
+ free(line);
+ if (found) return y;
+ }
+ return 0;
+}
+
+/* The number of entries from the entry reading last_value, going towards
+ * the oldest, that read last_value, last_value - 1, ...; 0 if there is no
+ * entry reading last_value. */
+static int
+_backlog_run(Termpty *ty, int last_value)
+{
+ char last_text[16];
+ int y, run = 0, value = last_value, n = _backlog_entries(ty);
+
+ snprintf(last_text, sizeof(last_text), "%d", last_value);
+ y = _backlog_find(ty, last_text);
+ if (y == 0) return 0;
+ for (; y <= n && value >= 1; y++, value--)
+ {
+ char expected[16], *line = tytest_tymux_backlog_line(ty, y);
+ Eina_Bool match;
+
+ snprintf(expected, sizeof(expected), "%d", value);
+ match = line && !strcmp(line, expected);
+ free(line);
+ if (!match) break;
+ run++;
+ }
+ return run;
+}
+
+static int
+_backlog_wide_find(Termpty *ty)
+{
+ int y, n = _backlog_entries(ty);
+
+ for (y = 1; y <= n; y++)
+ {
+ if (_backlog_row(ty, y)->w >= WIDE_MIN) return y;
+ }
+ return 0;
+}
+
+static Eina_Bool
+_screen_rows_done(void *data)
+{
+ Termpty *ty = data;
+
+ return ty->h == SCREEN_ROWS;
+}
+
+static Eina_Bool
+_seq_done(void *data)
+{
+ return _backlog_run(data, SEQ_CHECKED) == SEQ_CHECKED;
+}
+
+static Eina_Bool
+_wide_done(void *data)
+{
+ return _backlog_wide_find(data) != 0;
+}
+
+static Eina_Bool
+_reattach_done(void *data)
+{
+ return _backlog_wide_find(data) != 0 &&
+ _backlog_run(data, SEQ_CHECKED) >= REATTACH_RUN_MIN;
+}
+
+static ssize_t
+_backlog_rows_expected(Termpty *ty)
+{
+ ssize_t rows = 0;
+ int y, n = _backlog_entries(ty);
+
+ for (y = 1; y <= n; y++)
+ {
+ unsigned int w = _backlog_row(ty, y)->w;
+
+ rows += (w == 0) ? 1 : DIV_ROUND_UP(w, ty->w);
+ }
+ return rows;
+}
+
+static int
+_live_backlog_body(void)
+{
+ TermTymux *ts, *ts2;
+ Termpty *ty, *ty2;
+ int y;
+
+ assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+ assert(ty->w == 80);
+ /* a short screen, so that the lines checked below have scrolled into
+ * the backlog by the time the shell is done */
+ termtymux_resize(ts, 80, SCREEN_ROWS);
+ assert(tytest_tymux_wait(_screen_rows_done, ty, 5.0));
+
+ assert(tytest_tymux_type(ts, ty, "seq 1 3000\n"));
+ assert(tytest_tymux_wait(_seq_done, ty, 20.0));
+
+ assert(tytest_tymux_type(ts, ty, "printf '%5000s\\n' x\n"));
+ assert(tytest_tymux_type(ts, ty, "seq 1 30\n"));
+ assert(tytest_tymux_wait(_wide_done, ty, 20.0));
+ y = _backlog_wide_find(ty);
+ assert(y > 0);
+ assert(termpty_backlog_length(ty) == _backlog_rows_expected(ty));
+ assert(_backlog_row(ty, y)->w >= WIDE_MIN);
+
+ termtymux_free(ts);
+
+ assert(tytest_tymux_start(SESSION, 80, 24, &ts2, &ty2));
+ assert(tytest_tymux_wait(_reattach_done, ty2, 20.0));
+ assert(_backlog_run(ty2, SEQ_CHECKED) >= REATTACH_RUN_MIN);
+ assert(_backlog_wide_find(ty2) > 0);
+ assert(termpty_backlog_length(ty2) == _backlog_rows_expected(ty2));
+
+ tytest_tymux_stop(ts2);
+ return 0;
+}
+
+int
+tytest_tymux_live_backlog(void)
+{
+ return tytest_tymux_live_run(_live_backlog_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
new file mode 100644
index 00000000..b168999d
--- /dev/null
+++ b/src/bin/tytest_tymux.h
@@ -0,0 +1,47 @@
+#ifndef TERMINOLOGY_TYTEST_TYMUX_H_
+#define TERMINOLOGY_TYTEST_TYMUX_H_ 1
+
+/* Live tests: a real tymuxd, driven through the native client termtymux.c,
+ * on a private XDG_RUNTIME_DIR. They spawn processes and take seconds, so
+ * 'tytest all' does not run them; the tymux-live meson test names them. */
+
+#include <Eina.h>
+#include <sys/types.h>
+#include "termpty.h"
+#include "termtymux.h"
+
+/* Runs body() in a forked child with a private XDG_RUNTIME_DIR and a shell
+ * environment tymuxd sessions can be tested in. Whatever happens to the
+ * child, a failed assert included, the tymuxd processes started on that
+ * directory are ended and the directory is removed. Returns body()'s
+ * result, or -1 if the child did not exit normally. */
+int tytest_tymux_live_run(int (*body)(void));
+
+/* Attach to the session called name, starting its daemon if needed, with a
+ * minimal Config (scrollback 10000) and TSRV_ATTACH_HIJACK. Only valid in
+ * a tytest_tymux_live_run() body. */
+Eina_Bool tytest_tymux_start(const char *name, int cols, int rows,
+ TermTymux **ts, Termpty **ty);
+
+/* Iterates the main loop until done(data) returns EINA_TRUE or timeout
+ * seconds have passed. Returns whether done() was satisfied. */
+Eina_Bool tytest_tymux_wait(Eina_Bool (*done)(void *data), void *data,
+ double timeout);
+
+/* Writes text as if typed, through the shadow pty's write hook. */
+Eina_Bool tytest_tymux_type(TermTymux *ts, Termpty *ty, const char *text);
+
+/* The text of backlog entry backlog_y, 1 being the newest, with trailing
+ * blanks trimmed; NULL if there is no such entry. The caller frees it. */
+char *tytest_tymux_backlog_line(Termpty *ty, int backlog_y);
+
+/* Ends ts's daemon, waits for it to exit and frees ts. */
+void tytest_tymux_stop(TermTymux *ts);
+
+/* How many of the daemon's open files are memfds whose name starts with
+ * prefix. */
+int tytest_tymux_daemon_memfds(TermTymux *ts, const char *prefix);
+
+int tytest_tymux_live_backlog(void);
+
+#endif
diff --git a/tests/meson.build b/tests/meson.build
index 275e628f..de9726eb 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -97,6 +97,12 @@ if tests
args: [files('tymux.py'), tymux.full_path()],
depends: [tymux, tymuxd],
timeout: 120)
+ # Drives a real tymuxd through the native client built into tytest.
+ test('tymux-live',
+ tytest,
+ args: ['tymux_live_backlog'],
+ depends: [tymuxd],
+ timeout: 120)
endif
endif
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.