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