diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-19 16:07:55 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-20 20:39:30 +0200 |
commit | d766a9b089aefff63bca3e9914f5332af65efd12 (patch) | |
tree | 507ed59787e1a61770831034ebf7c3ff20558193 /tests/canonicalize_name.c | |
parent | f2981c2ff581e4e425c7b23da43aee29f25232c5 (diff) |
Add unit tests
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/canonicalize_name.c')
-rw-r--r-- | tests/canonicalize_name.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/canonicalize_name.c b/tests/canonicalize_name.c new file mode 100644 index 0000000..ad12752 --- /dev/null +++ b/tests/canonicalize_name.c @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "util.h" + +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +static const struct { + const char *in; + const char *out; +} must_work[] = { + { "", "" }, + { "/", "" }, + { "\\", "" }, + { "///", "" }, + { "\\\\\\", "" }, + { "/\\//\\\\/", "" }, + { "foo/bar/test", "foo/bar/test" }, + { "foo\\bar\\test", "foo/bar/test" }, + { "/foo/bar/test/", "foo/bar/test" }, + { "\\foo\\bar\\test\\", "foo/bar/test" }, + { "///foo//bar//test///", "foo/bar/test" }, + { "./foo/././bar/test/./.", "foo/bar/test" }, + { "./foo/././", "foo" }, + { ".", "" }, + { "./", "" }, + { "./.", "" }, + { "foo/.../bar", "foo/.../bar" }, + { "foo/.test/bar", "foo/.test/bar" }, +}; + +static const char *must_not_work[] = { + "..", + "foo/../bar", + "../foo/bar", + "foo/bar/..", + "foo/bar/../", +}; + +int main(void) +{ + char buffer[512]; + size_t i; + + for (i = 0; i < sizeof(must_work) / sizeof(must_work[0]); ++i) { + strcpy(buffer, must_work[i].in); + + if (canonicalize_name(buffer)) { + fprintf(stderr, "Test case rejected: '%s'\n", + must_work[i].in); + return EXIT_FAILURE; + } + + if (strcmp(buffer, must_work[i].out) != 0) { + fprintf(stderr, "Expected result: %s\n", + must_work[i].out); + fprintf(stderr, "Actual result: %s\n", buffer); + return EXIT_FAILURE; + } + } + + for (i = 0; i < sizeof(must_not_work) / sizeof(must_not_work[0]); ++i) { + strcpy(buffer, must_not_work[i]); + + if (canonicalize_name(buffer) == 0) { + fprintf(stderr, "Test case accepted: '%s'\n", + must_not_work[i]); + fprintf(stderr, "Transformed into: '%s'\n", buffer); + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} |