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