dtsystemsettings -list-panes prints the panes (ID, name, category, keywords; a PANE_LATER pane is left out) before any X setup. dtlaunch asks for them once (pipe and exec, no shell, a 3 s poll limit; no waitpid, SIGCHLD is ignored there) and model.c parses and matches them: the name at full score, a keyword at half, the ID checked because it ends up in dtsystemsettings -pane ID. A result reads '[settings] Displays (Hardware)' and starts through the same Parental Controls as a program (run_args). dtlaunch -text STRING fills the field of the first opening, for tests and tools/mcde-shot.sh. Measured: make mcde-linux 'check programs/dtlaunch programs/dtsystemsettings' exit=0, test_model PASS (test_panes: parse incl. unsafe IDs, matching incl. a German keyword); -list-panes: 24 lines, exit 0 with DISPLAY unset, no dock; Spotlight-mcde.png: typing 'disp' lists '[settings] Displays (Hardware)' second; Return on 'displays' starts 'dtsystemsettings -pane displays' and a window 'Displays' appears (Xvfb). Not done: the grouped two-column list, documents/mail/PDF/web results, the dtbar magnifier, the clipboard without the 1 s poll (t0185, t0080). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
256 lines
7.0 KiB
C
256 lines
7.0 KiB
C
/*
|
|
* test_model.c -- the tests of model.c (make check)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "model.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static int
|
|
calc_is(const char *e, double want)
|
|
{
|
|
double v;
|
|
|
|
return mx_calc(e, &v) == 0 && v > want - 1e-9 && v < want + 1e-9;
|
|
}
|
|
|
|
static int
|
|
calc_fails(const char *e)
|
|
{
|
|
double v;
|
|
|
|
return mx_calc(e, &v) != 0;
|
|
}
|
|
|
|
static void
|
|
test_match(void)
|
|
{
|
|
CHECK(mx_match("", "anything") > 0);
|
|
CHECK(mx_match("dtf", "dtfile") > 0);
|
|
CHECK(mx_match("DTF", "dtfile") > 0);
|
|
CHECK(mx_match("xyz", "dtfile") == 0);
|
|
CHECK(mx_match("fd", "dtfile") == 0); /* order matters */
|
|
/* the start beats the middle, a word start beats a letter inside */
|
|
CHECK(mx_match("term", "dtterm") < mx_match("term", "terminal"));
|
|
CHECK(mx_match("fm", "file manager") > mx_match("fm", "pathfmt"));
|
|
/* letters in a row beat scattered ones */
|
|
CHECK(mx_match("cal", "calendar") > mx_match("cal", "cxaxl"));
|
|
}
|
|
|
|
static void
|
|
test_calc(void)
|
|
{
|
|
char buf[64];
|
|
|
|
CHECK(calc_is("1+2", 3));
|
|
CHECK(calc_is("=2*3+4", 10));
|
|
CHECK(calc_is("2*(3+4)", 14));
|
|
CHECK(calc_is("= -3 + 5", 2));
|
|
CHECK(calc_is("2^3^2", 512)); /* right associative */
|
|
CHECK(calc_is("7 % 4", 3));
|
|
CHECK(calc_is("=10/4", 2.5));
|
|
CHECK(calc_fails("1/0"));
|
|
CHECK(calc_fails("12")); /* a number is no calculation */
|
|
CHECK(calc_fails("firefox"));
|
|
CHECK(calc_fails("=2+"));
|
|
CHECK(calc_fails("=(1+2"));
|
|
CHECK(calc_fails("-5")); /* a leading minus alone */
|
|
mx_calc_format(2.5, buf, sizeof buf);
|
|
CHECK(strcmp(buf, "2.5") == 0);
|
|
mx_calc_format(3.0, buf, sizeof buf);
|
|
CHECK(strcmp(buf, "3") == 0);
|
|
mx_calc_format(-0.0, buf, sizeof buf);
|
|
CHECK(strcmp(buf, "0") == 0);
|
|
}
|
|
|
|
static void
|
|
test_snippets(const char *dir)
|
|
{
|
|
char path[1024];
|
|
FILE *f;
|
|
MxSnippet *s;
|
|
size_t n;
|
|
char *u;
|
|
|
|
u = mx_unescape("a\\nb\\tc\\\\d");
|
|
CHECK(u != NULL && strcmp(u, "a\nb\tc\\d") == 0);
|
|
free(u);
|
|
|
|
(void)snprintf(path, sizeof path, "%s/snippets", dir);
|
|
CHECK(mx_snippets_load(path, &s, &n) == 0 && n == 0); /* no file */
|
|
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fputs("# a comment\n", f);
|
|
fputs("addr\tRua Nova 1\\n1000 Lisboa\n", f);
|
|
fputs("no tab on this line\n", f);
|
|
fputs("\tno keyword\n", f);
|
|
fputs("sig\t-- \\nKonrad\r\n", f);
|
|
fclose(f);
|
|
CHECK(mx_snippets_load(path, &s, &n) == 0);
|
|
CHECK(n == 2);
|
|
if (n == 2) {
|
|
CHECK(strcmp(s[0].key, "addr") == 0);
|
|
CHECK(strcmp(s[0].text, "Rua Nova 1\n1000 Lisboa") == 0);
|
|
CHECK(strcmp(s[1].key, "sig") == 0);
|
|
CHECK(strcmp(s[1].text, "-- \nKonrad") == 0);
|
|
}
|
|
mx_snippets_free(s, n);
|
|
(void)unlink(path);
|
|
}
|
|
|
|
static void
|
|
test_programs(const char *dir)
|
|
{
|
|
char d1[1024], d2[1024], f[1100], p[2200];
|
|
char **v;
|
|
size_t n, i;
|
|
FILE *fp;
|
|
const char *names[] = { "beta", "alpha", ".hidden" };
|
|
|
|
(void)snprintf(d1, sizeof d1, "%s/bin1", dir);
|
|
(void)snprintf(d2, sizeof d2, "%s/bin2", dir);
|
|
(void)mkdir(d1, 0755);
|
|
(void)mkdir(d2, 0755);
|
|
for (i = 0; i < 3; i++) {
|
|
(void)snprintf(f, sizeof f, "%s/%s", d1, names[i]);
|
|
fp = fopen(f, "w");
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
(void)chmod(f, 0755);
|
|
}
|
|
(void)snprintf(f, sizeof f, "%s/alpha", d2); /* a duplicate */
|
|
fp = fopen(f, "w");
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
(void)chmod(f, 0755);
|
|
(void)snprintf(f, sizeof f, "%s/notexec", d2);
|
|
fp = fopen(f, "w");
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
(void)chmod(f, 0644);
|
|
|
|
(void)snprintf(p, sizeof p, "%s:%s:/nonexistent", d1, d2);
|
|
CHECK(mx_path_programs(p, &v, &n) == 0);
|
|
CHECK(n == 2);
|
|
if (n == 2) {
|
|
CHECK(strcmp(v[0], "alpha") == 0);
|
|
CHECK(strcmp(v[1], "beta") == 0);
|
|
}
|
|
mx_strv_free(v, n);
|
|
}
|
|
|
|
/* the panes of System Settings (t0185): the text of `dtsystemsettings
|
|
* -list-panes', untrusted because the ID ends up in an exec */
|
|
static void
|
|
test_panes(void)
|
|
{
|
|
static const char text[] =
|
|
"displays\tDisplays\tHardware\tdisplays monitors resolution aufloesung bildschirm\n"
|
|
"sound\tSound\tHardware\tsound audio lautstaerke volume ton\n"
|
|
"desktop\tDesktop & Screen Saver\tPersonal\tdesktop background bildschirmschoner\n"
|
|
"network\tNetwork\tInternet & Network\n" /* no keywords */
|
|
"\n" /* blank */
|
|
"bad id\tBad\tSystem\tx\n" /* blank in the ID */
|
|
"a;b\tBad\tSystem\tx\n"
|
|
"$(id)\tBad\tSystem\tx\n"
|
|
"-x\tDash first is fine\tSystem\tx\n" /* '-' is allowed */
|
|
"short\tonly two fields\n"
|
|
"noname\t\tSystem\tx\n"
|
|
"nocat\tName\t\tx\n"
|
|
"last\tLast One\tSystem\tkeyword with tab\tand more"; /* no newline */
|
|
MxPane *p;
|
|
size_t n, i;
|
|
int best, s_disp, s_bild, s_none;
|
|
|
|
CHECK(mx_panes_parse(NULL, &p, &n) == 0 && n == 0 && p == NULL);
|
|
CHECK(mx_panes_parse("", &p, &n) == 0 && n == 0);
|
|
CHECK(mx_panes_parse(text, &p, &n) == 0);
|
|
CHECK(n == 6);
|
|
if (n != 6)
|
|
return;
|
|
CHECK(strcmp(p[0].id, "displays") == 0);
|
|
CHECK(strcmp(p[0].name, "Displays") == 0);
|
|
CHECK(strcmp(p[0].category, "Hardware") == 0);
|
|
CHECK(strncmp(p[0].keywords, "displays monitors", 17) == 0);
|
|
CHECK(strcmp(p[2].name, "Desktop & Screen Saver") == 0);
|
|
CHECK(strcmp(p[3].id, "network") == 0 && p[3].keywords[0] == '\0');
|
|
CHECK(strcmp(p[4].id, "-x") == 0);
|
|
CHECK(strcmp(p[5].id, "last") == 0);
|
|
/* the fourth tab stays in the keywords */
|
|
CHECK(strchr(p[5].keywords, '\t') != NULL);
|
|
for (i = 0; i < n; i++) /* none of the bad IDs got through */
|
|
CHECK(strpbrk(p[i].id, " ;$()") == NULL);
|
|
|
|
/* matching: the name, then a keyword at half the score */
|
|
s_disp = mx_pane_match("disp", &p[0]);
|
|
CHECK(s_disp > 0);
|
|
CHECK(s_disp == mx_match("disp", "Displays")); /* the name wins */
|
|
s_bild = mx_pane_match("bildschirm", &p[0]); /* German keyword */
|
|
CHECK(s_bild > 0);
|
|
CHECK(s_bild == mx_match("bildschirm", "bildschirm") / 2);
|
|
CHECK(mx_pane_match("bildschirm", &p[2]) > 0); /* another pane's word */
|
|
s_none = mx_pane_match("zzz", &p[0]);
|
|
CHECK(s_none == 0);
|
|
best = mx_pane_match("sound", &p[1]);
|
|
CHECK(best == mx_match("sound", "Sound"));
|
|
CHECK(mx_pane_match("net", &p[3]) > 0); /* no keywords: name only */
|
|
CHECK(mx_pane_match("zzz", &p[3]) == 0);
|
|
/* a pane's keyword does not make another pane match */
|
|
CHECK(mx_pane_match("lautstaerke", &p[0]) == 0);
|
|
mx_panes_free(p, n);
|
|
}
|
|
|
|
/* remove what test_programs made; test_snippets removes its own file */
|
|
static void
|
|
cleanup(const char *dir)
|
|
{
|
|
const char *files[] = { "bin1/alpha", "bin1/beta", "bin1/.hidden",
|
|
"bin2/alpha", "bin2/notexec", "bin1", "bin2" };
|
|
char p[1100];
|
|
size_t i;
|
|
|
|
for (i = 0; i < sizeof files / sizeof files[0]; i++) {
|
|
(void)snprintf(p, sizeof p, "%s/%s", dir, files[i]);
|
|
if (unlink(p) != 0)
|
|
(void)rmdir(p);
|
|
}
|
|
(void)rmdir(dir);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char dir[] = "/tmp/dtlaunch-test.XXXXXX";
|
|
|
|
if (mkdtemp(dir) == NULL) {
|
|
perror("mkdtemp");
|
|
return 2;
|
|
}
|
|
test_match();
|
|
test_calc();
|
|
test_snippets(dir);
|
|
test_programs(dir);
|
|
test_panes();
|
|
cleanup(dir);
|
|
printf("%s: %d failures\n", failures ? "FAIL" : "PASS", failures);
|
|
return failures ? 1 : 0;
|
|
}
|