simplefiles

A lightweight C CGI web application for managing files.
git clone https://scm.kuandu.systems/git-raw/simplefiles.git
Log | Files | Refs | README | LICENSE

config.c (3498B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <sys/stat.h>
      5 #include <unistd.h>
      6 
      7 #include "config.h"
      8 
      9 #define TEST_ROOT "/tmp/simplefiles-config-test"
     10 #define TEST_STORAGE TEST_ROOT "/storage"
     11 #define TEST_REPO TEST_STORAGE "/default"
     12 #define TEST_CONFIG TEST_REPO "/.config"
     13 
     14 struct config_test_case {
     15 	const char *name;
     16 	const char *contents;
     17 	off_t expected_upload;
     18 	off_t expected_quota;
     19 };
     20 
     21 static const struct config_test_case config_tests[] = {
     22 	{
     23 		"valid values",
     24 		"MAX_UPLOAD=123456\n"
     25 		"QUOTA=987654\n",
     26 		123456,
     27 		987654,
     28 	},
     29 
     30 	{
     31 		"whitespace around values",
     32 		"MAX_UPLOAD = 222222\n"
     33 		"QUOTA= 333333\n",
     34 		222222,
     35 		333333,
     36 	},
     37 
     38 	{
     39 		"invalid values rejected",
     40 		"MAX_UPLOAD=-1\n"
     41 		"QUOTA=abc\n",
     42 		DEFAULT_MAX_UPLOAD,
     43 		DEFAULT_REPO_QUOTA,
     44 	},
     45 
     46 	{
     47 		"zero values rejected",
     48 		"MAX_UPLOAD=0\n"
     49 		"QUOTA=0\n",
     50 		DEFAULT_MAX_UPLOAD,
     51 		DEFAULT_REPO_QUOTA,
     52 	},
     53 
     54 	{
     55 		"comments ignored",
     56 		"# MAX_UPLOAD=1\n"
     57 		"# QUOTA=2\n"
     58 		"MAX_UPLOAD=444444\n"
     59 		"QUOTA=555555\n",
     60 		444444,
     61 		555555,
     62 	},
     63 };
     64 
     65 
     66 static int
     67 mkdir_p(const char *path)
     68 {
     69 	if (mkdir(path, 0700) == -1) {
     70 		if (access(path, F_OK) == 0)
     71 			return 0;
     72 
     73 		return -1;
     74 	}
     75 
     76 	return 0;
     77 }
     78 
     79 
     80 static int
     81 setup_environment(void)
     82 {
     83 	if (mkdir_p(TEST_ROOT) == -1)
     84 		return -1;
     85 
     86 	if (mkdir_p(TEST_STORAGE) == -1)
     87 		return -1;
     88 
     89 	if (mkdir_p(TEST_REPO) == -1)
     90 		return -1;
     91 
     92 	setenv("STORAGE_ROOT", TEST_STORAGE, 1);
     93 	setenv("SCRIPT_NAME", "/default/upload.cgi", 1);
     94 
     95 	return 0;
     96 }
     97 
     98 
     99 static int
    100 write_config(const char *contents)
    101 {
    102 	FILE *fp;
    103 
    104 	fp = fopen(TEST_CONFIG, "w");
    105 	if (fp == NULL)
    106 		return -1;
    107 
    108 	if (contents != NULL)
    109 		fprintf(fp, "%s", contents);
    110 
    111 	fclose(fp);
    112 
    113 	return 0;
    114 }
    115 
    116 
    117 static int
    118 test_repository_config(void)
    119 {
    120 	size_t i;
    121 	int failures = 0;
    122 
    123 	for (i = 0;
    124 	    i < sizeof(config_tests) / sizeof(config_tests[0]);
    125 	    i++) {
    126 
    127 		if (write_config(config_tests[i].contents) == -1) {
    128 			fprintf(stderr,
    129 			    "FAIL: %s: cannot write config\n",
    130 			    config_tests[i].name);
    131 			failures++;
    132 			continue;
    133 		}
    134 
    135 		if (config_max_upload() !=
    136 		    config_tests[i].expected_upload) {
    137 			fprintf(stderr,
    138 			    "FAIL: %s: config_max_upload() returned %lld, "
    139 			    "expected %lld\n",
    140 			    config_tests[i].name,
    141 			    (long long)config_max_upload(),
    142 			    (long long)config_tests[i].expected_upload);
    143 			failures++;
    144 			continue;
    145 		}
    146 
    147 		if (config_repo_quota() !=
    148 		    config_tests[i].expected_quota) {
    149 			fprintf(stderr,
    150 			    "FAIL: %s: config_repo_quota() returned %lld, "
    151 			    "expected %lld\n",
    152 			    config_tests[i].name,
    153 			    (long long)config_repo_quota(),
    154 			    (long long)config_tests[i].expected_quota);
    155 			failures++;
    156 			continue;
    157 		}
    158 
    159 		printf("ok: %s\n", config_tests[i].name);
    160 	}
    161 
    162 	return failures;
    163 }
    164 
    165 
    166 static int
    167 test_missing_config(void)
    168 {
    169 	unlink(TEST_CONFIG);
    170 
    171 	if (config_max_upload() != DEFAULT_MAX_UPLOAD) {
    172 		fprintf(stderr,
    173 		    "FAIL: missing config max upload\n");
    174 		return 1;
    175 	}
    176 
    177 	if (config_repo_quota() != DEFAULT_REPO_QUOTA) {
    178 		fprintf(stderr,
    179 		    "FAIL: missing config quota\n");
    180 		return 1;
    181 	}
    182 
    183 	printf("ok: missing config uses defaults\n");
    184 
    185 	return 0;
    186 }
    187 
    188 
    189 int
    190 main(void)
    191 {
    192 	int failures;
    193 
    194 	failures = 0;
    195 
    196 	if (setup_environment() == -1) {
    197 		fprintf(stderr,
    198 		    "FAIL: setup failed\n");
    199 		return EXIT_FAILURE;
    200 	}
    201 
    202 	failures += test_repository_config();
    203 	failures += test_missing_config();
    204 
    205 	if (failures != 0) {
    206 		fprintf(stderr,
    207 		    "%d test(s) failed\n",
    208 		    failures);
    209 		return EXIT_FAILURE;
    210 	}
    211 
    212 	printf("all tests passed\n");
    213 
    214 	return EXIT_SUCCESS;
    215 }