3e9bbbbb5ae64307d1c020beee2e529593c48acf
[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(struct ltt_session *session)
146 {
147 int ret;
148
149 ret = session_destroy(session);
150
151 if (ret == LTTCOMM_OK) {
152 /* Validate */
153 if (session == NULL) {
154 return 0;
155 }
156 ret = find_session_name(session->name);
157 if (ret < 0) {
158 /* Success, -1 means that the sesion is NOT found */
159 return 0;
160 } else {
161 /* Fail */
162 return -1;
163 }
164 }
165
166 return 0;
167 }
168
169 static int fuzzing_create_args(void)
170 {
171 int ret;
172
173 ret = create_one_session(NULL, NULL);
174 if (ret > 0) {
175 printf("Session created with (null),(null)\n");
176 return -1;
177 }
178
179 ret = create_one_session(NULL, PATH1);
180 if (ret > 0) {
181 printf("Session created with (null), %s)\n", PATH1);
182 return -1;
183 }
184
185 ret = create_one_session(SESSION1, NULL);
186 if (ret > 0) {
187 printf("Session created with %s, (null)\n", SESSION1);
188 return -1;
189 }
190
191 /* Session list must be 0 */
192 assert(!session_list->count);
193
194 return 0;
195 }
196
197 static int fuzzing_destroy_args(void)
198 {
199 int ret;
200
201 ret = destroy_one_session(NULL);
202 if (ret > 0) {
203 printf("Session destroyed with (null)\n");
204 return -1;
205 }
206
207 /* Session list must be 0 */
208 assert(!session_list->count);
209
210 return 0;
211 }
212
213 /*
214 * This test is supposed to fail at the second create call. If so, return 0 for
215 * test success, else -1.
216 */
217 static int two_session_same_name(void)
218 {
219 int ret;
220
221 ret = create_one_session(SESSION1, PATH1);
222 if (ret < 0) {
223 /* Fail */
224 return -1;
225 }
226
227 ret = create_one_session(SESSION1, PATH1);
228 if (ret < 0) {
229 /* Success */
230 return 0;
231 }
232
233 /* Fail */
234 return -1;
235 }
236
237 int main(int argc, char **argv)
238 {
239 int ret, i;
240 char *tmp_name;
241 struct ltt_session *iter, *tmp;
242
243 srand(time(NULL));
244
245 printf("\nTesting Sessions:\n-----------\n");
246
247 session_list = session_get_list();
248 if (session_list == NULL) {
249 return -1;
250 }
251
252 printf("Create 1 session %s: ", SESSION1);
253 ret = create_one_session(SESSION1, PATH1);
254 if (ret < 0) {
255 return -1;
256 }
257 PRINT_OK();
258
259 printf("Validating created session %s: ", SESSION1);
260 tmp = session_find_by_name(SESSION1);
261 if (tmp == NULL) {
262 return -1;
263 }
264 /* Basic init session values */
265 assert(tmp->kernel_session == NULL);
266 assert(tmp->ust_session_list.count == 0);
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.037047 seconds and 3 git commands to generate.