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