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