2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
31 #include <bin/lttng-sessiond/session.h>
32 #include <bin/lttng-sessiond/ust-app.h>
33 #include <bin/lttng-sessiond/ht-cleanup.h>
34 #include <bin/lttng-sessiond/health-sessiond.h>
35 #include <bin/lttng-sessiond/thread.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/common.h>
39 #define SESSION1 "test1"
41 #define MAX_SESSIONS 10000
42 #define RANDOM_STRING_LEN 11
44 /* Number of TAP tests in this file */
47 static struct ltt_session_list
*session_list
;
50 int lttng_opt_quiet
= 1;
51 int lttng_opt_verbose
= 0;
54 static const char alphanum
[] =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
57 "abcdefghijklmnopqrstuvwxyz";
58 static char random_string
[RANDOM_STRING_LEN
];
61 * Return random string of 10 characters.
64 static char *get_random_string(void)
68 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
69 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
72 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
78 * Return 0 if session name is found, else -1
80 static int find_session_name(char *name
)
82 struct ltt_session
*iter
;
84 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
85 if (strcmp(iter
->name
, name
) == 0) {
93 static int session_list_count(void)
96 struct ltt_session
*iter
;
98 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
105 * Empty session list manually.
107 static void empty_session_list(void)
109 struct ltt_session
*iter
, *tmp
;
112 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
113 session_destroy(iter
);
115 session_unlock_list();
117 /* Session list must be 0 */
118 assert(!session_list_count());
122 * Test creation of 1 session
124 static int create_one_session(char *name
)
127 enum lttng_error_code ret_code
;
128 struct ltt_session
*session
= NULL
;
131 ret_code
= session_create(name
, geteuid(), getegid(), &session
);
132 session_put(session
);
133 if (ret_code
== LTTNG_OK
) {
135 ret
= find_session_name(name
);
137 /* Session not found by name */
138 printf("session not found after creation\n");
145 if (ret_code
== LTTNG_ERR_EXIST_SESS
) {
146 printf("(session already exists) ");
151 session_unlock_list();
156 * Test deletion of 1 session
158 static int destroy_one_session(struct ltt_session
*session
)
161 char session_name
[NAME_MAX
];
163 strncpy(session_name
, session
->name
, sizeof(session_name
));
164 session_name
[sizeof(session_name
) - 1] = '\0';
166 session_destroy(session
);
167 session_put(session
);
169 ret
= find_session_name(session_name
);
171 /* Success, -1 means that the sesion is NOT found */
181 * This test is supposed to fail at the second create call. If so, return 0 for
182 * test success, else -1.
184 static int two_session_same_name(void)
187 struct ltt_session
*sess
;
189 ret
= create_one_session(SESSION1
);
197 sess
= session_find_by_name(SESSION1
);
201 session_unlock_list();
210 session_unlock_list();
215 void test_session_list(void)
217 session_list
= session_get_list();
218 ok(session_list
!= NULL
, "Session list: not NULL");
221 void test_create_one_session(void)
223 ok(create_one_session(SESSION1
) == 0,
224 "Create session: %s",
228 void test_validate_session(void)
230 struct ltt_session
*tmp
;
233 tmp
= session_find_by_name(SESSION1
);
236 "Validating session: session found");
239 ok(tmp
->kernel_session
== NULL
&&
241 "Validating session: basic sanity check");
243 skip(1, "Skipping session validation check as session was not found");
251 session_unlock_list();
254 void test_destroy_session(void)
256 struct ltt_session
*tmp
;
259 tmp
= session_find_by_name(SESSION1
);
262 "Destroying session: session found");
265 ok(destroy_one_session(tmp
) == 0,
266 "Destroying session: %s destroyed",
269 skip(1, "Skipping session destruction as it was not found");
271 session_unlock_list();
274 void test_duplicate_session(void)
276 ok(two_session_same_name() == 0,
277 "Duplicate session creation");
280 void test_session_name_generation(void)
282 struct ltt_session
*session
= NULL
;
283 enum lttng_error_code ret_code
;
284 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
287 ret_code
= session_create(NULL
, geteuid(), getegid(), &session
);
288 ok(ret_code
== LTTNG_OK
,
289 "Create session with a NULL name (auto-generate a name)");
291 skip(1, "Skipping session name generation tests as session_create() failed.");
294 diag("Automatically-generated session name: %s", *session
->name
?
295 session
->name
: "ERROR");
296 ok(*session
->name
&& !strncmp(expected_session_name_prefix
, session
->name
,
297 sizeof(DEFAULT_SESSION_NAME
) - 1),
298 "Auto-generated session name starts with %s",
299 DEFAULT_SESSION_NAME
);
301 session_put(session
);
302 session_unlock_list();
305 void test_large_session_number(void)
307 int ret
, i
, failed
= 0;
308 struct ltt_session
*iter
, *tmp
;
310 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
311 char *tmp_name
= get_random_string();
312 ret
= create_one_session(tmp_name
);
314 diag("session %d (name: %s) creation failed", i
, tmp_name
);
320 "Large sessions number: created %u sessions",
326 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
327 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
328 assert(session_get(iter
));
329 ret
= destroy_one_session(iter
);
331 diag("session %d destroy failed", i
);
336 session_unlock_list();
338 ok(failed
== 0 && session_list_count() == 0,
339 "Large sessions number: destroyed %u sessions",
343 int main(int argc
, char **argv
)
345 struct lttng_thread
*ht_cleanup_thread
;
347 plan_tests(NUM_TESTS
);
349 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
350 ht_cleanup_thread
= launch_ht_cleanup_thread();
351 assert(ht_cleanup_thread
);
352 lttng_thread_put(ht_cleanup_thread
);
354 diag("Sessions unit tests");
356 rcu_register_thread();
360 test_create_one_session();
362 test_validate_session();
364 test_destroy_session();
366 test_duplicate_session();
368 empty_session_list();
370 test_session_name_generation();
372 test_large_session_number();
374 rcu_unregister_thread();
375 lttng_thread_list_shutdown_orphans();
377 return exit_status();