Generate session name and default output on sessiond's end
[lttng-tools.git] / tests / unit / test_session.c
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 #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>
26 #include <sys/types.h>
27 #include <urcu.h>
28
29 #include <tap/tap.h>
30
31 #include <bin/lttng-sessiond/session.h>
32 #include <bin/lttng-sessiond/ust-app.h>
33 #include <bin/lttng-sessiond/ht-cleanup.h>
34 #include <bin/lttng-sessiond/health-sessiond.h>
35 #include <bin/lttng-sessiond/thread.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/common.h>
38
39 #define SESSION1 "test1"
40
41 #define MAX_SESSIONS 10000
42 #define RANDOM_STRING_LEN 11
43
44 /* Number of TAP tests in this file */
45 #define NUM_TESTS 11
46
47 struct health_app *health_sessiond;
48 static struct ltt_session_list *session_list;
49
50 /* For error.h */
51 int lttng_opt_quiet = 1;
52 int lttng_opt_verbose = 0;
53 int lttng_opt_mi;
54
55 int ust_consumerd32_fd;
56 int ust_consumerd64_fd;
57
58 static const char alphanum[] =
59 "0123456789"
60 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
61 "abcdefghijklmnopqrstuvwxyz";
62 static char random_string[RANDOM_STRING_LEN];
63
64 /*
65 * Return random string of 10 characters.
66 * Not thread-safe.
67 */
68 static char *get_random_string(void)
69 {
70 int i;
71
72 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
73 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
74 }
75
76 random_string[RANDOM_STRING_LEN - 1] = '\0';
77
78 return random_string;
79 }
80
81 /*
82 * Return 0 if session name is found, else -1
83 */
84 static 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
97 static 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
108 /*
109 * Empty session list manually.
110 */
111 static void empty_session_list(void)
112 {
113 struct ltt_session *iter, *tmp;
114
115 session_lock_list();
116 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
117 session_destroy(iter);
118 }
119 session_unlock_list();
120
121 /* Session list must be 0 */
122 assert(!session_list_count());
123 }
124
125 /*
126 * Test creation of 1 session
127 */
128 static int create_one_session(char *name)
129 {
130 int ret;
131 enum lttng_error_code ret_code;
132 struct ltt_session *session = NULL;
133
134 session_lock_list();
135 ret_code = session_create(name, geteuid(), getegid(), &session);
136 session_put(session);
137 if (ret_code == LTTNG_OK) {
138 /* Validate */
139 ret = find_session_name(name);
140 if (ret < 0) {
141 /* Session not found by name */
142 printf("session not found after creation\n");
143 ret = -1;
144 goto end;
145 } else {
146 /* Success */
147 ret = 0;
148 goto end;
149 }
150 } else {
151 if (ret_code == LTTNG_ERR_EXIST_SESS) {
152 printf("(session already exists) ");
153 }
154 ret = -1;
155 goto end;
156 }
157 ret = 0;
158 end:
159 session_unlock_list();
160 return ret;
161 }
162
163 /*
164 * Test deletion of 1 session
165 */
166 static int destroy_one_session(struct ltt_session *session)
167 {
168 int ret;
169 char session_name[NAME_MAX];
170
171 strncpy(session_name, session->name, sizeof(session_name));
172 session_name[sizeof(session_name) - 1] = '\0';
173
174 session_destroy(session);
175 session_put(session);
176
177 ret = find_session_name(session_name);
178 if (ret < 0) {
179 /* Success, -1 means that the sesion is NOT found */
180 ret = 0;
181 } else {
182 /* Fail */
183 ret = -1;
184 }
185 return ret;
186 }
187
188 /*
189 * This test is supposed to fail at the second create call. If so, return 0 for
190 * test success, else -1.
191 */
192 static int two_session_same_name(void)
193 {
194 int ret;
195 struct ltt_session *sess;
196
197 ret = create_one_session(SESSION1);
198 if (ret < 0) {
199 /* Fail */
200 ret = -1;
201 goto end;
202 }
203
204 session_lock_list();
205 sess = session_find_by_name(SESSION1);
206 if (sess) {
207 /* Success */
208 session_put(sess);
209 session_unlock_list();
210 ret = 0;
211 goto end_unlock;
212 } else {
213 /* Fail */
214 ret = -1;
215 goto end_unlock;
216 }
217 end_unlock:
218 session_unlock_list();
219 end:
220 return ret;
221 }
222
223 void test_session_list(void)
224 {
225 session_list = session_get_list();
226 ok(session_list != NULL, "Session list: not NULL");
227 }
228
229 void test_create_one_session(void)
230 {
231 ok(create_one_session(SESSION1) == 0,
232 "Create session: %s",
233 SESSION1);
234 }
235
236 void test_validate_session(void)
237 {
238 struct ltt_session *tmp;
239
240 session_lock_list();
241 tmp = session_find_by_name(SESSION1);
242
243 ok(tmp != NULL,
244 "Validating session: session found");
245
246 ok(tmp->kernel_session == NULL &&
247 strlen(tmp->name),
248 "Validating session: basic sanity check");
249
250 session_lock(tmp);
251 session_unlock(tmp);
252 session_put(tmp);
253 session_unlock_list();
254 }
255
256 void test_destroy_session(void)
257 {
258 struct ltt_session *tmp;
259
260 session_lock_list();
261 tmp = session_find_by_name(SESSION1);
262
263 ok(tmp != NULL,
264 "Destroying session: session found");
265
266 ok(destroy_one_session(tmp) == 0,
267 "Destroying session: %s destroyed",
268 SESSION1);
269 session_unlock_list();
270 }
271
272 void test_duplicate_session(void)
273 {
274 ok(two_session_same_name() == 0,
275 "Duplicate session creation");
276 }
277
278 void test_session_name_generation(void)
279 {
280 struct ltt_session *session = NULL;
281 enum lttng_error_code ret_code;
282 const char *expected_session_name_prefix = DEFAULT_SESSION_NAME;
283
284 session_lock_list();
285 ret_code = session_create(NULL, geteuid(), getegid(), &session);
286 ok(ret_code == LTTNG_OK,
287 "Create session with a NULL name (auto-generate a name)");
288 if (!session) {
289 skip(1, "Skipping session name generation tests as session_create() failed.");
290 goto end;
291 }
292 diag("Automatically-generated session name: %s", *session->name ?
293 session->name : "ERROR");
294 ok(*session->name && !strncmp(expected_session_name_prefix, session->name,
295 sizeof(DEFAULT_SESSION_NAME) - 1),
296 "Auto-generated session name starts with %s",
297 DEFAULT_SESSION_NAME);
298 end:
299 session_put(session);
300 session_unlock_list();
301 }
302
303 void test_large_session_number(void)
304 {
305 int ret, i, failed = 0;
306 struct ltt_session *iter, *tmp;
307
308 for (i = 0; i < MAX_SESSIONS; i++) {
309 char *tmp_name = get_random_string();
310 ret = create_one_session(tmp_name);
311 if (ret < 0) {
312 diag("session %d (name: %s) creation failed", i, tmp_name);
313 ++failed;
314 }
315 }
316
317 ok(failed == 0,
318 "Large sessions number: created %u sessions",
319 MAX_SESSIONS);
320
321 failed = 0;
322
323 session_lock_list();
324 for (i = 0; i < MAX_SESSIONS; i++) {
325 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
326 assert(session_get(iter));
327 ret = destroy_one_session(iter);
328 if (ret < 0) {
329 diag("session %d destroy failed", i);
330 ++failed;
331 }
332 }
333 }
334 session_unlock_list();
335
336 ok(failed == 0 && session_list_count() == 0,
337 "Large sessions number: destroyed %u sessions",
338 MAX_SESSIONS);
339 }
340
341 int main(int argc, char **argv)
342 {
343 struct lttng_thread *ht_cleanup_thread;
344
345 plan_tests(NUM_TESTS);
346
347 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
348 ht_cleanup_thread = launch_ht_cleanup_thread();
349 assert(ht_cleanup_thread);
350 lttng_thread_put(ht_cleanup_thread);
351
352 diag("Sessions unit tests");
353
354 rcu_register_thread();
355
356 test_session_list();
357
358 test_create_one_session();
359
360 test_validate_session();
361
362 test_destroy_session();
363
364 test_duplicate_session();
365
366 empty_session_list();
367
368 test_session_name_generation();
369
370 test_large_session_number();
371
372 rcu_unregister_thread();
373 lttng_thread_list_shutdown_orphans();
374
375 return exit_status();
376 }
This page took 0.036716 seconds and 5 git commands to generate.