Initialize all stack variables to zero, fix uninitialized loglevel variables
[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>
6df2e2c9 27#include <sys/types.h>
63371d1e 28
10a8a223
DG
29#include <bin/lttng-sessiond/session.h>
30#include <common/sessiond-comm/sessiond-comm.h>
f6a9efaa 31
897b8e23 32#include "utils.h"
63371d1e
DG
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
54static struct ltt_session_list *session_list;
55
56/* For lttngerr.h */
57int opt_quiet = 1;
58int opt_verbose = 0;
59
60static const char alphanum[] =
61 "0123456789"
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
63 "abcdefghijklmnopqrstuvwxyz";
64
65/*
66 * Return random string of 10 characters.
67 */
68static 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 */
85static 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 */
101static 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 */
118static int create_one_session(char *name, char *path)
119{
120 int ret;
121
6df2e2c9 122 ret = session_create(name, path, geteuid(), getegid());
54d01ffb 123 if (ret == LTTCOMM_OK) {
63371d1e
DG
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 }
54d01ffb
DG
134 } else {
135 if (ret == LTTCOMM_EXIST_SESS) {
63371d1e
DG
136 printf("(session already exists) ");
137 }
138 return -1;
139 }
140
141 return 0;
142}
143
144/*
145 * Test deletion of 1 session
146 */
271933a4 147static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
148{
149 int ret;
150
271933a4 151 ret = session_destroy(session);
54d01ffb
DG
152
153 if (ret == LTTCOMM_OK) {
63371d1e 154 /* Validate */
271933a4
DG
155 if (session == NULL) {
156 return 0;
157 }
158 ret = find_session_name(session->name);
63371d1e
DG
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 }
63371d1e
DG
166 }
167
168 return 0;
169}
170
171static int fuzzing_create_args(void)
172{
173 int ret;
174
175 ret = create_one_session(NULL, NULL);
54d01ffb 176 if (ret > 0) {
63371d1e
DG
177 printf("Session created with (null),(null)\n");
178 return -1;
179 }
180
181 ret = create_one_session(NULL, PATH1);
54d01ffb 182 if (ret > 0) {
63371d1e
DG
183 printf("Session created with (null), %s)\n", PATH1);
184 return -1;
185 }
186
187 ret = create_one_session(SESSION1, NULL);
54d01ffb 188 if (ret > 0) {
63371d1e
DG
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
199static int fuzzing_destroy_args(void)
200{
201 int ret;
202
203 ret = destroy_one_session(NULL);
54d01ffb 204 if (ret > 0) {
63371d1e
DG
205 printf("Session destroyed with (null)\n");
206 return -1;
207 }
208
63371d1e
DG
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 */
219static 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
239int 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
54d01ffb 249 session_list = session_get_list();
63371d1e
DG
250 if (session_list == NULL) {
251 return -1;
252 }
253
254 printf("Create 1 session %s: ", SESSION1);
bcf480b0 255 fflush(stdout);
63371d1e
DG
256 ret = create_one_session(SESSION1, PATH1);
257 if (ret < 0) {
258 return -1;
259 }
897b8e23 260 PRINT_OK();
63371d1e
DG
261
262 printf("Validating created session %s: ", SESSION1);
bcf480b0 263 fflush(stdout);
54d01ffb 264 tmp = session_find_by_name(SESSION1);
63371d1e
DG
265 if (tmp == NULL) {
266 return -1;
267 }
268 /* Basic init session values */
269 assert(tmp->kernel_session == NULL);
63371d1e
DG
270 assert(strlen(tmp->path));
271 assert(strlen(tmp->name));
54d01ffb
DG
272 session_lock(tmp);
273 session_unlock(tmp);
63371d1e 274
897b8e23 275 PRINT_OK();
63371d1e
DG
276
277 printf("Destroy 1 session %s: ", SESSION1);
bcf480b0 278 fflush(stdout);
271933a4 279 ret = destroy_one_session(tmp);
63371d1e
DG
280 if (ret < 0) {
281 return -1;
282 }
897b8e23 283 PRINT_OK();
63371d1e
DG
284
285 printf("Two session with same name: ");
bcf480b0 286 fflush(stdout);
63371d1e
DG
287 ret = two_session_same_name();
288 if (ret < 0) {
289 return -1;
290 }
897b8e23 291 PRINT_OK();
63371d1e
DG
292
293 empty_session_list();
294
295 printf("Fuzzing create_session arguments: ");
bcf480b0 296 fflush(stdout);
63371d1e
DG
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: ");
bcf480b0 304 fflush(stdout);
63371d1e
DG
305 ret = fuzzing_destroy_args();
306 if (ret < 0) {
307 return -1;
308 }
897b8e23 309 PRINT_OK();
63371d1e
DG
310
311 printf("Creating %d sessions: ", MAX_SESSIONS);
bcf480b0 312 fflush(stdout);
63371d1e
DG
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 }
897b8e23 322 PRINT_OK();
63371d1e
DG
323
324 printf("Destroying %d sessions: ", MAX_SESSIONS);
bcf480b0 325 fflush(stdout);
63371d1e
DG
326 for (i = 0; i < MAX_SESSIONS; i++) {
327 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
271933a4 328 ret = destroy_one_session(iter);
63371d1e
DG
329 if (ret < 0) {
330 printf("session %d (name: %s) creation failed\n", i, iter->name);
331 return -1;
332 }
333 }
334 }
897b8e23 335 PRINT_OK();
63371d1e
DG
336
337 /* Session list must be 0 */
338 assert(!session_list->count);
339
340 /* Success */
341 return 0;
342}
This page took 0.037346 seconds and 4 git commands to generate.