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