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