Tests: Fix first line of output to follow TAP guidelines
[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 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <sys/types.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 <common/sessiond-comm/sessiond-comm.h>
34 #include <common/common.h>
35
36 #define SESSION1 "test1"
37
38 /* This path will NEVER be created in this test */
39 #define PATH1 "/tmp/.test-junk-lttng"
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 12
46
47 static struct ltt_session_list *session_list;
48
49 /* For lttngerr.h */
50 int lttng_opt_quiet = 1;
51 int lttng_opt_verbose = 0;
52
53 int ust_consumerd32_fd;
54 int ust_consumerd64_fd;
55
56 static const char alphanum[] =
57 "0123456789"
58 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
59 "abcdefghijklmnopqrstuvwxyz";
60 static char random_string[RANDOM_STRING_LEN];
61
62 /*
63 * Return random string of 10 characters.
64 * Not thread-safe.
65 */
66 static char *get_random_string(void)
67 {
68 int i;
69
70 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
71 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
72 }
73
74 random_string[RANDOM_STRING_LEN - 1] = '\0';
75
76 return random_string;
77 }
78
79 /*
80 * Return 0 if session name is found, else -1
81 */
82 static int find_session_name(char *name)
83 {
84 struct ltt_session *iter;
85
86 cds_list_for_each_entry(iter, &session_list->head, list) {
87 if (strcmp(iter->name, name) == 0) {
88 return 0;
89 }
90 }
91
92 return -1;
93 }
94
95 static int session_list_count(void)
96 {
97 int count = 0;
98 struct ltt_session *iter;
99
100 cds_list_for_each_entry(iter, &session_list->head, list) {
101 count++;
102 }
103 return count;
104 }
105
106 /*
107 * Empty session list manually.
108 */
109 static void empty_session_list(void)
110 {
111 struct ltt_session *iter, *tmp;
112
113 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
114 cds_list_del(&iter->list);
115 free(iter);
116 }
117
118 /* Session list must be 0 */
119 assert(!session_list_count());
120 }
121
122 /*
123 * Test creation of 1 session
124 */
125 static int create_one_session(char *name, char *path)
126 {
127 int ret;
128
129 ret = session_create(name, path, geteuid(), getegid());
130 if (ret == LTTNG_OK) {
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 }
141 } else {
142 if (ret == LTTNG_ERR_EXIST_SESS) {
143 printf("(session already exists) ");
144 }
145 return -1;
146 }
147
148 return 0;
149 }
150
151 /*
152 * Test deletion of 1 session
153 */
154 static int destroy_one_session(struct ltt_session *session)
155 {
156 int ret;
157
158 ret = session_destroy(session);
159
160 if (ret == LTTNG_OK) {
161 /* Validate */
162 if (session == NULL) {
163 return 0;
164 }
165 ret = find_session_name(session->name);
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 }
173 }
174
175 return 0;
176 }
177
178 /*
179 * This test is supposed to fail at the second create call. If so, return 0 for
180 * test success, else -1.
181 */
182 static int two_session_same_name(void)
183 {
184 int ret;
185 struct ltt_session *sess;
186
187 ret = create_one_session(SESSION1, PATH1);
188 if (ret < 0) {
189 /* Fail */
190 return -1;
191 }
192
193 sess = session_find_by_name(SESSION1);
194 if (sess) {
195 /* Success */
196 return 0;
197 }
198
199 /* Fail */
200 return -1;
201 }
202
203 void test_session_list(void)
204 {
205 session_list = session_get_list();
206 ok(session_list != NULL, "Session list: not NULL");
207 }
208
209 void test_create_one_session(void)
210 {
211 ok(create_one_session(SESSION1, PATH1) == 0,
212 "Create session: %s",
213 SESSION1);
214 }
215
216 void test_validate_session(void)
217 {
218 struct ltt_session *tmp;
219
220 tmp = session_find_by_name(SESSION1);
221
222 ok(tmp != NULL,
223 "Validating session: session found");
224
225 ok(tmp->kernel_session == NULL &&
226 strlen(tmp->path) &&
227 strlen(tmp->name),
228 "Validating session: basic sanity check");
229
230 session_lock(tmp);
231 session_unlock(tmp);
232 }
233
234 void test_destroy_session(void)
235 {
236 struct ltt_session *tmp;
237
238 tmp = session_find_by_name(SESSION1);
239
240 ok(tmp != NULL,
241 "Destroying session: session found");
242
243 ok(destroy_one_session(tmp) == 0,
244 "Destroying session: %s destroyed",
245 SESSION1);
246 }
247
248 void test_duplicate_session(void)
249 {
250 ok(two_session_same_name() == 0,
251 "Duplicate session creation");
252 }
253
254 void test_bogus_session_param(void)
255 {
256 ok(create_one_session(NULL, NULL) < 0,
257 "Create session with bogus param: NULL, NULL should fail");
258
259 ok(create_one_session(NULL, PATH1) < 0,
260 "Create session with bogus param: NULL, %s should fail",
261 PATH1);
262
263 ok(session_list_count() == 0,
264 "Create session with bogus param: session list empty");
265 }
266
267 void test_large_session_number(void)
268 {
269 int ret, i, failed = 0;
270 struct ltt_session *iter, *tmp;
271
272 for (i = 0; i < MAX_SESSIONS; i++) {
273 char *tmp_name = get_random_string();
274 ret = create_one_session(tmp_name, PATH1);
275 if (ret < 0) {
276 diag("session %d (name: %s) creation failed", i, tmp_name);
277 ++failed;
278 }
279 }
280
281 ok(failed == 0,
282 "Large sessions number: created %u sessions",
283 MAX_SESSIONS);
284
285 failed = 0;
286
287 for (i = 0; i < MAX_SESSIONS; i++) {
288 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
289 ret = destroy_one_session(iter);
290 if (ret < 0) {
291 diag("session %d (name: %s) destroy failed", i, iter->name);
292 ++failed;
293 }
294 }
295 }
296
297 ok(failed == 0 && session_list_count() == 0,
298 "Large sessions number: destroyed %u sessions",
299 MAX_SESSIONS);
300 }
301
302 int main(int argc, char **argv)
303 {
304 plan_tests(NUM_TESTS);
305
306 diag("Sessions unit tests");
307
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
324 return exit_status();
325 }
This page took 0.035638 seconds and 4 git commands to generate.