aboutsummaryrefslogtreecommitdiff
path: root/tests/filename_sane.c
blob: a9f7224122b49b84f811b27c8e339ecdbfd858d5 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * filename_sane.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "fstree.h"

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

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

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

static const char *must_not_work_here[] = {
#if defined(_WIN32) || defined(__WINDOWS__) || defined(TEST_WIN32)
	"fo<o", "fo>o", "fo:o", "fo\"o",
	"fo|o", "fo?o", "fo*o", "fo\ro",
	"CON", "PRN", "AUX", "NUL",
	"COM1", "COM2", "LPT1", "LPT2",
	"con", "prn", "aux", "nul",
	"com1", "com2", "lpt1", "lpt2",
	"AUX.txt", "aux.txt", "NUL.txt", "nul.txt",
#endif
	NULL,
};

int main(void)
{
	size_t i;

	for (i = 0; must_work[i] != NULL; ++i) {
		assert(is_filename_sane(must_work[i], false));
		assert(is_filename_sane(must_work[i], true));
	}

	for (i = 0; must_not_work[i] != NULL; ++i) {
		assert(!is_filename_sane(must_not_work[i], false));
		assert(!is_filename_sane(must_not_work[i], true));
	}

	for (i = 0; must_not_work_here[i] != NULL; ++i) {
		assert( is_filename_sane(must_not_work_here[i], false));
		assert(!is_filename_sane(must_not_work_here[i], true));
	}

	return EXIT_SUCCESS;
}