talloc: fix testsuite after no_owner changes
[metze/samba/wip.git] / lib / talloc / testsuite.c
index 3d490ddf49319139093893caff827c9fd0346bc6..13010030459152080eeca7447daa0e65da431c60 100644 (file)
@@ -66,8 +66,8 @@ static double timeval_elapsed(struct timeval *tv)
 
 #define CHECK_SIZE(test, ptr, tsize) do { \
        if (talloc_total_size(ptr) != (tsize)) { \
-               printf("failed: %s [\nwrong '%s' tree size: got %u  expected %u\n]\n", \
-                      test, #ptr, \
+               printf("failed: %s [\n%s: wrong '%s' tree size: got %u  expected %u\n]\n", \
+                      test, __location__, #ptr, \
                       (unsigned)talloc_total_size(ptr), \
                       (unsigned)tsize); \
                talloc_report_full(ptr, stdout); \
@@ -77,8 +77,8 @@ static double timeval_elapsed(struct timeval *tv)
 
 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
        if (talloc_total_blocks(ptr) != (tblocks)) { \
-               printf("failed: %s [\nwrong '%s' tree blocks: got %u  expected %u\n]\n", \
-                      test, #ptr, \
+               printf("failed: %s [\n%s: wrong '%s' tree blocks: got %u  expected %u\n]\n", \
+                      test, __location__, #ptr, \
                       (unsigned)talloc_total_blocks(ptr), \
                       (unsigned)tblocks); \
                talloc_report_full(ptr, stdout); \
@@ -88,8 +88,8 @@ static double timeval_elapsed(struct timeval *tv)
 
 #define CHECK_PARENT(test, ptr, parent) do { \
        if (talloc_parent(ptr) != (parent)) { \
-               printf("failed: %s [\n'%s' has wrong parent: got %p  expected %p\n]\n", \
-                      test, #ptr, \
+               printf("failed: %s [\n%s: '%s' has wrong parent: got %p  expected %p\n]\n", \
+                      test, __location__, #ptr, \
                       talloc_parent(ptr), \
                       (parent)); \
                talloc_report_full(ptr, stdout); \
@@ -99,6 +99,18 @@ static double timeval_elapsed(struct timeval *tv)
        } \
 } while (0)
 
+#define CHECK_NOT_PARENT(test, ptr, parent) do { \
+       if (talloc_parent(ptr) == (parent)) { \
+               printf("failed: %s [\n%s: '%s' has wrong parent: got %p  expected a difference\n]\n", \
+                      test, __location__, #ptr, \
+                      (parent)); \
+               talloc_report_full(ptr, stdout); \
+               talloc_report_full(parent, stdout); \
+               talloc_report_full(NULL, stdout); \
+               return false; \
+       } \
+} while (0)
+
 
 /*
   test references 
@@ -128,15 +140,15 @@ static bool test_ref1(void)
        talloc_free(p2);
        talloc_report_full(root, stderr);
 
-       CHECK_BLOCKS("ref1", p1, 5);
+       CHECK_BLOCKS("ref1", p1, 4);
        CHECK_BLOCKS("ref1", p2, 1);
-       CHECK_BLOCKS("ref1", r1, 1);
+       CHECK_BLOCKS("ref1", r1, 2);
 
        fprintf(stderr, "Freeing p1\n");
        talloc_free(p1);
        talloc_report_full(root, stderr);
 
-       CHECK_BLOCKS("ref1", r1, 1);
+       CHECK_BLOCKS("ref1", r1, 2);
 
        fprintf(stderr, "Freeing r1\n");
        talloc_free(r1);
@@ -180,7 +192,7 @@ static bool test_ref2(void)
        CHECK_BLOCKS("ref2", r1, 2);
 
        fprintf(stderr, "Freeing ref\n");
-       talloc_free(ref);
+       talloc_unlink(r1, ref);
        talloc_report_full(root, stderr);
 
        CHECK_BLOCKS("ref2", p1, 5);
@@ -370,12 +382,11 @@ static bool test_misc(void)
        talloc_increase_ref_count(p1);
        talloc_increase_ref_count(p1);
        talloc_increase_ref_count(p1);
-       CHECK_BLOCKS("misc", p1, 1);
-       CHECK_BLOCKS("misc", root, 2);
-       talloc_free(p1);
+       CHECK_BLOCKS("misc", NULL, 7);
        CHECK_BLOCKS("misc", p1, 1);
        CHECK_BLOCKS("misc", root, 2);
        talloc_unlink(NULL, p1);
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 1);
        CHECK_BLOCKS("misc", root, 2);
        p2 = talloc_strdup(p1, "foo");
@@ -384,28 +395,38 @@ static bool test_misc(void)
        torture_assert("misc", talloc_unlink(p1, p2) == 0,
                "failed: talloc_unlink() of parent should succeed\n");
        talloc_free(p1);
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 1);
-       CHECK_BLOCKS("misc", root, 2);
+       CHECK_BLOCKS("misc", root, 1);
+       talloc_unlink(NULL, p1);
+       CHECK_BLOCKS("misc", NULL, 5);
+       CHECK_BLOCKS("misc", p1, 1);
+       CHECK_BLOCKS("misc", root, 1);
 
        name = talloc_set_name(p1, "my name is %s", "foo");
        torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
                "failed: wrong name after talloc_set_name(my name is foo)");
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 2);
-       CHECK_BLOCKS("misc", root, 3);
+       CHECK_BLOCKS("misc", root, 1);
 
        talloc_set_name_const(p1, NULL);
        torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
                "failed: wrong name after talloc_set_name(NULL)");
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 2);
-       CHECK_BLOCKS("misc", root, 3);
+       CHECK_BLOCKS("misc", root, 1);
 
        torture_assert("misc", talloc_free(NULL) == -1, 
                                   "talloc_free(NULL) should give -1\n");
 
        talloc_set_destructor(p1, fail_destructor);
-       torture_assert("misc", talloc_free(p1) == -1, 
-               "Failed destructor should cause talloc_free to fail\n");
+       torture_assert("misc", talloc_unlink(NULL, p1) == -1,
+               "Failed destructor should cause talloc_unlink to fail\n");
        talloc_set_destructor(p1, NULL);
+       CHECK_BLOCKS("misc", NULL, 6);
+       CHECK_BLOCKS("misc", p1, 2);
+       CHECK_BLOCKS("misc", root, 1);
 
        talloc_report(root, stderr);
 
@@ -439,13 +460,18 @@ static bool test_misc(void)
        d = talloc_realloc(p1, d, double, 0x20000000);
        torture_assert("misc", !d, "failed: integer overflow not detected\n");
 
-       talloc_free(p1);
+       CHECK_BLOCKS("misc", NULL, 7);
+       talloc_unlink(NULL, p1);
+       CHECK_BLOCKS("misc", NULL, 3);
        CHECK_BLOCKS("misc", root, 1);
 
        p1 = talloc_named(root, 100, "%d bytes", 100);
+       CHECK_BLOCKS("misc", NULL, 5);
        CHECK_BLOCKS("misc", p1, 2);
        CHECK_BLOCKS("misc", root, 3);
        talloc_unlink(root, p1);
+       CHECK_BLOCKS("misc", NULL, 3);
+       CHECK_BLOCKS("misc", root, 1);
 
        p1 = talloc_init("%d bytes", 200);
        p2 = talloc_asprintf(p1, "my test '%s'", "string");
@@ -765,16 +791,25 @@ static bool test_unref_reparent(void)
        p2 = talloc_named_const(root, 1, "parent by reference");
 
        c1 = talloc_named_const(p1, 1, "child");
+       CHECK_BLOCKS("unref_reparent", root, 4);
+       CHECK_SIZE("unref_reparent", root, 3);
        talloc_reference(p2, c1);
+       CHECK_BLOCKS("unref_reparent", root, 5);
+       CHECK_SIZE("unref_reparent", root, 3);
 
+       CHECK_BLOCKS("unref_reparent", root, 5);
+       CHECK_SIZE("unref_reparent", root, 3);
        CHECK_PARENT("unref_reparent", c1, p1);
 
        talloc_free(p1);
 
-       CHECK_PARENT("unref_reparent", c1, p2);
+       CHECK_BLOCKS("unref_reparent", root, 3);
+       CHECK_SIZE("unref_reparent", root, 1);
+       CHECK_NOT_PARENT("unref_reparent", c1, p2);
 
        talloc_unlink(p2, c1);
 
+       CHECK_BLOCKS("unref_reparent", root, 2);
        CHECK_SIZE("unref_reparent", root, 1);
 
        talloc_free(p2);
@@ -856,23 +891,38 @@ static bool test_speed(void)
 
 static bool test_lifeless(void)
 {
-       void *top = talloc_new(NULL);
+       void *top = talloc_named_const(NULL, 0, "top");
        char *parent, *child; 
-       void *child_owner = talloc_new(NULL);
+       void *child_owner = talloc_named_const(NULL, 0, "child_owner");
 
        printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
 
        parent = talloc_strdup(top, "parent");
+       CHECK_BLOCKS("lifeless", NULL, 5);
        child = talloc_strdup(parent, "child");  
+       CHECK_BLOCKS("lifeless", NULL, 6);
        (void)talloc_reference(child, parent);
+       CHECK_BLOCKS("lifeless", NULL, 7);
        (void)talloc_reference(child_owner, child); 
-       talloc_report_full(top, stderr);
+       CHECK_BLOCKS("lifeless", NULL, 8);
+       CHECK_BLOCKS("lifeless", top, 4);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
        talloc_unlink(top, parent);
+       CHECK_BLOCKS("lifeless", NULL, 8);
+       CHECK_BLOCKS("lifeless", top, 1);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
+       CHECK_BLOCKS("lifeless", child, 2);
        talloc_free(child);
-       talloc_report_full(top, stderr);
+       CHECK_BLOCKS("lifeless", NULL, 8);
+       CHECK_BLOCKS("lifeless", top, 1);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
+       CHECK_BLOCKS("lifeless", child, 2);
        talloc_free(top);
+       CHECK_BLOCKS("lifeless", NULL, 7);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
+       CHECK_BLOCKS("lifeless", child, 2);
        talloc_free(child_owner);
-       talloc_free(child);
+       CHECK_BLOCKS("lifeless", NULL, 2);
 
        printf("success: lifeless\n");
        return true;
@@ -1103,6 +1153,12 @@ static bool test_pool(void)
        return true;
 }
 
+static void test_reset(void)
+{
+       talloc_disable_null_tracking();
+       talloc_enable_null_tracking();
+}
+
 struct torture_context;
 bool torture_local_talloc(struct torture_context *tctx)
 {
@@ -1110,33 +1166,53 @@ bool torture_local_talloc(struct torture_context *tctx)
 
        setlinebuf(stdout);
 
-       talloc_disable_null_tracking();
-       talloc_enable_null_tracking();
-
+       test_reset();
        ret &= test_ref1();
+       test_reset();
        ret &= test_ref2();
+       test_reset();
        ret &= test_ref3();
+       test_reset();
        ret &= test_ref4();
+       test_reset();
        ret &= test_unlink1(); 
+       test_reset();
        ret &= test_misc();
+       test_reset();
        ret &= test_realloc();
+       test_reset();
        ret &= test_realloc_child(); 
+       test_reset();
        ret &= test_steal(); 
+       test_reset();
        ret &= test_move(); 
+       test_reset();
        ret &= test_unref_reparent();
+       test_reset();
        ret &= test_realloc_fn(); 
+       test_reset();
        ret &= test_type();
+       test_reset();
        ret &= test_lifeless(); 
+       test_reset();
        ret &= test_loop();
+       test_reset();
        ret &= test_free_parent_deny_child(); 
+       test_reset();
        ret &= test_talloc_ptrtype();
+       test_reset();
        ret &= test_talloc_free_in_destructor();
+       test_reset();
        ret &= test_pool();
 
        if (ret) {
+               test_reset();
                ret &= test_speed();
        }
+       test_reset();
        ret &= test_autofree();
 
+       test_reset();
+
        return ret;
 }