Fix: build failure with -fno-common
[lttng-tools.git] / tests / unit / test_session.c
CommitLineData
63371d1e
DG
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
63371d1e
DG
19#include <assert.h>
20#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <time.h>
6df2e2c9 26#include <sys/types.h>
8273250b 27#include <urcu.h>
63371d1e 28
83c55082
CB
29#include <tap/tap.h>
30
10a8a223 31#include <bin/lttng-sessiond/session.h>
7972aab2 32#include <bin/lttng-sessiond/ust-app.h>
5e97de00
JG
33#include <bin/lttng-sessiond/ht-cleanup.h>
34#include <bin/lttng-sessiond/health-sessiond.h>
a3707772 35#include <bin/lttng-sessiond/thread.h>
10a8a223 36#include <common/sessiond-comm/sessiond-comm.h>
f73fabfd 37#include <common/common.h>
f6a9efaa 38
63371d1e
DG
39#define SESSION1 "test1"
40
63371d1e 41#define MAX_SESSIONS 10000
98612240 42#define RANDOM_STRING_LEN 11
63371d1e 43
83c55082 44/* Number of TAP tests in this file */
dec56f6c 45#define NUM_TESTS 11
63371d1e
DG
46
47static struct ltt_session_list *session_list;
48
ad7c9c18 49/* For error.h */
97e19046
DG
50int lttng_opt_quiet = 1;
51int lttng_opt_verbose = 0;
c7e35b03 52int lttng_opt_mi;
63371d1e
DG
53
54static const char alphanum[] =
55 "0123456789"
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
57 "abcdefghijklmnopqrstuvwxyz";
98612240 58static char random_string[RANDOM_STRING_LEN];
63371d1e
DG
59
60/*
61 * Return random string of 10 characters.
98612240 62 * Not thread-safe.
63371d1e
DG
63 */
64static char *get_random_string(void)
65{
66 int i;
63371d1e 67
98612240
MD
68 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
69 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
63371d1e
DG
70 }
71
98612240 72 random_string[RANDOM_STRING_LEN - 1] = '\0';
63371d1e 73
98612240 74 return random_string;
63371d1e
DG
75}
76
77/*
78 * Return 0 if session name is found, else -1
79 */
80static int find_session_name(char *name)
81{
82 struct ltt_session *iter;
83
84 cds_list_for_each_entry(iter, &session_list->head, list) {
85 if (strcmp(iter->name, name) == 0) {
86 return 0;
87 }
88 }
89
90 return -1;
91}
92
cc305a0b
MD
93static int session_list_count(void)
94{
95 int count = 0;
96 struct ltt_session *iter;
97
98 cds_list_for_each_entry(iter, &session_list->head, list) {
99 count++;
100 }
101 return count;
102}
103
63371d1e
DG
104/*
105 * Empty session list manually.
106 */
107static void empty_session_list(void)
108{
109 struct ltt_session *iter, *tmp;
110
e32d7f27 111 session_lock_list();
63371d1e 112 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
23324029 113 session_destroy(iter);
63371d1e 114 }
e32d7f27 115 session_unlock_list();
63371d1e
DG
116
117 /* Session list must be 0 */
cc305a0b 118 assert(!session_list_count());
63371d1e
DG
119}
120
121/*
122 * Test creation of 1 session
123 */
dec56f6c 124static int create_one_session(char *name)
63371d1e
DG
125{
126 int ret;
b178f53e
JG
127 enum lttng_error_code ret_code;
128 struct ltt_session *session = NULL;
63371d1e 129
b178f53e 130 session_lock_list();
e3876bf0 131 ret_code = session_create(name, geteuid(), getegid(), &session);
b178f53e
JG
132 session_put(session);
133 if (ret_code == LTTNG_OK) {
63371d1e
DG
134 /* Validate */
135 ret = find_session_name(name);
136 if (ret < 0) {
137 /* Session not found by name */
138 printf("session not found after creation\n");
b178f53e 139 ret = -1;
63371d1e
DG
140 } else {
141 /* Success */
b178f53e 142 ret = 0;
63371d1e 143 }
54d01ffb 144 } else {
b178f53e 145 if (ret_code == LTTNG_ERR_EXIST_SESS) {
63371d1e
DG
146 printf("(session already exists) ");
147 }
b178f53e 148 ret = -1;
63371d1e 149 }
3517bb68 150
b178f53e
JG
151 session_unlock_list();
152 return ret;
63371d1e
DG
153}
154
155/*
156 * Test deletion of 1 session
157 */
271933a4 158static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
159{
160 int ret;
59891c42 161 char session_name[NAME_MAX];
63371d1e 162
4cd95b52 163 strncpy(session_name, session->name, sizeof(session_name));
59891c42 164 session_name[sizeof(session_name) - 1] = '\0';
54d01ffb 165
e32d7f27
JG
166 session_destroy(session);
167 session_put(session);
63371d1e 168
e32d7f27
JG
169 ret = find_session_name(session_name);
170 if (ret < 0) {
171 /* Success, -1 means that the sesion is NOT found */
172 ret = 0;
173 } else {
174 /* Fail */
175 ret = -1;
176 }
177 return ret;
63371d1e
DG
178}
179
63371d1e
DG
180/*
181 * This test is supposed to fail at the second create call. If so, return 0 for
182 * test success, else -1.
183 */
184static int two_session_same_name(void)
185{
186 int ret;
00e2e675 187 struct ltt_session *sess;
63371d1e 188
dec56f6c 189 ret = create_one_session(SESSION1);
63371d1e
DG
190 if (ret < 0) {
191 /* Fail */
e32d7f27
JG
192 ret = -1;
193 goto end;
63371d1e
DG
194 }
195
e32d7f27 196 session_lock_list();
00e2e675
DG
197 sess = session_find_by_name(SESSION1);
198 if (sess) {
63371d1e 199 /* Success */
e32d7f27
JG
200 session_put(sess);
201 session_unlock_list();
202 ret = 0;
203 goto end_unlock;
204 } else {
205 /* Fail */
206 ret = -1;
207 goto end_unlock;
63371d1e 208 }
e32d7f27
JG
209end_unlock:
210 session_unlock_list();
211end:
212 return ret;
63371d1e
DG
213}
214
c109c263 215static void test_session_list(void)
63371d1e 216{
83c55082
CB
217 session_list = session_get_list();
218 ok(session_list != NULL, "Session list: not NULL");
219}
63371d1e 220
c109c263 221static void test_create_one_session(void)
83c55082 222{
dec56f6c 223 ok(create_one_session(SESSION1) == 0,
83c55082
CB
224 "Create session: %s",
225 SESSION1);
226}
63371d1e 227
c109c263 228static void test_validate_session(void)
83c55082
CB
229{
230 struct ltt_session *tmp;
63371d1e 231
e32d7f27 232 session_lock_list();
83c55082 233 tmp = session_find_by_name(SESSION1);
63371d1e 234
83c55082
CB
235 ok(tmp != NULL,
236 "Validating session: session found");
237
d61d06f0
JG
238 if (tmp) {
239 ok(tmp->kernel_session == NULL &&
240 strlen(tmp->name),
241 "Validating session: basic sanity check");
242 } else {
243 skip(1, "Skipping session validation check as session was not found");
244 goto end;
245 }
63371d1e 246
54d01ffb
DG
247 session_lock(tmp);
248 session_unlock(tmp);
e32d7f27 249 session_put(tmp);
d61d06f0 250end:
e32d7f27 251 session_unlock_list();
83c55082 252}
63371d1e 253
c109c263 254static void test_destroy_session(void)
83c55082
CB
255{
256 struct ltt_session *tmp;
63371d1e 257
e32d7f27 258 session_lock_list();
83c55082 259 tmp = session_find_by_name(SESSION1);
63371d1e 260
83c55082
CB
261 ok(tmp != NULL,
262 "Destroying session: session found");
63371d1e 263
acd4994e
JG
264 if (tmp) {
265 ok(destroy_one_session(tmp) == 0,
266 "Destroying session: %s destroyed",
267 SESSION1);
268 } else {
269 skip(1, "Skipping session destruction as it was not found");
270 }
e32d7f27 271 session_unlock_list();
83c55082 272}
63371d1e 273
c109c263 274static void test_duplicate_session(void)
83c55082
CB
275{
276 ok(two_session_same_name() == 0,
277 "Duplicate session creation");
278}
279
c109c263 280static void test_session_name_generation(void)
83c55082 281{
b178f53e
JG
282 struct ltt_session *session = NULL;
283 enum lttng_error_code ret_code;
284 const char *expected_session_name_prefix = DEFAULT_SESSION_NAME;
83c55082 285
b178f53e 286 session_lock_list();
e3876bf0 287 ret_code = session_create(NULL, geteuid(), getegid(), &session);
b178f53e
JG
288 ok(ret_code == LTTNG_OK,
289 "Create session with a NULL name (auto-generate a name)");
290 if (!session) {
291 skip(1, "Skipping session name generation tests as session_create() failed.");
292 goto end;
293 }
294 diag("Automatically-generated session name: %s", *session->name ?
295 session->name : "ERROR");
296 ok(*session->name && !strncmp(expected_session_name_prefix, session->name,
297 sizeof(DEFAULT_SESSION_NAME) - 1),
298 "Auto-generated session name starts with %s",
299 DEFAULT_SESSION_NAME);
300end:
301 session_put(session);
302 session_unlock_list();
83c55082
CB
303}
304
c109c263 305static void test_large_session_number(void)
83c55082
CB
306{
307 int ret, i, failed = 0;
308 struct ltt_session *iter, *tmp;
63371d1e 309
63371d1e 310 for (i = 0; i < MAX_SESSIONS; i++) {
e6dd5671 311 char *tmp_name = get_random_string();
dec56f6c 312 ret = create_one_session(tmp_name);
63371d1e 313 if (ret < 0) {
83c55082
CB
314 diag("session %d (name: %s) creation failed", i, tmp_name);
315 ++failed;
db9b8b88 316 }
63371d1e 317 }
63371d1e 318
83c55082
CB
319 ok(failed == 0,
320 "Large sessions number: created %u sessions",
321 MAX_SESSIONS);
322
323 failed = 0;
324
e32d7f27 325 session_lock_list();
63371d1e
DG
326 for (i = 0; i < MAX_SESSIONS; i++) {
327 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
e32d7f27 328 assert(session_get(iter));
271933a4 329 ret = destroy_one_session(iter);
63371d1e 330 if (ret < 0) {
59891c42 331 diag("session %d destroy failed", i);
83c55082 332 ++failed;
63371d1e
DG
333 }
334 }
335 }
e32d7f27 336 session_unlock_list();
63371d1e 337
83c55082
CB
338 ok(failed == 0 && session_list_count() == 0,
339 "Large sessions number: destroyed %u sessions",
340 MAX_SESSIONS);
341}
63371d1e 342
83c55082
CB
343int main(int argc, char **argv)
344{
a3707772
JG
345 struct lttng_thread *ht_cleanup_thread;
346
83c55082
CB
347 plan_tests(NUM_TESTS);
348
5e97de00 349 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
a3707772
JG
350 ht_cleanup_thread = launch_ht_cleanup_thread();
351 assert(ht_cleanup_thread);
352 lttng_thread_put(ht_cleanup_thread);
5e97de00 353
e3bef725
CB
354 diag("Sessions unit tests");
355
8273250b
JR
356 rcu_register_thread();
357
83c55082
CB
358 test_session_list();
359
360 test_create_one_session();
361
362 test_validate_session();
363
364 test_destroy_session();
365
366 test_duplicate_session();
367
368 empty_session_list();
369
b178f53e 370 test_session_name_generation();
83c55082
CB
371
372 test_large_session_number();
373
8273250b 374 rcu_unregister_thread();
a3707772 375 lttng_thread_list_shutdown_orphans();
5e97de00 376
83c55082 377 return exit_status();
63371d1e 378}
This page took 0.123594 seconds and 5 git commands to generate.