Commit | Line | Data |
---|---|---|
63371d1e DG |
1 | /* |
2 | * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
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 | |
7 | * of the License. | |
8 | * | |
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. | |
13 | * | |
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. | |
17 | */ | |
18 | ||
63371d1e DG |
19 | #include <assert.h> |
20 | #include <errno.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <time.h> | |
6df2e2c9 | 26 | #include <sys/types.h> |
8273250b | 27 | #include <urcu.h> |
63371d1e | 28 | |
83c55082 CB |
29 | #include <tap/tap.h> |
30 | ||
10a8a223 | 31 | #include <bin/lttng-sessiond/session.h> |
7972aab2 | 32 | #include <bin/lttng-sessiond/ust-app.h> |
5e97de00 JG |
33 | #include <bin/lttng-sessiond/ht-cleanup.h> |
34 | #include <bin/lttng-sessiond/health-sessiond.h> | |
a3707772 | 35 | #include <bin/lttng-sessiond/thread.h> |
10a8a223 | 36 | #include <common/sessiond-comm/sessiond-comm.h> |
f73fabfd | 37 | #include <common/common.h> |
f6a9efaa | 38 | |
63371d1e DG |
39 | #define SESSION1 "test1" |
40 | ||
63371d1e | 41 | #define MAX_SESSIONS 10000 |
98612240 | 42 | #define RANDOM_STRING_LEN 11 |
63371d1e | 43 | |
83c55082 | 44 | /* Number of TAP tests in this file */ |
dec56f6c | 45 | #define NUM_TESTS 11 |
63371d1e | 46 | |
5e97de00 | 47 | struct health_app *health_sessiond; |
63371d1e DG |
48 | static struct ltt_session_list *session_list; |
49 | ||
ad7c9c18 | 50 | /* For error.h */ |
97e19046 DG |
51 | int lttng_opt_quiet = 1; |
52 | int lttng_opt_verbose = 0; | |
c7e35b03 | 53 | int lttng_opt_mi; |
63371d1e | 54 | |
7972aab2 DG |
55 | int ust_consumerd32_fd; |
56 | int ust_consumerd64_fd; | |
57 | ||
63371d1e DG |
58 | static const char alphanum[] = |
59 | "0123456789" | |
60 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
61 | "abcdefghijklmnopqrstuvwxyz"; | |
98612240 | 62 | static char random_string[RANDOM_STRING_LEN]; |
63371d1e DG |
63 | |
64 | /* | |
65 | * Return random string of 10 characters. | |
98612240 | 66 | * Not thread-safe. |
63371d1e DG |
67 | */ |
68 | static char *get_random_string(void) | |
69 | { | |
70 | int i; | |
63371d1e | 71 | |
98612240 MD |
72 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
73 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; | |
63371d1e DG |
74 | } |
75 | ||
98612240 | 76 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
63371d1e | 77 | |
98612240 | 78 | return random_string; |
63371d1e DG |
79 | } |
80 | ||
81 | /* | |
82 | * Return 0 if session name is found, else -1 | |
83 | */ | |
84 | static int find_session_name(char *name) | |
85 | { | |
86 | struct ltt_session *iter; | |
87 | ||
88 | cds_list_for_each_entry(iter, &session_list->head, list) { | |
89 | if (strcmp(iter->name, name) == 0) { | |
90 | return 0; | |
91 | } | |
92 | } | |
93 | ||
94 | return -1; | |
95 | } | |
96 | ||
cc305a0b MD |
97 | static int session_list_count(void) |
98 | { | |
99 | int count = 0; | |
100 | struct ltt_session *iter; | |
101 | ||
102 | cds_list_for_each_entry(iter, &session_list->head, list) { | |
103 | count++; | |
104 | } | |
105 | return count; | |
106 | } | |
107 | ||
63371d1e DG |
108 | /* |
109 | * Empty session list manually. | |
110 | */ | |
111 | static void empty_session_list(void) | |
112 | { | |
113 | struct ltt_session *iter, *tmp; | |
114 | ||
e32d7f27 | 115 | session_lock_list(); |
63371d1e | 116 | cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { |
23324029 | 117 | session_destroy(iter); |
63371d1e | 118 | } |
e32d7f27 | 119 | session_unlock_list(); |
63371d1e DG |
120 | |
121 | /* Session list must be 0 */ | |
cc305a0b | 122 | assert(!session_list_count()); |
63371d1e DG |
123 | } |
124 | ||
125 | /* | |
126 | * Test creation of 1 session | |
127 | */ | |
dec56f6c | 128 | static int create_one_session(char *name) |
63371d1e DG |
129 | { |
130 | int ret; | |
b178f53e JG |
131 | enum lttng_error_code ret_code; |
132 | struct ltt_session *session = NULL; | |
63371d1e | 133 | |
b178f53e | 134 | session_lock_list(); |
e3876bf0 | 135 | ret_code = session_create(name, geteuid(), getegid(), &session); |
b178f53e JG |
136 | session_put(session); |
137 | if (ret_code == LTTNG_OK) { | |
63371d1e DG |
138 | /* Validate */ |
139 | ret = find_session_name(name); | |
140 | if (ret < 0) { | |
141 | /* Session not found by name */ | |
142 | printf("session not found after creation\n"); | |
b178f53e | 143 | ret = -1; |
63371d1e DG |
144 | } else { |
145 | /* Success */ | |
b178f53e | 146 | ret = 0; |
63371d1e | 147 | } |
54d01ffb | 148 | } else { |
b178f53e | 149 | if (ret_code == LTTNG_ERR_EXIST_SESS) { |
63371d1e DG |
150 | printf("(session already exists) "); |
151 | } | |
b178f53e | 152 | ret = -1; |
63371d1e | 153 | } |
3517bb68 | 154 | |
b178f53e JG |
155 | session_unlock_list(); |
156 | return ret; | |
63371d1e DG |
157 | } |
158 | ||
159 | /* | |
160 | * Test deletion of 1 session | |
161 | */ | |
271933a4 | 162 | static int destroy_one_session(struct ltt_session *session) |
63371d1e DG |
163 | { |
164 | int ret; | |
59891c42 | 165 | char session_name[NAME_MAX]; |
63371d1e | 166 | |
4cd95b52 | 167 | strncpy(session_name, session->name, sizeof(session_name)); |
59891c42 | 168 | session_name[sizeof(session_name) - 1] = '\0'; |
54d01ffb | 169 | |
e32d7f27 JG |
170 | session_destroy(session); |
171 | session_put(session); | |
63371d1e | 172 | |
e32d7f27 JG |
173 | ret = find_session_name(session_name); |
174 | if (ret < 0) { | |
175 | /* Success, -1 means that the sesion is NOT found */ | |
176 | ret = 0; | |
177 | } else { | |
178 | /* Fail */ | |
179 | ret = -1; | |
180 | } | |
181 | return ret; | |
63371d1e DG |
182 | } |
183 | ||
63371d1e DG |
184 | /* |
185 | * This test is supposed to fail at the second create call. If so, return 0 for | |
186 | * test success, else -1. | |
187 | */ | |
188 | static int two_session_same_name(void) | |
189 | { | |
190 | int ret; | |
00e2e675 | 191 | struct ltt_session *sess; |
63371d1e | 192 | |
dec56f6c | 193 | ret = create_one_session(SESSION1); |
63371d1e DG |
194 | if (ret < 0) { |
195 | /* Fail */ | |
e32d7f27 JG |
196 | ret = -1; |
197 | goto end; | |
63371d1e DG |
198 | } |
199 | ||
e32d7f27 | 200 | session_lock_list(); |
00e2e675 DG |
201 | sess = session_find_by_name(SESSION1); |
202 | if (sess) { | |
63371d1e | 203 | /* Success */ |
e32d7f27 JG |
204 | session_put(sess); |
205 | session_unlock_list(); | |
206 | ret = 0; | |
207 | goto end_unlock; | |
208 | } else { | |
209 | /* Fail */ | |
210 | ret = -1; | |
211 | goto end_unlock; | |
63371d1e | 212 | } |
e32d7f27 JG |
213 | end_unlock: |
214 | session_unlock_list(); | |
215 | end: | |
216 | return ret; | |
63371d1e DG |
217 | } |
218 | ||
c109c263 | 219 | static void test_session_list(void) |
63371d1e | 220 | { |
83c55082 CB |
221 | session_list = session_get_list(); |
222 | ok(session_list != NULL, "Session list: not NULL"); | |
223 | } | |
63371d1e | 224 | |
c109c263 | 225 | static void test_create_one_session(void) |
83c55082 | 226 | { |
dec56f6c | 227 | ok(create_one_session(SESSION1) == 0, |
83c55082 CB |
228 | "Create session: %s", |
229 | SESSION1); | |
230 | } | |
63371d1e | 231 | |
c109c263 | 232 | static void test_validate_session(void) |
83c55082 CB |
233 | { |
234 | struct ltt_session *tmp; | |
63371d1e | 235 | |
e32d7f27 | 236 | session_lock_list(); |
83c55082 | 237 | tmp = session_find_by_name(SESSION1); |
63371d1e | 238 | |
83c55082 CB |
239 | ok(tmp != NULL, |
240 | "Validating session: session found"); | |
241 | ||
d61d06f0 JG |
242 | if (tmp) { |
243 | ok(tmp->kernel_session == NULL && | |
244 | strlen(tmp->name), | |
245 | "Validating session: basic sanity check"); | |
246 | } else { | |
247 | skip(1, "Skipping session validation check as session was not found"); | |
248 | goto end; | |
249 | } | |
63371d1e | 250 | |
54d01ffb DG |
251 | session_lock(tmp); |
252 | session_unlock(tmp); | |
e32d7f27 | 253 | session_put(tmp); |
d61d06f0 | 254 | end: |
e32d7f27 | 255 | session_unlock_list(); |
83c55082 | 256 | } |
63371d1e | 257 | |
c109c263 | 258 | static void test_destroy_session(void) |
83c55082 CB |
259 | { |
260 | struct ltt_session *tmp; | |
63371d1e | 261 | |
e32d7f27 | 262 | session_lock_list(); |
83c55082 | 263 | tmp = session_find_by_name(SESSION1); |
63371d1e | 264 | |
83c55082 CB |
265 | ok(tmp != NULL, |
266 | "Destroying session: session found"); | |
63371d1e | 267 | |
acd4994e JG |
268 | if (tmp) { |
269 | ok(destroy_one_session(tmp) == 0, | |
270 | "Destroying session: %s destroyed", | |
271 | SESSION1); | |
272 | } else { | |
273 | skip(1, "Skipping session destruction as it was not found"); | |
274 | } | |
e32d7f27 | 275 | session_unlock_list(); |
83c55082 | 276 | } |
63371d1e | 277 | |
c109c263 | 278 | static void test_duplicate_session(void) |
83c55082 CB |
279 | { |
280 | ok(two_session_same_name() == 0, | |
281 | "Duplicate session creation"); | |
282 | } | |
283 | ||
c109c263 | 284 | static void test_session_name_generation(void) |
83c55082 | 285 | { |
b178f53e JG |
286 | struct ltt_session *session = NULL; |
287 | enum lttng_error_code ret_code; | |
288 | const char *expected_session_name_prefix = DEFAULT_SESSION_NAME; | |
83c55082 | 289 | |
b178f53e | 290 | session_lock_list(); |
e3876bf0 | 291 | ret_code = session_create(NULL, geteuid(), getegid(), &session); |
b178f53e JG |
292 | ok(ret_code == LTTNG_OK, |
293 | "Create session with a NULL name (auto-generate a name)"); | |
294 | if (!session) { | |
295 | skip(1, "Skipping session name generation tests as session_create() failed."); | |
296 | goto end; | |
297 | } | |
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); | |
304 | end: | |
305 | session_put(session); | |
306 | session_unlock_list(); | |
83c55082 CB |
307 | } |
308 | ||
c109c263 | 309 | static void test_large_session_number(void) |
83c55082 CB |
310 | { |
311 | int ret, i, failed = 0; | |
312 | struct ltt_session *iter, *tmp; | |
63371d1e | 313 | |
63371d1e | 314 | for (i = 0; i < MAX_SESSIONS; i++) { |
e6dd5671 | 315 | char *tmp_name = get_random_string(); |
dec56f6c | 316 | ret = create_one_session(tmp_name); |
63371d1e | 317 | if (ret < 0) { |
83c55082 CB |
318 | diag("session %d (name: %s) creation failed", i, tmp_name); |
319 | ++failed; | |
db9b8b88 | 320 | } |
63371d1e | 321 | } |
63371d1e | 322 | |
83c55082 CB |
323 | ok(failed == 0, |
324 | "Large sessions number: created %u sessions", | |
325 | MAX_SESSIONS); | |
326 | ||
327 | failed = 0; | |
328 | ||
e32d7f27 | 329 | session_lock_list(); |
63371d1e DG |
330 | for (i = 0; i < MAX_SESSIONS; i++) { |
331 | cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { | |
e32d7f27 | 332 | assert(session_get(iter)); |
271933a4 | 333 | ret = destroy_one_session(iter); |
63371d1e | 334 | if (ret < 0) { |
59891c42 | 335 | diag("session %d destroy failed", i); |
83c55082 | 336 | ++failed; |
63371d1e DG |
337 | } |
338 | } | |
339 | } | |
e32d7f27 | 340 | session_unlock_list(); |
63371d1e | 341 | |
83c55082 CB |
342 | ok(failed == 0 && session_list_count() == 0, |
343 | "Large sessions number: destroyed %u sessions", | |
344 | MAX_SESSIONS); | |
345 | } | |
63371d1e | 346 | |
83c55082 CB |
347 | int main(int argc, char **argv) |
348 | { | |
a3707772 JG |
349 | struct lttng_thread *ht_cleanup_thread; |
350 | ||
83c55082 CB |
351 | plan_tests(NUM_TESTS); |
352 | ||
5e97de00 | 353 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); |
a3707772 JG |
354 | ht_cleanup_thread = launch_ht_cleanup_thread(); |
355 | assert(ht_cleanup_thread); | |
356 | lttng_thread_put(ht_cleanup_thread); | |
5e97de00 | 357 | |
e3bef725 CB |
358 | diag("Sessions unit tests"); |
359 | ||
8273250b JR |
360 | rcu_register_thread(); |
361 | ||
83c55082 CB |
362 | test_session_list(); |
363 | ||
364 | test_create_one_session(); | |
365 | ||
366 | test_validate_session(); | |
367 | ||
368 | test_destroy_session(); | |
369 | ||
370 | test_duplicate_session(); | |
371 | ||
372 | empty_session_list(); | |
373 | ||
b178f53e | 374 | test_session_name_generation(); |
83c55082 CB |
375 | |
376 | test_large_session_number(); | |
377 | ||
8273250b | 378 | rcu_unregister_thread(); |
a3707772 | 379 | lttng_thread_list_shutdown_orphans(); |
5e97de00 | 380 | |
83c55082 | 381 | return exit_status(); |
63371d1e | 382 | } |