summaryrefslogtreecommitdiffstats
path: root/tests/isc/file_test.c
blob: 6b75ba24905173186143ac8c3e267f4258389d27 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#include <fcntl.h>
#include <inttypes.h>
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define UNIT_TESTING
#include <cmocka.h>

#include <isc/file.h>
#include <isc/result.h>
#include <isc/util.h>

#include <tests/isc.h>

#define NAME	  "internal"
#define SHA	  "3bed2cb3a3acf7b6a8ef408420cc682d5520e26976d354254f528c965612054f"
#define TRUNC_SHA "3bed2cb3a3acf7b6"

#define BAD1	 "in/internal"
#define BADHASH1 "8bbb97a888791399"

#define BAD2	 "Internal"
#define BADHASH2 "2ea1842b445b0c81"

#define F(x) "testdata/file/" x ".test"

static void
touch(const char *filename) {
	int fd;

	unlink(filename);
	fd = creat(filename, 0644);
	if (fd != -1) {
		close(fd);
	}
}

/* test sanitized filenames */
ISC_RUN_TEST_IMPL(isc_file_sanitize) {
	isc_result_t result;
	char buf[1024];

	UNUSED(state);

	assert_return_code(chdir(TESTS_DIR), 0);

	result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(strcmp(buf, F(NAME)), 0);

	touch(F(TRUNC_SHA));
	result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(strcmp(buf, F(TRUNC_SHA)), 0);

	touch(F(SHA));
	result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(strcmp(buf, F(SHA)), 0);

	result = isc_file_sanitize("testdata/file", BAD1, "test", buf, 1024);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(strcmp(buf, F(BADHASH1)), 0);

	result = isc_file_sanitize("testdata/file", BAD2, "test", buf, 1024);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(strcmp(buf, F(BADHASH2)), 0);

	unlink(F(TRUNC_SHA));
	unlink(F(SHA));
}

/* test filename templates */
ISC_RUN_TEST_IMPL(isc_file_template) {
	isc_result_t result;
	char buf[1024];

	UNUSED(state);

	assert_return_code(chdir(TESTS_DIR), 0);

	result = isc_file_template("/absolute/path", "file-XXXXXXXX", buf,
				   sizeof(buf));
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_string_equal(buf, "/absolute/file-XXXXXXXX");

	result = isc_file_template("relative/path", "file-XXXXXXXX", buf,
				   sizeof(buf));
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_string_equal(buf, "relative/file-XXXXXXXX");

	result = isc_file_template("/trailing/slash/", "file-XXXXXXXX", buf,
				   sizeof(buf));
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_string_equal(buf, "/trailing/slash/file-XXXXXXXX");

	result = isc_file_template("relative/trailing/slash/", "file-XXXXXXXX",
				   buf, sizeof(buf));
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_string_equal(buf, "relative/trailing/slash/file-XXXXXXXX");

	result = isc_file_template("/", "file-XXXXXXXX", buf, sizeof(buf));
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_string_equal(buf, "/file-XXXXXXXX");

	result = isc_file_template("noslash", "file-XXXXXXXX", buf,
				   sizeof(buf));
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_string_equal(buf, "file-XXXXXXXX");

	result = isc_file_template(NULL, "file-XXXXXXXX", buf, sizeof(buf));
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_string_equal(buf, "file-XXXXXXXX");
}

ISC_TEST_LIST_START

ISC_TEST_ENTRY(isc_file_sanitize)
ISC_TEST_ENTRY(isc_file_template)

ISC_TEST_LIST_END

ISC_TEST_MAIN