2014-11-04 19:43:27 -08:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2014-11-04 19:43:27 -08:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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 http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2014-11-04 19:43:27 -08:00
|
|
|
*/
|
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <setjmp.h>
|
2018-10-23 22:41:12 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2014-11-04 19:43:27 -08:00
|
|
|
#include <stdlib.h>
|
2014-11-06 18:02:32 +11:00
|
|
|
#include <string.h>
|
2014-11-04 19:43:27 -08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
2014-11-04 19:43:27 -08:00
|
|
|
|
|
|
|
#include <isc/file.h>
|
|
|
|
#include <isc/result.h>
|
2018-10-23 22:41:12 -07:00
|
|
|
#include <isc/util.h>
|
2014-11-04 19:43:27 -08:00
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2014-11-17 17:39:00 -08:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
touch(const char *filename)
|
|
|
|
{
|
2014-11-17 17:39:00 -08:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
unlink(filename);
|
|
|
|
fd = creat(filename, 0644);
|
2018-10-23 22:41:12 -07:00
|
|
|
if (fd != -1) {
|
2014-11-17 17:39:00 -08:00
|
|
|
close(fd);
|
2018-10-23 22:41:12 -07:00
|
|
|
}
|
2014-11-17 17:39:00 -08:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
/* test sanitized filenames */
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_file_sanitize_test(void **state)
|
|
|
|
{
|
2014-11-04 19:43:27 -08:00
|
|
|
isc_result_t result;
|
2020-02-12 13:59:18 +01:00
|
|
|
char buf[1024];
|
2014-11-04 19:43:27 -08:00
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
assert_return_code(chdir(TESTS), 0);
|
2014-11-06 18:02:32 +11:00
|
|
|
|
2014-11-04 19:43:27 -08:00
|
|
|
result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(strcmp(buf, F(NAME)), 0);
|
2014-11-04 19:43:27 -08:00
|
|
|
|
2014-11-17 17:39:00 -08:00
|
|
|
touch(F(TRUNC_SHA));
|
|
|
|
result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(strcmp(buf, F(TRUNC_SHA)), 0);
|
2014-11-17 17:39:00 -08:00
|
|
|
|
|
|
|
touch(F(SHA));
|
|
|
|
result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024);
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(strcmp(buf, F(SHA)), 0);
|
2014-11-04 19:43:27 -08:00
|
|
|
|
|
|
|
result = isc_file_sanitize("testdata/file", BAD1, "test", buf, 1024);
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(strcmp(buf, F(BADHASH1)), 0);
|
2014-11-04 19:43:27 -08:00
|
|
|
|
|
|
|
result = isc_file_sanitize("testdata/file", BAD2, "test", buf, 1024);
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(strcmp(buf, F(BADHASH2)), 0);
|
2014-11-04 19:43:27 -08:00
|
|
|
|
|
|
|
unlink(F(TRUNC_SHA));
|
|
|
|
unlink(F(SHA));
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
/* test filename templates */
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_file_template_test(void **state)
|
|
|
|
{
|
2017-04-23 23:16:53 -07:00
|
|
|
isc_result_t result;
|
2020-02-12 13:59:18 +01:00
|
|
|
char buf[1024];
|
2017-04-23 23:16:53 -07:00
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
assert_return_code(chdir(TESTS), 0);
|
2017-04-23 23:16:53 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_file_template("/absolute/path", "file-XXXXXXXX", buf,
|
|
|
|
sizeof(buf));
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(buf, "/absolute/file-XXXXXXXX");
|
2017-04-23 23:16:53 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_file_template("relative/path", "file-XXXXXXXX", buf,
|
|
|
|
sizeof(buf));
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(buf, "relative/file-XXXXXXXX");
|
2017-04-23 23:16:53 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_file_template("/trailing/slash/", "file-XXXXXXXX", buf,
|
|
|
|
sizeof(buf));
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(buf, "/trailing/slash/file-XXXXXXXX");
|
2017-04-23 23:16:53 -07:00
|
|
|
|
|
|
|
result = isc_file_template("relative/trailing/slash/", "file-XXXXXXXX",
|
|
|
|
buf, sizeof(buf));
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(buf, "relative/trailing/slash/file-XXXXXXXX");
|
2017-04-23 23:16:53 -07:00
|
|
|
|
|
|
|
result = isc_file_template("/", "file-XXXXXXXX", buf, sizeof(buf));
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(buf, "/file-XXXXXXXX");
|
2017-04-23 23:16:53 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result =
|
|
|
|
isc_file_template("noslash", "file-XXXXXXXX", buf, sizeof(buf));
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(buf, "file-XXXXXXXX");
|
2017-04-23 23:16:53 -07:00
|
|
|
|
|
|
|
result = isc_file_template(NULL, "file-XXXXXXXX", buf, sizeof(buf));
|
2018-10-23 22:41:12 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(buf, "file-XXXXXXXX");
|
2017-04-23 23:16:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
int
|
2020-02-12 13:59:18 +01:00
|
|
|
main(void)
|
|
|
|
{
|
2018-10-23 22:41:12 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test(isc_file_sanitize_test),
|
|
|
|
cmocka_unit_test(isc_file_template_test),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-12 13:59:18 +01:00
|
|
|
main(void)
|
|
|
|
{
|
2018-10-23 22:41:12 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2014-11-04 19:43:27 -08:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:12 -07:00
|
|
|
#endif
|