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