Tests: Add health check tests to configure
[lttng-tools.git] / tests / tools / test_sessions.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
10a8a223
DG
29#include <bin/lttng-sessiond/session.h>
30#include <common/sessiond-comm/sessiond-comm.h>
f73fabfd 31#include <common/common.h>
f6a9efaa 32
897b8e23 33#include "utils.h"
63371d1e
DG
34
35#define SESSION1 "test1"
36
37/* This path will NEVER be created in this test */
38#define PATH1 "/tmp/.test-junk-lttng"
39
40#define MAX_SESSIONS 10000
98612240 41#define RANDOM_STRING_LEN 11
63371d1e
DG
42
43/*
44 * String of 263 caracters. NAME_MAX + "OVERFLOW". If OVERFLOW appears in the
45 * session name, we have a problem.
46 *
47 * NAME_MAX = 255
48 */
49#define OVERFLOW_SESSION_NAME \
50 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \
51 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \
52 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \
53 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc" \
54 "OVERFLOW"
55
56static struct ltt_session_list *session_list;
57
58/* For lttngerr.h */
97e19046
DG
59int lttng_opt_quiet = 1;
60int lttng_opt_verbose = 0;
63371d1e
DG
61
62static const char alphanum[] =
63 "0123456789"
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
65 "abcdefghijklmnopqrstuvwxyz";
98612240 66static char random_string[RANDOM_STRING_LEN];
63371d1e
DG
67
68/*
69 * Return random string of 10 characters.
98612240 70 * Not thread-safe.
63371d1e
DG
71 */
72static char *get_random_string(void)
73{
74 int i;
63371d1e 75
98612240
MD
76 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
77 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
63371d1e
DG
78 }
79
98612240 80 random_string[RANDOM_STRING_LEN - 1] = '\0';
63371d1e 81
98612240 82 return random_string;
63371d1e
DG
83}
84
85/*
86 * Return 0 if session name is found, else -1
87 */
88static int find_session_name(char *name)
89{
90 struct ltt_session *iter;
91
92 cds_list_for_each_entry(iter, &session_list->head, list) {
93 if (strcmp(iter->name, name) == 0) {
94 return 0;
95 }
96 }
97
98 return -1;
99}
100
cc305a0b
MD
101static int session_list_count(void)
102{
103 int count = 0;
104 struct ltt_session *iter;
105
106 cds_list_for_each_entry(iter, &session_list->head, list) {
107 count++;
108 }
109 return count;
110}
111
63371d1e
DG
112/*
113 * Empty session list manually.
114 */
115static void empty_session_list(void)
116{
117 struct ltt_session *iter, *tmp;
118
119 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
120 cds_list_del(&iter->list);
63371d1e
DG
121 free(iter);
122 }
123
124 /* Session list must be 0 */
cc305a0b 125 assert(!session_list_count());
63371d1e
DG
126}
127
128/*
129 * Test creation of 1 session
130 */
131static int create_one_session(char *name, char *path)
132{
133 int ret;
134
6df2e2c9 135 ret = session_create(name, path, geteuid(), getegid());
f73fabfd 136 if (ret == LTTNG_OK) {
63371d1e
DG
137 /* Validate */
138 ret = find_session_name(name);
139 if (ret < 0) {
140 /* Session not found by name */
141 printf("session not found after creation\n");
142 return -1;
143 } else {
144 /* Success */
145 return 0;
146 }
54d01ffb 147 } else {
f73fabfd 148 if (ret == LTTNG_ERR_EXIST_SESS) {
63371d1e
DG
149 printf("(session already exists) ");
150 }
151 return -1;
152 }
153
154 return 0;
155}
156
157/*
158 * Test deletion of 1 session
159 */
271933a4 160static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
161{
162 int ret;
163
271933a4 164 ret = session_destroy(session);
54d01ffb 165
f73fabfd 166 if (ret == LTTNG_OK) {
63371d1e 167 /* Validate */
271933a4
DG
168 if (session == NULL) {
169 return 0;
170 }
171 ret = find_session_name(session->name);
63371d1e
DG
172 if (ret < 0) {
173 /* Success, -1 means that the sesion is NOT found */
174 return 0;
175 } else {
176 /* Fail */
177 return -1;
178 }
63371d1e
DG
179 }
180
181 return 0;
182}
183
184static int fuzzing_create_args(void)
185{
186 int ret;
187
188 ret = create_one_session(NULL, NULL);
54d01ffb 189 if (ret > 0) {
63371d1e
DG
190 printf("Session created with (null),(null)\n");
191 return -1;
192 }
193
194 ret = create_one_session(NULL, PATH1);
54d01ffb 195 if (ret > 0) {
63371d1e
DG
196 printf("Session created with (null), %s)\n", PATH1);
197 return -1;
198 }
199
63371d1e 200 /* Session list must be 0 */
cc305a0b 201 assert(!session_list_count());
63371d1e
DG
202
203 return 0;
204}
205
206static int fuzzing_destroy_args(void)
207{
208 int ret;
209
210 ret = destroy_one_session(NULL);
54d01ffb 211 if (ret > 0) {
63371d1e
DG
212 printf("Session destroyed with (null)\n");
213 return -1;
214 }
215
63371d1e 216 /* Session list must be 0 */
cc305a0b 217 assert(!session_list_count());
63371d1e
DG
218
219 return 0;
220}
221
222/*
223 * This test is supposed to fail at the second create call. If so, return 0 for
224 * test success, else -1.
225 */
226static int two_session_same_name(void)
227{
228 int ret;
00e2e675 229 struct ltt_session *sess;
63371d1e
DG
230
231 ret = create_one_session(SESSION1, PATH1);
232 if (ret < 0) {
233 /* Fail */
234 return -1;
235 }
236
00e2e675
DG
237 sess = session_find_by_name(SESSION1);
238 if (sess) {
63371d1e
DG
239 /* Success */
240 return 0;
241 }
242
243 /* Fail */
244 return -1;
245}
246
247int main(int argc, char **argv)
248{
249 int ret, i;
63371d1e
DG
250 struct ltt_session *iter, *tmp;
251
252 srand(time(NULL));
253
254 printf("\nTesting Sessions:\n-----------\n");
255
54d01ffb 256 session_list = session_get_list();
63371d1e
DG
257 if (session_list == NULL) {
258 return -1;
259 }
260
261 printf("Create 1 session %s: ", SESSION1);
bcf480b0 262 fflush(stdout);
63371d1e
DG
263 ret = create_one_session(SESSION1, PATH1);
264 if (ret < 0) {
265 return -1;
266 }
897b8e23 267 PRINT_OK();
63371d1e
DG
268
269 printf("Validating created session %s: ", SESSION1);
bcf480b0 270 fflush(stdout);
54d01ffb 271 tmp = session_find_by_name(SESSION1);
63371d1e
DG
272 if (tmp == NULL) {
273 return -1;
274 }
275 /* Basic init session values */
276 assert(tmp->kernel_session == NULL);
63371d1e
DG
277 assert(strlen(tmp->path));
278 assert(strlen(tmp->name));
54d01ffb
DG
279 session_lock(tmp);
280 session_unlock(tmp);
63371d1e 281
897b8e23 282 PRINT_OK();
63371d1e
DG
283
284 printf("Destroy 1 session %s: ", SESSION1);
bcf480b0 285 fflush(stdout);
271933a4 286 ret = destroy_one_session(tmp);
63371d1e
DG
287 if (ret < 0) {
288 return -1;
289 }
897b8e23 290 PRINT_OK();
63371d1e
DG
291
292 printf("Two session with same name: ");
bcf480b0 293 fflush(stdout);
63371d1e
DG
294 ret = two_session_same_name();
295 if (ret < 0) {
296 return -1;
297 }
897b8e23 298 PRINT_OK();
63371d1e
DG
299
300 empty_session_list();
301
302 printf("Fuzzing create_session arguments: ");
bcf480b0 303 fflush(stdout);
63371d1e
DG
304 ret = fuzzing_create_args();
305 if (ret < 0) {
306 return -1;
307 }
897b8e23 308 PRINT_OK();
63371d1e
DG
309
310 printf("Fuzzing destroy_session argument: ");
bcf480b0 311 fflush(stdout);
63371d1e
DG
312 ret = fuzzing_destroy_args();
313 if (ret < 0) {
314 return -1;
315 }
897b8e23 316 PRINT_OK();
63371d1e
DG
317
318 printf("Creating %d sessions: ", MAX_SESSIONS);
bcf480b0 319 fflush(stdout);
63371d1e 320 for (i = 0; i < MAX_SESSIONS; i++) {
e6dd5671
MD
321 char *tmp_name = get_random_string();
322
323 ret = create_one_session(tmp_name, PATH1);
63371d1e
DG
324 if (ret < 0) {
325 printf("session %d (name: %s) creation failed\n", i, tmp_name);
326 return -1;
327 }
db9b8b88
DG
328
329 if ((i % 1000) == 0) {
330 fprintf(stdout, "%d..", i);
331 fflush(stdout);
332 }
63371d1e 333 }
897b8e23 334 PRINT_OK();
63371d1e
DG
335
336 printf("Destroying %d sessions: ", MAX_SESSIONS);
bcf480b0 337 fflush(stdout);
63371d1e
DG
338 for (i = 0; i < MAX_SESSIONS; i++) {
339 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
271933a4 340 ret = destroy_one_session(iter);
63371d1e
DG
341 if (ret < 0) {
342 printf("session %d (name: %s) creation failed\n", i, iter->name);
343 return -1;
344 }
345 }
346 }
897b8e23 347 PRINT_OK();
63371d1e
DG
348
349 /* Session list must be 0 */
cc305a0b 350 assert(!session_list_count());
63371d1e
DG
351
352 /* Success */
353 return 0;
354}
This page took 0.04216 seconds and 4 git commands to generate.