Tests: Add a trace statistics utility
[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 #include <common/common.h>
32
33 #include "utils.h"
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
41 #define RANDOM_STRING_LEN 11
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
56 static struct ltt_session_list *session_list;
57
58 /* For lttngerr.h */
59 int lttng_opt_quiet = 1;
60 int lttng_opt_verbose = 0;
61
62 static const char alphanum[] =
63 "0123456789"
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
65 "abcdefghijklmnopqrstuvwxyz";
66 static char random_string[RANDOM_STRING_LEN];
67
68 /*
69 * Return random string of 10 characters.
70 * Not thread-safe.
71 */
72 static char *get_random_string(void)
73 {
74 int i;
75
76 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
77 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
78 }
79
80 random_string[RANDOM_STRING_LEN - 1] = '\0';
81
82 return random_string;
83 }
84
85 /*
86 * Return 0 if session name is found, else -1
87 */
88 static 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
101 static 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
112 /*
113 * Empty session list manually.
114 */
115 static 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);
121 free(iter);
122 }
123
124 /* Session list must be 0 */
125 assert(!session_list_count());
126 }
127
128 /*
129 * Test creation of 1 session
130 */
131 static int create_one_session(char *name, char *path)
132 {
133 int ret;
134
135 ret = session_create(name, path, geteuid(), getegid());
136 if (ret == LTTNG_OK) {
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 }
147 } else {
148 if (ret == LTTNG_ERR_EXIST_SESS) {
149 printf("(session already exists) ");
150 }
151 return -1;
152 }
153
154 return 0;
155 }
156
157 /*
158 * Test deletion of 1 session
159 */
160 static int destroy_one_session(struct ltt_session *session)
161 {
162 int ret;
163
164 ret = session_destroy(session);
165
166 if (ret == LTTNG_OK) {
167 /* Validate */
168 if (session == NULL) {
169 return 0;
170 }
171 ret = find_session_name(session->name);
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 }
179 }
180
181 return 0;
182 }
183
184 static int fuzzing_create_args(void)
185 {
186 int ret;
187
188 ret = create_one_session(NULL, NULL);
189 if (ret > 0) {
190 printf("Session created with (null),(null)\n");
191 return -1;
192 }
193
194 ret = create_one_session(NULL, PATH1);
195 if (ret > 0) {
196 printf("Session created with (null), %s)\n", PATH1);
197 return -1;
198 }
199
200 /* Session list must be 0 */
201 assert(!session_list_count());
202
203 return 0;
204 }
205
206 static int fuzzing_destroy_args(void)
207 {
208 int ret;
209
210 ret = destroy_one_session(NULL);
211 if (ret > 0) {
212 printf("Session destroyed with (null)\n");
213 return -1;
214 }
215
216 /* Session list must be 0 */
217 assert(!session_list_count());
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 */
226 static int two_session_same_name(void)
227 {
228 int ret;
229 struct ltt_session *sess;
230
231 ret = create_one_session(SESSION1, PATH1);
232 if (ret < 0) {
233 /* Fail */
234 return -1;
235 }
236
237 sess = session_find_by_name(SESSION1);
238 if (sess) {
239 /* Success */
240 return 0;
241 }
242
243 /* Fail */
244 return -1;
245 }
246
247 int main(int argc, char **argv)
248 {
249 int ret, i;
250 struct ltt_session *iter, *tmp;
251
252 srand(time(NULL));
253
254 printf("\nTesting Sessions:\n-----------\n");
255
256 session_list = session_get_list();
257 if (session_list == NULL) {
258 return -1;
259 }
260
261 printf("Create 1 session %s: ", SESSION1);
262 fflush(stdout);
263 ret = create_one_session(SESSION1, PATH1);
264 if (ret < 0) {
265 return -1;
266 }
267 PRINT_OK();
268
269 printf("Validating created session %s: ", SESSION1);
270 fflush(stdout);
271 tmp = session_find_by_name(SESSION1);
272 if (tmp == NULL) {
273 return -1;
274 }
275 /* Basic init session values */
276 assert(tmp->kernel_session == NULL);
277 assert(strlen(tmp->path));
278 assert(strlen(tmp->name));
279 session_lock(tmp);
280 session_unlock(tmp);
281
282 PRINT_OK();
283
284 printf("Destroy 1 session %s: ", SESSION1);
285 fflush(stdout);
286 ret = destroy_one_session(tmp);
287 if (ret < 0) {
288 return -1;
289 }
290 PRINT_OK();
291
292 printf("Two session with same name: ");
293 fflush(stdout);
294 ret = two_session_same_name();
295 if (ret < 0) {
296 return -1;
297 }
298 PRINT_OK();
299
300 empty_session_list();
301
302 printf("Fuzzing create_session arguments: ");
303 fflush(stdout);
304 ret = fuzzing_create_args();
305 if (ret < 0) {
306 return -1;
307 }
308 PRINT_OK();
309
310 printf("Fuzzing destroy_session argument: ");
311 fflush(stdout);
312 ret = fuzzing_destroy_args();
313 if (ret < 0) {
314 return -1;
315 }
316 PRINT_OK();
317
318 printf("Creating %d sessions: ", MAX_SESSIONS);
319 fflush(stdout);
320 for (i = 0; i < MAX_SESSIONS; i++) {
321 char *tmp_name = get_random_string();
322
323 ret = create_one_session(tmp_name, PATH1);
324 if (ret < 0) {
325 printf("session %d (name: %s) creation failed\n", i, tmp_name);
326 return -1;
327 }
328
329 if ((i % 1000) == 0) {
330 fprintf(stdout, "%d..", i);
331 fflush(stdout);
332 }
333 }
334 PRINT_OK();
335
336 printf("Destroying %d sessions: ", MAX_SESSIONS);
337 fflush(stdout);
338 for (i = 0; i < MAX_SESSIONS; i++) {
339 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
340 ret = destroy_one_session(iter);
341 if (ret < 0) {
342 printf("session %d (name: %s) creation failed\n", i, iter->name);
343 return -1;
344 }
345 }
346 }
347 PRINT_OK();
348
349 /* Session list must be 0 */
350 assert(!session_list_count());
351
352 /* Success */
353 return 0;
354 }
This page took 0.037289 seconds and 4 git commands to generate.