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