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