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)) { \
root = talloc_new(NULL);
- p1 = talloc(root, 0x7fffffff);
+ p1 = talloc_size(root, 0x7fffffff);
if (p1) {
printf("failed: large talloc allowed\n");
return False;
talloc_report(root, stdout);
- p2 = talloc_zero(p1, 20);
+ p2 = talloc_zero_size(p1, 20);
if (p2[19] != 0) {
printf("Failed to give zero memory\n");
return False;
CHECK_BLOCKS(p1, 3);
talloc_free(p2);
- d = talloc_array_p(p1, double, 0x20000000);
+ d = talloc_array(p1, double, 0x20000000);
if (d) {
printf("failed: integer overflow not detected\n");
return False;
}
- d = talloc_realloc_p(p1, d, double, 0x20000000);
+ d = talloc_realloc(p1, d, double, 0x20000000);
if (d) {
printf("failed: integer overflow not detected\n");
return False;
root = talloc_new(NULL);
- p1 = talloc(root, 10);
+ p1 = talloc_size(root, 10);
CHECK_SIZE(p1, 10);
- p1 = talloc_realloc(NULL, p1, 20);
+ p1 = talloc_realloc_size(NULL, p1, 20);
CHECK_SIZE(p1, 20);
- talloc(p1, 0);
+ talloc_new(p1);
- p2 = talloc_realloc(p1, NULL, 30);
+ p2 = talloc_realloc_size(p1, NULL, 30);
- talloc(p1, 0);
+ talloc_new(p1);
- p2 = talloc_realloc(p1, p2, 40);
+ p2 = talloc_realloc_size(p1, p2, 40);
CHECK_SIZE(p2, 40);
CHECK_SIZE(root, 60);
CHECK_BLOCKS(p1, 4);
- p1 = talloc_realloc(NULL, p1, 20);
+ p1 = talloc_realloc_size(NULL, p1, 20);
CHECK_SIZE(p1, 60);
talloc_increase_ref_count(p2);
- if (talloc_realloc(NULL, p2, 5) != NULL) {
+ if (talloc_realloc_size(NULL, p2, 5) != NULL) {
printf("failed: talloc_realloc() on a referenced pointer should fail\n");
return False;
}
CHECK_BLOCKS(p1, 4);
- talloc_realloc(NULL, p2, 0);
- talloc_realloc(NULL, p2, 0);
+ talloc_realloc_size(NULL, p2, 0);
+ talloc_realloc_size(NULL, p2, 0);
CHECK_BLOCKS(p1, 3);
- if (talloc_realloc(NULL, p1, 0x7fffffff) != NULL) {
+ if (talloc_realloc_size(NULL, p1, 0x7fffffff) != NULL) {
printf("failed: oversize talloc should fail\n");
return False;
}
- talloc_realloc(NULL, p1, 0);
+ talloc_realloc_size(NULL, p1, 0);
CHECK_BLOCKS(root, 1);
CHECK_SIZE(root, 0);
int count;
struct el2 {
const char *name;
- } **list;
+ } **list, **list2, **list3;
} *el1;
struct el2 *el2;
printf("TESTING REALLOC WITH CHILD\n");
- root = talloc(NULL, 0);
+ root = talloc_new(NULL);
- el1 = talloc_p(root, struct el1);
- el1->list = talloc_p(el1, struct el2 *);
- el1->list[0] = talloc_p(el1->list, struct el2);
+ el1 = talloc(root, struct el1);
+ 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_p(el1->list, struct el2);
+ 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;
- el1->list = talloc_realloc_p(el1, el1->list, struct el2 *, 2);
- el1->list[1] = el2;
+ printf("TESTING talloc type checking\n");
+
+ root = talloc_new(NULL);
+
+ el1 = talloc(root, struct el1);
+
+ el1->count = 1;
+
+ 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);
printf("TESTING STEAL\n");
- root = talloc(NULL, 0);
+ root = talloc_new(NULL);
- p1 = talloc_array_p(root, char, 10);
+ p1 = talloc_array(root, char, 10);
CHECK_SIZE(p1, 10);
- p2 = talloc_realloc_p(root, NULL, char, 20);
+ p2 = talloc_realloc(root, NULL, char, 20);
CHECK_SIZE(p1, 10);
CHECK_SIZE(root, 30);
talloc_free(root);
- p1 = talloc(NULL, 3);
+ 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(NULL, 0);
+ root = talloc_new(NULL);
p1 = talloc_realloc_fn(root, NULL, 10);
CHECK_BLOCKS(root, 2);
*/
static BOOL test_speed(void)
{
- void *ctx = talloc(NULL, 0);
+ void *ctx = talloc_new(NULL);
unsigned count;
struct timeval tv;
count = 0;
do {
void *p1, *p2, *p3;
- p1 = talloc(ctx, count);
+ p1 = talloc_size(ctx, count);
p2 = talloc_strdup(p1, "foo bar");
- p3 = talloc(p1, 300);
+ p3 = talloc_size(p1, 300);
talloc_free(p1);
count += 3;
} while (timeval_elapsed(&tv) < 5.0);
}
-BOOL torture_local_talloc(void)
+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_unlink(top, parent);
+ talloc_free(child);
+ talloc_report_full(top, stdout);
+ talloc_free(top);
+ return True;
+}
+
+
+BOOL torture_local_talloc(struct torture_context *torture)
{
BOOL ret = True;
ret &= test_realloc_child();
ret &= test_steal();
ret &= test_unref_reparent();
- ret &= test_ldb();
+ ret &= test_realloc_fn();
+ ret &= test_type();
+ ret &= test_lifeless();
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;