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
;
38 static const char alphanum
[] =
40 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "abcdefghijklmnopqrstuvwxyz";
42 static char random_string
[RANDOM_STRING_LEN
];
45 * Return random string of 10 characters.
48 static char *get_random_string(void)
52 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
53 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
56 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
62 * Return 0 if session name is found, else -1
64 static int find_session_name(const char *name
)
66 struct ltt_session
*iter
;
68 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
69 if (strcmp(iter
->name
, name
) == 0) {
77 static int session_list_count(void)
80 struct ltt_session
*iter
;
82 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
89 * Empty session list manually.
91 static void empty_session_list(void)
93 struct ltt_session
*iter
, *tmp
;
96 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
97 session_destroy(iter
);
99 session_unlock_list();
101 /* Session list must be 0 */
102 assert(!session_list_count());
106 * Test creation of 1 session
108 static int create_one_session(const char *name
)
111 enum lttng_error_code ret_code
;
112 struct ltt_session
*session
= NULL
;
115 ret_code
= session_create(name
, geteuid(), getegid(), &session
);
116 session_put(session
);
117 if (ret_code
== LTTNG_OK
) {
119 ret
= find_session_name(name
);
121 /* Session not found by name */
122 printf("session not found after creation\n");
129 if (ret_code
== LTTNG_ERR_EXIST_SESS
) {
130 printf("(session already exists) ");
135 session_unlock_list();
140 * Test deletion of 1 session
142 static int destroy_one_session(struct ltt_session
*session
)
145 char session_name
[NAME_MAX
];
147 strncpy(session_name
, session
->name
, sizeof(session_name
));
148 session_name
[sizeof(session_name
) - 1] = '\0';
150 session_destroy(session
);
151 session_put(session
);
153 ret
= find_session_name(session_name
);
155 /* Success, -1 means that the sesion is NOT found */
165 * This test is supposed to fail at the second create call. If so, return 0 for
166 * test success, else -1.
168 static int two_session_same_name(void)
171 struct ltt_session
*sess
;
173 ret
= create_one_session(SESSION1
);
181 sess
= session_find_by_name(SESSION1
);
185 session_unlock_list();
194 session_unlock_list();
199 static void test_session_list(void)
201 session_list
= session_get_list();
202 ok(session_list
!= NULL
, "Session list: not NULL");
205 static void test_create_one_session(void)
207 ok(create_one_session(SESSION1
) == 0,
208 "Create session: %s",
212 static void test_validate_session(void)
214 struct ltt_session
*tmp
;
217 tmp
= session_find_by_name(SESSION1
);
220 "Validating session: session found");
223 ok(tmp
->kernel_session
== NULL
&&
225 "Validating session: basic sanity check");
227 skip(1, "Skipping session validation check as session was not found");
235 session_unlock_list();
238 static void test_destroy_session(void)
240 struct ltt_session
*tmp
;
243 tmp
= session_find_by_name(SESSION1
);
246 "Destroying session: session found");
249 ok(destroy_one_session(tmp
) == 0,
250 "Destroying session: %s destroyed",
253 skip(1, "Skipping session destruction as it was not found");
255 session_unlock_list();
258 static void test_duplicate_session(void)
260 ok(two_session_same_name() == 0,
261 "Duplicate session creation");
264 static void test_session_name_generation(void)
266 struct ltt_session
*session
= NULL
;
267 enum lttng_error_code ret_code
;
268 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
271 ret_code
= session_create(NULL
, geteuid(), getegid(), &session
);
272 ok(ret_code
== LTTNG_OK
,
273 "Create session with a NULL name (auto-generate a name)");
275 skip(1, "Skipping session name generation tests as session_create() failed.");
278 diag("Automatically-generated session name: %s", *session
->name
?
279 session
->name
: "ERROR");
280 ok(*session
->name
&& !strncmp(expected_session_name_prefix
, session
->name
,
281 sizeof(DEFAULT_SESSION_NAME
) - 1),
282 "Auto-generated session name starts with %s",
283 DEFAULT_SESSION_NAME
);
285 session_put(session
);
286 session_unlock_list();
289 static void test_large_session_number(void)
291 int ret
, i
, failed
= 0;
292 struct ltt_session
*iter
, *tmp
;
294 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
295 char *tmp_name
= get_random_string();
296 ret
= create_one_session(tmp_name
);
298 diag("session %d (name: %s) creation failed", i
, tmp_name
);
304 "Large sessions number: created %u sessions",
310 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
311 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
312 assert(session_get(iter
));
313 ret
= destroy_one_session(iter
);
315 diag("session %d destroy failed", i
);
320 session_unlock_list();
322 ok(failed
== 0 && session_list_count() == 0,
323 "Large sessions number: destroyed %u sessions",
327 int main(int argc
, char **argv
)
329 struct lttng_thread
*ht_cleanup_thread
;
331 plan_tests(NUM_TESTS
);
333 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
334 ht_cleanup_thread
= launch_ht_cleanup_thread();
335 assert(ht_cleanup_thread
);
336 lttng_thread_put(ht_cleanup_thread
);
338 diag("Sessions unit tests");
340 rcu_register_thread();
344 test_create_one_session();
346 test_validate_session();
348 test_destroy_session();
350 test_duplicate_session();
352 empty_session_list();
354 test_session_name_generation();
356 test_large_session_number();
358 rcu_unregister_thread();
359 lttng_thread_list_shutdown_orphans();
361 return exit_status();