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