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