test: share one daemon and D-Bus harness across live tests
Six live tests carried private copies of the same daemon harness: the
private XDG_RUNTIME_DIR, the fork/exec with captured stderr, the
readiness waits, the SIGTERM and SIGKILL paths, the IBus address file
reader, the socket connect, and the IPC probe. Three of them also
carried the same libdbus helpers.
Extract one implementation into tests/live.c and, so that the binaries
that do not link dbus-1 keep not linking it, the D-Bus half into
tests/livebus.c. Both are compiled into each binary the way ../ipc.c
already is. gtk_live_test keeps its own fake-server harness and only
takes fail, nowms and leftms; xim_live_test keeps its own Xvfb and
process-group daemon spawn, which must inherit DISPLAY, and takes the
temporary directory, timing and cleanup halves.
The copies had drifted; the harness keeps the stricter behaviour.
- nowms reports a broken clock (-1) instead of pretending it read
zero, and leftms turns that into an expired deadline, so a loop
ends in a timeout failure rather than spinning. livesetup checks
the clock once up front, as daemon_restart_test did.
- Timeouts compare with <= 0, not == 0.
- readuntil keeps the three-way result (complete, peer closed, error)
from ipc_live_test and daemon_restart_test rather than folding
peer closure into ECONNRESET.
- The daemon's stdout and stderr are both captured, and a failing
setenv is reported, for every daemon; daemon_failure_test captured
stderr only and said nothing about setenv.
- The child keeps daemon_restart_test's careful redirect that also
works when the pipe lands on fd 1 or 2.
- parseaddress requires a positive declared PID.
- killdaemon reports ECHILD after SIGKILL as a failure; one copy
accepted it. It now reaps with a blocking waitpid, which SIGKILL
guarantees will return, instead of daemon_restart_test's polled
wait with its own timeout diagnostic.
- stopdaemon tolerates ESRCH on SIGTERM, a benign race two copies
reported as an error.
- liveclean removes the socket and address files and then rmdirs
each directory, reporting leftovers, rather than deleting the
temporary root recursively.
- ibus_live_test now waits for the IPC socket and the address file
by polling, dropping its inotify variant; it asserted only the
address file before.
This commit is contained in:
@@ -2,31 +2,26 @@
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/keysym.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <locale.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "ipc.h"
|
||||
#include "live.h"
|
||||
|
||||
enum
|
||||
{
|
||||
Starttimeout = 8000,
|
||||
Eventtimeout = 4000,
|
||||
Stoptimeout = 3000,
|
||||
};
|
||||
|
||||
typedef struct Child Child;
|
||||
@@ -42,15 +37,9 @@ struct Child
|
||||
|
||||
struct Run
|
||||
{
|
||||
Live l;
|
||||
Child xvfb;
|
||||
Child daemon;
|
||||
char root[256];
|
||||
char runtime[320];
|
||||
char config[320];
|
||||
char ibus[352];
|
||||
char bus[384];
|
||||
char home[320];
|
||||
char socket[384];
|
||||
char display[32];
|
||||
};
|
||||
|
||||
@@ -66,61 +55,6 @@ struct Prelog
|
||||
|
||||
static int xerror;
|
||||
|
||||
static int
|
||||
fail(char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf(stderr, "xim_live_test: ");
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fputc('\n', stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int64_t
|
||||
nowms(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
if(clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
|
||||
return 0;
|
||||
return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
static int
|
||||
leftms(int64_t deadline)
|
||||
{
|
||||
int64_t n;
|
||||
|
||||
n = deadline - nowms();
|
||||
if(n <= 0)
|
||||
return 0;
|
||||
if(n > 0x7fffffff)
|
||||
return 0x7fffffff;
|
||||
return n;
|
||||
}
|
||||
|
||||
static void
|
||||
pausems(int n)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
ts.tv_sec = n / 1000;
|
||||
ts.tv_nsec = (n % 1000) * 1000000L;
|
||||
while(nanosleep(&ts, &ts) < 0 && errno == EINTR)
|
||||
;
|
||||
}
|
||||
|
||||
static int
|
||||
makedir(char *path)
|
||||
{
|
||||
if(mkdir(path, 0700) == 0)
|
||||
return 1;
|
||||
return fail("mkdir %s: %s", path, strerror(errno));
|
||||
}
|
||||
|
||||
static int
|
||||
childlog(char *path)
|
||||
{
|
||||
@@ -220,7 +154,7 @@ spawnxvfb(Run *r, char *program)
|
||||
}
|
||||
|
||||
static int
|
||||
startdaemon(Run *r, char *program, char *mapdir)
|
||||
spawndaemon(Run *r, char *program, char *mapdir)
|
||||
{
|
||||
int errfd;
|
||||
pid_t pid;
|
||||
@@ -263,7 +197,7 @@ waitsocket(Run *r)
|
||||
|
||||
deadline = nowms() + Starttimeout;
|
||||
while(leftms(deadline) > 0){
|
||||
if(lstat(r->socket, &st) == 0 && S_ISSOCK(st.st_mode) &&
|
||||
if(lstat(r->l.socket, &st) == 0 && S_ISSOCK(st.st_mode) &&
|
||||
(st.st_mode & 0777) == 0600)
|
||||
return 1;
|
||||
if(waitpid(r->daemon.pid, &status, WNOHANG) == r->daemon.pid){
|
||||
@@ -281,40 +215,24 @@ start(Run *r, char *daemon, char *mapdir, char *xvfb)
|
||||
memset(r, 0, sizeof *r);
|
||||
r->xvfb.pid = r->daemon.pid = -1;
|
||||
r->xvfb.pgid = r->daemon.pgid = -1;
|
||||
snprintf(r->root, sizeof r->root, "/tmp/strans-xim.XXXXXX");
|
||||
if(mkdtemp(r->root) == NULL)
|
||||
return fail("mkdtemp: %s", strerror(errno));
|
||||
if(snprintf(r->runtime, sizeof r->runtime, "%s/runtime", r->root)
|
||||
>= (int)sizeof r->runtime ||
|
||||
snprintf(r->config, sizeof r->config, "%s/config", r->root)
|
||||
>= (int)sizeof r->config ||
|
||||
snprintf(r->ibus, sizeof r->ibus, "%s/ibus", r->config)
|
||||
>= (int)sizeof r->ibus ||
|
||||
snprintf(r->bus, sizeof r->bus, "%s/bus", r->ibus)
|
||||
>= (int)sizeof r->bus ||
|
||||
snprintf(r->home, sizeof r->home, "%s/home", r->root)
|
||||
>= (int)sizeof r->home ||
|
||||
snprintf(r->socket, sizeof r->socket, "%s/strans.sock", r->runtime)
|
||||
>= (int)sizeof r->socket ||
|
||||
snprintf(r->xvfb.log, sizeof r->xvfb.log, "%s/xvfb.log", r->root)
|
||||
if(!livesetup(&r->l, "xim"))
|
||||
return 0;
|
||||
if(snprintf(r->xvfb.log, sizeof r->xvfb.log, "%s/xvfb.log", r->l.root)
|
||||
>= (int)sizeof r->xvfb.log ||
|
||||
snprintf(r->daemon.log, sizeof r->daemon.log, "%s/daemon.log", r->root)
|
||||
snprintf(r->daemon.log, sizeof r->daemon.log, "%s/daemon.log", r->l.root)
|
||||
>= (int)sizeof r->daemon.log)
|
||||
return fail("temporary path is too long");
|
||||
if(!makedir(r->runtime) || !makedir(r->config) ||
|
||||
!makedir(r->ibus) || !makedir(r->bus) || !makedir(r->home))
|
||||
return 0;
|
||||
if(!spawnxvfb(r, xvfb))
|
||||
return 0;
|
||||
if(setenv("DISPLAY", r->display, 1) < 0 ||
|
||||
setenv("XMODIFIERS", "@im=strans", 1) < 0 ||
|
||||
setenv("XDG_RUNTIME_DIR", r->runtime, 1) < 0 ||
|
||||
setenv("XDG_CONFIG_HOME", r->config, 1) < 0 ||
|
||||
setenv("HOME", r->home, 1) < 0 ||
|
||||
setenv("XDG_RUNTIME_DIR", r->l.runtime, 1) < 0 ||
|
||||
setenv("XDG_CONFIG_HOME", r->l.config, 1) < 0 ||
|
||||
setenv("HOME", r->l.home, 1) < 0 ||
|
||||
unsetenv("DBUS_SESSION_BUS_ADDRESS") < 0 ||
|
||||
unsetenv("IBUS_ADDRESS") < 0)
|
||||
return fail("set private environment: %s", strerror(errno));
|
||||
if(!startdaemon(r, daemon, mapdir))
|
||||
if(!spawndaemon(r, daemon, mapdir))
|
||||
return 0;
|
||||
return waitsocket(r);
|
||||
}
|
||||
@@ -364,50 +282,22 @@ stopchild(Child *c)
|
||||
c->pgid = -1;
|
||||
}
|
||||
|
||||
static void
|
||||
cleardir(char *path)
|
||||
{
|
||||
char name[512];
|
||||
struct dirent *de;
|
||||
DIR *dir;
|
||||
|
||||
dir = opendir(path);
|
||||
if(dir == NULL)
|
||||
return;
|
||||
while((de = readdir(dir)) != NULL){
|
||||
if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
|
||||
continue;
|
||||
if(snprintf(name, sizeof name, "%s/%s", path, de->d_name)
|
||||
< (int)sizeof name)
|
||||
unlink(name);
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
static void
|
||||
cleanup(Run *r, int noisy)
|
||||
{
|
||||
stopchild(&r->daemon);
|
||||
stopchild(&r->xvfb);
|
||||
if(noisy){
|
||||
fprintf(stderr, "xim_live_test: daemon log:\n");
|
||||
fprintf(stderr, "%s: daemon log:\n", testname);
|
||||
showlog(r->daemon.log);
|
||||
fprintf(stderr, "xim_live_test: Xvfb log:\n");
|
||||
fprintf(stderr, "%s: Xvfb log:\n", testname);
|
||||
showlog(r->xvfb.log);
|
||||
}
|
||||
if(r->socket[0] != '\0')
|
||||
unlink(r->socket);
|
||||
cleardir(r->bus);
|
||||
if(r->daemon.log[0] != '\0')
|
||||
unlink(r->daemon.log);
|
||||
if(r->xvfb.log[0] != '\0')
|
||||
unlink(r->xvfb.log);
|
||||
if(r->bus[0] != '\0') rmdir(r->bus);
|
||||
if(r->ibus[0] != '\0') rmdir(r->ibus);
|
||||
if(r->config[0] != '\0') rmdir(r->config);
|
||||
if(r->runtime[0] != '\0') rmdir(r->runtime);
|
||||
if(r->home[0] != '\0') rmdir(r->home);
|
||||
if(r->root[0] != '\0') rmdir(r->root);
|
||||
liveclean(&r->l);
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -417,8 +307,7 @@ xerr(Display *dpy, XErrorEvent *e)
|
||||
|
||||
xerror++;
|
||||
XGetErrorText(dpy, e->error_code, msg, sizeof msg);
|
||||
fprintf(stderr, "xim_live_test: X error: %s (request %d.%d)\n",
|
||||
msg, e->request_code, e->minor_code);
|
||||
fail("X error: %s (request %d.%d)", msg, e->request_code, e->minor_code);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -835,6 +724,7 @@ main(int argc, char **argv)
|
||||
int ok;
|
||||
char *xvfb;
|
||||
|
||||
testname = "xim_live_test";
|
||||
if(argc != 3 && argc != 4){
|
||||
fprintf(stderr, "usage: xim_live_test daemon mapdir [Xvfb]\n");
|
||||
return 2;
|
||||
|
||||
Reference in New Issue
Block a user