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>
30 #include <bin/lttng-sessiond/session.h>
31 #include <bin/lttng-sessiond/ust-app.h>
32 #include <bin/lttng-sessiond/ht-cleanup.h>
33 #include <bin/lttng-sessiond/health-sessiond.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/common.h>
37 #define SESSION1 "test1"
39 #define MAX_SESSIONS 10000
40 #define RANDOM_STRING_LEN 11
42 /* Number of TAP tests in this file */
45 struct health_app
*health_sessiond
;
46 static struct ltt_session_list
*session_list
;
47 static pthread_t ht_cleanup_thread
;
50 int lttng_opt_quiet
= 1;
51 int lttng_opt_verbose
= 0;
54 int ust_consumerd32_fd
;
55 int ust_consumerd64_fd
;
57 static const char alphanum
[] =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
60 "abcdefghijklmnopqrstuvwxyz";
61 static char random_string
[RANDOM_STRING_LEN
];
64 * Return random string of 10 characters.
67 static char *get_random_string(void)
71 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
72 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
75 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
81 * Return 0 if session name is found, else -1
83 static int find_session_name(char *name
)
85 struct ltt_session
*iter
;
87 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
88 if (strcmp(iter
->name
, name
) == 0) {
96 static int session_list_count(void)
99 struct ltt_session
*iter
;
101 cds_list_for_each_entry(iter
, &session_list
->head
, list
) {
108 * Empty session list manually.
110 static void empty_session_list(void)
112 struct ltt_session
*iter
, *tmp
;
114 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
115 session_destroy(iter
);
118 /* Session list must be 0 */
119 assert(!session_list_count());
123 * Test creation of 1 session
125 static int create_one_session(char *name
)
129 ret
= session_create(name
, geteuid(), getegid());
130 if (ret
== LTTNG_OK
) {
132 ret
= find_session_name(name
);
134 /* Session not found by name */
135 printf("session not found after creation\n");
142 if (ret
== LTTNG_ERR_EXIST_SESS
) {
143 printf("(session already exists) ");
152 * Test deletion of 1 session
154 static int destroy_one_session(struct ltt_session
*session
)
157 char session_name
[NAME_MAX
];
159 strncpy(session_name
, session
->name
, sizeof(session
->name
));
160 session_name
[sizeof(session_name
) - 1] = '\0';
162 ret
= session_destroy(session
);
163 if (ret
== LTTNG_OK
) {
164 ret
= find_session_name(session_name
);
166 /* Success, -1 means that the sesion is NOT found */
178 * This test is supposed to fail at the second create call. If so, return 0 for
179 * test success, else -1.
181 static int two_session_same_name(void)
184 struct ltt_session
*sess
;
186 ret
= create_one_session(SESSION1
);
192 sess
= session_find_by_name(SESSION1
);
202 void test_session_list(void)
204 session_list
= session_get_list();
205 ok(session_list
!= NULL
, "Session list: not NULL");
208 void test_create_one_session(void)
210 ok(create_one_session(SESSION1
) == 0,
211 "Create session: %s",
215 void test_validate_session(void)
217 struct ltt_session
*tmp
;
219 tmp
= session_find_by_name(SESSION1
);
222 "Validating session: session found");
224 ok(tmp
->kernel_session
== NULL
&&
226 "Validating session: basic sanity check");
232 void test_destroy_session(void)
234 struct ltt_session
*tmp
;
236 tmp
= session_find_by_name(SESSION1
);
239 "Destroying session: session found");
241 ok(destroy_one_session(tmp
) == 0,
242 "Destroying session: %s destroyed",
246 void test_duplicate_session(void)
248 ok(two_session_same_name() == 0,
249 "Duplicate session creation");
252 void test_bogus_session_param(void)
254 ok(create_one_session(NULL
) < 0,
255 "Create session with bogus param: NULL should fail");
257 ok(session_list_count() == 0,
258 "Create session with bogus param: session list empty");
261 void test_large_session_number(void)
263 int ret
, i
, failed
= 0;
264 struct ltt_session
*iter
, *tmp
;
266 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
267 char *tmp_name
= get_random_string();
268 ret
= create_one_session(tmp_name
);
270 diag("session %d (name: %s) creation failed", i
, tmp_name
);
276 "Large sessions number: created %u sessions",
281 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
282 cds_list_for_each_entry_safe(iter
, tmp
, &session_list
->head
, list
) {
283 ret
= destroy_one_session(iter
);
285 diag("session %d destroy failed", i
);
291 ok(failed
== 0 && session_list_count() == 0,
292 "Large sessions number: destroyed %u sessions",
296 int main(int argc
, char **argv
)
298 plan_tests(NUM_TESTS
);
300 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
301 assert(!init_ht_cleanup_thread(&ht_cleanup_thread
));
303 diag("Sessions unit tests");
307 test_create_one_session();
309 test_validate_session();
311 test_destroy_session();
313 test_duplicate_session();
315 empty_session_list();
317 test_bogus_session_param();
319 test_large_session_number();
321 assert(!fini_ht_cleanup_thread(&ht_cleanup_thread
));
323 return exit_status();