Lazily initialize max poll set size
[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
63371d1e
DG
19#include <assert.h>
20#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <time.h>
6df2e2c9 26#include <sys/types.h>
63371d1e 27
83c55082
CB
28#include <tap/tap.h>
29
10a8a223 30#include <bin/lttng-sessiond/session.h>
7972aab2 31#include <bin/lttng-sessiond/ust-app.h>
10a8a223 32#include <common/sessiond-comm/sessiond-comm.h>
f73fabfd 33#include <common/common.h>
f6a9efaa 34
63371d1e
DG
35#define SESSION1 "test1"
36
63371d1e 37#define MAX_SESSIONS 10000
98612240 38#define RANDOM_STRING_LEN 11
63371d1e 39
83c55082 40/* Number of TAP tests in this file */
dec56f6c 41#define NUM_TESTS 11
63371d1e
DG
42
43static struct ltt_session_list *session_list;
44
ad7c9c18 45/* For error.h */
97e19046
DG
46int lttng_opt_quiet = 1;
47int lttng_opt_verbose = 0;
c7e35b03 48int lttng_opt_mi;
63371d1e 49
7972aab2
DG
50int ust_consumerd32_fd;
51int ust_consumerd64_fd;
52
63371d1e
DG
53static const char alphanum[] =
54 "0123456789"
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
56 "abcdefghijklmnopqrstuvwxyz";
98612240 57static char random_string[RANDOM_STRING_LEN];
63371d1e
DG
58
59/*
60 * Return random string of 10 characters.
98612240 61 * Not thread-safe.
63371d1e
DG
62 */
63static char *get_random_string(void)
64{
65 int i;
63371d1e 66
98612240
MD
67 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
68 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
63371d1e
DG
69 }
70
98612240 71 random_string[RANDOM_STRING_LEN - 1] = '\0';
63371d1e 72
98612240 73 return random_string;
63371d1e
DG
74}
75
76/*
77 * Return 0 if session name is found, else -1
78 */
79static int find_session_name(char *name)
80{
81 struct ltt_session *iter;
82
83 cds_list_for_each_entry(iter, &session_list->head, list) {
84 if (strcmp(iter->name, name) == 0) {
85 return 0;
86 }
87 }
88
89 return -1;
90}
91
cc305a0b
MD
92static int session_list_count(void)
93{
94 int count = 0;
95 struct ltt_session *iter;
96
97 cds_list_for_each_entry(iter, &session_list->head, list) {
98 count++;
99 }
100 return count;
101}
102
63371d1e
DG
103/*
104 * Empty session list manually.
105 */
106static void empty_session_list(void)
107{
108 struct ltt_session *iter, *tmp;
109
110 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
23324029 111 session_destroy(iter);
63371d1e
DG
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 */
dec56f6c 121static int create_one_session(char *name)
63371d1e
DG
122{
123 int ret;
124
dec56f6c 125 ret = session_create(name, 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;
59891c42 153 char session_name[NAME_MAX];
63371d1e 154
59891c42
DG
155 strncpy(session_name, session->name, sizeof(session->name));
156 session_name[sizeof(session_name) - 1] = '\0';
54d01ffb 157
59891c42 158 ret = session_destroy(session);
f73fabfd 159 if (ret == LTTNG_OK) {
59891c42 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
63371d1e
DG
173/*
174 * This test is supposed to fail at the second create call. If so, return 0 for
175 * test success, else -1.
176 */
177static int two_session_same_name(void)
178{
179 int ret;
00e2e675 180 struct ltt_session *sess;
63371d1e 181
dec56f6c 182 ret = create_one_session(SESSION1);
63371d1e
DG
183 if (ret < 0) {
184 /* Fail */
185 return -1;
186 }
187
00e2e675
DG
188 sess = session_find_by_name(SESSION1);
189 if (sess) {
63371d1e
DG
190 /* Success */
191 return 0;
192 }
193
194 /* Fail */
195 return -1;
196}
197
83c55082 198void test_session_list(void)
63371d1e 199{
83c55082
CB
200 session_list = session_get_list();
201 ok(session_list != NULL, "Session list: not NULL");
202}
63371d1e 203
83c55082
CB
204void test_create_one_session(void)
205{
dec56f6c 206 ok(create_one_session(SESSION1) == 0,
83c55082
CB
207 "Create session: %s",
208 SESSION1);
209}
63371d1e 210
83c55082
CB
211void test_validate_session(void)
212{
213 struct ltt_session *tmp;
63371d1e 214
83c55082 215 tmp = session_find_by_name(SESSION1);
63371d1e 216
83c55082
CB
217 ok(tmp != NULL,
218 "Validating session: session found");
219
220 ok(tmp->kernel_session == NULL &&
83c55082
CB
221 strlen(tmp->name),
222 "Validating session: basic sanity check");
63371d1e 223
54d01ffb
DG
224 session_lock(tmp);
225 session_unlock(tmp);
83c55082 226}
63371d1e 227
83c55082
CB
228void test_destroy_session(void)
229{
230 struct ltt_session *tmp;
63371d1e 231
83c55082 232 tmp = session_find_by_name(SESSION1);
63371d1e 233
83c55082
CB
234 ok(tmp != NULL,
235 "Destroying session: session found");
63371d1e 236
83c55082
CB
237 ok(destroy_one_session(tmp) == 0,
238 "Destroying session: %s destroyed",
239 SESSION1);
240}
63371d1e 241
83c55082
CB
242void test_duplicate_session(void)
243{
244 ok(two_session_same_name() == 0,
245 "Duplicate session creation");
246}
247
248void test_bogus_session_param(void)
249{
dec56f6c
DG
250 ok(create_one_session(NULL) < 0,
251 "Create session with bogus param: NULL should fail");
83c55082
CB
252
253 ok(session_list_count() == 0,
254 "Create session with bogus param: session list empty");
255}
256
257void test_large_session_number(void)
258{
259 int ret, i, failed = 0;
260 struct ltt_session *iter, *tmp;
63371d1e 261
63371d1e 262 for (i = 0; i < MAX_SESSIONS; i++) {
e6dd5671 263 char *tmp_name = get_random_string();
dec56f6c 264 ret = create_one_session(tmp_name);
63371d1e 265 if (ret < 0) {
83c55082
CB
266 diag("session %d (name: %s) creation failed", i, tmp_name);
267 ++failed;
db9b8b88 268 }
63371d1e 269 }
63371d1e 270
83c55082
CB
271 ok(failed == 0,
272 "Large sessions number: created %u sessions",
273 MAX_SESSIONS);
274
275 failed = 0;
276
63371d1e
DG
277 for (i = 0; i < MAX_SESSIONS; i++) {
278 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
271933a4 279 ret = destroy_one_session(iter);
63371d1e 280 if (ret < 0) {
59891c42 281 diag("session %d destroy failed", i);
83c55082 282 ++failed;
63371d1e
DG
283 }
284 }
285 }
63371d1e 286
83c55082
CB
287 ok(failed == 0 && session_list_count() == 0,
288 "Large sessions number: destroyed %u sessions",
289 MAX_SESSIONS);
290}
63371d1e 291
83c55082
CB
292int main(int argc, char **argv)
293{
83c55082
CB
294 plan_tests(NUM_TESTS);
295
e3bef725
CB
296 diag("Sessions unit tests");
297
83c55082
CB
298 test_session_list();
299
300 test_create_one_session();
301
302 test_validate_session();
303
304 test_destroy_session();
305
306 test_duplicate_session();
307
308 empty_session_list();
309
310 test_bogus_session_param();
311
312 test_large_session_number();
313
314 return exit_status();
63371d1e 315}
This page took 0.062808 seconds and 5 git commands to generate.