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