summaryrefslogtreecommitdiff
path: root/tests/libutil/filename_sane.c
blob: 1a2cf7248ac91d8453143ae593172c23fe6e506b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * filename_sane.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "util/util.h"
#include "util/test.h"

static const char *must_work[] = {
	"foobar",
	"test.txt",
	NULL,
};

static const char *must_not_work[] = {
	".",
	"..",
	"/foo",
	"foo/",
	"foo/bar",
	NULL,
};

int main(int argc, char **argv)
{
	size_t i;
	(void)argc; (void)argv;

	for (i = 0; must_work[i] != NULL; ++i) {
		if (!is_filename_sane(must_work[i])) {
			fprintf(stderr, "%s was rejected!\n", must_work[i]);
			return EXIT_FAILURE;
		}
	}

	for (i = 0; must_not_work[i] != NULL; ++i) {
		if (is_filename_sane(must_not_work[i])) {
			fprintf(stderr, "%s was accepted!\n",
				must_not_work[i]);
			return EXIT_FAILURE;
		}
	}

	return EXIT_SUCCESS;
}