2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <sys/types.h>
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>
22 #include <common/make-unique.hpp>
23 #include <common/sessiond-comm/sessiond-comm.hpp>
27 #define SESSION1 "test1"
29 #define MAX_SESSIONS 10000
30 #define RANDOM_STRING_LEN 11
32 /* Number of TAP tests in this file */
35 static struct ltt_session_list
*session_list
;
37 static const char alphanum
[] =
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz";
41 static char random_string
[RANDOM_STRING_LEN
];
44 * Return random string of 10 characters.
47 static char *get_random_string(void)
51 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
52 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
55 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
61 * Return 0 if session name is found, else -1
63 static int find_session_name(const char *name
)
65 struct ltt_session
*iter
;
67 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
68 if (strcmp(iter
->name
, name
) == 0) {
76 static int session_list_count(void)
79 struct ltt_session
*iter
;
81 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
88 * Empty session list manually.
90 static void empty_session_list(void)
92 struct ltt_session
*iter
, *tmp
;
95 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
96 session_destroy(iter
);
98 session_unlock_list();
100 /* Session list must be 0 */
101 LTTNG_ASSERT(!session_list_count());
105 * Test creation of 1 session
107 static int create_one_session(const char *name
)
110 enum lttng_error_code ret_code
;
111 struct ltt_session
*session
= NULL
;
112 lttng::trace_format_descriptor::uptr trace_format_descriptor
=
113 lttng::make_unique
<lttng::trace_format_descriptor_ctf1
>();
116 ret_code
= session_create(name
, geteuid(), getegid(), trace_format_descriptor
, &session
);
117 session_put(session
);
118 if (ret_code
== LTTNG_OK
) {
120 ret
= find_session_name(name
);
122 /* Session not found by name */
123 printf("session not found after creation\n");
130 if (ret_code
== LTTNG_ERR_EXIST_SESS
) {
131 printf("(session already exists) ");
136 session_unlock_list();
141 * Test deletion of 1 session
143 static int destroy_one_session(struct ltt_session
*session
)
146 char session_name
[NAME_MAX
];
148 strncpy(session_name
, session
->name
, sizeof(session_name
));
149 session_name
[sizeof(session_name
) - 1] = '\0';
151 session_destroy(session
);
152 session_put(session
);
154 ret
= find_session_name(session_name
);
156 /* Success, -1 means that the session is NOT found */
166 * This test is supposed to fail at the second create call. If so, return 0 for
167 * test success, else -1.
169 static int two_session_same_name(void)
172 struct ltt_session
*sess
;
174 ret
= create_one_session(SESSION1
);
182 sess
= session_find_by_name(SESSION1
);
186 session_unlock_list();
195 session_unlock_list();
200 static void test_session_list(void)
202 session_list
= session_get_list();
203 ok(session_list
!= NULL
, "Session list: not NULL");
206 static void test_create_one_session(void)
208 ok(create_one_session(SESSION1
) == 0,
209 "Create session: %s",
213 static void test_validate_session(void)
215 struct ltt_session
*tmp
;
218 tmp
= session_find_by_name(SESSION1
);
221 "Validating session: session found");
224 ok(tmp
->kernel_session
== NULL
&&
226 "Validating session: basic sanity check");
228 skip(1, "Skipping session validation check as session was not found");
236 session_unlock_list();
239 static void test_destroy_session(void)
241 struct ltt_session
*tmp
;
244 tmp
= session_find_by_name(SESSION1
);
247 "Destroying session: session found");
250 ok(destroy_one_session(tmp
) == 0,
251 "Destroying session: %s destroyed",
254 skip(1, "Skipping session destruction as it was not found");
256 session_unlock_list();
259 static void test_duplicate_session(void)
261 ok(two_session_same_name() == 0,
262 "Duplicate session creation");
265 static void test_session_name_generation(void)
267 struct ltt_session
*session
= NULL
;
268 enum lttng_error_code ret_code
;
269 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
270 lttng::trace_format_descriptor::uptr trace_format_descriptor
=
271 lttng::make_unique
<lttng::trace_format_descriptor_ctf1
>();
274 ret_code
= session_create(NULL
, geteuid(), getegid(), trace_format_descriptor
, &session
);
275 ok(ret_code
== LTTNG_OK
,
276 "Create session with a NULL name (auto-generate a name)");
278 skip(1, "Skipping session name generation tests as session_create() failed.");
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
);
288 session_put(session
);
289 session_unlock_list();
292 static void test_large_session_number(void)
294 int ret
, i
, failed
= 0;
295 struct ltt_session
*iter
, *tmp
;
297 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
298 char *tmp_name
= get_random_string();
299 ret
= create_one_session(tmp_name
);
301 diag("session %d (name: %s) creation failed", i
, tmp_name
);
307 "Large sessions number: created %u sessions",
313 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
314 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
315 LTTNG_ASSERT(session_get(iter
));
316 ret
= destroy_one_session(iter
);
318 diag("session %d destroy failed", i
);
323 session_unlock_list();
325 ok(failed
== 0 && session_list_count() == 0,
326 "Large sessions number: destroyed %u sessions",
333 plan_tests(NUM_TESTS
);
335 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
337 diag("Sessions unit tests");
339 rcu_register_thread();
343 test_create_one_session();
345 test_validate_session();
347 test_destroy_session();
349 test_duplicate_session();
351 empty_session_list();
353 test_session_name_generation();
355 test_large_session_number();
357 rcu_unregister_thread();
358 lttng_thread_list_shutdown_orphans();
360 return exit_status();