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