fc3fbc1ebd98053094050b9694e3d4942a099cbc
[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 static int session_list_count(void)
101 {
102 int count = 0;
103 struct ltt_session *iter;
104
105 cds_list_for_each_entry(iter, &session_list->head, list) {
106 count++;
107 }
108 return count;
109 }
110
111 /*
112 * Empty session list manually.
113 */
114 static void empty_session_list(void)
115 {
116 struct ltt_session *iter, *tmp;
117
118 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
119 cds_list_del(&iter->list);
120 free(iter);
121 }
122
123 /* Session list must be 0 */
124 assert(!session_list_count());
125 }
126
127 /*
128 * Test creation of 1 session
129 */
130 static int create_one_session(char *name, char *path)
131 {
132 int ret;
133
134 ret = session_create(name, path, geteuid(), getegid());
135 if (ret == LTTCOMM_OK) {
136 /* Validate */
137 ret = find_session_name(name);
138 if (ret < 0) {
139 /* Session not found by name */
140 printf("session not found after creation\n");
141 return -1;
142 } else {
143 /* Success */
144 return 0;
145 }
146 } else {
147 if (ret == LTTCOMM_EXIST_SESS) {
148 printf("(session already exists) ");
149 }
150 return -1;
151 }
152
153 return 0;
154 }
155
156 /*
157 * Test deletion of 1 session
158 */
159 static int destroy_one_session(struct ltt_session *session)
160 {
161 int ret;
162
163 ret = session_destroy(session);
164
165 if (ret == LTTCOMM_OK) {
166 /* Validate */
167 if (session == NULL) {
168 return 0;
169 }
170 ret = find_session_name(session->name);
171 if (ret < 0) {
172 /* Success, -1 means that the sesion is NOT found */
173 return 0;
174 } else {
175 /* Fail */
176 return -1;
177 }
178 }
179
180 return 0;
181 }
182
183 static int fuzzing_create_args(void)
184 {
185 int ret;
186
187 ret = create_one_session(NULL, NULL);
188 if (ret > 0) {
189 printf("Session created with (null),(null)\n");
190 return -1;
191 }
192
193 ret = create_one_session(NULL, PATH1);
194 if (ret > 0) {
195 printf("Session created with (null), %s)\n", PATH1);
196 return -1;
197 }
198
199 ret = create_one_session(SESSION1, NULL);
200 if (ret > 0) {
201 printf("Session created with %s, (null)\n", SESSION1);
202 return -1;
203 }
204
205 /* Session list must be 0 */
206 assert(!session_list_count());
207
208 return 0;
209 }
210
211 static int fuzzing_destroy_args(void)
212 {
213 int ret;
214
215 ret = destroy_one_session(NULL);
216 if (ret > 0) {
217 printf("Session destroyed with (null)\n");
218 return -1;
219 }
220
221 /* Session list must be 0 */
222 assert(!session_list_count());
223
224 return 0;
225 }
226
227 /*
228 * This test is supposed to fail at the second create call. If so, return 0 for
229 * test success, else -1.
230 */
231 static int two_session_same_name(void)
232 {
233 int ret;
234 struct ltt_session *sess;
235
236 ret = create_one_session(SESSION1, PATH1);
237 if (ret < 0) {
238 /* Fail */
239 return -1;
240 }
241
242 sess = session_find_by_name(SESSION1);
243 if (sess) {
244 /* Success */
245 return 0;
246 }
247
248 /* Fail */
249 return -1;
250 }
251
252 int main(int argc, char **argv)
253 {
254 int ret, i;
255 struct ltt_session *iter, *tmp;
256
257 srand(time(NULL));
258
259 printf("\nTesting Sessions:\n-----------\n");
260
261 session_list = session_get_list();
262 if (session_list == NULL) {
263 return -1;
264 }
265
266 printf("Create 1 session %s: ", SESSION1);
267 fflush(stdout);
268 ret = create_one_session(SESSION1, PATH1);
269 if (ret < 0) {
270 return -1;
271 }
272 PRINT_OK();
273
274 printf("Validating created session %s: ", SESSION1);
275 fflush(stdout);
276 tmp = session_find_by_name(SESSION1);
277 if (tmp == NULL) {
278 return -1;
279 }
280 /* Basic init session values */
281 assert(tmp->kernel_session == NULL);
282 assert(strlen(tmp->path));
283 assert(strlen(tmp->name));
284 session_lock(tmp);
285 session_unlock(tmp);
286
287 PRINT_OK();
288
289 printf("Destroy 1 session %s: ", SESSION1);
290 fflush(stdout);
291 ret = destroy_one_session(tmp);
292 if (ret < 0) {
293 return -1;
294 }
295 PRINT_OK();
296
297 printf("Two session with same name: ");
298 fflush(stdout);
299 ret = two_session_same_name();
300 if (ret < 0) {
301 return -1;
302 }
303 PRINT_OK();
304
305 empty_session_list();
306
307 printf("Fuzzing create_session arguments: ");
308 fflush(stdout);
309 ret = fuzzing_create_args();
310 if (ret < 0) {
311 return -1;
312 }
313 PRINT_OK();
314
315 printf("Fuzzing destroy_session argument: ");
316 fflush(stdout);
317 ret = fuzzing_destroy_args();
318 if (ret < 0) {
319 return -1;
320 }
321 PRINT_OK();
322
323 printf("Creating %d sessions: ", MAX_SESSIONS);
324 fflush(stdout);
325 for (i = 0; i < MAX_SESSIONS; i++) {
326 char *tmp_name = get_random_string();
327
328 ret = create_one_session(tmp_name, PATH1);
329 if (ret < 0) {
330 printf("session %d (name: %s) creation failed\n", i, tmp_name);
331 return -1;
332 }
333
334 if ((i % 1000) == 0) {
335 fprintf(stdout, "%d..", i);
336 fflush(stdout);
337 }
338 }
339 PRINT_OK();
340
341 printf("Destroying %d sessions: ", MAX_SESSIONS);
342 fflush(stdout);
343 for (i = 0; i < MAX_SESSIONS; i++) {
344 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
345 ret = destroy_one_session(iter);
346 if (ret < 0) {
347 printf("session %d (name: %s) creation failed\n", i, iter->name);
348 return -1;
349 }
350 }
351 }
352 PRINT_OK();
353
354 /* Session list must be 0 */
355 assert(!session_list_count());
356
357 /* Success */
358 return 0;
359 }
This page took 0.037875 seconds and 4 git commands to generate.