Print an error message when a session fails to load
[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
19#define _GNU_SOURCE
20#include <assert.h>
21#include <errno.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <time.h>
6df2e2c9 27#include <sys/types.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>
10a8a223 33#include <common/sessiond-comm/sessiond-comm.h>
f73fabfd 34#include <common/common.h>
f6a9efaa 35
63371d1e
DG
36#define SESSION1 "test1"
37
63371d1e 38#define MAX_SESSIONS 10000
98612240 39#define RANDOM_STRING_LEN 11
63371d1e 40
83c55082 41/* Number of TAP tests in this file */
dec56f6c 42#define NUM_TESTS 11
63371d1e
DG
43
44static struct ltt_session_list *session_list;
45
ad7c9c18 46/* For error.h */
97e19046
DG
47int lttng_opt_quiet = 1;
48int lttng_opt_verbose = 0;
63371d1e 49
7972aab2
DG
50int ust_consumerd32_fd;
51int ust_consumerd64_fd;
52
63371d1e
DG
53static const char alphanum[] =
54 "0123456789"
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
56 "abcdefghijklmnopqrstuvwxyz";
98612240 57static char random_string[RANDOM_STRING_LEN];
63371d1e
DG
58
59/*
60 * Return random string of 10 characters.
98612240 61 * Not thread-safe.
63371d1e
DG
62 */
63static char *get_random_string(void)
64{
65 int i;
63371d1e 66
98612240
MD
67 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
68 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
63371d1e
DG
69 }
70
98612240 71 random_string[RANDOM_STRING_LEN - 1] = '\0';
63371d1e 72
98612240 73 return random_string;
63371d1e
DG
74}
75
76/*
77 * Return 0 if session name is found, else -1
78 */
79static int find_session_name(char *name)
80{
81 struct ltt_session *iter;
82
83 cds_list_for_each_entry(iter, &session_list->head, list) {
84 if (strcmp(iter->name, name) == 0) {
85 return 0;
86 }
87 }
88
89 return -1;
90}
91
cc305a0b
MD
92static int session_list_count(void)
93{
94 int count = 0;
95 struct ltt_session *iter;
96
97 cds_list_for_each_entry(iter, &session_list->head, list) {
98 count++;
99 }
100 return count;
101}
102
63371d1e
DG
103/*
104 * Empty session list manually.
105 */
106static void empty_session_list(void)
107{
108 struct ltt_session *iter, *tmp;
109
110 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
111 cds_list_del(&iter->list);
63371d1e
DG
112 free(iter);
113 }
114
115 /* Session list must be 0 */
cc305a0b 116 assert(!session_list_count());
63371d1e
DG
117}
118
119/*
120 * Test creation of 1 session
121 */
dec56f6c 122static int create_one_session(char *name)
63371d1e
DG
123{
124 int ret;
125
dec56f6c 126 ret = session_create(name, geteuid(), getegid());
f73fabfd 127 if (ret == LTTNG_OK) {
63371d1e
DG
128 /* Validate */
129 ret = find_session_name(name);
130 if (ret < 0) {
131 /* Session not found by name */
132 printf("session not found after creation\n");
133 return -1;
134 } else {
135 /* Success */
136 return 0;
137 }
54d01ffb 138 } else {
f73fabfd 139 if (ret == LTTNG_ERR_EXIST_SESS) {
63371d1e
DG
140 printf("(session already exists) ");
141 }
142 return -1;
143 }
144
145 return 0;
146}
147
148/*
149 * Test deletion of 1 session
150 */
271933a4 151static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
152{
153 int ret;
59891c42 154 char session_name[NAME_MAX];
63371d1e 155
59891c42
DG
156 strncpy(session_name, session->name, sizeof(session->name));
157 session_name[sizeof(session_name) - 1] = '\0';
54d01ffb 158
59891c42 159 ret = session_destroy(session);
f73fabfd 160 if (ret == LTTNG_OK) {
59891c42 161 ret = find_session_name(session_name);
63371d1e
DG
162 if (ret < 0) {
163 /* Success, -1 means that the sesion is NOT found */
164 return 0;
165 } else {
166 /* Fail */
167 return -1;
168 }
63371d1e
DG
169 }
170
171 return 0;
172}
173
63371d1e
DG
174/*
175 * This test is supposed to fail at the second create call. If so, return 0 for
176 * test success, else -1.
177 */
178static int two_session_same_name(void)
179{
180 int ret;
00e2e675 181 struct ltt_session *sess;
63371d1e 182
dec56f6c 183 ret = create_one_session(SESSION1);
63371d1e
DG
184 if (ret < 0) {
185 /* Fail */
186 return -1;
187 }
188
00e2e675
DG
189 sess = session_find_by_name(SESSION1);
190 if (sess) {
63371d1e
DG
191 /* Success */
192 return 0;
193 }
194
195 /* Fail */
196 return -1;
197}
198
83c55082 199void test_session_list(void)
63371d1e 200{
83c55082
CB
201 session_list = session_get_list();
202 ok(session_list != NULL, "Session list: not NULL");
203}
63371d1e 204
83c55082
CB
205void test_create_one_session(void)
206{
dec56f6c 207 ok(create_one_session(SESSION1) == 0,
83c55082
CB
208 "Create session: %s",
209 SESSION1);
210}
63371d1e 211
83c55082
CB
212void test_validate_session(void)
213{
214 struct ltt_session *tmp;
63371d1e 215
83c55082 216 tmp = session_find_by_name(SESSION1);
63371d1e 217
83c55082
CB
218 ok(tmp != NULL,
219 "Validating session: session found");
220
221 ok(tmp->kernel_session == NULL &&
83c55082
CB
222 strlen(tmp->name),
223 "Validating session: basic sanity check");
63371d1e 224
54d01ffb
DG
225 session_lock(tmp);
226 session_unlock(tmp);
83c55082 227}
63371d1e 228
83c55082
CB
229void test_destroy_session(void)
230{
231 struct ltt_session *tmp;
63371d1e 232
83c55082 233 tmp = session_find_by_name(SESSION1);
63371d1e 234
83c55082
CB
235 ok(tmp != NULL,
236 "Destroying session: session found");
63371d1e 237
83c55082
CB
238 ok(destroy_one_session(tmp) == 0,
239 "Destroying session: %s destroyed",
240 SESSION1);
241}
63371d1e 242
83c55082
CB
243void test_duplicate_session(void)
244{
245 ok(two_session_same_name() == 0,
246 "Duplicate session creation");
247}
248
249void test_bogus_session_param(void)
250{
dec56f6c
DG
251 ok(create_one_session(NULL) < 0,
252 "Create session with bogus param: NULL should fail");
83c55082
CB
253
254 ok(session_list_count() == 0,
255 "Create session with bogus param: session list empty");
256}
257
258void test_large_session_number(void)
259{
260 int ret, i, failed = 0;
261 struct ltt_session *iter, *tmp;
63371d1e 262
63371d1e 263 for (i = 0; i < MAX_SESSIONS; i++) {
e6dd5671 264 char *tmp_name = get_random_string();
dec56f6c 265 ret = create_one_session(tmp_name);
63371d1e 266 if (ret < 0) {
83c55082
CB
267 diag("session %d (name: %s) creation failed", i, tmp_name);
268 ++failed;
db9b8b88 269 }
63371d1e 270 }
63371d1e 271
83c55082
CB
272 ok(failed == 0,
273 "Large sessions number: created %u sessions",
274 MAX_SESSIONS);
275
276 failed = 0;
277
63371d1e
DG
278 for (i = 0; i < MAX_SESSIONS; i++) {
279 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
271933a4 280 ret = destroy_one_session(iter);
63371d1e 281 if (ret < 0) {
59891c42 282 diag("session %d destroy failed", i);
83c55082 283 ++failed;
63371d1e
DG
284 }
285 }
286 }
63371d1e 287
83c55082
CB
288 ok(failed == 0 && session_list_count() == 0,
289 "Large sessions number: destroyed %u sessions",
290 MAX_SESSIONS);
291}
63371d1e 292
83c55082
CB
293int main(int argc, char **argv)
294{
83c55082
CB
295 plan_tests(NUM_TESTS);
296
e3bef725
CB
297 diag("Sessions unit tests");
298
83c55082
CB
299 test_session_list();
300
301 test_create_one_session();
302
303 test_validate_session();
304
305 test_destroy_session();
306
307 test_duplicate_session();
308
309 empty_session_list();
310
311 test_bogus_session_param();
312
313 test_large_session_number();
314
315 return exit_status();
63371d1e 316}
This page took 0.053194 seconds and 4 git commands to generate.