git: e1e77dd23ced - main - dbm: Add tests for dbm_open
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 25 Jul 2025 09:23:24 UTC
The branch main has been updated by bnovkov:
URL: https://cgit.FreeBSD.org/src/commit/?id=e1e77dd23cedc6d19e48e03d9454c5a3d552ed50
commit e1e77dd23cedc6d19e48e03d9454c5a3d552ed50
Author: Bojan Novković <bnovkov@FreeBSD.org>
AuthorDate: 2025-07-24 15:23:51 +0000
Commit: Bojan Novković <bnovkov@FreeBSD.org>
CommitDate: 2025-07-25 09:23:00 +0000
dbm: Add tests for dbm_open
Sponsored by: Klara, Inc.
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D51492
---
lib/libc/tests/db/Makefile | 2 ++
lib/libc/tests/db/dbm_open_test.c | 43 +++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/lib/libc/tests/db/Makefile b/lib/libc/tests/db/Makefile
index f1f33bd2bafc..54b38b94a581 100644
--- a/lib/libc/tests/db/Makefile
+++ b/lib/libc/tests/db/Makefile
@@ -7,6 +7,8 @@ PROGS+= h_lfsr
${PACKAGE}FILES+= README
+ATF_TESTS_C+= dbm_open_test
+
NETBSD_ATF_TESTS_C+= db_hash_seq_test
NETBSD_ATF_TESTS_SH+= db_test
ATF_TESTS_SH_SED_db_test= -e 's,/bin/csh,/bin/cat,g'
diff --git a/lib/libc/tests/db/dbm_open_test.c b/lib/libc/tests/db/dbm_open_test.c
new file mode 100644
index 000000000000..18d398e16b2a
--- /dev/null
+++ b/lib/libc/tests/db/dbm_open_test.c
@@ -0,0 +1,43 @@
+/*-
+ * Copyright (c) 2025 Klara, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/mman.h>
+
+#include <fcntl.h>
+#include <ndbm.h>
+#include <stdio.h>
+
+#include <atf-c.h>
+
+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)
+{
+ const char *path = "tmp";
+ const char *dbname = "tmp.db";
+
+ /*
+ * 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, _PROT_ALL) == NULL);
+ ATF_REQUIRE(!atf_utils_file_exists(dbname));
+ ATF_CHECK(dbm_open(path, O_RDONLY | O_CREAT, _PROT_ALL) != NULL);
+ ATF_REQUIRE(atf_utils_file_exists(dbname));
+ ATF_CHECK(dbm_open(path, O_RDONLY | O_CREAT | O_EXCL, _PROT_ALL) == NULL);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, dbm_open_missing_test);
+ return (atf_no_error());
+}