/* * test_profiles.c -- the tests of Profiles.c (make check) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. */ #define _POSIX_C_SOURCE 200809L /* fmemopen */ #include #include #include #include #include #include "Profiles.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static McdeProfile p[MCDE_PROFILES_MAX]; static char err[256]; static int parse(const char *text) { FILE *fp = fmemopen((void *) text, strlen(text), "r"); int n; err[0] = '\0'; n = McdeProfilesParse(fp, p, MCDE_PROFILES_MAX, err, sizeof err); fclose(fp); return n; } static void test_parse(void) { const McdeProfile *d; CHECK(parse("") == 0); CHECK(parse("# only a comment\n\n") == 0); CHECK(parse( "profile Default\n" "profile Dark\n" "\tfont DejaVu Sans Mono 12\n" "\tforeground #d8d8d8\n" " background #1c1c1c \n" "\tcommand ksh -l -c 'echo hi; exec ksh'\n" "profile Big Hack\n" "\tfont Hack\n") == 3); CHECK(strcmp(p[0].name, "Default") == 0 && p[0].font[0] == '\0' && p[0].fontSize == 0 && p[0].command[0] == '\0'); d = McdeProfileFind(p, 3, "Dark"); CHECK(d == &p[1]); CHECK(strcmp(d->font, "DejaVu Sans Mono") == 0 && d->fontSize == 12); CHECK(strcmp(d->foreground, "#d8d8d8") == 0); CHECK(strcmp(d->background, "#1c1c1c") == 0); CHECK(strcmp(d->command, "ksh -l -c 'echo hi; exec ksh'") == 0); CHECK(strcmp(p[2].name, "Big Hack") == 0); CHECK(strcmp(p[2].font, "Hack") == 0 && p[2].fontSize == 0); CHECK(McdeProfileFind(p, 3, "dark") == NULL); CHECK(McdeProfileFind(p, 3, NULL) == NULL); /* a number in a family name that is not the size */ CHECK(parse("profile A\nfont Terminus2\n") == 1 && strcmp(p[0].font, "Terminus2") == 0 && p[0].fontSize == 0); } static void test_errors(void) { CHECK(parse("font Hack 12\n") == -1 && strncmp(err, "1:", 2) == 0); CHECK(parse("profile A\ncolour red\n") == -1 && strstr(err, "unknown key") != NULL && strncmp(err, "2:", 2) == 0); CHECK(parse("profile A\nprofile A\n") == -1 && strstr(err, "twice") != NULL); CHECK(parse("profile\n") == -1); CHECK(parse("profile A\nfont Hack 2\n") == -1); CHECK(parse("profile A\nfont Hack 200\n") == -1); CHECK(parse("profile A\nforeground " "#0123456789012345678901234567890123456789\n") == -1); } static void test_widget_name(void) { char b[32]; CHECK(McdeProfileWidgetName("Dark", b, sizeof b) && strcmp(b, "profile_Dark") == 0); CHECK(McdeProfileWidgetName("Big Hack.2*", b, sizeof b) && strcmp(b, "profile_Big_Hack_2_") == 0); CHECK(!McdeProfileWidgetName("a name much too long for it", b, 16)); } static void test_load(void) { char home[] = "/tmp/test_profilesXXXXXX", dir[64], file[96], sys[96]; const char *sysfiles[] = { NULL, NULL }; FILE *fp; if (mkdtemp(home) == NULL) { CHECK(!"mkdtemp"); return; } snprintf(dir, sizeof dir, "%s/.dt", home); snprintf(file, sizeof file, "%s/dtterm.profiles", dir); snprintf(sys, sizeof sys, "%s/sys.profiles", home); sysfiles[0] = sys; CHECK(McdeProfilesLoad(home, sysfiles, p, MCDE_PROFILES_MAX) == 0); fp = fopen(sys, "w"); fputs("profile System\n", fp); fclose(fp); CHECK(McdeProfilesLoad(home, sysfiles, p, MCDE_PROFILES_MAX) == 1 && strcmp(p[0].name, "System") == 0); /* the user's file wins over the system's */ mkdir(dir, 0700); fp = fopen(file, "w"); fputs("profile Mine\nprofile Other\n", fp); fclose(fp); CHECK(McdeProfilesLoad(home, sysfiles, p, MCDE_PROFILES_MAX) == 2 && strcmp(p[0].name, "Mine") == 0); /* a broken file gives no profiles */ fp = fopen(file, "w"); fputs("nonsense\n", fp); fclose(fp); CHECK(McdeProfilesLoad(home, sysfiles, p, MCDE_PROFILES_MAX) == 0); unlink(file); rmdir(dir); unlink(sys); rmdir(home); } int main(void) { test_parse(); test_errors(); test_widget_name(); test_load(); if (failures == 0) printf("PASS test_profiles\n"); return failures == 0 ? 0 : 1; }