summaryrefslogtreecommitdiffstats
path: root/lib/talloc/testsuite.c
blob: 282ebc6956db29b2a23cefac22515f4a7c44fdef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
/* 
   Unix SMB/CIFS implementation.

   local testing of talloc routines.

   Copyright (C) Andrew Tridgell 2004
   
     ** NOTE! The following LGPL license applies to the talloc
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL
   
   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 3 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
   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, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/time.h"
#include <talloc.h>

#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif

#include <unistd.h>
#include <sys/wait.h>

#ifdef NDEBUG
#undef NDEBUG
#endif

#include <assert.h>

#include "talloc_testsuite.h"

static struct timeval private_timeval_current(void)
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return tv;
}

static double private_timeval_elapsed(struct timeval *tv)
{
	struct timeval tv2 = private_timeval_current();
	return (tv2.tv_sec - tv->tv_sec) + 
	       (tv2.tv_usec - tv->tv_usec)*1.0e-6;
}

#define torture_assert(test, expr, str) if (!(expr)) { \
	printf("failure: %s [\n%s: Expression %s failed: %s\n]\n", \
		test, __location__, #expr, str); \
	return false; \
}

#define torture_assert_str_equal(test, arg1, arg2, desc) \
	if (arg1 == NULL && arg2 == NULL) { /* OK, both NULL == equal */ \
	} else if (arg1 == NULL || arg2 == NULL) {			\
		return false;						\
	} else if (strcmp(arg1, arg2)) {			\
		printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
		   test, __location__, arg1, arg2, desc); \
		return false; \
	}

#define CHECK_SIZE(test, ptr, tsize) do { \
	if (talloc_total_size(ptr) != (tsize)) { \
		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); \
		return false; \
	} \
} while (0)

#define CHECK_BLOCKS(test, ptr, tblocks) do { \
	if (talloc_total_blocks(ptr) != (tblocks)) { \
		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); \
		return false; \
	} \
} while (0)

#define CHECK_PARENT(test, ptr, parent) do { \
	if (talloc_parent(ptr) != (parent)) { \
		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); \
		talloc_report_full(parent, stdout); \
		talloc_report_full(NULL, stdout); \
		return false; \
	} \
} while (0)

static unsigned int test_abort_count;

#if 0
static void test_abort_fn(const char *reason)
{
	printf("# test_abort_fn(%s)\n", reason);
	test_abort_count++;
}

static void test_abort_start(void)
{
	test_abort_count = 0;
	talloc_set_abort_fn(test_abort_fn);
}
#endif

static void test_abort_stop(void)
{
	test_abort_count = 0;
	talloc_set_abort_fn(NULL);
}

static void test_log_stdout(const char *message)
{
	fprintf(stdout, "%s", message);
}

/*
  test references 
*/
static bool test_ref1(void)
{
	void *root, *p1, *p2, *ref, *r1;

	printf("test: ref1\n# SINGLE REFERENCE FREE\n");

	root = talloc_named_const(NULL, 0, "root");
	p1 = talloc_named_const(root, 1, "p1");
	p2 = talloc_named_const(p1, 1, "p2");
	talloc_named_const(p1, 1, "x1");
	talloc_named_const(p1, 2, "x2");
	talloc_named_const(p1, 3, "x3");

	r1 = talloc_named_const(root, 1, "r1");	
	ref = talloc_reference(r1, p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref1", p1, 5);
	CHECK_BLOCKS("ref1", p2, 1);
	CHECK_BLOCKS("ref1", ref, 1);
	CHECK_BLOCKS("ref1", r1, 2);

	fprintf(stderr, "Freeing p2\n");
	talloc_unlink(r1, p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref1", p1, 5);
	CHECK_BLOCKS("ref1", p2, 1);
	CHECK_BLOCKS("ref1", r1, 1);

	fprintf(stderr, "Freeing p1\n");
	talloc_free(p1);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref1", r1, 1);

	fprintf(stderr, "Freeing r1\n");
	talloc_free(r1);
	talloc_report_full(NULL, stderr);

	fprintf(stderr, "Testing NULL\n");
	if (talloc_reference(root, NULL)) {
		return false;
	}

	CHECK_BLOCKS("ref1", root, 1);

	CHECK_SIZE("ref1", root, 0);

	talloc_free(root);
	printf("success: ref1\n");
	return true;
}

/*
  test references 
*/
static bool test_ref2(void)
{
	void *root, *p1, *p2, *ref, *r1;

	printf("test: ref2\n# DOUBLE REFERENCE FREE\n");
	root = talloc_named_const(NULL, 0, "root");
	p1 = talloc_named_const(root, 1, "p1");
	talloc_named_const(p1, 1, "x1");
	talloc_named_const(p1, 1, "x2");
	talloc_named_const(p1, 1, "x3");
	p2 = talloc_named_const(p1, 1, "p2");

	r1 = talloc_named_const(root, 1, "r1");	
	ref = talloc_reference(r1, p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref2", p1, 5);
	CHECK_BLOCKS("ref2", p2, 1);
	CHECK_BLOCKS("ref2", r1, 2);

	fprintf(stderr, "Freeing ref\n");
	talloc_unlink(r1, ref);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref2", p1, 5);
	CHECK_BLOCKS("ref2", p2, 1);
	CHECK_BLOCKS("ref2", r1, 1);

	fprintf(stderr, "Freeing p2\n");
	talloc_free(p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref2", p1, 4);
	CHECK_BLOCKS("ref2", r1, 1);

	fprintf(stderr, "Freeing p1\n");
	talloc_free(p1);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref2", r1, 1);

	fprintf(stderr, "Freeing r1\n");
	talloc_free(r1);
	talloc_report_full(root, stderr);

	CHECK_SIZE("ref2", root, 0);

	talloc_free(root);
	printf("success: ref2\n");
	return true;
}

/*
  test references 
*/
static bool test_ref3(void)
{
	void *root, *p1, *p2, *ref, *r1;

	printf("test: ref3\n# PARENT REFERENCE FREE\n");

	root = talloc_named_const(NULL, 0, "root");
	p1 = talloc_named_const(root, 1, "p1");
	p2 = talloc_named_const(root, 1, "p2");
	r1 = talloc_named_const(p1, 1, "r1");
	ref = talloc_reference(p2, r1);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref3", p1, 2);
	CHECK_BLOCKS("ref3", p2, 2);
	CHECK_BLOCKS("ref3", r1, 1);
	CHECK_BLOCKS("ref3", ref, 1);

	fprintf(stderr, "Freeing p1\n");
	talloc_free(p1);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref3", p2, 2);
	CHECK_BLOCKS("ref3", r1, 1);

	fprintf(stderr, "Freeing p2\n");
	talloc_free(p2);
	talloc_report_full(root, stderr);

	CHECK_SIZE("ref3", root, 0);

	talloc_free(root);

	printf("success: ref3\n");
	return true;
}

/*
  test references 
*/
static bool test_ref4(void)
{
	void *root, *p1, *p2, *ref, *r1;

	printf("test: ref4\n# REFERRER REFERENCE FREE\n");

	root = talloc_named_const(NULL, 0, "root");
	p1 = talloc_named_const(root, 1, "p1");
	talloc_named_const(p1, 1, "x1");
	talloc_named_const(p1, 1, "x2");
	talloc_named_const(p1, 1, "x3");
	p2 = talloc_named_const(p1, 1, "p2");

	r1 = talloc_named_const(root, 1, "r1");	
	ref = talloc_reference(r1, p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref4", p1, 5);
	CHECK_BLOCKS("ref4", p2, 1);
	CHECK_BLOCKS("ref4", ref, 1);
	CHECK_BLOCKS("ref4", r1, 2);

	fprintf(stderr, "Freeing r1\n");
	talloc_free(r1);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref4", p1, 5);
	CHECK_BLOCKS("ref4", p2, 1);

	fprintf(stderr, "Freeing p2\n");
	talloc_free(p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("ref4", p1, 4);

	fprintf(stderr, "Freeing p1\n");
	talloc_free(p1);
	talloc_report_full(root, stderr);

	CHECK_SIZE("ref4", root, 0);

	talloc_free(root);

	printf("success: ref4\n");
	return true;
}


/*
  test references 
*/
static bool test_unlink1(void)
{
	void *root, *p1, *p2, *ref, *r1;

	printf("test: unlink\n# UNLINK\n");

	root = talloc_named_const(NULL, 0, "root");
	p1 = talloc_named_const(root, 1, "p1");
	talloc_named_const(p1, 1, "x1");
	talloc_named_const(p1, 1, "x2");
	talloc_named_const(p1, 1, "x3");
	p2 = talloc_named_const(p1, 1, "p2");

	r1 = talloc_named_const(p1, 1, "r1");	
	ref = talloc_reference(r1, p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("unlink", p1, 7);
	CHECK_BLOCKS("unlink", p2, 1);
	CHECK_BLOCKS("unlink", ref, 1);
	CHECK_BLOCKS("unlink", r1, 2);

	fprintf(stderr, "Unreferencing r1\n");
	talloc_unlink(r1, p2);
	talloc_report_full(root, stderr);

	CHECK_BLOCKS("unlink", p1, 6);
	CHECK_BLOCKS("unlink", p2, 1);
	CHECK_BLOCKS("unlink", r1, 1);

	fprintf(stderr, "Freeing p1\n");
	talloc_free(p1);
	talloc_report_full(root, stderr);

	CHECK_SIZE("unlink", root, 0);

	talloc_free(root);

	printf("success: unlink\n");
	return true;
}

static int fail_destructor(void *ptr)
{
	return -1;
}

/*
  miscellaneous tests to try to get a higher test coverage percentage
*/
static bool test_misc(void)
{
	void *root, *p1;
	char *p2;
	double *d;
	const char *name;

	printf("test: misc\n# MISCELLANEOUS\n");

	root = talloc_new(NULL);

	p1 = talloc_size(root, 0x7fffffff);
	torture_assert("misc", !p1, "failed: large talloc allowed\n");

	p1 = talloc_strdup(root, "foo");
	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_unlink(NULL, p1);
	CHECK_BLOCKS("misc", p1, 1);
	CHECK_BLOCKS("misc", root, 2);
	talloc_unlink(NULL, p1);
	CHECK_BLOCKS("misc", p1, 1);
	CHECK_BLOCKS("misc", root, 2);
	p2 = talloc_strdup(p1, "foo");
	torture_assert("misc", talloc_unlink(root, p2) == -1,
				   "failed: talloc_unlink() of non-reference context should return -1\n");
	torture_assert("misc", talloc_unlink(p1, p2) == 0,
		"failed: talloc_unlink() of parent should succeed\n");
	talloc_unlink(NULL, p1);
	CHECK_BLOCKS("misc", p1, 1);
	CHECK_BLOCKS("misc", root, 2);

	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)");
	torture_assert_str_equal("misc", talloc_get_name(p1), name,
		"failed: wrong name after talloc_set_name(my name is foo)");
	CHECK_BLOCKS("misc", p1, 2);
	CHECK_BLOCKS("misc", root, 3);

	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", p1, 2);
	CHECK_BLOCKS("misc", root, 3);

	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");
	talloc_set_destructor(p1, NULL);

	talloc_report(root, stderr);


	p2 = (char *)talloc_zero_size(p1, 20);
	torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
	talloc_free(p2);

	torture_assert("misc", talloc_strdup(root, NULL) == NULL,
		"failed: strdup on NULL should give NULL\n");

	p2 = talloc_strndup(p1, "foo", 2);
	torture_assert("misc", strcmp("fo", p2) == 0, 
				   "strndup doesn't work\n");
	p2 = talloc_asprintf_append_buffer(p2, "o%c", 'd');
	torture_assert("misc", strcmp("food", p2) == 0, 
				   "talloc_asprintf_append_buffer doesn't work\n");
	CHECK_BLOCKS("misc", p2, 1);
	CHECK_BLOCKS("misc", p1, 3);

	p2 = talloc_asprintf_append_buffer(NULL, "hello %s", "world");
	torture_assert("misc", strcmp("hello world", p2) == 0,
		"talloc_asprintf_append_buffer doesn't work\n");
	CHECK_BLOCKS("misc", p2, 1);
	CHECK_BLOCKS("misc", p1, 3);
	talloc_free(p2);

	d = talloc_array(p1, double, 0x20000000);
	torture_assert("misc", !d, "failed: integer overflow not detected\n");

	d = talloc_realloc(p1, d, double, 0x20000000);
	torture_assert("misc", !d, "failed: integer overflow not detected\n");

	talloc_free(p1);
	CHECK_BLOCKS("misc", root, 1);

	p1 = talloc_named(root, 100, "%d bytes", 100);
	CHECK_BLOCKS("misc", p1, 2);
	CHECK_BLOCKS("misc", root, 3);
	talloc_unlink(root, p1);

	p1 = talloc_init("%d bytes", 200);
	p2 = talloc_asprintf(p1, "my test '%s'", "string");
	torture_assert_str_equal("misc", p2, "my test 'string'",
		"failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
	CHECK_BLOCKS("misc", p1, 3);
	CHECK_SIZE("misc", p2, 17);
	CHECK_BLOCKS("misc", root, 1);
	talloc_unlink(NULL, p1);

	p1 = talloc_named_const(root, 10, "p1");
	p2 = (char *)talloc_named_const(root, 20, "p2");
	(void)talloc_reference(p1, p2);
	talloc_report_full(root, stderr);
	talloc_unlink(root, p2);
	talloc_report_full(root, stderr);
	CHECK_BLOCKS("misc", p2, 1);
	CHECK_BLOCKS("misc", p1, 2);
	CHECK_BLOCKS("misc", root, 3);
	talloc_unlink(p1, p2);
	talloc_unlink(root, p1);

	p1 = talloc_named_const(root, 10, "p1");
	p2 = (char *)talloc_named_const(root, 20, "p2");
	(void)talloc_reference(NULL, p2);
	talloc_report_full(root, stderr);
	talloc_unlink(root, p2);
	talloc_report_full(root, stderr);
	CHECK_BLOCKS("misc", p2, 1);
	CHECK_BLOCKS("misc", p1, 1);
	CHECK_BLOCKS("misc", root, 2);
	talloc_unlink(NULL, p2);
	talloc_unlink(root, p1);

	/* Test that talloc_unlink is a no-op */

	torture_assert("misc", talloc_unlink(root, NULL) == -1,
		"failed: talloc_unlink(root, NULL) == -1\n");

	talloc_report(root, stderr);
	talloc_report(NULL, stderr);

	CHECK_SIZE("misc", root, 0);

	talloc_free(root);

	CHECK_SIZE("misc", NULL, 0);

	talloc_enable_null_tracking_no_autofree();
	talloc_enable_leak_report();
	talloc_enable_leak_report_full();

	printf("success: misc\n");

	return true;
}


/*
  test realloc
*/
static bool test_realloc(void)
{
	void *root, *p1, *p2;

	printf("test: realloc\n# REALLOC\n");

	root = talloc_new(NULL);

	p1 = talloc_size(root, 10);
	CHECK_SIZE("realloc", p1, 10);

	p1 = talloc_realloc_size(NULL, p1, 20);
	CHECK_SIZE("realloc", p1, 20);

	talloc_new(p1);

	p2 = talloc_realloc_size(p1, NULL, 30);

	talloc_new(p1);

	p2 = talloc_realloc_size(p1, p2, 40);

	CHECK_SIZE("realloc", p2, 40);
	CHECK_SIZE("realloc", root, 60);
	CHECK_BLOCKS("realloc", p1, 4);

	p1 = talloc_realloc_size(NULL, p1, 20);
	CHECK_SIZE("realloc", p1, 60);

	talloc_increase_ref_count(p2);
	torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
		"failed: talloc_realloc() on a referenced pointer should fail\n");
	CHECK_BLOCKS("realloc", p1, 4);

	talloc_realloc_size(NULL, p2, 0);
	talloc_realloc_size(NULL, p2, 0);
	CHECK_BLOCKS("realloc", p1, 4);
	talloc_realloc_size(p1, p2, 0);
	CHECK_BLOCKS("realloc", p1, 3);

	torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
		"failed: oversize talloc should fail\n");

	talloc_realloc_size(NULL, p1, 0);
	CHECK_BLOCKS("realloc", root, 4);
	talloc_realloc_size(root, p1, 0);
	CHECK_BLOCKS("realloc", root, 1);

	CHECK_SIZE("realloc", root, 0);

	talloc_free(root);

	printf("success: realloc\n");

	return true;
}

/*
  test realloc with a child
*/
static bool test_realloc_child(void)
{
	void *root;
	struct el2 {
		const char *name;
	} *el2, *el2_2, *el2_3, **el_list_save;
	struct el1 {
		int count;
		struct el2 **list, **list2, **list3;
	} *el1;

	printf("test: REALLOC WITH CHILD\n");

	root = talloc_new(NULL);

	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(el1->list, struct el2);
	CHECK_PARENT("el2", el2, el1->list);
	el2_2 = talloc(el1->list2, struct el2);
	CHECK_PARENT("el2", el2_2, el1->list2);
	el2_3 = talloc(el1->list3, struct el2);
	CHECK_PARENT("el2", el2_3, el1->list3);

	el_list_save = el1->list;
	el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
	if (el1->list == el_list_save) {
		printf("failure: talloc_realloc didn't move pointer");
		return false;
	}

	CHECK_PARENT("el1_after_realloc", el1->list, el1);
	el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
	CHECK_PARENT("el1_after_realloc", el1->list2, el1);
	el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
	CHECK_PARENT("el1_after_realloc", el1->list3, el1);

	CHECK_PARENT("el2", el2, el1->list);
	CHECK_PARENT("el2", el2_2, el1->list2);
	CHECK_PARENT("el2", el2_3, el1->list3);

	/* Finally check realloc with multiple children */
	el1 = talloc_realloc(root, el1, struct el1, 100);
	CHECK_PARENT("el1->list", el1->list, el1);
	CHECK_PARENT("el1->list2", el1->list2, el1);
	CHECK_PARENT("el1->list3", el1->list3, el1);

	talloc_free(root);

	printf("success: REALLOC WITH CHILD\n");
	return true;
}

/*
  test type checking
*/
static bool test_type(void)
{
	void *root;
	struct el1 {
		int count;
	};
	struct el2 {
		int count;
	};
	struct el1 *el1;

	printf("test: type\n# talloc type checking\n");

	root = talloc_new(NULL);

	el1 = talloc(root, struct el1);

	el1->count = 1;

	torture_assert("type", talloc_get_type(el1, struct el1) == el1,
		"type check failed on el1\n");
	torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
		"type check failed on el1 with el2\n");
	talloc_set_type(el1, struct el2);
	torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
		"type set failed on el1 with el2\n");

	talloc_free(root);

	printf("success: type\n");
	return true;
}

/*
  test steal
*/
static bool test_steal(void)
{
	void *root, *p1, *p2;

	printf("test: steal\n# STEAL\n");

	root = talloc_new(NULL);

	p1 = talloc_array(root, char, 10);
	CHECK_SIZE("steal", p1, 10);

	p2 = talloc_realloc(root, NULL, char, 20);
	CHECK_SIZE("steal", p1, 10);
	CHECK_SIZE("steal", root, 30);

	torture_assert("steal", talloc_steal(p1, NULL) == NULL,
		"failed: stealing NULL should give NULL\n");

	torture_assert("steal", talloc_steal(p1, p1) == p1,
		"failed: stealing to ourselves is a nop\n");
	CHECK_BLOCKS("steal", root, 3);
	CHECK_SIZE("steal", root, 30);

	talloc_steal(NULL, p1);
	talloc_steal(NULL, p2);
	CHECK_BLOCKS("steal", root, 1);
	CHECK_SIZE("steal", root, 0);

	talloc_free(p1);
	talloc_steal(root, p2);
	CHECK_BLOCKS("steal", root, 2);
	CHECK_SIZE("steal", root, 20);
	
	talloc_free(p2);

	CHECK_BLOCKS("steal", root, 1);
	CHECK_SIZE("steal", root, 0);

	talloc_free(root);

	p1 = talloc_size(NULL, 3);
	talloc_report_full(NULL, stderr);
	CHECK_SIZE("steal", NULL, 3);
	talloc_free(p1);

	printf("success: steal\n");
	return true;
}

/*
  test move
*/
static bool test_move(void)
{
	void *root;
	struct t_move {
		char *p;
		int *x;
	} *t1, *t2;

	printf("test: move\n# MOVE\n");

	root = talloc_new(NULL);

	t1 = talloc(root, struct t_move);
	t2 = talloc(root, struct t_move);
	t1->p = talloc_strdup(t1, "foo");
	t1->x = talloc(t1, int);
	*t1->x = 42;

	t2->p = talloc_move(t2, &t1->p);
	t2->x = talloc_move(t2, &t1->x);
	torture_assert("move", t1->p == NULL && t1->x == NULL &&
	    strcmp(t2->p, "foo") == 0 && *t2->x == 42,
		"talloc move failed");

	talloc_free(root);

	printf("success: move\n");

	return true;
}

/*
  test talloc_realloc_fn
*/
static bool test_realloc_fn(void)
{
	void *root, *p1;

	printf("test: realloc_fn\n# talloc_realloc_fn\n");

	root = talloc_new(NULL);

	p1 = talloc_realloc_fn(root, NULL, 10);
	CHECK_BLOCKS("realloc_fn", root, 2);
	CHECK_SIZE("realloc_fn", root, 10);
	p1 = talloc_realloc_fn(root, p1, 20);
	CHECK_BLOCKS("realloc_fn", root, 2);
	CHECK_SIZE("realloc_fn", root, 20);
	p1 = talloc_realloc_fn(root, p1, 0);
	CHECK_BLOCKS("realloc_fn", root, 1);
	CHECK_SIZE("realloc_fn", root, 0);

	talloc_free(root);

	printf("success: realloc_fn\n");
	return true;
}


static bool test_unref_reparent(void)
{
	void *root, *p1, *p2, *c1;

	printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");

	root = talloc_named_const(NULL, 0, "root");
	p1 = talloc_named_const(root, 1, "orig parent");
	p2 = talloc_named_const(root, 1, "parent by reference");

	c1 = talloc_named_const(p1, 1, "child");
	talloc_reference(p2, c1);

	CHECK_PARENT("unref_reparent", c1, p1);

	talloc_free(p1);

	CHECK_PARENT("unref_reparent", c1, p2);

	talloc_unlink(p2, c1);

	CHECK_SIZE("unref_reparent", root, 1);

	talloc_free(p2);
	talloc_free(root);

	printf("success: unref_reparent\n");
	return true;
}

/*
  measure the speed of talloc versus malloc
*/
static bool test_speed(void)
{
	void *ctx = talloc_new(NULL);
	unsigned count;
	const int loop = 1000;
	int i;
	struct timeval tv;

	printf("test: speed\n# TALLOC VS MALLOC SPEED\n");

	tv = private_timeval_current();
	count = 0;
	do {
		void *p1, *p2, *p3;
		for (i=0;i<loop;i++) {
			p1 = talloc_size(ctx, loop % 100);
			p2 = talloc_strdup(p1, "foo bar");
			p3 = talloc_size(p1, 300);
			(void)p2;
			(void)p3;
			talloc_free(p1);
		}
		count += 3 * loop;
	} while (private_timeval_elapsed(&tv) < 5.0);

	fprintf(stderr, "talloc: %.0f ops/sec\n", count/private_timeval_elapsed(&tv));

	talloc_free(ctx);

	ctx = talloc_pool(NULL, 1024);

	tv = private_timeval_current();
	count = 0;
	do {
		void *p1, *p2, *p3;
		for (i=0;i<loop;i++) {
			p1 = talloc_size(ctx, loop % 100);
			p2 = talloc_strdup(p1, "foo bar");
			p3 = talloc_size(p1, 300);
			(void)p2;
			(void)p3;
			talloc_free(p1);
		}
		count += 3 * loop;
	} while (private_timeval_elapsed(&tv) < 5.0);

	talloc_free(ctx);

	fprintf(stderr, "talloc_pool: %.0f ops/sec\n", count/private_timeval_elapsed(&tv));

	tv = private_timeval_current();
	count = 0;
	do {
		void *p1, *p2, *p3;
		for (i=0;i<loop;i++) {
			p1 = malloc(loop % 100);
			p2 = strdup("foo bar");
			p3 = malloc(300);
			free(p1);
			free(p2);
			free(p3);
		}
		count += 3 * loop;
	} while (private_timeval_elapsed(&tv) < 5.0);
	fprintf(stderr, "malloc: %.0f ops/sec\n", count/private_timeval_elapsed(&tv));

	printf("success: speed\n");

	return true;
}

static bool test_lifeless(void)
{
	void *top = talloc_new(NULL);
	char *parent, *child; 
	void *child_owner = talloc_new(NULL);

	printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");

	parent = talloc_strdup(top, "parent");
	child = talloc_strdup(parent, "child");  
	(void)talloc_reference(child, parent);
	(void)talloc_reference(child_owner, child); 
	talloc_report_full(top, stderr);
	talloc_unlink(top, parent);
	talloc_unlink(top, child);
	talloc_report_full(top, stderr);
	talloc_free(top);
	talloc_free(child_owner);
	talloc_free(child);

	printf("success: lifeless\n");
	return true;
}

static int loop_destructor_count;

static int test_loop_destructor(char *ptr)
{
	loop_destructor_count++;
	return 0;
}

static bool test_loop(void)
{
	void *top = talloc_new(NULL);
	char *parent;
	struct req1 {
		char *req2, *req3;
	} *req1;

	printf("test: loop\n# 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");
	(void)talloc_reference(req1->req3, req1);
	talloc_report_full(top, stderr);
	talloc_free(parent);
	talloc_report_full(top, stderr);
	talloc_report_full(NULL, stderr);
	talloc_free(top);

	torture_assert("loop", loop_destructor_count == 1, 
				   "FAILED TO FIRE LOOP DESTRUCTOR\n");
	loop_destructor_count = 0;

	printf("success: loop\n");
	return true;
}

static int realloc_parent_destructor_count;

static int test_realloc_parent_destructor(char *ptr)
{
	realloc_parent_destructor_count++;
	return 0;
}

static bool test_realloc_on_destructor_parent(void)
{
	void *top = talloc_new(NULL);
	char *parent;
	char *a, *b, *C, *D;
	realloc_parent_destructor_count = 0;

	printf("test: free_for_exit\n# TALLOC FREE FOR EXIT\n");

	parent = talloc_strdup(top, "parent");
	a = talloc_strdup(parent, "a");
	b = talloc_strdup(a, "b");
	C = talloc_strdup(a, "C");
	D = talloc_strdup(b, "D");
	talloc_set_destructor(D, test_realloc_parent_destructor);
	/* Capitalised ones have destructors.
	 *
	 * parent --> a -> b -> D
	 *              -> c
	 */

	a = talloc_realloc(parent, a, char, 2048);

	torture_assert("check talloc_realloc", a != NULL, "talloc_realloc failed");

	talloc_set_destructor(C, test_realloc_parent_destructor);
	/*
	 * parent --> a[2048] -> b -> D
	 *                    -> C
	 *
	 */

	talloc_free(parent);

	torture_assert("check destructor realloc_parent_destructor",
		       realloc_parent_destructor_count == 2,
		       "FAILED TO FIRE free_for_exit_destructor\n");


	printf("success: free_for_exit\n");
	talloc_free(top); /* make ASAN happy */

	return true;
}

static int fail_destructor_str(char *ptr)
{
	return -1;
}

static bool test_free_parent_deny_child(void)
{
	void *top = talloc_new(NULL);
	char *level1;
	char *level2;
	char *level3;

	printf("test: free_parent_deny_child\n# 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("free_parent_deny_child", level3, top);

	talloc_free(top);

	printf("success: free_parent_deny_child\n");
	return true;
}

struct new_parent {
	void *new_parent;
	char val[20];
};

static int reparenting_destructor(struct new_parent *np)
{
	talloc_set_destructor(np, NULL);
	(void)talloc_move(np->new_parent, &np);
	return -1;
}

static bool test_free_parent_reparent_child(void)
{
	void *top = talloc_new(NULL);
	char *level1;
	char *alternate_level1;
	char *level2;
	struct new_parent *level3;

	printf("test: free_parent_reparent_child\n# "
		"TALLOC FREE PARENT REPARENT CHILD\n");

	level1 = talloc_strdup(top, "level1");
	alternate_level1 = talloc_strdup(top, "alternate_level1");
	level2 = talloc_strdup(level1, "level2");
	level3 = talloc(level2, struct new_parent);
	level3->new_parent = alternate_level1;
	memset(level3->val, 'x', sizeof(level3->val));

	talloc_set_destructor(level3, reparenting_destructor);
	talloc_free(level1);

	CHECK_PARENT("free_parent_reparent_child",
		level3, alternate_level1);

	talloc_free(top);

	printf("success: free_parent_reparent_child\n");
	return true;
}

static bool test_free_parent_reparent_child_in_pool(void)
{
	void *top = talloc_new(NULL);
	char *level1;
	char *alternate_level1;
	char *level2;
	void *pool;
	struct new_parent *level3;

	printf("test: free_parent_reparent_child_in_pool\n# "
		"TALLOC FREE PARENT REPARENT CHILD IN POOL\n");

	pool = talloc_pool(top, 1024);
	level1 = talloc_strdup(pool, "level1");
	alternate_level1 = talloc_strdup(top, "alternate_level1");
	level2 = talloc_strdup(level1, "level2");
	level3 = talloc(level2, struct new_parent);
	level3->new_parent = alternate_level1;
	memset(level3->val, 'x', sizeof(level3->val));

	talloc_set_destructor(level3, reparenting_destructor);
	talloc_free(level1);
	talloc_set_destructor(level3, NULL);

	CHECK_PARENT("free_parent_reparent_child_in_pool",
		level3, alternate_level1);

	/* Even freeing alternate_level1 should leave pool alone. */
	talloc_free(alternate_level1);
	talloc_free(top);

	printf("success: free_parent_reparent_child_in_pool\n");
	return true;
}


static bool test_talloc_ptrtype(void)
{
	void *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("test: ptrtype\n# TALLOC PTRTYPE\n");

	s1 = talloc_ptrtype(top, s1);location1 = __location__;

	if (talloc_get_size(s1) != sizeof(struct struct1)) {
		printf("failure: ptrtype [\n"
		  "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
		  "]\n", (unsigned long)talloc_get_size(s1),
		           (unsigned long)sizeof(struct struct1));
		return false;
	}

	if (strcmp(location1, talloc_get_name(s1)) != 0) {
		printf("failure: ptrtype [\n"
		  "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
			talloc_get_name(s1), location1);
		return false;
	}

	s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;

	if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
		printf("failure: ptrtype [\n"
			   "talloc_array_ptrtype() allocated the wrong size "
		       "%lu (should be %lu)\n]\n",
			(unsigned long)talloc_get_size(s2),
		    (unsigned long)(sizeof(struct struct1)*10));
		return false;
	}

	if (strcmp(location2, talloc_get_name(s2)) != 0) {
		printf("failure: ptrtype [\n"
		"talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
			talloc_get_name(s2), location2);
		return false;
	}

	s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;

	if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
		printf("failure: ptrtype [\n"
			   "talloc_array_ptrtype() allocated the wrong size "
		       "%lu (should be %lu)\n]\n",
			   (unsigned long)talloc_get_size(s3),
		       (unsigned long)(sizeof(struct struct1 *)*10));
		return false;
	}

	torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
		"talloc_array_ptrtype() sets the wrong name");

	s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;

	if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
		printf("failure: ptrtype [\n"
		      "talloc_array_ptrtype() allocated the wrong size "
		       "%lu (should be %lu)\n]\n",
			   (unsigned long)talloc_get_size(s4),
		       (unsigned long)(sizeof(struct struct1 **)*10));
		return false;
	}

	torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
		"talloc_array_ptrtype() sets the wrong name");

	talloc_free(top);

	printf("success: ptrtype\n");
	return true;
}

static int _test_talloc_free_in_destructor(void **ptr)
{
	talloc_free(*ptr);
	return 0;
}

static bool test_talloc_free_in_destructor(void)
{
	void *level0;
	void *level1;
	void *level2;
	void *level3;
	void *level4;
	void **level5;

	printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");

	level0 = talloc_new(NULL);
	level1 = talloc_new(level0);
	level2 = talloc_new(level1);
	level3 = talloc_new(level2);
	level4 = talloc_new(level3);
	level5 = talloc(level4, void *);

	*level5 = level3;
	(void)talloc_reference(level0, level3);
	(void)talloc_reference(level3, level3);
	(void)talloc_reference(level5, level3);

	talloc_set_destructor(level5, _test_talloc_free_in_destructor);

	talloc_free(level1);

	talloc_free(level0);

	talloc_free(level3); /* make ASAN happy */

	printf("success: free_in_destructor\n");
	return true;
}

static bool test_autofree(void)
{
#if _SAMBA_BUILD_ < 4
	/* autofree test would kill smbtorture */
	void *p;
	printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");

	p = talloc_autofree_context();
	talloc_free(p);

	p = talloc_autofree_context();
	talloc_free(p);

	printf("success: autofree\n");
#endif
	return true;
}

static bool test_pool(void)
{
	void *pool;
	void *p1, *p2, *p3, *p4;
	void *p2_2;

	pool = talloc_pool(NULL, 1024);

	p1 = talloc_size(pool, 80);
	memset(p1, 0x11, talloc_get_size(p1));
	p2 = talloc_size(pool, 20);
	memset(p2, 0x11, talloc_get_size(p2));
	p3 = talloc_size(p1, 50);
	memset(p3, 0x11, talloc_get_size(p3));
	p4 = talloc_size(p3, 1000);
	memset(p4, 0x11, talloc_get_size(p4));

	p2_2 = talloc_realloc_size(pool, p2, 20+1);
	torture_assert("pool realloc 20+1", p2_2 == p2, "failed: pointer changed");
	memset(p2, 0x11, talloc_get_size(p2));
	p2_2 = talloc_realloc_size(pool, p2, 20-1);
	torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
	memset(p2, 0x11, talloc_get_size(p2));
	p2_2 = talloc_realloc_size(pool, p2, 20-1);
	torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
	memset(p2, 0x11, talloc_get_size(p2));

	talloc_free(p3);

	/* this should reclaim the memory of p4 and p3 */
	p2_2 = talloc_realloc_size(pool, p2, 400);
	torture_assert("pool realloc 400", p2_2 == p2, "failed: pointer changed");
	memset(p2, 0x11, talloc_get_size(p2));

	talloc_free(p1);

	/* this should reclaim the memory of p1 */
	p2_2 = talloc_realloc_size(pool, p2, 800);
	torture_assert("pool realloc 800", p2_2 == p1, "failed: pointer not changed");
	p2 = p2_2;
	memset(p2, 0x11, talloc_get_size(p2));

	/* this should do a malloc */
	p2_2 = talloc_realloc_size(pool, p2, 1800);
	torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
	p2 = p2_2;
	memset(p2, 0x11, talloc_get_size(p2));

	/* this should reclaim the memory from the pool */
	p3 = talloc_size(pool, 80);
	torture_assert("pool alloc 80", p3 == p1, "failed: pointer changed");
	memset(p3, 0x11, talloc_get_size(p3));

	talloc_free(p2);
	talloc_free(p3);

	p1 = talloc_size(pool, 80);
	memset(p1, 0x11, talloc_get_size(p1));
	p2 = talloc_size(pool, 20);
	memset(p2, 0x11, talloc_get_size(p2));

	talloc_free(p1);

	p2_2 = talloc_realloc_size(pool, p2, 20-1);
	torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
	memset(p2, 0x11, talloc_get_size(p2));
	p2_2 = talloc_realloc_size(pool, p2, 20-1);
	torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
	memset(p2, 0x11, talloc_get_size(p2));

	/* this should do a malloc */
	p2_2 = talloc_realloc_size(pool, p2, 1800);
	torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
	p2 = p2_2;
	memset(p2, 0x11, talloc_get_size(p2));

	/* this should reclaim the memory from the pool */
	p3 = talloc_size(pool, 800);
	torture_assert("pool alloc 800", p3 == p1, "failed: pointer changed");
	memset(p3, 0x11, talloc_get_size(p3));

	talloc_free(pool);

	return true;
}

static bool test_pool_steal(void)
{
	void *root;
	void *pool;
	void *p1, *p2;
	void *p1_2, *p2_2;
	size_t hdr;
	size_t ofs1, ofs2;

	root = talloc_new(NULL);
	pool = talloc_pool(root, 1024);

	p1 = talloc_size(pool, 4 * 16);
	torture_assert("pool allocate 4 * 16", p1 != NULL, "failed ");
	memset(p1, 0x11, talloc_get_size(p1));
	p2 = talloc_size(pool, 4 * 16);
	torture_assert("pool allocate 4 * 16", p2 > p1, "failed: !(p2 > p1) ");
	memset(p2, 0x11, talloc_get_size(p2));

	ofs1 = PTR_DIFF(p2, p1);
	hdr = ofs1 - talloc_get_size(p1);

	talloc_steal(root, p1);
	talloc_steal(root, p2);

	talloc_free(pool);

	p1_2 = p1;

	p1_2 = talloc_realloc_size(root, p1, 5 * 16);
	torture_assert("pool realloc 5 * 16", p1_2 > p2, "failed: pointer not changed");
	memset(p1_2, 0x11, talloc_get_size(p1_2));
	ofs1 = PTR_DIFF(p1_2, p2);
	ofs2 = talloc_get_size(p2) + hdr;

	torture_assert("pool realloc ", ofs1 == ofs2, "failed: pointer offset unexpected");

	p2_2 = talloc_realloc_size(root, p2, 3 * 16);
	torture_assert("pool realloc 5 * 16", p2_2 == p2, "failed: pointer changed");
	memset(p2_2, 0x11, talloc_get_size(p2_2));

	talloc_free(p1_2);

	p2_2 = p2;

	/* now we should reclaim the full pool */
	p2_2 = talloc_realloc_size(root, p2, 8 * 16);
	torture_assert("pool realloc 8 * 16", p2_2 == p1, "failed: pointer not expected");
	p2 = p2_2;
	memset(p2_2, 0x11, talloc_get_size(p2_2));

	/* now we malloc and free the full pool space */
	p2_2 = talloc_realloc_size(root, p2, 2 * 1024);
	torture_assert("pool realloc 2 * 1024", p2_2 != p1, "failed: pointer not expected");
	memset(p2_2, 0x11, talloc_get_size(p2_2));

	talloc_free(p2_2);

	talloc_free(root);

	return true;
}

static bool test_pool_nest(void)
{
	void *p1, *p2, *p3;
	void *e = talloc_new(NULL);

	p1 = talloc_pool(NULL, 1024);
	torture_assert("talloc_pool", p1 != NULL, "failed");

	p2 = talloc_pool(p1, 500);
	torture_assert("talloc_pool", p2 != NULL, "failed");

	p3 = talloc_size(p2, 10);

	talloc_steal(e, p3);

	talloc_free(p2);

	talloc_free(p3);

	talloc_free(p1);

	talloc_free(e); /* make ASAN happy */

	return true;
}

struct pooled {
	char *s1;
	char *s2;
	char *s3;
};

static bool test_pooled_object(void)
{
	struct pooled *p;
	const char *s1 = "hello";
	const char *s2 = "world";
	const char *s3 = "";

	p = talloc_pooled_object(NULL, struct pooled, 3,
			strlen(s1)+strlen(s2)+strlen(s3)+3);

	if (talloc_get_size(p) != sizeof(struct pooled)) {
		return false;
	}

	p->s1 = talloc_strdup(p, s1);

	TALLOC_FREE(p->s1);
	p->s1 = talloc_strdup(p, s2);
	TALLOC_FREE(p->s1);

	p->s1 = talloc_strdup(p, s1);
	p->s2 = talloc_strdup(p, s2);
	p->s3 = talloc_strdup(p, s3);

	TALLOC_FREE(p);
	return true;
}

static bool test_free_ref_null_context(void)
{
	void *p1, *p2, *p3;
	int ret;

	talloc_disable_null_tracking();
	p1 = talloc_new(NULL);
	p2 = talloc_new(NULL);

	p3 = talloc_reference(p2, p1);
	torture_assert("reference", p3 == p1, "failed: reference on null");

	ret = talloc_free(p1);
	torture_assert("ref free with null parent", ret == 0, "failed: free with null parent");
	talloc_free(p2);

	talloc_enable_null_tracking_no_autofree();
	p1 = talloc_new(NULL);
	p2 = talloc_new(NULL);

	p3 = talloc_reference(p2, p1);
	torture_assert("reference", p3 == p1, "failed: reference on null");

	ret = talloc_free(p1);
	torture_assert("ref free with null tracked parent", ret == 0, "failed: free with null parent");
	talloc_free(p2);

	return true;
}

static bool test_rusty(void)
{
	void *root;
	char *p1;

	talloc_enable_null_tracking();
	root = talloc_new(NULL);
	p1 = talloc_strdup(root, "foo");
	talloc_increase_ref_count(p1);
	talloc_report_full(root, stdout);
	talloc_free(root);
	CHECK_BLOCKS("null_context", NULL, 2);
	talloc_free(p1); /* make ASAN happy */

	return true;
}

static bool test_free_children(void)
{
	void *root;
	char *p1, *p2;
	const char *name, *name2;

	talloc_enable_null_tracking();
	root = talloc_new(NULL);
	p1 = talloc_strdup(root, "foo1");
	p2 = talloc_strdup(p1, "foo2");
	(void)p2;

	talloc_set_name(p1, "%s", "testname");
	talloc_free_children(p1);
	/* check its still a valid talloc ptr */
	talloc_get_size(talloc_get_name(p1));
	if (strcmp(talloc_get_name(p1), "testname") != 0) {
		return false;
	}

	talloc_set_name(p1, "%s", "testname");
	name = talloc_get_name(p1);
	talloc_free_children(p1);
	/* check its still a valid talloc ptr */
	talloc_get_size(talloc_get_name(p1));
	torture_assert("name", name == talloc_get_name(p1), "name ptr changed");
	torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname") == 0,
		       "wrong name");
	CHECK_BLOCKS("name1", p1, 2);

	/* note that this does not free the old child name */
	talloc_set_name_const(p1, "testname2");
	name2 = talloc_get_name(p1);
	/* but this does */
	talloc_free_children(p1);
	(void)name2;
	torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname2") == 0,
		       "wrong name");
	CHECK_BLOCKS("name1", p1, 1);

	talloc_report_full(root, stdout);
	talloc_free(root);
	return true;
}

static bool test_memlimit(void)
{
	void *root;
	char *l1, *l2, *l3, *l4, *l5, *t;
	char *pool;
	int i;

	printf("test: memlimit\n# MEMORY LIMITS\n");

	printf("==== talloc_new(NULL)\n");
	root = talloc_new(NULL);

	talloc_report_full(root, stdout);

	printf("==== talloc_size(root, 2048)\n");
	l1 = talloc_size(root, 2048);
	torture_assert("memlimit", l1 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_free(l1)\n");
	talloc_free(l1);

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(root, level 1)\n");
	l1 = talloc_strdup(root, "level 1");
	torture_assert("memlimit", l1 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_set_memlimit(l1, 2048)\n");
	torture_assert("memlimit", talloc_set_memlimit(l1, 2048) == 0,
		"failed: setting memlimit should never fail\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_size(root, 2048)\n");
	l2 = talloc_size(l1, 2048);
	torture_assert("memlimit", l2 == NULL,
		"failed: alloc should fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(l1, level 2)\n");
	l2 = talloc_strdup(l1, "level 2");
	torture_assert("memlimit", l2 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_free(l2)\n");
	talloc_free(l2);

	talloc_report_full(root, stdout);

	printf("==== talloc_size(NULL, 2048)\n");
	l2 = talloc_size(NULL, 2048);

	talloc_report_full(root, stdout);

	printf("==== talloc_steal(l1, l2)\n");
	talloc_steal(l1, l2);

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(l2, level 3)\n");
	l3 = talloc_strdup(l2, "level 3");
	torture_assert("memlimit", l3 == NULL,
		"failed: alloc should fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_free(l2)\n");
	talloc_free(l2);

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(NULL, level 2)\n");
	l2 = talloc_strdup(NULL, "level 2");
	talloc_steal(l1, l2);

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(l2, level 3)\n");
	l3 = talloc_strdup(l2, "level 3");
	torture_assert("memlimit", l3 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_set_memlimit(l3, 1024)\n");
	torture_assert("memlimit", talloc_set_memlimit(l3, 1024) == 0,
		"failed: setting memlimit should never fail\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(l3, level 4)\n");
	l4 = talloc_strdup(l3, "level 4");
	torture_assert("memlimit", l4 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_set_memlimit(l4, 512)\n");
	torture_assert("memlimit", talloc_set_memlimit(l4, 512) == 0,
		"failed: setting memlimit should never fail\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(l4, level 5)\n");
	l5 = talloc_strdup(l4, "level 5");
	torture_assert("memlimit", l5 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_realloc(NULL, l5, char, 600)\n");
	t = talloc_realloc(NULL, l5, char, 600);
	torture_assert("memlimit", t == NULL,
		"failed: alloc should fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_realloc(NULL, l5, char, 5)\n");
	l5 = talloc_realloc(NULL, l5, char, 5);
	torture_assert("memlimit", l5 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(l3, level 4)\n");
	l4 = talloc_strdup(l3, "level 4");
	torture_assert("memlimit", l4 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_set_memlimit(l4, 512)\n");
	torture_assert("memlimit", talloc_set_memlimit(l4, 512) == 0,
		"failed: setting memlimit should never fail\n");

	talloc_report_full(root, stdout);

	printf("==== talloc_strdup(l4, level 5)\n");
	l5 = talloc_strdup(l4, "level 5");
	torture_assert("memlimit", l5 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);

	printf("==== Make new temp context and steal l5\n");
	t = talloc_new(root);
	talloc_steal(t, l5);

	talloc_report_full(root, stdout);

	printf("==== talloc_size(t, 2048)\n");
	l1 = talloc_size(t, 2048);
	torture_assert("memlimit", l1 != NULL,
		"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(root, stdout);
	talloc_free(root);

	/* Test memlimits with pools. */
	printf("==== talloc_pool(NULL, 10*1024)\n");
	pool = talloc_pool(NULL, 10*1024);
	torture_assert("memlimit", pool != NULL,
		"failed: alloc should not fail due to memory limit\n");

	printf("==== talloc_set_memlimit(pool, 10*1024)\n");
	talloc_set_memlimit(pool, 10*1024);
	for (i = 0; i < 9; i++) {
		printf("==== talloc_size(pool, 1024) %i/10\n", i + 1);
		l1 = talloc_size(pool, 1024);
		torture_assert("memlimit", l1 != NULL,
			"failed: alloc should not fail due to memory limit\n");
		talloc_report_full(pool, stdout);
	}
	/* The next alloc should fail. */
	printf("==== talloc_size(pool, 1024) 10/10\n");
	l2 = talloc_size(pool, 1024);
	torture_assert("memlimit", l2 == NULL,
			"failed: alloc should fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	/* Moving one of the children shouldn't change the limit,
	   as it's still inside the pool. */

	printf("==== talloc_new(NULL)\n");
	root = talloc_new(NULL);

	printf("==== talloc_steal(root, l1)\n");
	talloc_steal(root, l1);

	printf("==== talloc_size(pool, 1024)\n");
	l2 = talloc_size(pool, 1024);
	torture_assert("memlimit", l2 == NULL,
			"failed: alloc should fail due to memory limit\n");

	printf("==== talloc_free_children(pool)\n");
	talloc_free(l1);
	talloc_free_children(pool);

	printf("==== talloc_size(pool, 1024)\n");
	l1 = talloc_size(pool, 1024);

	/* try reallocs of increasing size */
	for (i = 1; i < 9; i++) {
		printf("==== talloc_realloc_size(NULL, l1, %i*1024) %i/10\n", i, i + 1);
		l1 = talloc_realloc_size(NULL, l1, i*1024);
		torture_assert("memlimit", l1 != NULL,
			"failed: realloc should not fail due to memory limit\n");
		talloc_report_full(pool, stdout);
	}
	/* The next alloc should fail. */
	printf("==== talloc_realloc_size(NULL, l1, 10*1024) 10/10\n");
	l2 = talloc_realloc_size(NULL, l1, 10*1024);
	torture_assert("memlimit", l2 == NULL,
			"failed: realloc should fail due to memory limit\n");

	/* Increase the memlimit */
	printf("==== talloc_set_memlimit(pool, 11*1024)\n");
	talloc_set_memlimit(pool, 11*1024);

	/* The final realloc should still fail
	   as the entire realloced chunk needs to be moved out of the pool */
	printf("==== talloc_realloc_size(NULL, l1, 10*1024) 10/10\n");
	l2 = talloc_realloc_size(NULL, l1, 10*1024);
	torture_assert("memlimit", l2 == NULL,
			"failed: realloc should fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	printf("==== talloc_set_memlimit(pool, 21*1024)\n");
	talloc_set_memlimit(pool, 21*1024);

	/* There's now sufficient space to move the chunk out of the pool */
	printf("==== talloc_realloc_size(NULL, l1, 10*1024) 10/10\n");
	l2 = talloc_realloc_size(NULL, l1, 10*1024);
	torture_assert("memlimit", l2 != NULL,
			"failed: realloc should not fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	/* ...which should mean smaller allocations can now occur within the pool */
	printf("==== talloc_size(pool, 9*1024)\n");
	l1 = talloc_size(pool, 9*1024);
	torture_assert("memlimit", l1 != NULL,
			"failed: new allocations should be allowed in the pool\n");

	talloc_report_full(pool, stdout);

	/* But reallocs bigger than the pool will still fail */
	printf("==== talloc_realloc_size(NULL, l1, 10*1024)\n");
	l2 = talloc_realloc_size(NULL, l1, 10*1024);
	torture_assert("memlimit", l2 == NULL,
			"failed: realloc should fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	/* ..as well as allocs */
	printf("==== talloc_size(pool, 1024)\n");
	l1 = talloc_size(pool, 1024);
	torture_assert("memlimit", l1 == NULL,
			"failed: alloc should fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	printf("==== talloc_free_children(pool)\n");
	talloc_free_children(pool);

	printf("==== talloc_set_memlimit(pool, 1024)\n");
	talloc_set_memlimit(pool, 1024);

	/* We should still be able to allocate up to the pool limit
	   because the memlimit only applies to new heap allocations */
	printf("==== talloc_size(pool, 9*1024)\n");
	l1 = talloc_size(pool, 9*1024);
	torture_assert("memlimit", l1 != NULL,
			"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	l1 = talloc_size(pool, 1024);
	torture_assert("memlimit", l1 == NULL,
			"failed: alloc should fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	printf("==== talloc_free_children(pool)\n");
	talloc_free_children(pool);

	printf("==== talloc_set_memlimit(pool, 10*1024)\n");
	talloc_set_memlimit(pool, 10*1024);

	printf("==== talloc_size(pool, 1024)\n");
	l1 = talloc_size(pool, 1024);
	torture_assert("memlimit", l1 != NULL,
			"failed: alloc should not fail due to memory limit\n");

	talloc_report_full(pool, stdout);

	talloc_free(pool);
	talloc_free(root);
	printf("success: memlimit\n");

	return true;
}

#ifdef HAVE_PTHREAD

#define NUM_THREADS 100

/* Sync variables. */
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t condvar = PTHREAD_COND_INITIALIZER;
static void *intermediate_ptr;

/* Subthread. */
static void *thread_fn(void *arg)
{
	int ret;
	const char *ctx_name = (const char *)arg;
	void *sub_ctx = NULL;
	/*
	 * Do stuff that creates a new talloc hierarchy in
	 * this thread.
	 */
	void *top_ctx = talloc_named_const(NULL, 0, "top");
	if (top_ctx == NULL) {
		return NULL;
	}
	sub_ctx = talloc_named_const(top_ctx, 100, ctx_name);
	if (sub_ctx == NULL) {
		return NULL;
	}

	/*
	 * Now transfer a pointer from our hierarchy
	 * onto the intermediate ptr.
	 */
	ret = pthread_mutex_lock(&mtx);
	if (ret != 0) {
		talloc_free(top_ctx);
		return NULL;
	}
	/* Wait for intermediate_ptr to be free. */
	while (intermediate_ptr != NULL) {
		ret = pthread_cond_wait(&condvar, &mtx);
		if (ret != 0) {
			talloc_free(top_ctx);
			ret = pthread_mutex_unlock(&mtx);
			assert(ret == 0);
			return NULL;
		}
	}

	/* and move our memory onto it from our toplevel hierarchy. */
	intermediate_ptr = talloc_move(NULL, &sub_ctx);

	/* Tell the main thread it's ready for pickup. */
	pthread_cond_broadcast(&condvar);
	ret = pthread_mutex_unlock(&mtx);
	assert(ret == 0);

	talloc_free(top_ctx);
	return NULL;
}

/* Main thread. */
static bool test_pthread_talloc_passing(void)
{
	int i;
	int ret;
	char str_array[NUM_THREADS][20];
	pthread_t thread_id;
	void *mem_ctx;

	/*
	 * Important ! Null tracking breaks threaded talloc.
	 * It *must* be turned off.
	 */
	talloc_disable_null_tracking();

	printf("test: pthread_talloc_passing\n# PTHREAD TALLOC PASSING\n");

	/* Main thread toplevel context. */
	mem_ctx = talloc_named_const(NULL, 0, "toplevel");
	if (mem_ctx == NULL) {
		printf("failed to create toplevel context\n");
		return false;
	}

	/*
	 * Spin off NUM_THREADS threads.
	 * They will use their own toplevel contexts.
	 */
	for (i = 0; i < NUM_THREADS; i++) {
		ret = snprintf(str_array[i],
			       20,
			       "thread:%d",
			       i);
		if (ret < 0) {
			printf("snprintf %d failed\n", i);
			return false;
		}
		ret = pthread_create(&thread_id,
				NULL,
				thread_fn,
				str_array[i]);
		if (ret != 0) {
			printf("failed to create thread %d (%d)\n", i, ret);
			return false;
		}
	}

	printf("Created %d threads\n", NUM_THREADS);

	/* Now wait for NUM_THREADS transfers of the talloc'ed memory. */
	for (i = 0; i < NUM_THREADS; i++) {
		ret = pthread_mutex_lock(&mtx);
		if (ret != 0) {
			printf("pthread_mutex_lock %d failed (%d)\n", i, ret);
			talloc_free(mem_ctx);
			return false;
		}

		/* Wait for intermediate_ptr to have our data. */
		while (intermediate_ptr == NULL) {
			ret = pthread_cond_wait(&condvar, &mtx);
			if (ret != 0) {
				printf("pthread_cond_wait %d failed (%d)\n", i,
					ret);
				talloc_free(mem_ctx);
				ret = pthread_mutex_unlock(&mtx);
				assert(ret == 0);
			}
		}

		/* and move it onto our toplevel hierarchy. */
		(void)talloc_move(mem_ctx, &intermediate_ptr);

		/* Tell the sub-threads we're ready for another. */
		pthread_cond_broadcast(&condvar);
		ret = pthread_mutex_unlock(&mtx);
		assert(ret == 0);
	}

	CHECK_SIZE("pthread_talloc_passing", mem_ctx, NUM_THREADS * 100);
#if 1
	/* Dump the hierarchy. */
	talloc_report(mem_ctx, stdout);
#endif
	talloc_free(mem_ctx);
	printf("success: pthread_talloc_passing\n");
	return true;
}
#endif

static void test_magic_protection_abort(const char *reason)
{
	/* exit with errcode 42 to communicate successful test to the parent process */
	if (strcmp(reason, "Bad talloc magic value - unknown value") == 0) {
		_exit(42);
	} else {
		printf("talloc aborted for an unexpected reason\n");
	}
}

static int test_magic_protection_destructor(int *ptr)
{
	_exit(404); /* Not 42 */
}

static bool test_magic_protection(void)
{
	void *pool = talloc_pool(NULL, 1024);
	int *p1, *p2;
	pid_t pid;
	int exit_status;

	printf("test: magic_protection\n");
	p1 = talloc(pool, int);
	p2 = talloc(pool, int);

	/* To avoid complaints from the compiler assign values to the p1 & p2. */
	*p1 = 6;
	*p2 = 9;

	pid = fork();
	if (pid == 0) {
		talloc_set_abort_fn(test_magic_protection_abort);
		talloc_set_destructor(p2, test_magic_protection_destructor);

		/*
		 * Simulate a security attack
		 * by triggering a buffer overflow in memset to overwrite the
		 * constructor in the next pool chunk.
		 *
		 * Real attacks would attempt to set a real destructor.
		 */
		memset(p1, '\0', 32);

		/* Then the attack takes effect when the memory's freed. */
		talloc_free(pool);

		/* Never reached. Make compilers happy */
		return true;
	}

	while (wait(&exit_status) != pid);

	talloc_free(pool); /* make ASAN happy */

	if (!WIFEXITED(exit_status)) {
		printf("Child exited through unexpected abnormal means\n");
		return false;
	}
	if (WEXITSTATUS(exit_status) != 42) {
		printf("Child exited with wrong exit status\n");
		return false;
	}
	if (WIFSIGNALED(exit_status)) {
		printf("Child received unexpected signal\n");
		return false;
	}

	printf("success: magic_protection\n");
	return true;
}

static void test_magic_free_protection_abort(const char *reason)
{
	/* exit with errcode 42 to communicate successful test to the parent process */
	if (strcmp(reason, "Bad talloc magic value - access after free") == 0) {
		_exit(42);
	}
	/* not 42 */
	_exit(404);
}

static bool test_magic_free_protection(void)
{
	void *pool = talloc_pool(NULL, 1024);
	int *p1, *p2, *p3;
	pid_t pid;
	int exit_status;

	printf("test: magic_free_protection\n");
	p1 = talloc(pool, int);
	p2 = talloc(pool, int);

	/* To avoid complaints from the compiler assign values to the p1 & p2. */
	*p1 = 6;
	*p2 = 9;

	p3 = talloc_realloc(pool, p2, int, 2048);
	torture_assert("pool realloc 2048",
		       p3 != p2,
		       "failed: pointer not changed");

	/*
	 * Now access the memory in the pool after the realloc().  It
	 * should be marked as free, so use of the old pointer should
	 * trigger the abort function
	 */
	pid = fork();
	if (pid == 0) {
		talloc_set_abort_fn(test_magic_free_protection_abort);

		talloc_get_name(p2);

		/* Never reached. Make compilers happy */
		return true;
	}

	while (wait(&exit_status) != pid);

	if (!WIFEXITED(exit_status)) {
		printf("Child exited through unexpected abnormal means\n");
		return false;
	}
	if (WEXITSTATUS(exit_status) != 42) {
		printf("Child exited with wrong exit status\n");
		return false;
	}
	if (WIFSIGNALED(exit_status)) {
		printf("Child received unexpected signal\n");
		return false;
	}

	talloc_free(pool);

	printf("success: magic_free_protection\n");
	return true;
}

static void test_reset(void)
{
	talloc_set_log_fn(test_log_stdout);
	test_abort_stop();
	talloc_disable_null_tracking();
	talloc_enable_null_tracking_no_autofree();
}

bool torture_local_talloc(struct torture_context *tctx)
{
	bool ret = true;

	setlinebuf(stdout);

	test_reset();
	ret &= test_pooled_object();
	test_reset();
	ret &= test_pool_nest();
	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_realloc_on_destructor_parent();
	test_reset();
	ret &= test_free_parent_reparent_child();
	test_reset();
	ret &= test_free_parent_reparent_child_in_pool();
	test_reset();
	ret &= test_talloc_ptrtype();
	test_reset();
	ret &= test_talloc_free_in_destructor();
	test_reset();
	ret &= test_pool();
	test_reset();
	ret &= test_pool_steal();
	test_reset();
	ret &= test_free_ref_null_context();
	test_reset();
	ret &= test_rusty();
	test_reset();
	ret &= test_free_children();
	test_reset();
	ret &= test_memlimit();
#ifdef HAVE_PTHREAD
	test_reset();
	ret &= test_pthread_talloc_passing();
#endif


	if (ret) {
		test_reset();
		ret &= test_speed();
	}
	test_reset();
	ret &= test_autofree();
	test_reset();
	ret &= test_magic_protection();
	test_reset();
	ret &= test_magic_free_protection();

	test_reset();
	talloc_disable_null_tracking();
	return ret;
}