2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
19 #include <common/compat/errno.h>
20 #include <bin/lttng-sessiond/session.h>
21 #include <bin/lttng-sessiond/ust-app.h>
22 #include <bin/lttng-sessiond/ht-cleanup.h>
23 #include <bin/lttng-sessiond/health-sessiond.h>
24 #include <bin/lttng-sessiond/thread.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26 #include <common/common.h>
28 #define SESSION1 "test1"
30 #define MAX_SESSIONS 10000
31 #define RANDOM_STRING_LEN 11
33 /* Number of TAP tests in this file */
36 static struct ltt_session_list
*session_list
;
39 int lttng_opt_quiet
= 1;
40 int lttng_opt_verbose
= 0;
43 static const char alphanum
[] =
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46 "abcdefghijklmnopqrstuvwxyz";
47 static char random_string
[RANDOM_STRING_LEN
];
50 * Return random string of 10 characters.
53 static char *get_random_string(void)
57 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
58 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
61 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
67 * Return 0 if session name is found, else -1
69 static int find_session_name(const char *name
)
71 struct ltt_session
*iter
;
73 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
74 if (strcmp(iter
->name
, name
) == 0) {
82 static int session_list_count(void)
85 struct ltt_session
*iter
;
87 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
94 * Empty session list manually.
96 static void empty_session_list(void)
98 struct ltt_session
*iter
, *tmp
;
101 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
102 session_destroy(iter
);
104 session_unlock_list();
106 /* Session list must be 0 */
107 assert(!session_list_count());
111 * Test creation of 1 session
113 static int create_one_session(const char *name
)
116 enum lttng_error_code ret_code
;
117 struct ltt_session
*session
= NULL
;
120 ret_code
= session_create(name
, geteuid(), getegid(), &session
);
121 session_put(session
);
122 if (ret_code
== LTTNG_OK
) {
124 ret
= find_session_name(name
);
126 /* Session not found by name */
127 printf("session not found after creation\n");
134 if (ret_code
== LTTNG_ERR_EXIST_SESS
) {
135 printf("(session already exists) ");
140 session_unlock_list();
145 * Test deletion of 1 session
147 static int destroy_one_session(struct ltt_session
*session
)
150 char session_name
[NAME_MAX
];
152 strncpy(session_name
, session
->name
, sizeof(session_name
));
153 session_name
[sizeof(session_name
) - 1] = '\0';
155 session_destroy(session
);
156 session_put(session
);
158 ret
= find_session_name(session_name
);
160 /* Success, -1 means that the sesion is NOT found */
170 * This test is supposed to fail at the second create call. If so, return 0 for
171 * test success, else -1.
173 static int two_session_same_name(void)
176 struct ltt_session
*sess
;
178 ret
= create_one_session(SESSION1
);
186 sess
= session_find_by_name(SESSION1
);
190 session_unlock_list();
199 session_unlock_list();
204 static void test_session_list(void)
206 session_list
= session_get_list();
207 ok(session_list
!= NULL
, "Session list: not NULL");
210 static void test_create_one_session(void)
212 ok(create_one_session(SESSION1
) == 0,
213 "Create session: %s",
217 static void test_validate_session(void)
219 struct ltt_session
*tmp
;
222 tmp
= session_find_by_name(SESSION1
);
225 "Validating session: session found");
228 ok(tmp
->kernel_session
== NULL
&&
230 "Validating session: basic sanity check");
232 skip(1, "Skipping session validation check as session was not found");
240 session_unlock_list();
243 static void test_destroy_session(void)
245 struct ltt_session
*tmp
;
248 tmp
= session_find_by_name(SESSION1
);
251 "Destroying session: session found");
254 ok(destroy_one_session(tmp
) == 0,
255 "Destroying session: %s destroyed",
258 skip(1, "Skipping session destruction as it was not found");
260 session_unlock_list();
263 static void test_duplicate_session(void)
265 ok(two_session_same_name() == 0,
266 "Duplicate session creation");
269 static void test_session_name_generation(void)
271 struct ltt_session
*session
= NULL
;
272 enum lttng_error_code ret_code
;
273 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
276 ret_code
= session_create(NULL
, geteuid(), getegid(), &session
);
277 ok(ret_code
== LTTNG_OK
,
278 "Create session with a NULL name (auto-generate a name)");
280 skip(1, "Skipping session name generation tests as session_create() failed.");
283 diag("Automatically-generated session name: %s", *session
->name
?
284 session
->name
: "ERROR");
285 ok(*session
->name
&& !strncmp(expected_session_name_prefix
, session
->name
,
286 sizeof(DEFAULT_SESSION_NAME
) - 1),
287 "Auto-generated session name starts with %s",
288 DEFAULT_SESSION_NAME
);
290 session_put(session
);
291 session_unlock_list();
294 static void test_large_session_number(void)
296 int ret
, i
, failed
= 0;
297 struct ltt_session
*iter
, *tmp
;
299 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
300 char *tmp_name
= get_random_string();
301 ret
= create_one_session(tmp_name
);
303 diag("session %d (name: %s) creation failed", i
, tmp_name
);
309 "Large sessions number: created %u sessions",
315 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
316 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
317 assert(session_get(iter
));
318 ret
= destroy_one_session(iter
);
320 diag("session %d destroy failed", i
);
325 session_unlock_list();
327 ok(failed
== 0 && session_list_count() == 0,
328 "Large sessions number: destroyed %u sessions",
332 int main(int argc
, char **argv
)
334 struct lttng_thread
*ht_cleanup_thread
;
336 plan_tests(NUM_TESTS
);
338 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
339 ht_cleanup_thread
= launch_ht_cleanup_thread();
340 assert(ht_cleanup_thread
);
341 lttng_thread_put(ht_cleanup_thread
);
343 diag("Sessions unit tests");
345 rcu_register_thread();
349 test_create_one_session();
351 test_validate_session();
353 test_destroy_session();
355 test_duplicate_session();
357 empty_session_list();
359 test_session_name_generation();
361 test_large_session_number();
363 rcu_unregister_thread();
364 lttng_thread_list_shutdown_orphans();
366 return exit_status();