vscode: Add configurations to run the executables under the debugger
[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
e32d7f27 91 session_lock_list();
28ab034a 92 cds_list_for_each_entry_safe (iter, tmp, &session_list->head, list) {
23324029 93 session_destroy(iter);
63371d1e 94 }
e32d7f27 95 session_unlock_list();
63371d1e
DG
96
97 /* Session list must be 0 */
a0377dfe 98 LTTNG_ASSERT(!session_list_count());
63371d1e
DG
99}
100
101/*
102 * Test creation of 1 session
103 */
b53d4e59 104static int create_one_session(const char *name)
63371d1e
DG
105{
106 int ret;
b178f53e 107 enum lttng_error_code ret_code;
cd9adb8b 108 struct ltt_session *session = nullptr;
63371d1e 109
b178f53e 110 session_lock_list();
e3876bf0 111 ret_code = session_create(name, geteuid(), getegid(), &session);
b178f53e
JG
112 session_put(session);
113 if (ret_code == LTTNG_OK) {
63371d1e
DG
114 /* Validate */
115 ret = find_session_name(name);
116 if (ret < 0) {
117 /* Session not found by name */
118 printf("session not found after creation\n");
b178f53e 119 ret = -1;
63371d1e
DG
120 } else {
121 /* Success */
b178f53e 122 ret = 0;
63371d1e 123 }
54d01ffb 124 } else {
b178f53e 125 if (ret_code == LTTNG_ERR_EXIST_SESS) {
63371d1e
DG
126 printf("(session already exists) ");
127 }
b178f53e 128 ret = -1;
63371d1e 129 }
3517bb68 130
b178f53e
JG
131 session_unlock_list();
132 return ret;
63371d1e
DG
133}
134
135/*
136 * Test deletion of 1 session
137 */
271933a4 138static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
139{
140 int ret;
59891c42 141 char session_name[NAME_MAX];
63371d1e 142
4cd95b52 143 strncpy(session_name, session->name, sizeof(session_name));
59891c42 144 session_name[sizeof(session_name) - 1] = '\0';
54d01ffb 145
e32d7f27
JG
146 session_destroy(session);
147 session_put(session);
63371d1e 148
e32d7f27
JG
149 ret = find_session_name(session_name);
150 if (ret < 0) {
151 /* Success, -1 means that the sesion is NOT found */
152 ret = 0;
153 } else {
154 /* Fail */
155 ret = -1;
156 }
157 return ret;
63371d1e
DG
158}
159
63371d1e
DG
160/*
161 * This test is supposed to fail at the second create call. If so, return 0 for
162 * test success, else -1.
163 */
cd9adb8b 164static int two_session_same_name()
63371d1e
DG
165{
166 int ret;
00e2e675 167 struct ltt_session *sess;
63371d1e 168
dec56f6c 169 ret = create_one_session(SESSION1);
63371d1e
DG
170 if (ret < 0) {
171 /* Fail */
e32d7f27
JG
172 ret = -1;
173 goto end;
63371d1e
DG
174 }
175
e32d7f27 176 session_lock_list();
00e2e675
DG
177 sess = session_find_by_name(SESSION1);
178 if (sess) {
63371d1e 179 /* Success */
e32d7f27
JG
180 session_put(sess);
181 session_unlock_list();
182 ret = 0;
183 goto end_unlock;
184 } else {
185 /* Fail */
186 ret = -1;
187 goto end_unlock;
63371d1e 188 }
e32d7f27
JG
189end_unlock:
190 session_unlock_list();
191end:
192 return ret;
63371d1e
DG
193}
194
cd9adb8b 195static void test_session_list()
63371d1e 196{
83c55082 197 session_list = session_get_list();
cd9adb8b 198 ok(session_list != nullptr, "Session list: not NULL");
83c55082 199}
63371d1e 200
cd9adb8b 201static void test_create_one_session()
83c55082 202{
28ab034a 203 ok(create_one_session(SESSION1) == 0, "Create session: %s", SESSION1);
83c55082 204}
63371d1e 205
cd9adb8b 206static void test_validate_session()
83c55082
CB
207{
208 struct ltt_session *tmp;
63371d1e 209
e32d7f27 210 session_lock_list();
83c55082 211 tmp = session_find_by_name(SESSION1);
63371d1e 212
cd9adb8b 213 ok(tmp != nullptr, "Validating session: session found");
83c55082 214
d61d06f0 215 if (tmp) {
cd9adb8b 216 ok(tmp->kernel_session == nullptr && strlen(tmp->name),
d61d06f0
JG
217 "Validating session: basic sanity check");
218 } else {
219 skip(1, "Skipping session validation check as session was not found");
220 goto end;
221 }
63371d1e 222
54d01ffb
DG
223 session_lock(tmp);
224 session_unlock(tmp);
e32d7f27 225 session_put(tmp);
d61d06f0 226end:
e32d7f27 227 session_unlock_list();
83c55082 228}
63371d1e 229
cd9adb8b 230static void test_destroy_session()
83c55082
CB
231{
232 struct ltt_session *tmp;
63371d1e 233
e32d7f27 234 session_lock_list();
83c55082 235 tmp = session_find_by_name(SESSION1);
63371d1e 236
cd9adb8b 237 ok(tmp != nullptr, "Destroying session: session found");
63371d1e 238
acd4994e 239 if (tmp) {
28ab034a 240 ok(destroy_one_session(tmp) == 0, "Destroying session: %s destroyed", SESSION1);
acd4994e
JG
241 } else {
242 skip(1, "Skipping session destruction as it was not found");
243 }
e32d7f27 244 session_unlock_list();
83c55082 245}
63371d1e 246
cd9adb8b 247static void test_duplicate_session()
83c55082 248{
28ab034a 249 ok(two_session_same_name() == 0, "Duplicate session creation");
83c55082
CB
250}
251
cd9adb8b 252static void test_session_name_generation()
83c55082 253{
cd9adb8b 254 struct ltt_session *session = nullptr;
b178f53e
JG
255 enum lttng_error_code ret_code;
256 const char *expected_session_name_prefix = DEFAULT_SESSION_NAME;
83c55082 257
b178f53e 258 session_lock_list();
cd9adb8b 259 ret_code = session_create(nullptr, geteuid(), getegid(), &session);
28ab034a 260 ok(ret_code == LTTNG_OK, "Create session with a NULL name (auto-generate a name)");
b178f53e
JG
261 if (!session) {
262 skip(1, "Skipping session name generation tests as session_create() failed.");
263 goto end;
264 }
28ab034a
JG
265 diag("Automatically-generated session name: %s", *session->name ? session->name : "ERROR");
266 ok(*session->name &&
267 !strncmp(expected_session_name_prefix,
268 session->name,
269 sizeof(DEFAULT_SESSION_NAME) - 1),
270 "Auto-generated session name starts with %s",
271 DEFAULT_SESSION_NAME);
b178f53e
JG
272end:
273 session_put(session);
274 session_unlock_list();
83c55082
CB
275}
276
cd9adb8b 277static void test_large_session_number()
83c55082
CB
278{
279 int ret, i, failed = 0;
280 struct ltt_session *iter, *tmp;
63371d1e 281
63371d1e 282 for (i = 0; i < MAX_SESSIONS; i++) {
e6dd5671 283 char *tmp_name = get_random_string();
dec56f6c 284 ret = create_one_session(tmp_name);
63371d1e 285 if (ret < 0) {
83c55082
CB
286 diag("session %d (name: %s) creation failed", i, tmp_name);
287 ++failed;
db9b8b88 288 }
63371d1e 289 }
63371d1e 290
28ab034a 291 ok(failed == 0, "Large sessions number: created %u sessions", MAX_SESSIONS);
83c55082
CB
292
293 failed = 0;
294
e32d7f27 295 session_lock_list();
63371d1e 296 for (i = 0; i < MAX_SESSIONS; i++) {
28ab034a 297 cds_list_for_each_entry_safe (iter, tmp, &session_list->head, list) {
a0377dfe 298 LTTNG_ASSERT(session_get(iter));
271933a4 299 ret = destroy_one_session(iter);
63371d1e 300 if (ret < 0) {
59891c42 301 diag("session %d destroy failed", i);
83c55082 302 ++failed;
63371d1e
DG
303 }
304 }
305 }
e32d7f27 306 session_unlock_list();
63371d1e 307
83c55082
CB
308 ok(failed == 0 && session_list_count() == 0,
309 "Large sessions number: destroyed %u sessions",
310 MAX_SESSIONS);
311}
63371d1e 312
cd9adb8b 313int main()
83c55082 314{
83c55082
CB
315 plan_tests(NUM_TESTS);
316
412d7227 317 the_health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5e97de00 318
e3bef725
CB
319 diag("Sessions unit tests");
320
8273250b
JR
321 rcu_register_thread();
322
83c55082
CB
323 test_session_list();
324
325 test_create_one_session();
326
327 test_validate_session();
328
329 test_destroy_session();
330
331 test_duplicate_session();
332
333 empty_session_list();
334
b178f53e 335 test_session_name_generation();
83c55082
CB
336
337 test_large_session_number();
338
8273250b 339 rcu_unregister_thread();
a3707772 340 lttng_thread_list_shutdown_orphans();
5e97de00 341
83c55082 342 return exit_status();
63371d1e 343}
This page took 0.082641 seconds and 4 git commands to generate.