summaryrefslogtreecommitdiff
path: root/tests/libutil/fix_win32_filename.c
blob: e8932d69704d24b4a2116cf9225362509029e284 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * fix_win32_filename.c
 *
 * Copyright (C) 2024 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "util/test.h"
#include "util/util.h"

static const struct {
	const char *path;
	const char *result;
} test_data[] = {
	{ "foo", "foo" },
	{ "foo/bar", "foo/bar" },
	{ "foo/bar.txt", "foo/bar.txt" },
	{ "COM1", "COM1_" },
	{ "COM1.txt", "COM1_.txt" },
	{ "foo.aux", "foo.aux_" },
	{ "foo/bar/test.LPT1/bla", "foo/bar/test.LPT1_/bla" },
	{ "C:\\/foo/COM1.bla/bar",
	  "C\xEF\x80\xBA\xEF\x81\x9c/foo/COM1_.bla/bar" },
	{ "com1", "com1_" },
	{ "COM1_", "COM1__" },
	{ "COM1__", "COM1___" },
	{ "COM1___", "COM1____" },
};

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

	for (size_t i = 0; i < sizeof(test_data) / sizeof(test_data[0]); ++i) {
		char *result = fix_win32_filename(test_data[i].path);
		size_t out_len = strlen(test_data[i].result);

		if (result == NULL) {
			fprintf(stderr, "OOM for test case %u (%s)?\n",
				(unsigned int)i, test_data[i].path);
			return EXIT_FAILURE;
		}

		if (out_len != strlen(result) ||
		    memcmp(result, test_data[i].result, out_len) != 0) {
			fprintf(stderr,
				"Mismatch for %s -> %s, got %s instead!\n",
				test_data[i].path, test_data[i].result,
				result);
			free(result);
			return EXIT_FAILURE;
		}

		free(result);
	}

	return EXIT_SUCCESS;
}