run-test_loop.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. static int loop_destructor_count;
  53. static int test_loop_destructor(char *ptr)
  54. {
  55. loop_destructor_count++;
  56. return 0;
  57. }
  58. static bool test_loop(const struct torture_context *ctx)
  59. {
  60. void *top = talloc_new(ctx);
  61. char *parent;
  62. struct req1 {
  63. char *req2, *req3;
  64. } *req1;
  65. bool ret = false;
  66. if (!top)
  67. goto out;
  68. parent = talloc_strdup(top, "parent");
  69. if (!parent)
  70. goto out;
  71. req1 = talloc(parent, struct req1);
  72. if (!req1)
  73. goto out;
  74. req1->req2 = talloc_strdup(req1, "req2");
  75. if (!req1->req2)
  76. goto out;
  77. talloc_set_destructor(req1->req2, test_loop_destructor);
  78. req1->req3 = talloc_strdup(req1, "req3");
  79. if (!req1->req3)
  80. goto out;
  81. (void)talloc_reference(req1->req3, req1);
  82. talloc_free(parent);
  83. torture_assert("loop", loop_destructor_count == 1,
  84. "FAILED TO FIRE LOOP DESTRUCTOR\n");
  85. loop_destructor_count = 0;
  86. ret = true;
  87. out:
  88. talloc_free(top);
  89. return ret;
  90. }
  91. int main(int argc, char *argv[])
  92. {
  93. plan_tests(3);
  94. failtest_init(argc, argv);
  95. talloc_enable_null_tracking();
  96. if (null_context) {
  97. ok1(test_loop(NULL));
  98. /* This closes the leak, but don't free any other leaks! */
  99. ok1(!talloc_chunk_from_ptr(null_context)->child);
  100. talloc_disable_null_tracking();
  101. }
  102. failtest_exit(exit_status());
  103. }