Commit | Line | Data |
---|---|---|
63371d1e DG |
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> | |
6df2e2c9 | 27 | #include <sys/types.h> |
63371d1e | 28 | |
83c55082 CB |
29 | #include <tap/tap.h> |
30 | ||
10a8a223 | 31 | #include <bin/lttng-sessiond/session.h> |
7972aab2 | 32 | #include <bin/lttng-sessiond/ust-app.h> |
10a8a223 | 33 | #include <common/sessiond-comm/sessiond-comm.h> |
f73fabfd | 34 | #include <common/common.h> |
f6a9efaa | 35 | |
63371d1e DG |
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 | |
98612240 | 42 | #define RANDOM_STRING_LEN 11 |
63371d1e | 43 | |
83c55082 CB |
44 | /* Number of TAP tests in this file */ |
45 | #define NUM_TESTS 12 | |
63371d1e DG |
46 | |
47 | static struct ltt_session_list *session_list; | |
48 | ||
49 | /* For lttngerr.h */ | |
97e19046 DG |
50 | int lttng_opt_quiet = 1; |
51 | int lttng_opt_verbose = 0; | |
63371d1e | 52 | |
7972aab2 DG |
53 | int ust_consumerd32_fd; |
54 | int ust_consumerd64_fd; | |
55 | ||
63371d1e DG |
56 | static const char alphanum[] = |
57 | "0123456789" | |
58 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
59 | "abcdefghijklmnopqrstuvwxyz"; | |
98612240 | 60 | static char random_string[RANDOM_STRING_LEN]; |
63371d1e DG |
61 | |
62 | /* | |
63 | * Return random string of 10 characters. | |
98612240 | 64 | * Not thread-safe. |
63371d1e DG |
65 | */ |
66 | static char *get_random_string(void) | |
67 | { | |
68 | int i; | |
63371d1e | 69 | |
98612240 MD |
70 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
71 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; | |
63371d1e DG |
72 | } |
73 | ||
98612240 | 74 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
63371d1e | 75 | |
98612240 | 76 | return random_string; |
63371d1e DG |
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 | ||
cc305a0b MD |
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 | ||
63371d1e DG |
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); | |
63371d1e DG |
115 | free(iter); |
116 | } | |
117 | ||
118 | /* Session list must be 0 */ | |
cc305a0b | 119 | assert(!session_list_count()); |
63371d1e DG |
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 | ||
6df2e2c9 | 129 | ret = session_create(name, path, geteuid(), getegid()); |
f73fabfd | 130 | if (ret == LTTNG_OK) { |
63371d1e DG |
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 | } | |
54d01ffb | 141 | } else { |
f73fabfd | 142 | if (ret == LTTNG_ERR_EXIST_SESS) { |
63371d1e DG |
143 | printf("(session already exists) "); |
144 | } | |
145 | return -1; | |
146 | } | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | /* | |
152 | * Test deletion of 1 session | |
153 | */ | |
271933a4 | 154 | static int destroy_one_session(struct ltt_session *session) |
63371d1e DG |
155 | { |
156 | int ret; | |
157 | ||
271933a4 | 158 | ret = session_destroy(session); |
54d01ffb | 159 | |
f73fabfd | 160 | if (ret == LTTNG_OK) { |
63371d1e | 161 | /* Validate */ |
271933a4 DG |
162 | if (session == NULL) { |
163 | return 0; | |
164 | } | |
165 | ret = find_session_name(session->name); | |
63371d1e DG |
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 | } | |
63371d1e DG |
173 | } |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
63371d1e DG |
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; | |
00e2e675 | 185 | struct ltt_session *sess; |
63371d1e DG |
186 | |
187 | ret = create_one_session(SESSION1, PATH1); | |
188 | if (ret < 0) { | |
189 | /* Fail */ | |
190 | return -1; | |
191 | } | |
192 | ||
00e2e675 DG |
193 | sess = session_find_by_name(SESSION1); |
194 | if (sess) { | |
63371d1e DG |
195 | /* Success */ |
196 | return 0; | |
197 | } | |
198 | ||
199 | /* Fail */ | |
200 | return -1; | |
201 | } | |
202 | ||
83c55082 | 203 | void test_session_list(void) |
63371d1e | 204 | { |
83c55082 CB |
205 | session_list = session_get_list(); |
206 | ok(session_list != NULL, "Session list: not NULL"); | |
207 | } | |
63371d1e | 208 | |
83c55082 CB |
209 | void test_create_one_session(void) |
210 | { | |
211 | ok(create_one_session(SESSION1, PATH1) == 0, | |
212 | "Create session: %s", | |
213 | SESSION1); | |
214 | } | |
63371d1e | 215 | |
83c55082 CB |
216 | void test_validate_session(void) |
217 | { | |
218 | struct ltt_session *tmp; | |
63371d1e | 219 | |
83c55082 | 220 | tmp = session_find_by_name(SESSION1); |
63371d1e | 221 | |
83c55082 CB |
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"); | |
63371d1e | 229 | |
54d01ffb DG |
230 | session_lock(tmp); |
231 | session_unlock(tmp); | |
83c55082 | 232 | } |
63371d1e | 233 | |
83c55082 CB |
234 | void test_destroy_session(void) |
235 | { | |
236 | struct ltt_session *tmp; | |
63371d1e | 237 | |
83c55082 | 238 | tmp = session_find_by_name(SESSION1); |
63371d1e | 239 | |
83c55082 CB |
240 | ok(tmp != NULL, |
241 | "Destroying session: session found"); | |
63371d1e | 242 | |
83c55082 CB |
243 | ok(destroy_one_session(tmp) == 0, |
244 | "Destroying session: %s destroyed", | |
245 | SESSION1); | |
246 | } | |
63371d1e | 247 | |
83c55082 CB |
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; | |
63371d1e | 271 | |
63371d1e | 272 | for (i = 0; i < MAX_SESSIONS; i++) { |
e6dd5671 | 273 | char *tmp_name = get_random_string(); |
e6dd5671 | 274 | ret = create_one_session(tmp_name, PATH1); |
63371d1e | 275 | if (ret < 0) { |
83c55082 CB |
276 | diag("session %d (name: %s) creation failed", i, tmp_name); |
277 | ++failed; | |
db9b8b88 | 278 | } |
63371d1e | 279 | } |
63371d1e | 280 | |
83c55082 CB |
281 | ok(failed == 0, |
282 | "Large sessions number: created %u sessions", | |
283 | MAX_SESSIONS); | |
284 | ||
285 | failed = 0; | |
286 | ||
63371d1e DG |
287 | for (i = 0; i < MAX_SESSIONS; i++) { |
288 | cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { | |
271933a4 | 289 | ret = destroy_one_session(iter); |
63371d1e | 290 | if (ret < 0) { |
83c55082 CB |
291 | diag("session %d (name: %s) destroy failed", i, iter->name); |
292 | ++failed; | |
63371d1e DG |
293 | } |
294 | } | |
295 | } | |
63371d1e | 296 | |
83c55082 CB |
297 | ok(failed == 0 && session_list_count() == 0, |
298 | "Large sessions number: destroyed %u sessions", | |
299 | MAX_SESSIONS); | |
300 | } | |
63371d1e | 301 | |
83c55082 CB |
302 | int main(int argc, char **argv) |
303 | { | |
83c55082 CB |
304 | plan_tests(NUM_TESTS); |
305 | ||
e3bef725 CB |
306 | diag("Sessions unit tests"); |
307 | ||
83c55082 CB |
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(); | |
63371d1e | 325 | } |