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