run-test_ref.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Unix SMB/CIFS implementation.
  3. local testing of talloc routines.
  4. Copyright (C) Andrew Tridgell 2004
  5. Converted to ccan tests by Rusty Russell 2008
  6. ** NOTE! The following LGPL license applies to the talloc
  7. ** library. This does NOT imply that all of Samba is released
  8. ** under the LGPL
  9. This library is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU Lesser General Public
  11. License as published by the Free Software Foundation; either
  12. version 2 of the License, or (at your option) any later version.
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. Lesser General Public License for more details.
  17. You should have received a copy of the GNU Lesser General Public
  18. License along with this library; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <ccan/failtest/failtest_override.h>
  22. #include <ccan/talloc/talloc.c>
  23. #include <stdbool.h>
  24. #include <ccan/tap/tap.h>
  25. #include <ccan/failtest/failtest.h>
  26. #define torture_assert(test, expr, str) \
  27. ok(expr, "%s [\n%s: Expression %s failed: %s\n]\n", \
  28. test, __location__, #expr, str)
  29. #define torture_assert_str_equal(test, arg1, arg2, desc) \
  30. ok(strcmp(arg1, arg2) == 0, \
  31. "%s [\n%s: Expected %s, got %s: %s\n]\n", \
  32. test, __location__, arg1, arg2, desc)
  33. #define CHECK_SIZE(test, ptr, tsize) \
  34. ok(talloc_total_size(ptr) == (tsize), \
  35. "%s [\nwrong '%s' tree size: got %u expected %u\n]\n", \
  36. test, #ptr, \
  37. (unsigned)talloc_total_size(ptr), \
  38. (unsigned)tsize)
  39. #define CHECK_BLOCKS(test, ptr, tblocks) \
  40. ok(talloc_total_blocks(ptr) == (tblocks), \
  41. "%s [\nwrong '%s' tree blocks: got %u expected %u\n]\n", \
  42. test, #ptr, \
  43. (unsigned)talloc_total_blocks(ptr), \
  44. (unsigned)tblocks)
  45. #define CHECK_PARENT(test, ptr, parent) \
  46. ok(talloc_parent(ptr) == (parent), \
  47. "%s [\n'%s' has wrong parent: got %p expected %p\n]\n", \
  48. test, #ptr, \
  49. talloc_parent(ptr), \
  50. (parent))
  51. struct torture_context;
  52. /*
  53. test references
  54. */
  55. static bool test_ref1(const struct torture_context *ctx)
  56. {
  57. void *root, *p1, *p2, *ref, *r1;
  58. bool ret = false;
  59. root = talloc_named_const(ctx, 0, "root");
  60. if (!root)
  61. goto out;
  62. p1 = talloc_named_const(root, 1, "p1");
  63. if (!p1)
  64. goto out;
  65. p2 = talloc_named_const(p1, 1, "p2");
  66. if (!p2)
  67. goto out;
  68. if (!talloc_named_const(p1, 1, "x1"))
  69. goto out;
  70. if (!talloc_named_const(p1, 2, "x2"))
  71. goto out;
  72. if (!talloc_named_const(p1, 3, "x3"))
  73. goto out;
  74. r1 = talloc_named_const(root, 1, "r1");
  75. if (!r1)
  76. goto out;
  77. ref = talloc_reference(r1, p2);
  78. if (!ref)
  79. goto out;
  80. CHECK_BLOCKS("ref1", p1, 5);
  81. CHECK_BLOCKS("ref1", p2, 1);
  82. CHECK_BLOCKS("ref1", r1, 2);
  83. talloc_free(p2);
  84. CHECK_BLOCKS("ref1", p1, 5);
  85. CHECK_BLOCKS("ref1", p2, 1);
  86. CHECK_BLOCKS("ref1", r1, 1);
  87. talloc_free(p1);
  88. CHECK_BLOCKS("ref1", r1, 1);
  89. talloc_free(r1);
  90. if (talloc_reference(root, NULL)) {
  91. return false;
  92. }
  93. CHECK_BLOCKS("ref1", root, 1);
  94. CHECK_SIZE("ref1", root, 0);
  95. ret = true;
  96. out:
  97. talloc_free(root);
  98. return ret;
  99. }
  100. /*
  101. test references
  102. */
  103. static bool test_ref2(const struct torture_context *ctx)
  104. {
  105. void *root, *p1, *p2, *ref, *r1;
  106. bool ret = false;
  107. root = talloc_named_const(ctx, 0, "root");
  108. if (!root)
  109. goto out;
  110. p1 = talloc_named_const(root, 1, "p1");
  111. if (!p1)
  112. goto out;
  113. if (!talloc_named_const(p1, 1, "x1"))
  114. goto out;
  115. if (!talloc_named_const(p1, 1, "x2"))
  116. goto out;
  117. if (!talloc_named_const(p1, 1, "x3"))
  118. goto out;
  119. p2 = talloc_named_const(p1, 1, "p2");
  120. if (!p2)
  121. goto out;
  122. r1 = talloc_named_const(root, 1, "r1");
  123. if (!r1)
  124. goto out;
  125. ref = talloc_reference(r1, p2);
  126. if (!ref)
  127. goto out;
  128. CHECK_BLOCKS("ref2", p1, 5);
  129. CHECK_BLOCKS("ref2", p2, 1);
  130. CHECK_BLOCKS("ref2", r1, 2);
  131. talloc_free(ref);
  132. CHECK_BLOCKS("ref2", p1, 5);
  133. CHECK_BLOCKS("ref2", p2, 1);
  134. CHECK_BLOCKS("ref2", r1, 1);
  135. talloc_free(p2);
  136. CHECK_BLOCKS("ref2", p1, 4);
  137. CHECK_BLOCKS("ref2", r1, 1);
  138. talloc_free(p1);
  139. CHECK_BLOCKS("ref2", r1, 1);
  140. talloc_free(r1);
  141. CHECK_SIZE("ref2", root, 0);
  142. ret = true;
  143. out:
  144. talloc_free(root);
  145. return ret;
  146. }
  147. /*
  148. test references
  149. */
  150. static bool test_ref3(const struct torture_context *ctx)
  151. {
  152. void *root, *p1, *p2, *ref, *r1;
  153. bool ret = false;
  154. root = talloc_named_const(ctx, 0, "root");
  155. if (!root)
  156. goto out;
  157. p1 = talloc_named_const(root, 1, "p1");
  158. if (!p1)
  159. goto out;
  160. p2 = talloc_named_const(root, 1, "p2");
  161. if (!p2)
  162. goto out;
  163. r1 = talloc_named_const(p1, 1, "r1");
  164. if (!r1)
  165. goto out;
  166. ref = talloc_reference(p2, r1);
  167. if (!ref)
  168. goto out;
  169. CHECK_BLOCKS("ref3", p1, 2);
  170. CHECK_BLOCKS("ref3", p2, 2);
  171. CHECK_BLOCKS("ref3", r1, 1);
  172. talloc_free(p1);
  173. CHECK_BLOCKS("ref3", p2, 2);
  174. CHECK_BLOCKS("ref3", r1, 1);
  175. talloc_free(p2);
  176. CHECK_SIZE("ref3", root, 0);
  177. ret = true;
  178. out:
  179. talloc_free(root);
  180. return ret;
  181. }
  182. /*
  183. test references
  184. */
  185. static bool test_ref4(const struct torture_context *ctx)
  186. {
  187. void *root, *p1, *p2, *ref, *r1;
  188. bool ret = false;
  189. root = talloc_named_const(ctx, 0, "root");
  190. if (!root)
  191. goto out;
  192. p1 = talloc_named_const(root, 1, "p1");
  193. if (!p1)
  194. goto out;
  195. if (!talloc_named_const(p1, 1, "x1"))
  196. goto out;
  197. if (!talloc_named_const(p1, 1, "x2"))
  198. goto out;
  199. if (!talloc_named_const(p1, 1, "x3"))
  200. goto out;
  201. p2 = talloc_named_const(p1, 1, "p2");
  202. if (!p2)
  203. goto out;
  204. r1 = talloc_named_const(root, 1, "r1");
  205. if (!r1)
  206. goto out;
  207. ref = talloc_reference(r1, p2);
  208. if (!ref)
  209. goto out;
  210. CHECK_BLOCKS("ref4", p1, 5);
  211. CHECK_BLOCKS("ref4", p2, 1);
  212. CHECK_BLOCKS("ref4", r1, 2);
  213. talloc_free(r1);
  214. CHECK_BLOCKS("ref4", p1, 5);
  215. CHECK_BLOCKS("ref4", p2, 1);
  216. talloc_free(p2);
  217. CHECK_BLOCKS("ref4", p1, 4);
  218. talloc_free(p1);
  219. CHECK_SIZE("ref4", root, 0);
  220. ret = true;
  221. out:
  222. talloc_free(root);
  223. return ret;
  224. }
  225. int main(int argc, char *argv[])
  226. {
  227. plan_tests(34);
  228. failtest_init(argc, argv);
  229. talloc_enable_null_tracking();
  230. if (null_context) {
  231. ok1(test_ref1(NULL)
  232. && test_ref2(NULL)
  233. && test_ref3(NULL)
  234. && test_ref4(NULL));
  235. /* This closes the leak, but don't free any other leaks! */
  236. ok1(!talloc_chunk_from_ptr(null_context)->child);
  237. talloc_disable_null_tracking();
  238. }
  239. failtest_exit(exit_status());
  240. }