2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <sys/types.h>
18 #include <common/compat/errno.hpp>
19 #include <bin/lttng-sessiond/session.hpp>
20 #include <bin/lttng-sessiond/ust-app.hpp>
21 #include <bin/lttng-sessiond/ht-cleanup.hpp>
22 #include <bin/lttng-sessiond/health-sessiond.hpp>
23 #include <bin/lttng-sessiond/thread.hpp>
24 #include <common/sessiond-comm/sessiond-comm.hpp>
25 #include <common/common.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
;
114 ret_code
= session_create(name
, geteuid(), getegid(), &session
);
115 session_put(session
);
116 if (ret_code
== LTTNG_OK
) {
118 ret
= find_session_name(name
);
120 /* Session not found by name */
121 printf("session not found after creation\n");
128 if (ret_code
== LTTNG_ERR_EXIST_SESS
) {
129 printf("(session already exists) ");
134 session_unlock_list();
139 * Test deletion of 1 session
141 static int destroy_one_session(struct ltt_session
*session
)
144 char session_name
[NAME_MAX
];
146 strncpy(session_name
, session
->name
, sizeof(session_name
));
147 session_name
[sizeof(session_name
) - 1] = '\0';
149 session_destroy(session
);
150 session_put(session
);
152 ret
= find_session_name(session_name
);
154 /* Success, -1 means that the sesion is NOT found */
164 * This test is supposed to fail at the second create call. If so, return 0 for
165 * test success, else -1.
167 static int two_session_same_name(void)
170 struct ltt_session
*sess
;
172 ret
= create_one_session(SESSION1
);
180 sess
= session_find_by_name(SESSION1
);
184 session_unlock_list();
193 session_unlock_list();
198 static void test_session_list(void)
200 session_list
= session_get_list();
201 ok(session_list
!= NULL
, "Session list: not NULL");
204 static void test_create_one_session(void)
206 ok(create_one_session(SESSION1
) == 0,
207 "Create session: %s",
211 static void test_validate_session(void)
213 struct ltt_session
*tmp
;
216 tmp
= session_find_by_name(SESSION1
);
219 "Validating session: session found");
222 ok(tmp
->kernel_session
== NULL
&&
224 "Validating session: basic sanity check");
226 skip(1, "Skipping session validation check as session was not found");
234 session_unlock_list();
237 static void test_destroy_session(void)
239 struct ltt_session
*tmp
;
242 tmp
= session_find_by_name(SESSION1
);
245 "Destroying session: session found");
248 ok(destroy_one_session(tmp
) == 0,
249 "Destroying session: %s destroyed",
252 skip(1, "Skipping session destruction as it was not found");
254 session_unlock_list();
257 static void test_duplicate_session(void)
259 ok(two_session_same_name() == 0,
260 "Duplicate session creation");
263 static void test_session_name_generation(void)
265 struct ltt_session
*session
= NULL
;
266 enum lttng_error_code ret_code
;
267 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
270 ret_code
= session_create(NULL
, geteuid(), getegid(), &session
);
271 ok(ret_code
== LTTNG_OK
,
272 "Create session with a NULL name (auto-generate a name)");
274 skip(1, "Skipping session name generation tests as session_create() failed.");
277 diag("Automatically-generated session name: %s", *session
->name
?
278 session
->name
: "ERROR");
279 ok(*session
->name
&& !strncmp(expected_session_name_prefix
, session
->name
,
280 sizeof(DEFAULT_SESSION_NAME
) - 1),
281 "Auto-generated session name starts with %s",
282 DEFAULT_SESSION_NAME
);
284 session_put(session
);
285 session_unlock_list();
288 static void test_large_session_number(void)
290 int ret
, i
, failed
= 0;
291 struct ltt_session
*iter
, *tmp
;
293 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
294 char *tmp_name
= get_random_string();
295 ret
= create_one_session(tmp_name
);
297 diag("session %d (name: %s) creation failed", i
, tmp_name
);
303 "Large sessions number: created %u sessions",
309 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
310 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
311 LTTNG_ASSERT(session_get(iter
));
312 ret
= destroy_one_session(iter
);
314 diag("session %d destroy failed", i
);
319 session_unlock_list();
321 ok(failed
== 0 && session_list_count() == 0,
322 "Large sessions number: destroyed %u sessions",
326 int main(int argc
, char **argv
)
328 struct lttng_thread
*ht_cleanup_thread
;
330 plan_tests(NUM_TESTS
);
332 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
333 ht_cleanup_thread
= launch_ht_cleanup_thread();
334 LTTNG_ASSERT(ht_cleanup_thread
);
335 lttng_thread_put(ht_cleanup_thread
);
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();