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