Copyright (C) Andrew Tridgell 2004
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
+ ** NOTE! The following LGPL license applies to the talloc
+ ** library. This does NOT imply that all of Samba is released
+ ** under the LGPL
- This program is distributed in the hope that it will be useful,
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#ifdef _SAMBA_BUILD_
-#include "includes.h"
-#else
+#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+
+#ifdef HAVE_STDARG_H
#include <stdarg.h>
+#endif
+
#include <sys/time.h>
#include <time.h>
+
#include "talloc.h"
+
+#ifndef False
+#define False 0
+#endif
+#ifndef True
+#define True 1
+#endif
+#ifndef BOOL
+#define BOOL int
#endif
-/* the test suite can be built standalone, or as part of Samba */
-#ifndef _SAMBA_BUILD_
-typedef enum {False=0,True=1} BOOL;
+struct torture_context;
static struct timeval timeval_current(void)
{
return (tv2.tv_sec - tv->tv_sec) +
(tv2.tv_usec - tv->tv_usec)*1.0e-6;
}
-#endif /* _SAMBA_BUILD_ */
+#if SAMBA_VERSION_MAJOR<4
+#ifdef malloc
+#undef malloc
+#endif
+#ifdef strdup
+#undef strdup
+#endif
+#endif
#define CHECK_SIZE(ptr, tsize) do { \
if (talloc_total_size(ptr) != (tsize)) { \
} \
} while (0)
+#define CHECK_PARENT(ptr, parent) do { \
+ if (talloc_parent(ptr) != (parent)) { \
+ printf(__location__ " failed: '%s' has wrong parent: got %p expected %p\n", \
+ #ptr, \
+ talloc_parent(ptr), \
+ (parent)); \
+ talloc_report_full(ptr, stdout); \
+ talloc_report_full(parent, stdout); \
+ talloc_report_full(NULL, stdout); \
+ return False; \
+ } \
+} while (0)
+
/*
test references
void *root, *p1;
char *p2;
double *d;
+ const char *name;
printf("TESTING MISCELLANEOUS\n");
CHECK_BLOCKS(p1, 1);
CHECK_BLOCKS(root, 2);
- talloc_set_name(p1, "my name is %s", "foo");
+ name = talloc_set_name(p1, "my name is %s", "foo");
if (strcmp(talloc_get_name(p1), "my name is foo") != 0) {
- printf("failed: wrong name after talloc_set_name\n");
+ printf("failed: wrong name after talloc_set_name(my name is foo) - '%s'=>'%s'\n",
+ (name?name:"NULL"), talloc_get_name(p1));
return False;
}
CHECK_BLOCKS(p1, 2);
talloc_set_name_const(p1, NULL);
if (strcmp(talloc_get_name(p1), "UNNAMED") != 0) {
- printf("failed: wrong name after talloc_set_name(NULL)\n");
+ printf("failed: wrong name after talloc_set_name(NULL) - '%s'\n",
+ talloc_get_name(p1));
return False;
}
CHECK_BLOCKS(p1, 2);
p1 = talloc_init("%d bytes", 200);
p2 = talloc_asprintf(p1, "my test '%s'", "string");
+ if (strcmp(p2, "my test 'string'") != 0) {
+ printf("failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"\n", p2);
+ return False;
+ }
CHECK_BLOCKS(p1, 3);
CHECK_SIZE(p2, 17);
CHECK_BLOCKS(root, 1);
int count;
struct el2 {
const char *name;
- } **list;
+ } **list, **list2, **list3;
} *el1;
struct el2 *el2;
el1->list = talloc(el1, struct el2 *);
el1->list[0] = talloc(el1->list, struct el2);
el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
+
+ el1->list2 = talloc(el1, struct el2 *);
+ el1->list2[0] = talloc(el1->list2, struct el2);
+ el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
+
+ el1->list3 = talloc(el1, struct el2 *);
+ el1->list3[0] = talloc(el1->list3, struct el2);
+ el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
el2 = talloc(el1->list, struct el2);
+ el2 = talloc(el1->list2, struct el2);
+ el2 = talloc(el1->list3, struct el2);
+
+ el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
+ el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
+ el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
+
+ talloc_free(root);
+
+ return True;
+}
+
+
+/*
+ test type checking
+*/
+static BOOL test_type(void)
+{
+ void *root;
+ struct el1 {
+ int count;
+ };
+ struct el2 {
+ int count;
+ };
+ struct el1 *el1;
+
+ printf("TESTING talloc type checking\n");
+
+ root = talloc_new(NULL);
+
+ el1 = talloc(root, struct el1);
+
+ el1->count = 1;
- el1->list = talloc_realloc(el1, el1->list, struct el2 *, 2);
- el1->list[1] = el2;
+ if (talloc_get_type(el1, struct el1) != el1) {
+ printf("type check failed on el1\n");
+ return False;
+ }
+ if (talloc_get_type(el1, struct el2) != NULL) {
+ printf("type check failed on el1 with el2\n");
+ return False;
+ }
+ talloc_set_type(el1, struct el2);
+ if (talloc_get_type(el1, struct el2) != (struct el2 *)el1) {
+ printf("type set failed on el1 with el2\n");
+ return False;
+ }
talloc_free(root);
talloc_free(root);
p1 = talloc_size(NULL, 3);
+ talloc_report_full(NULL, stdout);
CHECK_SIZE(NULL, 3);
talloc_free(p1);
}
/*
- test ldb alloc fn
+ test talloc_realloc_fn
*/
-static BOOL test_ldb(void)
+static BOOL test_realloc_fn(void)
{
void *root, *p1;
- printf("TESTING LDB\n");
+ printf("TESTING talloc_realloc_fn\n");
root = talloc_new(NULL);
c1 = talloc_named_const(p1, 1, "child");
talloc_reference(p2, c1);
+ CHECK_PARENT(c1, p1);
+
talloc_free(p1);
+
+ CHECK_PARENT(c1, p2);
+
talloc_unlink(p2, c1);
CHECK_SIZE(root, 1);
}
-BOOL torture_local_talloc(void)
+static BOOL test_lifeless(void)
+{
+ char *top = talloc_new(NULL);
+ char *parent, *child;
+ char *child_owner = talloc_new(NULL);
+
+ printf("TESTING TALLOC_UNLINK LOOP\n");
+
+ parent = talloc_strdup(top, "parent");
+ child = talloc_strdup(parent, "child");
+ talloc_reference(child, parent);
+ talloc_reference(child_owner, child);
+ talloc_report_full(top, stdout);
+ talloc_unlink(top, parent);
+ talloc_free(child);
+ talloc_report_full(top, stdout);
+ talloc_free(top);
+ talloc_free(child_owner);
+ talloc_free(child);
+
+ return True;
+}
+
+static int loop_destructor_count;
+
+static int test_loop_destructor(char *ptr)
+{
+ printf("loop destructor\n");
+ loop_destructor_count++;
+ return 0;
+}
+
+static BOOL test_loop(void)
+{
+ char *top = talloc_new(NULL);
+ char *parent;
+ struct req1 {
+ char *req2, *req3;
+ } *req1;
+
+ printf("TESTING TALLOC LOOP DESTRUCTION\n");
+ parent = talloc_strdup(top, "parent");
+ req1 = talloc(parent, struct req1);
+ req1->req2 = talloc_strdup(req1, "req2");
+ talloc_set_destructor(req1->req2, test_loop_destructor);
+ req1->req3 = talloc_strdup(req1, "req3");
+ talloc_reference(req1->req3, req1);
+ talloc_report_full(top, stdout);
+ talloc_free(parent);
+ talloc_report_full(top, stdout);
+ talloc_report_full(NULL, stdout);
+ talloc_free(top);
+
+ if (loop_destructor_count != 1) {
+ printf("FAILED TO FIRE LOOP DESTRUCTOR\n");
+ return False;
+ }
+
+ return True;
+}
+
+static int fail_destructor_str(char *ptr)
+{
+ return -1;
+}
+
+static BOOL test_free_parent_deny_child(void)
+{
+ char *top = talloc_new(NULL);
+ char *level1;
+ char *level2;
+ char *level3;
+
+ printf("TESTING TALLOC FREE PARENT DENY CHILD\n");
+ level1 = talloc_strdup(top, "level1");
+ level2 = talloc_strdup(level1, "level2");
+ level3 = talloc_strdup(level2, "level3");
+
+ talloc_set_destructor(level3, fail_destructor_str);
+ talloc_free(level1);
+ talloc_set_destructor(level3, NULL);
+
+ CHECK_PARENT(level3, top);
+
+ talloc_free(top);
+
+ return True;
+}
+
+static BOOL test_talloc_ptrtype(void)
+{
+ BOOL ret = True;
+ char *top = talloc_new(NULL);
+ struct struct1 {
+ int foo;
+ int bar;
+ } *s1, *s2, **s3, ***s4;
+ const char *location1;
+ const char *location2;
+ const char *location3;
+ const char *location4;
+
+ printf("TESTING TALLOC PTRTYPE\n");
+ s1 = talloc_ptrtype(top, s1);location1 = __location__;
+
+ if (talloc_get_size(s1) != sizeof(struct struct1)) {
+ printf("%s: talloc_ptrtype() allocated the wrong size %u (should be %u)\n",
+ __location__, talloc_get_size(s1), sizeof(struct struct1));
+ ret = False;
+ }
+
+ if (strcmp(location1, talloc_get_name(s1)) != 0) {
+ printf("%s: talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+ __location__, talloc_get_name(s1), location1);
+ ret = False;
+ }
+
+ s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
+
+ if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
+ printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
+ __location__, talloc_get_size(s2), (sizeof(struct struct1)*10));
+ ret = False;
+ }
+
+ if (strcmp(location2, talloc_get_name(s2)) != 0) {
+ printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+ __location__, talloc_get_name(s2), location2);
+ ret = False;
+ }
+
+ s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
+
+ if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
+ printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
+ __location__, talloc_get_size(s3), (sizeof(struct struct1 *)*10));
+ ret = False;
+ }
+
+ if (strcmp(location3, talloc_get_name(s3)) != 0) {
+ printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+ __location__, talloc_get_name(s3), location3);
+ ret = False;
+ }
+
+ s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
+
+ if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
+ printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
+ __location__, talloc_get_size(s4), (sizeof(struct struct1 **)*10));
+ ret = False;
+ }
+
+ if (strcmp(location4, talloc_get_name(s4)) != 0) {
+ printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+ __location__, talloc_get_name(s4), location4);
+ ret = False;
+ }
+
+ talloc_free(top);
+
+ return ret;
+}
+
+BOOL torture_local_talloc(struct torture_context *torture)
{
BOOL ret = True;
+ talloc_enable_null_tracking();
+
ret &= test_ref1();
ret &= test_ref2();
ret &= test_ref3();
ret &= test_realloc_child();
ret &= test_steal();
ret &= test_unref_reparent();
- ret &= test_ldb();
+ ret &= test_realloc_fn();
+ ret &= test_type();
+ ret &= test_lifeless();
+ ret &= test_loop();
+ ret &= test_free_parent_deny_child();
+ ret &= test_talloc_ptrtype();
if (ret) {
ret &= test_speed();
}
-#ifndef _SAMBA_BUILD_
+#if !defined(_SAMBA_BUILD_) || ((SAMBA_VERSION_MAJOR==3)&&(SAMBA_VERSION_MINOR<9))
int main(void)
{
- if (!torture_local_talloc()) {
- printf("ERROR: TESTSUIE FAILED\n");
+ if (!torture_local_talloc(NULL)) {
+ printf("ERROR: TESTSUITE FAILED\n");
return -1;
}
return 0;