Fix hyphens in man pages
[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
98612240 40#define RANDOM_STRING_LEN 11
63371d1e
DG
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
55static struct ltt_session_list *session_list;
56
57/* For lttngerr.h */
58int opt_quiet = 1;
59int opt_verbose = 0;
60
61static const char alphanum[] =
62 "0123456789"
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
64 "abcdefghijklmnopqrstuvwxyz";
98612240 65static char random_string[RANDOM_STRING_LEN];
63371d1e
DG
66
67/*
68 * Return random string of 10 characters.
98612240 69 * Not thread-safe.
63371d1e
DG
70 */
71static char *get_random_string(void)
72{
73 int i;
63371d1e 74
98612240
MD
75 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
76 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
63371d1e
DG
77 }
78
98612240 79 random_string[RANDOM_STRING_LEN - 1] = '\0';
63371d1e 80
98612240 81 return random_string;
63371d1e
DG
82}
83
84/*
85 * Return 0 if session name is found, else -1
86 */
87static 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 */
103static 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 */
120static int create_one_session(char *name, char *path)
121{
122 int ret;
123
6df2e2c9 124 ret = session_create(name, path, geteuid(), getegid());
54d01ffb 125 if (ret == LTTCOMM_OK) {
63371d1e
DG
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 }
54d01ffb
DG
136 } else {
137 if (ret == LTTCOMM_EXIST_SESS) {
63371d1e
DG
138 printf("(session already exists) ");
139 }
140 return -1;
141 }
142
143 return 0;
144}
145
146/*
147 * Test deletion of 1 session
148 */
271933a4 149static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
150{
151 int ret;
152
271933a4 153 ret = session_destroy(session);
54d01ffb
DG
154
155 if (ret == LTTCOMM_OK) {
63371d1e 156 /* Validate */
271933a4
DG
157 if (session == NULL) {
158 return 0;
159 }
160 ret = find_session_name(session->name);
63371d1e
DG
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 }
63371d1e
DG
168 }
169
170 return 0;
171}
172
173static int fuzzing_create_args(void)
174{
175 int ret;
176
177 ret = create_one_session(NULL, NULL);
54d01ffb 178 if (ret > 0) {
63371d1e
DG
179 printf("Session created with (null),(null)\n");
180 return -1;
181 }
182
183 ret = create_one_session(NULL, PATH1);
54d01ffb 184 if (ret > 0) {
63371d1e
DG
185 printf("Session created with (null), %s)\n", PATH1);
186 return -1;
187 }
188
189 ret = create_one_session(SESSION1, NULL);
54d01ffb 190 if (ret > 0) {
63371d1e
DG
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
201static int fuzzing_destroy_args(void)
202{
203 int ret;
204
205 ret = destroy_one_session(NULL);
54d01ffb 206 if (ret > 0) {
63371d1e
DG
207 printf("Session destroyed with (null)\n");
208 return -1;
209 }
210
63371d1e
DG
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 */
221static 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
241int main(int argc, char **argv)
242{
243 int ret, i;
63371d1e
DG
244 struct ltt_session *iter, *tmp;
245
246 srand(time(NULL));
247
248 printf("\nTesting Sessions:\n-----------\n");
249
54d01ffb 250 session_list = session_get_list();
63371d1e
DG
251 if (session_list == NULL) {
252 return -1;
253 }
254
255 printf("Create 1 session %s: ", SESSION1);
bcf480b0 256 fflush(stdout);
63371d1e
DG
257 ret = create_one_session(SESSION1, PATH1);
258 if (ret < 0) {
259 return -1;
260 }
897b8e23 261 PRINT_OK();
63371d1e
DG
262
263 printf("Validating created session %s: ", SESSION1);
bcf480b0 264 fflush(stdout);
54d01ffb 265 tmp = session_find_by_name(SESSION1);
63371d1e
DG
266 if (tmp == NULL) {
267 return -1;
268 }
269 /* Basic init session values */
270 assert(tmp->kernel_session == NULL);
63371d1e
DG
271 assert(strlen(tmp->path));
272 assert(strlen(tmp->name));
54d01ffb
DG
273 session_lock(tmp);
274 session_unlock(tmp);
63371d1e 275
897b8e23 276 PRINT_OK();
63371d1e
DG
277
278 printf("Destroy 1 session %s: ", SESSION1);
bcf480b0 279 fflush(stdout);
271933a4 280 ret = destroy_one_session(tmp);
63371d1e
DG
281 if (ret < 0) {
282 return -1;
283 }
897b8e23 284 PRINT_OK();
63371d1e
DG
285
286 printf("Two session with same name: ");
bcf480b0 287 fflush(stdout);
63371d1e
DG
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: ");
bcf480b0 297 fflush(stdout);
63371d1e
DG
298 ret = fuzzing_create_args();
299 if (ret < 0) {
300 return -1;
301 }
897b8e23 302 PRINT_OK();
63371d1e
DG
303
304 printf("Fuzzing destroy_session argument: ");
bcf480b0 305 fflush(stdout);
63371d1e
DG
306 ret = fuzzing_destroy_args();
307 if (ret < 0) {
308 return -1;
309 }
897b8e23 310 PRINT_OK();
63371d1e
DG
311
312 printf("Creating %d sessions: ", MAX_SESSIONS);
bcf480b0 313 fflush(stdout);
63371d1e 314 for (i = 0; i < MAX_SESSIONS; i++) {
e6dd5671
MD
315 char *tmp_name = get_random_string();
316
317 ret = create_one_session(tmp_name, PATH1);
63371d1e
DG
318 if (ret < 0) {
319 printf("session %d (name: %s) creation failed\n", i, tmp_name);
320 return -1;
321 }
63371d1e 322 }
897b8e23 323 PRINT_OK();
63371d1e
DG
324
325 printf("Destroying %d sessions: ", MAX_SESSIONS);
bcf480b0 326 fflush(stdout);
63371d1e
DG
327 for (i = 0; i < MAX_SESSIONS; i++) {
328 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
271933a4 329 ret = destroy_one_session(iter);
63371d1e
DG
330 if (ret < 0) {
331 printf("session %d (name: %s) creation failed\n", i, iter->name);
332 return -1;
333 }
334 }
335 }
897b8e23 336 PRINT_OK();
63371d1e
DG
337
338 /* Session list must be 0 */
339 assert(!session_list->count);
340
341 /* Success */
342 return 0;
343}
This page took 0.059308 seconds and 4 git commands to generate.