1 /*- 2 * Copyright (c) 2025 Klara, Inc. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7 #include <sys/mman.h> 8 9 #include <fcntl.h> 10 #include <ndbm.h> 11 #include <stdio.h> 12 13 #include <atf-c.h> 14 15 ATF_TC(dbm_open_missing_test); 16 ATF_TC_HEAD(dbm_open_missing_test, tc) 17 { 18 atf_tc_set_md_var(tc, "descr", 19 "Test dbm_open when creating a new database"); 20 } 21 22 ATF_TC_BODY(dbm_open_missing_test, tc) 23 { 24 const char *path = "tmp"; 25 const char *dbname = "tmp.db"; 26 27 /* 28 * POSIX.1 specifies that a missing database file should 29 * always get created if O_CREAT is present, except when 30 * O_EXCL is specified as well. 31 */ 32 ATF_CHECK(dbm_open(path, O_RDONLY, _PROT_ALL) == NULL); 33 ATF_REQUIRE(!atf_utils_file_exists(dbname)); 34 ATF_CHECK(dbm_open(path, O_RDONLY | O_CREAT, _PROT_ALL) != NULL); 35 ATF_REQUIRE(atf_utils_file_exists(dbname)); 36 ATF_CHECK(dbm_open(path, O_RDONLY | O_CREAT | O_EXCL, _PROT_ALL) == NULL); 37 } 38 39 ATF_TP_ADD_TCS(tp) 40 { 41 ATF_TP_ADD_TC(tp, dbm_open_missing_test); 42 return (atf_no_error()); 43 } 44