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 struct health_app
*health_sessiond
;
48 static struct ltt_session_list
*session_list
;
51 int lttng_opt_quiet
= 1;
52 int lttng_opt_verbose
= 0;
55 int ust_consumerd32_fd
;
56 int ust_consumerd64_fd
;
58 static const char alphanum
[] =
60 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
61 "abcdefghijklmnopqrstuvwxyz";
62 static char random_string
[RANDOM_STRING_LEN
];
65 * Return random string of 10 characters.
68 static char *get_random_string(void)
72 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
73 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
76 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
82 * Return 0 if session name is found, else -1
84 static int find_session_name(char *name
)
86 struct ltt_session
*iter
;
88 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
89 if (strcmp(iter
->name
, name
) == 0) {
97 static int session_list_count(void)
100 struct ltt_session
*iter
;
102 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
109 * Empty session list manually.
111 static void empty_session_list(void)
113 struct ltt_session
*iter
, *tmp
;
116 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
117 session_destroy(iter
);
119 session_unlock_list();
121 /* Session list must be 0 */
122 assert(!session_list_count());
126 * Test creation of 1 session
128 static int create_one_session(char *name
)
131 enum lttng_error_code ret_code
;
132 struct ltt_session
*session
= NULL
;
135 ret_code
= session_create(name
, geteuid(), getegid(), &session
);
136 session_put(session
);
137 if (ret_code
== LTTNG_OK
) {
139 ret
= find_session_name(name
);
141 /* Session not found by name */
142 printf("session not found after creation\n");
149 if (ret_code
== LTTNG_ERR_EXIST_SESS
) {
150 printf("(session already exists) ");
155 session_unlock_list();
160 * Test deletion of 1 session
162 static int destroy_one_session(struct ltt_session
*session
)
165 char session_name
[NAME_MAX
];
167 strncpy(session_name
, session
->name
, sizeof(session_name
));
168 session_name
[sizeof(session_name
) - 1] = '\0';
170 session_destroy(session
);
171 session_put(session
);
173 ret
= find_session_name(session_name
);
175 /* Success, -1 means that the sesion is NOT found */
185 * This test is supposed to fail at the second create call. If so, return 0 for
186 * test success, else -1.
188 static int two_session_same_name(void)
191 struct ltt_session
*sess
;
193 ret
= create_one_session(SESSION1
);
201 sess
= session_find_by_name(SESSION1
);
205 session_unlock_list();
214 session_unlock_list();
219 static void test_session_list(void)
221 session_list
= session_get_list();
222 ok(session_list
!= NULL
, "Session list: not NULL");
225 static void test_create_one_session(void)
227 ok(create_one_session(SESSION1
) == 0,
228 "Create session: %s",
232 static void test_validate_session(void)
234 struct ltt_session
*tmp
;
237 tmp
= session_find_by_name(SESSION1
);
240 "Validating session: session found");
243 ok(tmp
->kernel_session
== NULL
&&
245 "Validating session: basic sanity check");
247 skip(1, "Skipping session validation check as session was not found");
255 session_unlock_list();
258 static void test_destroy_session(void)
260 struct ltt_session
*tmp
;
263 tmp
= session_find_by_name(SESSION1
);
266 "Destroying session: session found");
269 ok(destroy_one_session(tmp
) == 0,
270 "Destroying session: %s destroyed",
273 skip(1, "Skipping session destruction as it was not found");
275 session_unlock_list();
278 static void test_duplicate_session(void)
280 ok(two_session_same_name() == 0,
281 "Duplicate session creation");
284 static void test_session_name_generation(void)
286 struct ltt_session
*session
= NULL
;
287 enum lttng_error_code ret_code
;
288 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
291 ret_code
= session_create(NULL
, geteuid(), getegid(), &session
);
292 ok(ret_code
== LTTNG_OK
,
293 "Create session with a NULL name (auto-generate a name)");
295 skip(1, "Skipping session name generation tests as session_create() failed.");
298 diag("Automatically-generated session name: %s", *session
->name
?
299 session
->name
: "ERROR");
300 ok(*session
->name
&& !strncmp(expected_session_name_prefix
, session
->name
,
301 sizeof(DEFAULT_SESSION_NAME
) - 1),
302 "Auto-generated session name starts with %s",
303 DEFAULT_SESSION_NAME
);
305 session_put(session
);
306 session_unlock_list();
309 static void test_large_session_number(void)
311 int ret
, i
, failed
= 0;
312 struct ltt_session
*iter
, *tmp
;
314 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
315 char *tmp_name
= get_random_string();
316 ret
= create_one_session(tmp_name
);
318 diag("session %d (name: %s) creation failed", i
, tmp_name
);
324 "Large sessions number: created %u sessions",
330 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
331 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
332 assert(session_get(iter
));
333 ret
= destroy_one_session(iter
);
335 diag("session %d destroy failed", i
);
340 session_unlock_list();
342 ok(failed
== 0 && session_list_count() == 0,
343 "Large sessions number: destroyed %u sessions",
347 int main(int argc
, char **argv
)
349 struct lttng_thread
*ht_cleanup_thread
;
351 plan_tests(NUM_TESTS
);
353 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
354 ht_cleanup_thread
= launch_ht_cleanup_thread();
355 assert(ht_cleanup_thread
);
356 lttng_thread_put(ht_cleanup_thread
);
358 diag("Sessions unit tests");
360 rcu_register_thread();
364 test_create_one_session();
366 test_validate_session();
368 test_destroy_session();
370 test_duplicate_session();
372 empty_session_list();
374 test_session_name_generation();
376 test_large_session_number();
378 rcu_unregister_thread();
379 lttng_thread_list_shutdown_orphans();
381 return exit_status();