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