1*bcda20f6Schristos /* $NetBSD: file_test.c,v 1.3 2025/01/26 16:25:49 christos Exp $ */ 28aaca124Schristos 38aaca124Schristos /* 48aaca124Schristos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 58aaca124Schristos * 68aaca124Schristos * SPDX-License-Identifier: MPL-2.0 78aaca124Schristos * 88aaca124Schristos * This Source Code Form is subject to the terms of the Mozilla Public 98aaca124Schristos * License, v. 2.0. If a copy of the MPL was not distributed with this 108aaca124Schristos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 118aaca124Schristos * 128aaca124Schristos * See the COPYRIGHT file distributed with this work for additional 138aaca124Schristos * information regarding copyright ownership. 148aaca124Schristos */ 158aaca124Schristos 168aaca124Schristos #include <fcntl.h> 178aaca124Schristos #include <inttypes.h> 188aaca124Schristos #include <sched.h> /* IWYU pragma: keep */ 198aaca124Schristos #include <setjmp.h> 208aaca124Schristos #include <stdarg.h> 218aaca124Schristos #include <stddef.h> 228aaca124Schristos #include <stdlib.h> 238aaca124Schristos #include <string.h> 248aaca124Schristos #include <unistd.h> 258aaca124Schristos 268aaca124Schristos #define UNIT_TESTING 278aaca124Schristos #include <cmocka.h> 288aaca124Schristos 298aaca124Schristos #include <isc/file.h> 308aaca124Schristos #include <isc/result.h> 318aaca124Schristos #include <isc/util.h> 328aaca124Schristos 338aaca124Schristos #include <tests/isc.h> 348aaca124Schristos 358aaca124Schristos #define NAME "internal" 368aaca124Schristos #define SHA "3bed2cb3a3acf7b6a8ef408420cc682d5520e26976d354254f528c965612054f" 378aaca124Schristos #define TRUNC_SHA "3bed2cb3a3acf7b6" 388aaca124Schristos 398aaca124Schristos #define BAD1 "in/internal" 408aaca124Schristos #define BADHASH1 "8bbb97a888791399" 418aaca124Schristos 428aaca124Schristos #define BAD2 "Internal" 438aaca124Schristos #define BADHASH2 "2ea1842b445b0c81" 448aaca124Schristos 458aaca124Schristos #define F(x) "testdata/file/" x ".test" 468aaca124Schristos 478aaca124Schristos static void 488aaca124Schristos touch(const char *filename) { 498aaca124Schristos int fd; 508aaca124Schristos 518aaca124Schristos unlink(filename); 528aaca124Schristos fd = creat(filename, 0644); 538aaca124Schristos if (fd != -1) { 548aaca124Schristos close(fd); 558aaca124Schristos } 568aaca124Schristos } 578aaca124Schristos 588aaca124Schristos /* test sanitized filenames */ 598aaca124Schristos ISC_RUN_TEST_IMPL(isc_file_sanitize) { 608aaca124Schristos isc_result_t result; 618aaca124Schristos char buf[1024]; 628aaca124Schristos 638aaca124Schristos UNUSED(state); 648aaca124Schristos 658aaca124Schristos assert_return_code(chdir(TESTS_DIR), 0); 668aaca124Schristos 678aaca124Schristos result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024); 688aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 698aaca124Schristos assert_int_equal(strcmp(buf, F(NAME)), 0); 708aaca124Schristos 718aaca124Schristos touch(F(TRUNC_SHA)); 728aaca124Schristos result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024); 738aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 748aaca124Schristos assert_int_equal(strcmp(buf, F(TRUNC_SHA)), 0); 758aaca124Schristos 768aaca124Schristos touch(F(SHA)); 778aaca124Schristos result = isc_file_sanitize("testdata/file", NAME, "test", buf, 1024); 788aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 798aaca124Schristos assert_int_equal(strcmp(buf, F(SHA)), 0); 808aaca124Schristos 818aaca124Schristos result = isc_file_sanitize("testdata/file", BAD1, "test", buf, 1024); 828aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 838aaca124Schristos assert_int_equal(strcmp(buf, F(BADHASH1)), 0); 848aaca124Schristos 858aaca124Schristos result = isc_file_sanitize("testdata/file", BAD2, "test", buf, 1024); 868aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 878aaca124Schristos assert_int_equal(strcmp(buf, F(BADHASH2)), 0); 888aaca124Schristos 898aaca124Schristos unlink(F(TRUNC_SHA)); 908aaca124Schristos unlink(F(SHA)); 918aaca124Schristos } 928aaca124Schristos 938aaca124Schristos /* test filename templates */ 948aaca124Schristos ISC_RUN_TEST_IMPL(isc_file_template) { 958aaca124Schristos isc_result_t result; 968aaca124Schristos char buf[1024]; 978aaca124Schristos 988aaca124Schristos UNUSED(state); 998aaca124Schristos 1008aaca124Schristos assert_return_code(chdir(TESTS_DIR), 0); 1018aaca124Schristos 1028aaca124Schristos result = isc_file_template("/absolute/path", "file-XXXXXXXX", buf, 1038aaca124Schristos sizeof(buf)); 1048aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 1058aaca124Schristos assert_string_equal(buf, "/absolute/file-XXXXXXXX"); 1068aaca124Schristos 1078aaca124Schristos result = isc_file_template("relative/path", "file-XXXXXXXX", buf, 1088aaca124Schristos sizeof(buf)); 1098aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 1108aaca124Schristos assert_string_equal(buf, "relative/file-XXXXXXXX"); 1118aaca124Schristos 1128aaca124Schristos result = isc_file_template("/trailing/slash/", "file-XXXXXXXX", buf, 1138aaca124Schristos sizeof(buf)); 1148aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 1158aaca124Schristos assert_string_equal(buf, "/trailing/slash/file-XXXXXXXX"); 1168aaca124Schristos 1178aaca124Schristos result = isc_file_template("relative/trailing/slash/", "file-XXXXXXXX", 1188aaca124Schristos buf, sizeof(buf)); 1198aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 1208aaca124Schristos assert_string_equal(buf, "relative/trailing/slash/file-XXXXXXXX"); 1218aaca124Schristos 1228aaca124Schristos result = isc_file_template("/", "file-XXXXXXXX", buf, sizeof(buf)); 1238aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 1248aaca124Schristos assert_string_equal(buf, "/file-XXXXXXXX"); 1258aaca124Schristos 1268aaca124Schristos result = isc_file_template("noslash", "file-XXXXXXXX", buf, 1278aaca124Schristos sizeof(buf)); 1288aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 1298aaca124Schristos assert_string_equal(buf, "file-XXXXXXXX"); 1308aaca124Schristos 1318aaca124Schristos result = isc_file_template(NULL, "file-XXXXXXXX", buf, sizeof(buf)); 1328aaca124Schristos assert_int_equal(result, ISC_R_SUCCESS); 1338aaca124Schristos assert_string_equal(buf, "file-XXXXXXXX"); 1348aaca124Schristos } 1358aaca124Schristos 1368aaca124Schristos ISC_TEST_LIST_START 1378aaca124Schristos 1388aaca124Schristos ISC_TEST_ENTRY(isc_file_sanitize) 1398aaca124Schristos ISC_TEST_ENTRY(isc_file_template) 1408aaca124Schristos 1418aaca124Schristos ISC_TEST_LIST_END 1428aaca124Schristos 1438aaca124Schristos ISC_TEST_MAIN 144