Tests: abort test on memory allocation failure
[lttng-tools.git] / tests / unit / test_session.c
... / ...
CommitLineData
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
28#include <tap/tap.h>
29
30#include <bin/lttng-sessiond/session.h>
31#include <bin/lttng-sessiond/ust-app.h>
32#include <common/sessiond-comm/sessiond-comm.h>
33#include <common/common.h>
34
35#define SESSION1 "test1"
36
37#define MAX_SESSIONS 10000
38#define RANDOM_STRING_LEN 11
39
40/* Number of TAP tests in this file */
41#define NUM_TESTS 11
42
43static struct ltt_session_list *session_list;
44
45/* For error.h */
46int lttng_opt_quiet = 1;
47int lttng_opt_verbose = 0;
48int lttng_opt_mi;
49
50int ust_consumerd32_fd;
51int ust_consumerd64_fd;
52
53static const char alphanum[] =
54 "0123456789"
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
56 "abcdefghijklmnopqrstuvwxyz";
57static char random_string[RANDOM_STRING_LEN];
58
59/*
60 * Return random string of 10 characters.
61 * Not thread-safe.
62 */
63static char *get_random_string(void)
64{
65 int i;
66
67 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
68 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
69 }
70
71 random_string[RANDOM_STRING_LEN - 1] = '\0';
72
73 return random_string;
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
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
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) {
111 session_destroy(iter);
112 }
113
114 /* Session list must be 0 */
115 assert(!session_list_count());
116}
117
118/*
119 * Test creation of 1 session
120 */
121static int create_one_session(char *name)
122{
123 int ret;
124
125 ret = session_create(name, geteuid(), getegid());
126 if (ret == LTTNG_OK) {
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 }
137 } else {
138 if (ret == LTTNG_ERR_EXIST_SESS) {
139 printf("(session already exists) ");
140 }
141 return -1;
142 }
143
144 return 0;
145}
146
147/*
148 * Test deletion of 1 session
149 */
150static int destroy_one_session(struct ltt_session *session)
151{
152 int ret;
153 char session_name[NAME_MAX];
154
155 strncpy(session_name, session->name, sizeof(session->name));
156 session_name[sizeof(session_name) - 1] = '\0';
157
158 ret = session_destroy(session);
159 if (ret == LTTNG_OK) {
160 ret = find_session_name(session_name);
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 }
168 }
169
170 return 0;
171}
172
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;
180 struct ltt_session *sess;
181
182 ret = create_one_session(SESSION1);
183 if (ret < 0) {
184 /* Fail */
185 return -1;
186 }
187
188 sess = session_find_by_name(SESSION1);
189 if (sess) {
190 /* Success */
191 return 0;
192 }
193
194 /* Fail */
195 return -1;
196}
197
198void test_session_list(void)
199{
200 session_list = session_get_list();
201 ok(session_list != NULL, "Session list: not NULL");
202}
203
204void test_create_one_session(void)
205{
206 ok(create_one_session(SESSION1) == 0,
207 "Create session: %s",
208 SESSION1);
209}
210
211void test_validate_session(void)
212{
213 struct ltt_session *tmp;
214
215 tmp = session_find_by_name(SESSION1);
216
217 ok(tmp != NULL,
218 "Validating session: session found");
219
220 ok(tmp->kernel_session == NULL &&
221 strlen(tmp->name),
222 "Validating session: basic sanity check");
223
224 session_lock(tmp);
225 session_unlock(tmp);
226}
227
228void test_destroy_session(void)
229{
230 struct ltt_session *tmp;
231
232 tmp = session_find_by_name(SESSION1);
233
234 ok(tmp != NULL,
235 "Destroying session: session found");
236
237 ok(destroy_one_session(tmp) == 0,
238 "Destroying session: %s destroyed",
239 SESSION1);
240}
241
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{
250 ok(create_one_session(NULL) < 0,
251 "Create session with bogus param: NULL should fail");
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;
261
262 for (i = 0; i < MAX_SESSIONS; i++) {
263 char *tmp_name = get_random_string();
264 ret = create_one_session(tmp_name);
265 if (ret < 0) {
266 diag("session %d (name: %s) creation failed", i, tmp_name);
267 ++failed;
268 }
269 }
270
271 ok(failed == 0,
272 "Large sessions number: created %u sessions",
273 MAX_SESSIONS);
274
275 failed = 0;
276
277 for (i = 0; i < MAX_SESSIONS; i++) {
278 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
279 ret = destroy_one_session(iter);
280 if (ret < 0) {
281 diag("session %d destroy failed", i);
282 ++failed;
283 }
284 }
285 }
286
287 ok(failed == 0 && session_list_count() == 0,
288 "Large sessions number: destroyed %u sessions",
289 MAX_SESSIONS);
290}
291
292int main(int argc, char **argv)
293{
294 plan_tests(NUM_TESTS);
295
296 diag("Sessions unit tests");
297
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();
315}
This page took 0.024127 seconds and 4 git commands to generate.