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