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