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