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