summaryrefslogtreecommitdiff
path: root/lib/tdb2/test
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-09-14 07:20:13 +0930
committerRusty Russell <rusty@rustcorp.com.au>2011-09-14 07:20:13 +0930
commitb67acb7e2a7cde01321daa6116c635a565accd27 (patch)
tree0c8216cd0d20519cb68af1acec43144a5022f5e5 /lib/tdb2/test
parent1664702d2f0755a3caf7ba8dd433e34bb8d340b0 (diff)
downloadsamba-b67acb7e2a7cde01321daa6116c635a565accd27.tar.gz
samba-b67acb7e2a7cde01321daa6116c635a565accd27.tar.bz2
samba-b67acb7e2a7cde01321daa6116c635a565accd27.zip
tdb2: tdb_foreach()
Create an iterator over every open tdb (not internal TDBs). This is useful for re-establishing the tdb1-style active lock for CLEAR_IF_FIRST. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (Imported from CCAN commit 1a0c636bc38213bd0322db47529f78f2dc22ffdd)
Diffstat (limited to 'lib/tdb2/test')
-rw-r--r--lib/tdb2/test/run-tdb_foreach.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/tdb2/test/run-tdb_foreach.c b/lib/tdb2/test/run-tdb_foreach.c
new file mode 100644
index 0000000000..e34dfb86cd
--- /dev/null
+++ b/lib/tdb2/test/run-tdb_foreach.c
@@ -0,0 +1,93 @@
+#include <ccan/tdb2/tdb.c>
+#include <ccan/tdb2/open.c>
+#include <ccan/tdb2/free.c>
+#include <ccan/tdb2/lock.c>
+#include <ccan/tdb2/io.c>
+#include <ccan/tdb2/hash.c>
+#include <ccan/tdb2/transaction.c>
+#include <ccan/tdb2/check.c>
+#include <ccan/tap/tap.h>
+#include "logging.h"
+
+static int drop_count(struct tdb_context *tdb, unsigned int *count)
+{
+ if (--(*count) == 0)
+ return 1;
+ return 0;
+}
+
+static int set_found(struct tdb_context *tdb, bool found[3])
+{
+ unsigned int idx;
+
+ if (strcmp(tdb_name(tdb), "run-tdb_foreach0.tdb") == 0)
+ idx = 0;
+ else if (strcmp(tdb_name(tdb), "run-tdb_foreach1.tdb") == 0)
+ idx = 1;
+ else if (strcmp(tdb_name(tdb), "run-tdb_foreach2.tdb") == 0)
+ idx = 2;
+ else
+ abort();
+
+ if (found[idx])
+ abort();
+ found[idx] = true;
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ unsigned int i, count;
+ bool found[3];
+ struct tdb_context *tdb0, *tdb1, *tdb2;
+ int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
+ TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
+
+ plan_tests(sizeof(flags) / sizeof(flags[0]) * 8);
+ for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
+ tdb0 = tdb_open("run-tdb_foreach0.tdb", flags[i],
+ O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+ tdb1 = tdb_open("run-tdb_foreach1.tdb", flags[i],
+ O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+ tdb2 = tdb_open("run-tdb_foreach2.tdb", flags[i],
+ O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+
+ memset(found, 0, sizeof(found));
+ tdb_foreach(set_found, found);
+ ok1(found[0] && found[1] && found[2]);
+
+ /* Test premature iteration termination */
+ count = 1;
+ tdb_foreach(drop_count, &count);
+ ok1(count == 0);
+
+ tdb_close(tdb1);
+ memset(found, 0, sizeof(found));
+ tdb_foreach(set_found, found);
+ ok1(found[0] && !found[1] && found[2]);
+
+ tdb_close(tdb2);
+ memset(found, 0, sizeof(found));
+ tdb_foreach(set_found, found);
+ ok1(found[0] && !found[1] && !found[2]);
+
+ tdb1 = tdb_open("run-tdb_foreach1.tdb", flags[i],
+ O_RDWR, 0600, &tap_log_attr);
+ memset(found, 0, sizeof(found));
+ tdb_foreach(set_found, found);
+ ok1(found[0] && found[1] && !found[2]);
+
+ tdb_close(tdb0);
+ memset(found, 0, sizeof(found));
+ tdb_foreach(set_found, found);
+ ok1(!found[0] && found[1] && !found[2]);
+
+ tdb_close(tdb1);
+ memset(found, 0, sizeof(found));
+ tdb_foreach(set_found, found);
+ ok1(!found[0] && !found[1] && !found[2]);
+ ok1(tap_log_messages == 0);
+ }
+
+ return exit_status();
+}