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