aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/tests/db/dbm_open_test.c
blob: 8a3e888bf72c2713b7edd1b200b9b537890d9d1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*-
 * Copyright (c) 2025 Klara, Inc.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <fcntl.h>
#include <ndbm.h>
#include <stdio.h>

#include <atf-c.h>

static const char *path = "tmp";
static const char *dbname = "tmp.db";

ATF_TC(dbm_open_missing_test);
ATF_TC_HEAD(dbm_open_missing_test, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test dbm_open when creating a new database");
}

ATF_TC_BODY(dbm_open_missing_test, tc)
{

	/*
	 * POSIX.1 specifies that a missing database file should
	 * always get created if O_CREAT is present, except when
	 * O_EXCL is specified as well.
	 */
	ATF_CHECK(dbm_open(path, O_RDONLY, 0755) == NULL);
	ATF_REQUIRE(!atf_utils_file_exists(dbname));
	ATF_CHECK(dbm_open(path, O_RDONLY | O_CREAT, 0755) != NULL);
	ATF_REQUIRE(atf_utils_file_exists(dbname));
	ATF_CHECK(dbm_open(path, O_RDONLY | O_CREAT | O_EXCL, 0755) == NULL);
}

ATF_TC_WITHOUT_HEAD(dbm_open_wronly_test);
ATF_TC_BODY(dbm_open_wronly_test, tc)
{
	ATF_CHECK(dbm_open(path, O_WRONLY, 0755) == NULL);
	ATF_REQUIRE(!atf_utils_file_exists(dbname));
	ATF_CHECK(dbm_open(path, O_WRONLY | O_CREAT, 0755) != NULL);
	ATF_REQUIRE(atf_utils_file_exists(dbname));
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, dbm_open_missing_test);
	ATF_TP_ADD_TC(tp, dbm_open_wronly_test);
	return (atf_no_error());
}