xref: /freebsd/lib/libc/tests/db/dbm_nextkey_test.c (revision b5daf675efc746611c7cfcd1fa474b8905064c4b)
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_nextkey_test);
17 ATF_TC_HEAD(dbm_nextkey_test, tc)
18 {
19 	atf_tc_set_md_var(tc, "descr",
20 	    "Check that dbm_nextkey always returns NULL after reaching the end of the database");
21 }
22 
23 ATF_TC_BODY(dbm_nextkey_test, tc)
24 {
25 	DBM *db;
26 	datum key, data;
27 
28 	data.dptr = "bar";
29 	data.dsize = strlen("bar");
30 	key.dptr = "foo";
31 	key.dsize = strlen("foo");
32 
33 	db = dbm_open(path, O_RDWR | O_CREAT, 0755);
34 	ATF_CHECK(db != NULL);
35 	ATF_REQUIRE(atf_utils_file_exists(dbname));
36 	ATF_REQUIRE(dbm_store(db, key, data, DBM_INSERT) != -1);
37 
38 	key = dbm_firstkey(db);
39 	ATF_REQUIRE(key.dptr != NULL);
40 	key = dbm_nextkey(db);
41 	ATF_REQUIRE(key.dptr == NULL);
42 	key = dbm_nextkey(db);
43 	ATF_REQUIRE(key.dptr == NULL);
44 
45 	dbm_close(db);
46 }
47 
48 ATF_TP_ADD_TCS(tp)
49 {
50 	ATF_TP_ADD_TC(tp, dbm_nextkey_test);
51 
52 	return (atf_no_error());
53 }
54