Cross Reference: /sssd/src/tests/common_check.c
common_check.c revision b1798ddcfb952efb7cc8bacaf51b3486a7fec121
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
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher/*
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher SSSD
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher Common utilities for check-based tests using talloc.
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher Authors:
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher Martin Nagy <mnagy@redhat.com>
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher Copyright (C) Red Hat, Inc 2009
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher This program is free software; you can redistribute it and/or modify
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher it under the terms of the GNU General Public License as published by
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher the Free Software Foundation; either version 3 of the License, or
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher (at your option) any later version.
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher This program is distributed in the hope that it will be useful,
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher GNU General Public License for more details.
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher You should have received a copy of the GNU General Public License
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher*/
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher#include <stdio.h>
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher#include <check.h>
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher#include "tests/common.h"
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher#include "util/util.h"
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher#include "util/dlinklist.h"
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen GallagherTALLOC_CTX *global_talloc_context = NULL;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagherstruct size_snapshot {
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher struct size_snapshot *prev;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher struct size_snapshot *next;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher TALLOC_CTX *ctx;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher size_t bytes_allocated;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher};
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagherstatic struct size_snapshot *snapshot_stack;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallaghervoid
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher_check_leaks(TALLOC_CTX *ctx, size_t bytes, const char *location)
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher{
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher size_t bytes_allocated;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher bytes_allocated = talloc_total_size(ctx);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher if (bytes_allocated != bytes) {
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher fprintf(stderr, "Leak report for %s:\n", location);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher talloc_report_full(ctx, stderr);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher fail("%s: memory leaks detected, %d bytes still allocated",
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher location, bytes_allocated - bytes);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher }
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher}
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallaghervoid
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallaghercheck_leaks_push(TALLOC_CTX *ctx)
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher{
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher struct size_snapshot *snapshot;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher snapshot = talloc(NULL, struct size_snapshot);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher snapshot->ctx = ctx;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher snapshot->bytes_allocated = talloc_total_size(ctx);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher DLIST_ADD(snapshot_stack, snapshot);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher}
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallaghervoid
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher_check_leaks_pop(TALLOC_CTX *ctx, const char *location)
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher{
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher struct size_snapshot *snapshot;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher TALLOC_CTX *old_ctx;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher size_t bytes_allocated;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher if (snapshot_stack == NULL) {
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher fail("%s: trying to pop an empty stack");
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher }
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher snapshot = snapshot_stack;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher DLIST_REMOVE(snapshot_stack, snapshot);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher old_ctx = snapshot->ctx;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher bytes_allocated = snapshot->bytes_allocated;
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher fail_if(old_ctx != ctx, "Bad push/pop order");
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher talloc_zfree(snapshot);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher _check_leaks(old_ctx, bytes_allocated, location);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher}
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallaghervoid
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagherleak_check_setup(void)
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher{
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher talloc_enable_null_tracking();
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher global_talloc_context = talloc_new(NULL);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher fail_unless(global_talloc_context != NULL, "talloc_new failed");
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher check_leaks_push(global_talloc_context);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher}
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallaghervoid
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagherleak_check_teardown(void)
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher{
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher check_leaks_pop(global_talloc_context);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher if (snapshot_stack != NULL) {
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher fail("Exiting with a non-empty stack");
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher }
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher check_leaks(global_talloc_context, 0);
b1798ddcfb952efb7cc8bacaf51b3486a7fec121Stephen Gallagher}