Move completed trace archive chunks to an "archives" sub-folder
[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;
131
dec56f6c 132 ret = session_create(name, geteuid(), getegid());
f73fabfd 133 if (ret == LTTNG_OK) {
63371d1e
DG
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 return -1;
140 } else {
141 /* Success */
142 return 0;
143 }
54d01ffb 144 } else {
f73fabfd 145 if (ret == LTTNG_ERR_EXIST_SESS) {
63371d1e
DG
146 printf("(session already exists) ");
147 }
148 return -1;
149 }
150
151 return 0;
152}
153
154/*
155 * Test deletion of 1 session
156 */
271933a4 157static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
158{
159 int ret;
59891c42 160 char session_name[NAME_MAX];
63371d1e 161
4cd95b52 162 strncpy(session_name, session->name, sizeof(session_name));
59891c42 163 session_name[sizeof(session_name) - 1] = '\0';
54d01ffb 164
e32d7f27
JG
165 session_destroy(session);
166 session_put(session);
63371d1e 167
e32d7f27
JG
168 ret = find_session_name(session_name);
169 if (ret < 0) {
170 /* Success, -1 means that the sesion is NOT found */
171 ret = 0;
172 } else {
173 /* Fail */
174 ret = -1;
175 }
176 return ret;
63371d1e
DG
177}
178
63371d1e
DG
179/*
180 * This test is supposed to fail at the second create call. If so, return 0 for
181 * test success, else -1.
182 */
183static int two_session_same_name(void)
184{
185 int ret;
00e2e675 186 struct ltt_session *sess;
63371d1e 187
dec56f6c 188 ret = create_one_session(SESSION1);
63371d1e
DG
189 if (ret < 0) {
190 /* Fail */
e32d7f27
JG
191 ret = -1;
192 goto end;
63371d1e
DG
193 }
194
e32d7f27 195 session_lock_list();
00e2e675
DG
196 sess = session_find_by_name(SESSION1);
197 if (sess) {
63371d1e 198 /* Success */
e32d7f27
JG
199 session_put(sess);
200 session_unlock_list();
201 ret = 0;
202 goto end_unlock;
203 } else {
204 /* Fail */
205 ret = -1;
206 goto end_unlock;
63371d1e 207 }
e32d7f27
JG
208end_unlock:
209 session_unlock_list();
210end:
211 return ret;
63371d1e
DG
212}
213
83c55082 214void test_session_list(void)
63371d1e 215{
83c55082
CB
216 session_list = session_get_list();
217 ok(session_list != NULL, "Session list: not NULL");
218}
63371d1e 219
83c55082
CB
220void test_create_one_session(void)
221{
dec56f6c 222 ok(create_one_session(SESSION1) == 0,
83c55082
CB
223 "Create session: %s",
224 SESSION1);
225}
63371d1e 226
83c55082
CB
227void test_validate_session(void)
228{
229 struct ltt_session *tmp;
63371d1e 230
e32d7f27 231 session_lock_list();
83c55082 232 tmp = session_find_by_name(SESSION1);
63371d1e 233
83c55082
CB
234 ok(tmp != NULL,
235 "Validating session: session found");
236
237 ok(tmp->kernel_session == NULL &&
83c55082
CB
238 strlen(tmp->name),
239 "Validating session: basic sanity check");
63371d1e 240
54d01ffb
DG
241 session_lock(tmp);
242 session_unlock(tmp);
e32d7f27
JG
243 session_put(tmp);
244 session_unlock_list();
83c55082 245}
63371d1e 246
83c55082
CB
247void test_destroy_session(void)
248{
249 struct ltt_session *tmp;
63371d1e 250
e32d7f27 251 session_lock_list();
83c55082 252 tmp = session_find_by_name(SESSION1);
63371d1e 253
83c55082
CB
254 ok(tmp != NULL,
255 "Destroying session: session found");
63371d1e 256
83c55082
CB
257 ok(destroy_one_session(tmp) == 0,
258 "Destroying session: %s destroyed",
259 SESSION1);
e32d7f27 260 session_unlock_list();
83c55082 261}
63371d1e 262
83c55082
CB
263void test_duplicate_session(void)
264{
265 ok(two_session_same_name() == 0,
266 "Duplicate session creation");
267}
268
269void test_bogus_session_param(void)
270{
dec56f6c
DG
271 ok(create_one_session(NULL) < 0,
272 "Create session with bogus param: NULL should fail");
83c55082
CB
273
274 ok(session_list_count() == 0,
275 "Create session with bogus param: session list empty");
276}
277
278void test_large_session_number(void)
279{
280 int ret, i, failed = 0;
281 struct ltt_session *iter, *tmp;
63371d1e 282
63371d1e 283 for (i = 0; i < MAX_SESSIONS; i++) {
e6dd5671 284 char *tmp_name = get_random_string();
dec56f6c 285 ret = create_one_session(tmp_name);
63371d1e 286 if (ret < 0) {
83c55082
CB
287 diag("session %d (name: %s) creation failed", i, tmp_name);
288 ++failed;
db9b8b88 289 }
63371d1e 290 }
63371d1e 291
83c55082
CB
292 ok(failed == 0,
293 "Large sessions number: created %u sessions",
294 MAX_SESSIONS);
295
296 failed = 0;
297
e32d7f27 298 session_lock_list();
63371d1e
DG
299 for (i = 0; i < MAX_SESSIONS; i++) {
300 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
e32d7f27 301 assert(session_get(iter));
271933a4 302 ret = destroy_one_session(iter);
63371d1e 303 if (ret < 0) {
59891c42 304 diag("session %d destroy failed", i);
83c55082 305 ++failed;
63371d1e
DG
306 }
307 }
308 }
e32d7f27 309 session_unlock_list();
63371d1e 310
83c55082
CB
311 ok(failed == 0 && session_list_count() == 0,
312 "Large sessions number: destroyed %u sessions",
313 MAX_SESSIONS);
314}
63371d1e 315
83c55082
CB
316int main(int argc, char **argv)
317{
a3707772
JG
318 struct lttng_thread *ht_cleanup_thread;
319
83c55082
CB
320 plan_tests(NUM_TESTS);
321
5e97de00 322 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
a3707772
JG
323 ht_cleanup_thread = launch_ht_cleanup_thread();
324 assert(ht_cleanup_thread);
325 lttng_thread_put(ht_cleanup_thread);
5e97de00 326
e3bef725
CB
327 diag("Sessions unit tests");
328
8273250b
JR
329 rcu_register_thread();
330
83c55082
CB
331 test_session_list();
332
333 test_create_one_session();
334
335 test_validate_session();
336
337 test_destroy_session();
338
339 test_duplicate_session();
340
341 empty_session_list();
342
343 test_bogus_session_param();
344
345 test_large_session_number();
346
8273250b 347 rcu_unregister_thread();
a3707772 348 lttng_thread_list_shutdown_orphans();
5e97de00 349
83c55082 350 return exit_status();
63371d1e 351}
This page took 0.087881 seconds and 4 git commands to generate.