Network streaming support
[lttng-tools.git] / tests / tools / test_sessions.c
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>
27 #include <sys/types.h>
28
29 #include <bin/lttng-sessiond/session.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31
32 #include "utils.h"
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
40 #define RANDOM_STRING_LEN 11
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
55 static struct ltt_session_list *session_list;
56
57 /* For lttngerr.h */
58 int lttng_opt_quiet = 1;
59 int lttng_opt_verbose = 0;
60
61 static const char alphanum[] =
62 "0123456789"
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
64 "abcdefghijklmnopqrstuvwxyz";
65 static char random_string[RANDOM_STRING_LEN];
66
67 /*
68 * Return random string of 10 characters.
69 * Not thread-safe.
70 */
71 static char *get_random_string(void)
72 {
73 int i;
74
75 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
76 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
77 }
78
79 random_string[RANDOM_STRING_LEN - 1] = '\0';
80
81 return random_string;
82 }
83
84 /*
85 * Return 0 if session name is found, else -1
86 */
87 static 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 */
103 static 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 */
120 static int create_one_session(char *name, char *path)
121 {
122 int ret;
123
124 ret = session_create(name, path, geteuid(), getegid());
125 if (ret == LTTCOMM_OK) {
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 }
136 } else {
137 if (ret == LTTCOMM_EXIST_SESS) {
138 printf("(session already exists) ");
139 }
140 return -1;
141 }
142
143 return 0;
144 }
145
146 /*
147 * Test deletion of 1 session
148 */
149 static int destroy_one_session(struct ltt_session *session)
150 {
151 int ret;
152
153 ret = session_destroy(session);
154
155 if (ret == LTTCOMM_OK) {
156 /* Validate */
157 if (session == NULL) {
158 return 0;
159 }
160 ret = find_session_name(session->name);
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 }
168 }
169
170 return 0;
171 }
172
173 static int fuzzing_create_args(void)
174 {
175 int ret;
176
177 ret = create_one_session(NULL, NULL);
178 if (ret > 0) {
179 printf("Session created with (null),(null)\n");
180 return -1;
181 }
182
183 ret = create_one_session(NULL, PATH1);
184 if (ret > 0) {
185 printf("Session created with (null), %s)\n", PATH1);
186 return -1;
187 }
188
189 ret = create_one_session(SESSION1, NULL);
190 if (ret > 0) {
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
201 static int fuzzing_destroy_args(void)
202 {
203 int ret;
204
205 ret = destroy_one_session(NULL);
206 if (ret > 0) {
207 printf("Session destroyed with (null)\n");
208 return -1;
209 }
210
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 */
221 static int two_session_same_name(void)
222 {
223 int ret;
224 struct ltt_session *sess;
225
226 ret = create_one_session(SESSION1, PATH1);
227 if (ret < 0) {
228 /* Fail */
229 return -1;
230 }
231
232 sess = session_find_by_name(SESSION1);
233 if (sess) {
234 /* Success */
235 return 0;
236 }
237
238 /* Fail */
239 return -1;
240 }
241
242 int main(int argc, char **argv)
243 {
244 int ret, i;
245 struct ltt_session *iter, *tmp;
246
247 srand(time(NULL));
248
249 printf("\nTesting Sessions:\n-----------\n");
250
251 session_list = session_get_list();
252 if (session_list == NULL) {
253 return -1;
254 }
255
256 printf("Create 1 session %s: ", SESSION1);
257 fflush(stdout);
258 ret = create_one_session(SESSION1, PATH1);
259 if (ret < 0) {
260 return -1;
261 }
262 PRINT_OK();
263
264 printf("Validating created session %s: ", SESSION1);
265 fflush(stdout);
266 tmp = session_find_by_name(SESSION1);
267 if (tmp == NULL) {
268 return -1;
269 }
270 /* Basic init session values */
271 assert(tmp->kernel_session == NULL);
272 assert(strlen(tmp->path));
273 assert(strlen(tmp->name));
274 session_lock(tmp);
275 session_unlock(tmp);
276
277 PRINT_OK();
278
279 printf("Destroy 1 session %s: ", SESSION1);
280 fflush(stdout);
281 ret = destroy_one_session(tmp);
282 if (ret < 0) {
283 return -1;
284 }
285 PRINT_OK();
286
287 printf("Two session with same name: ");
288 fflush(stdout);
289 ret = two_session_same_name();
290 if (ret < 0) {
291 return -1;
292 }
293 PRINT_OK();
294
295 empty_session_list();
296
297 printf("Fuzzing create_session arguments: ");
298 fflush(stdout);
299 ret = fuzzing_create_args();
300 if (ret < 0) {
301 return -1;
302 }
303 PRINT_OK();
304
305 printf("Fuzzing destroy_session argument: ");
306 fflush(stdout);
307 ret = fuzzing_destroy_args();
308 if (ret < 0) {
309 return -1;
310 }
311 PRINT_OK();
312
313 printf("Creating %d sessions: ", MAX_SESSIONS);
314 fflush(stdout);
315 for (i = 0; i < MAX_SESSIONS; i++) {
316 char *tmp_name = get_random_string();
317
318 ret = create_one_session(tmp_name, PATH1);
319 if (ret < 0) {
320 printf("session %d (name: %s) creation failed\n", i, tmp_name);
321 return -1;
322 }
323
324 if ((i % 1000) == 0) {
325 fprintf(stdout, "%d..", i);
326 fflush(stdout);
327 }
328 }
329 PRINT_OK();
330
331 printf("Destroying %d sessions: ", MAX_SESSIONS);
332 fflush(stdout);
333 for (i = 0; i < MAX_SESSIONS; i++) {
334 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
335 ret = destroy_one_session(iter);
336 if (ret < 0) {
337 printf("session %d (name: %s) creation failed\n", i, iter->name);
338 return -1;
339 }
340 }
341 }
342 PRINT_OK();
343
344 /* Session list must be 0 */
345 assert(!session_list->count);
346
347 /* Success */
348 return 0;
349 }
This page took 0.036929 seconds and 5 git commands to generate.