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