docs: Add supported versions and fix-backport policy
[lttng-tools.git] / tests / unit / test_session.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <urcu.h>
15
16 #include <tap/tap.h>
17
18 #include <bin/lttng-sessiond/health-sessiond.hpp>
19 #include <bin/lttng-sessiond/session.hpp>
20 #include <bin/lttng-sessiond/thread.hpp>
21 #include <bin/lttng-sessiond/ust-app.hpp>
22 #include <common/common.hpp>
23 #include <common/compat/errno.hpp>
24 #include <common/sessiond-comm/sessiond-comm.hpp>
25
26 #define SESSION1 "test1"
27
28 #define MAX_SESSIONS 10000
29 #define RANDOM_STRING_LEN 11
30
31 /* Number of TAP tests in this file */
32 #define NUM_TESTS 11
33
34 static struct ltt_session_list *session_list;
35
36 static const char alphanum[] =
37 "0123456789"
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
39 "abcdefghijklmnopqrstuvwxyz";
40 static char random_string[RANDOM_STRING_LEN];
41
42 /*
43 * Return random string of 10 characters.
44 * Not thread-safe.
45 */
46 static char *get_random_string(void)
47 {
48 int i;
49
50 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
51 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
52 }
53
54 random_string[RANDOM_STRING_LEN - 1] = '\0';
55
56 return random_string;
57 }
58
59 /*
60 * Return 0 if session name is found, else -1
61 */
62 static int find_session_name(const char *name)
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
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
86 /*
87 * Empty session list manually.
88 */
89 static void empty_session_list(void)
90 {
91 struct ltt_session *iter, *tmp;
92
93 session_lock_list();
94 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
95 session_destroy(iter);
96 }
97 session_unlock_list();
98
99 /* Session list must be 0 */
100 LTTNG_ASSERT(!session_list_count());
101 }
102
103 /*
104 * Test creation of 1 session
105 */
106 static int create_one_session(const char *name)
107 {
108 int ret;
109 enum lttng_error_code ret_code;
110 struct ltt_session *session = NULL;
111
112 session_lock_list();
113 ret_code = session_create(name, geteuid(), getegid(), &session);
114 session_put(session);
115 if (ret_code == LTTNG_OK) {
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");
121 ret = -1;
122 } else {
123 /* Success */
124 ret = 0;
125 }
126 } else {
127 if (ret_code == LTTNG_ERR_EXIST_SESS) {
128 printf("(session already exists) ");
129 }
130 ret = -1;
131 }
132
133 session_unlock_list();
134 return ret;
135 }
136
137 /*
138 * Test deletion of 1 session
139 */
140 static int destroy_one_session(struct ltt_session *session)
141 {
142 int ret;
143 char session_name[NAME_MAX];
144
145 strncpy(session_name, session->name, sizeof(session_name));
146 session_name[sizeof(session_name) - 1] = '\0';
147
148 session_destroy(session);
149 session_put(session);
150
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;
160 }
161
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;
169 struct ltt_session *sess;
170
171 ret = create_one_session(SESSION1);
172 if (ret < 0) {
173 /* Fail */
174 ret = -1;
175 goto end;
176 }
177
178 session_lock_list();
179 sess = session_find_by_name(SESSION1);
180 if (sess) {
181 /* Success */
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;
190 }
191 end_unlock:
192 session_unlock_list();
193 end:
194 return ret;
195 }
196
197 static void test_session_list(void)
198 {
199 session_list = session_get_list();
200 ok(session_list != NULL, "Session list: not NULL");
201 }
202
203 static void test_create_one_session(void)
204 {
205 ok(create_one_session(SESSION1) == 0,
206 "Create session: %s",
207 SESSION1);
208 }
209
210 static void test_validate_session(void)
211 {
212 struct ltt_session *tmp;
213
214 session_lock_list();
215 tmp = session_find_by_name(SESSION1);
216
217 ok(tmp != NULL,
218 "Validating session: session found");
219
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 }
228
229 session_lock(tmp);
230 session_unlock(tmp);
231 session_put(tmp);
232 end:
233 session_unlock_list();
234 }
235
236 static void test_destroy_session(void)
237 {
238 struct ltt_session *tmp;
239
240 session_lock_list();
241 tmp = session_find_by_name(SESSION1);
242
243 ok(tmp != NULL,
244 "Destroying session: session found");
245
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 }
253 session_unlock_list();
254 }
255
256 static void test_duplicate_session(void)
257 {
258 ok(two_session_same_name() == 0,
259 "Duplicate session creation");
260 }
261
262 static void test_session_name_generation(void)
263 {
264 struct ltt_session *session = NULL;
265 enum lttng_error_code ret_code;
266 const char *expected_session_name_prefix = DEFAULT_SESSION_NAME;
267
268 session_lock_list();
269 ret_code = session_create(NULL, geteuid(), getegid(), &session);
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();
285 }
286
287 static void test_large_session_number(void)
288 {
289 int ret, i, failed = 0;
290 struct ltt_session *iter, *tmp;
291
292 for (i = 0; i < MAX_SESSIONS; i++) {
293 char *tmp_name = get_random_string();
294 ret = create_one_session(tmp_name);
295 if (ret < 0) {
296 diag("session %d (name: %s) creation failed", i, tmp_name);
297 ++failed;
298 }
299 }
300
301 ok(failed == 0,
302 "Large sessions number: created %u sessions",
303 MAX_SESSIONS);
304
305 failed = 0;
306
307 session_lock_list();
308 for (i = 0; i < MAX_SESSIONS; i++) {
309 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
310 LTTNG_ASSERT(session_get(iter));
311 ret = destroy_one_session(iter);
312 if (ret < 0) {
313 diag("session %d destroy failed", i);
314 ++failed;
315 }
316 }
317 }
318 session_unlock_list();
319
320 ok(failed == 0 && session_list_count() == 0,
321 "Large sessions number: destroyed %u sessions",
322 MAX_SESSIONS);
323 }
324
325 int main(void)
326 {
327
328 plan_tests(NUM_TESTS);
329
330 the_health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
331
332 diag("Sessions unit tests");
333
334 rcu_register_thread();
335
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
348 test_session_name_generation();
349
350 test_large_session_number();
351
352 rcu_unregister_thread();
353 lttng_thread_list_shutdown_orphans();
354
355 return exit_status();
356 }
This page took 0.035866 seconds and 4 git commands to generate.