Skip to contentSkip to conversation

unit test for pool locking

PATCH New Joe Thornber 1 participant 1 message Mar 28, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unit test for pool locking

Kabi,

Can you add this to your pool locking patchset please?
Is it worth factoring the checksum code out and doing a separate test for that?

Thanks,

- Joe

commit be4dd46d27d8bbe0170ed58c2baf9f73b2531f18

Author: Joe Thornber <[email protected]>
Date:   Mon Mar 28 16:09:17 2011 +0100

add a unit test for the pool locking api (not set/restore)

unit-tests/mm/Makefile.in +11 -2
index 1b3499e..29eaf0f 100644
@@ -18,10 +18,14 @@ top_builddir = @top_builddir@
 VPATH = @srcdir@
 
 SOURCES=\
-	pool_valgrind_t.c
+	pool_valgrind_t.c \
+	pool_locking_bad_t.c \
+	pool_locking_t.c
 
 TARGETS=\
-	pool_valgrind_t
+	pool_valgrind_t \
+	pool_locking_bad_t \
+	pool_locking_t
 
 include $(top_builddir)/make.tmpl
 DM_LIBS = -ldevmapper $(LIBS)
@@ -29,3 +33,8 @@ DM_LIBS = -ldevmapper $(LIBS)
 pool_valgrind_t: pool_valgrind_t.o
 	$(CC) $(CFLAGS) -o $@ pool_valgrind_t.o $(LDFLAGS) $(DM_LIBS)
 
+pool_locking_bad_t: pool_locking_bad_t.o
+	$(CC) $(CFLAGS) -o $@ pool_locking_bad_t.o $(LDFLAGS) $(DM_LIBS)
+
+pool_locking_t: pool_locking_t.o
+	$(CC) $(CFLAGS) -o $@ pool_locking_t.o $(LDFLAGS) $(DM_LIBS)
unit-tests/mm/TESTS +0 -0
index 3bf3154..310c661 100644
@@ -1 +1,3 @@
 valgrind pool awareness:valgrind ./pool_valgrind_t 2>&1

| ./check_results
+pool locking (bad case):./expect-failure ./pool_locking_bad_t +pool locking (good case):./pool_locking_t

unit-tests/mm/expect-failure new file +6 -0
new file mode 100755
index 0000000..0a75e9c
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+if $@
+then
+    exit 1
+fi
\ No newline at end of file
(quoted diff) +0 -0

b/unit-tests/mm/pool_locking_bad_t.c
new file mode 100644
index 0000000..6cd0992

unit-tests/mm/pool_locking_bad_t.c new file +39 -0
@@ -0,0 +1,39 @@
+#include "libdevmapper.h"
+
+#include <assert.h>
+
+static void check_lock_spots_mutation()
+{
+	struct dm_pool *p;
+	char *data = NULL;
+	unsigned i;
+
+	p = dm_pool_create("", 34);
+	assert(p);
+
+	for (i = 0; i < 677; i++) {
+		char *ptr = dm_pool_alloc(p, 3);
+		assert(ptr);
+
+		if (i == 123)
+			data = ptr;
+	}
+
+	data[0] = 'o';
+	data[1] = 'n';
+	data[2] = 'e';
+
+	assert(dm_pool_lock(p, 1));
+	data[0] = 't';
+	data[1] = 'w';
+	data[2] = 'o';
+	assert(dm_pool_unlock(p));
+	dm_pool_destroy(p);
+}
+
+int main(int argc, char **argv)
+{
+	/* we expect this to fail, hence the need */
+	check_lock_spots_mutation();
+	return 0;
+}
(quoted diff) +0 -0

b/unit-tests/mm/pool_locking_t.c
new file mode 100644
index 0000000..bf082d3

unit-tests/mm/pool_locking_t.c new file +39 -0
@@ -0,0 +1,39 @@
+#include "libdevmapper.h"
+
+#include <assert.h>
+
+static void check_unlock_allows_mutation()
+{
+	struct dm_pool *p;
+	char *data = NULL;
+	unsigned i;
+
+	p = dm_pool_create("", 34);
+	assert(p);
+
+	for (i = 0; i < 677; i++) {
+		char *ptr = dm_pool_alloc(p, 3);
+		assert(ptr);
+
+		if (i == 123)
+			data = ptr;
+	}
+
+	data[0] = 'o';
+	data[1] = 'n';
+	data[2] = 'e';
+
+	assert(dm_pool_lock(p, 1));
+	assert(dm_pool_locked(p));
+	assert(dm_pool_unlock(p));
+	data[0] = 't';
+	data[1] = 'w';
+	data[2] = 'o';
+	dm_pool_destroy(p);
+}
+
+int main(int argc, char **argv)
+{
+	check_unlock_allows_mutation();
+	return 0;
+}