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