Fix: Read from pointer after free
[lttng-tools.git] / tests / unit / test_session.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
21#include <errno.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <time.h>
27#include <sys/types.h>
28
29#include <tap/tap.h>
30
31#include <bin/lttng-sessiond/session.h>
32#include <bin/lttng-sessiond/ust-app.h>
33#include <common/sessiond-comm/sessiond-comm.h>
34#include <common/common.h>
35
36#define SESSION1 "test1"
37
38#define MAX_SESSIONS 10000
39#define RANDOM_STRING_LEN 11
40
41/* Number of TAP tests in this file */
42#define NUM_TESTS 11
43
44static struct ltt_session_list *session_list;
45
46/* For lttngerr.h */
47int lttng_opt_quiet = 1;
48int lttng_opt_verbose = 0;
49
50int ust_consumerd32_fd;
51int ust_consumerd64_fd;
52
53static const char alphanum[] =
54 "0123456789"
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
56 "abcdefghijklmnopqrstuvwxyz";
57static char random_string[RANDOM_STRING_LEN];
58
59/*
60 * Return random string of 10 characters.
61 * Not thread-safe.
62 */
63static char *get_random_string(void)
64{
65 int i;
66
67 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
68 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
69 }
70
71 random_string[RANDOM_STRING_LEN - 1] = '\0';
72
73 return random_string;
74}
75
76/*
77 * Return 0 if session name is found, else -1
78 */
79static int find_session_name(char *name)
80{
81 struct ltt_session *iter;
82
83 cds_list_for_each_entry(iter, &session_list->head, list) {
84 if (strcmp(iter->name, name) == 0) {
85 return 0;
86 }
87 }
88
89 return -1;
90}
91
92static int session_list_count(void)
93{
94 int count = 0;
95 struct ltt_session *iter;
96
97 cds_list_for_each_entry(iter, &session_list->head, list) {
98 count++;
99 }
100 return count;
101}
102
103/*
104 * Empty session list manually.
105 */
106static void empty_session_list(void)
107{
108 struct ltt_session *iter, *tmp;
109
110 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
111 cds_list_del(&iter->list);
112 free(iter);
113 }
114
115 /* Session list must be 0 */
116 assert(!session_list_count());
117}
118
119/*
120 * Test creation of 1 session
121 */
122static int create_one_session(char *name)
123{
124 int ret;
125
126 ret = session_create(name, geteuid(), getegid());
127 if (ret == LTTNG_OK) {
128 /* Validate */
129 ret = find_session_name(name);
130 if (ret < 0) {
131 /* Session not found by name */
132 printf("session not found after creation\n");
133 return -1;
134 } else {
135 /* Success */
136 return 0;
137 }
138 } else {
139 if (ret == LTTNG_ERR_EXIST_SESS) {
140 printf("(session already exists) ");
141 }
142 return -1;
143 }
144
145 return 0;
146}
147
148/*
149 * Test deletion of 1 session
150 */
151static int destroy_one_session(struct ltt_session *session)
152{
153 int ret;
154
155 ret = session_destroy(session);
156
157 if (ret == LTTNG_OK) {
158 /* Validate */
159 if (session == NULL) {
160 return 0;
161 }
162 ret = find_session_name(session->name);
163 if (ret < 0) {
164 /* Success, -1 means that the sesion is NOT found */
165 return 0;
166 } else {
167 /* Fail */
168 return -1;
169 }
170 }
171
172 return 0;
173}
174
175/*
176 * This test is supposed to fail at the second create call. If so, return 0 for
177 * test success, else -1.
178 */
179static int two_session_same_name(void)
180{
181 int ret;
182 struct ltt_session *sess;
183
184 ret = create_one_session(SESSION1);
185 if (ret < 0) {
186 /* Fail */
187 return -1;
188 }
189
190 sess = session_find_by_name(SESSION1);
191 if (sess) {
192 /* Success */
193 return 0;
194 }
195
196 /* Fail */
197 return -1;
198}
199
200void test_session_list(void)
201{
202 session_list = session_get_list();
203 ok(session_list != NULL, "Session list: not NULL");
204}
205
206void test_create_one_session(void)
207{
208 ok(create_one_session(SESSION1) == 0,
209 "Create session: %s",
210 SESSION1);
211}
212
213void test_validate_session(void)
214{
215 struct ltt_session *tmp;
216
217 tmp = session_find_by_name(SESSION1);
218
219 ok(tmp != NULL,
220 "Validating session: session found");
221
222 ok(tmp->kernel_session == NULL &&
223 strlen(tmp->name),
224 "Validating session: basic sanity check");
225
226 session_lock(tmp);
227 session_unlock(tmp);
228}
229
230void test_destroy_session(void)
231{
232 struct ltt_session *tmp;
233
234 tmp = session_find_by_name(SESSION1);
235
236 ok(tmp != NULL,
237 "Destroying session: session found");
238
239 ok(destroy_one_session(tmp) == 0,
240 "Destroying session: %s destroyed",
241 SESSION1);
242}
243
244void test_duplicate_session(void)
245{
246 ok(two_session_same_name() == 0,
247 "Duplicate session creation");
248}
249
250void test_bogus_session_param(void)
251{
252 ok(create_one_session(NULL) < 0,
253 "Create session with bogus param: NULL should fail");
254
255 ok(session_list_count() == 0,
256 "Create session with bogus param: session list empty");
257}
258
259void test_large_session_number(void)
260{
261 int ret, i, failed = 0;
262 struct ltt_session *iter, *tmp;
263
264 for (i = 0; i < MAX_SESSIONS; i++) {
265 char *tmp_name = get_random_string();
266 ret = create_one_session(tmp_name);
267 if (ret < 0) {
268 diag("session %d (name: %s) creation failed", i, tmp_name);
269 ++failed;
270 }
271 }
272
273 ok(failed == 0,
274 "Large sessions number: created %u sessions",
275 MAX_SESSIONS);
276
277 failed = 0;
278
279 for (i = 0; i < MAX_SESSIONS; i++) {
280 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
281 ret = destroy_one_session(iter);
282 if (ret < 0) {
283 diag("session %d (name: %s) destroy failed", i, iter->name);
284 ++failed;
285 }
286 }
287 }
288
289 ok(failed == 0 && session_list_count() == 0,
290 "Large sessions number: destroyed %u sessions",
291 MAX_SESSIONS);
292}
293
294int main(int argc, char **argv)
295{
296 plan_tests(NUM_TESTS);
297
298 diag("Sessions unit tests");
299
300 test_session_list();
301
302 test_create_one_session();
303
304 test_validate_session();
305
306 test_destroy_session();
307
308 test_duplicate_session();
309
310 empty_session_list();
311
312 test_bogus_session_param();
313
314 test_large_session_number();
315
316 return exit_status();
317}
This page took 0.024215 seconds and 4 git commands to generate.