Debian Codegraph @

tests/isc/file_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <fcntl.h>
15 #include <inttypes.h>
16 #include <sched.h> /* IWYU pragma: keep */
17 #include <setjmp.h>
18 #include <stdarg.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/file.h>
28 #include <isc/result.h>
29 #include <isc/util.h>
30
31 #include <tests/isc.h>
32
33 #define NAME "internal"
34 #define SHA "3bed2cb3a3acf7b6a8ef408420cc682d5520e26976d354254f528c965612054f"
35 #define TRUNC_SHA "3bed2cb3a3acf7b6"
36
37 #define BAD1 "in/internal"
38 #define BADHASH1 "8bbb97a888791399"
39
40 #define BAD2 "Internal"
41 #define BADHASH2 "2ea1842b445b0c81"
42
43 #define F(x) "testdata/file/" x ".test"
44
45 static void
46 touch(const char *filename) {
47 int fd;
48
49 unlink(filename);
50 fd = creat(filename, 0644);
51 if (fd != -1) {
52 close(fd);
53 }
54 }
55
56 /* test sanitized filenames */
57 ISC_RUN_TEST_IMPL(isc_file_sanitize) {
58 isc_result_t result;
59 char buf[1024];
60
61 UNUSED(state);
62
63 assert_return_code(chdir(TESTS_DIR), 0);
64
65 result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
66 assert_int_equal(result, ISC_R_SUCCESS);
67 assert_int_equal(strcmp(buf, F(NAME)), 0);
68
69 touch(F(TRUNC_SHA));
70 result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
71 assert_int_equal(result, ISC_R_SUCCESS);
72 assert_int_equal(strcmp(buf, F(TRUNC_SHA)), 0);
73
74 touch(F(SHA));
75 result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
76 assert_int_equal(result, ISC_R_SUCCESS);
77 assert_int_equal(strcmp(buf, F(SHA)), 0);
78
79 result = isc_file_sanitize("testdata/file", BAD1, "test", buf, 1024);
80 assert_int_equal(result, ISC_R_SUCCESS);
81 assert_int_equal(strcmp(buf, F(BADHASH1)), 0);
82
83 result = isc_file_sanitize("testdata/file", BAD2, "test", buf, 1024);
84 assert_int_equal(result, ISC_R_SUCCESS);
85 assert_int_equal(strcmp(buf, F(BADHASH2)), 0);
86
87 unlink(F(TRUNC_SHA));
88 unlink(F(SHA));
89 }
90
91 /* test filename templates */
92 ISC_RUN_TEST_IMPL(isc_file_template) {
93 isc_result_t result;
94 char buf[1024];
95
96 UNUSED(state);
97
98 assert_return_code(chdir(TESTS_DIR), 0);
99
100 result = isc_file_template("/absolute/path", "file-XXXXXXXX", buf,
101 sizeof(buf));
102 assert_int_equal(result, ISC_R_SUCCESS);
103 assert_string_equal(buf, "/absolute/file-XXXXXXXX");
104
105 result = isc_file_template("relative/path", "file-XXXXXXXX", buf,
106 sizeof(buf));
107 assert_int_equal(result, ISC_R_SUCCESS);
108 assert_string_equal(buf, "relative/file-XXXXXXXX");
109
110 result = isc_file_template("/trailing/slash/", "file-XXXXXXXX", buf,
111 sizeof(buf));
112 assert_int_equal(result, ISC_R_SUCCESS);
113 assert_string_equal(buf, "/trailing/slash/file-XXXXXXXX");
114
115 result = isc_file_template("relative/trailing/slash/", "file-XXXXXXXX",
116 buf, sizeof(buf));
117 assert_int_equal(result, ISC_R_SUCCESS);
118 assert_string_equal(buf, "relative/trailing/slash/file-XXXXXXXX");
119
120 result = isc_file_template("/", "file-XXXXXXXX", buf, sizeof(buf));
121 assert_int_equal(result, ISC_R_SUCCESS);
122 assert_string_equal(buf, "/file-XXXXXXXX");
123
124 result = isc_file_template("noslash", "file-XXXXXXXX", buf,
125 sizeof(buf));
126 assert_int_equal(result, ISC_R_SUCCESS);
127 assert_string_equal(buf, "file-XXXXXXXX");
128
129 result = isc_file_template(NULL, "file-XXXXXXXX", buf, sizeof(buf));
130 assert_int_equal(result, ISC_R_SUCCESS);
131 assert_string_equal(buf, "file-XXXXXXXX");
132 }
133
134 ISC_TEST_LIST_START
135
136 ISC_TEST_ENTRY(isc_file_sanitize)
137 ISC_TEST_ENTRY(isc_file_template)
138
139 ISC_TEST_LIST_END
140
141 ISC_TEST_MAIN