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