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