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 | |
10a8a223 DG |
29 | #include <bin/lttng-sessiond/session.h> |
30 | #include <common/sessiond-comm/sessiond-comm.h> | |
f6a9efaa | 31 | |
897b8e23 | 32 | #include "utils.h" |
63371d1e DG |
33 | |
34 | #define SESSION1 "test1" | |
35 | ||
36 | /* This path will NEVER be created in this test */ | |
37 | #define PATH1 "/tmp/.test-junk-lttng" | |
38 | ||
39 | #define MAX_SESSIONS 10000 | |
98612240 | 40 | #define RANDOM_STRING_LEN 11 |
63371d1e DG |
41 | |
42 | /* | |
43 | * String of 263 caracters. NAME_MAX + "OVERFLOW". If OVERFLOW appears in the | |
44 | * session name, we have a problem. | |
45 | * | |
46 | * NAME_MAX = 255 | |
47 | */ | |
48 | #define OVERFLOW_SESSION_NAME \ | |
49 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ | |
50 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ | |
51 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ | |
52 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc" \ | |
53 | "OVERFLOW" | |
54 | ||
55 | static struct ltt_session_list *session_list; | |
56 | ||
57 | /* For lttngerr.h */ | |
97e19046 DG |
58 | int lttng_opt_quiet = 1; |
59 | int lttng_opt_verbose = 0; | |
63371d1e DG |
60 | |
61 | static const char alphanum[] = | |
62 | "0123456789" | |
63 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
64 | "abcdefghijklmnopqrstuvwxyz"; | |
98612240 | 65 | static char random_string[RANDOM_STRING_LEN]; |
63371d1e DG |
66 | |
67 | /* | |
68 | * Return random string of 10 characters. | |
98612240 | 69 | * Not thread-safe. |
63371d1e DG |
70 | */ |
71 | static char *get_random_string(void) | |
72 | { | |
73 | int i; | |
63371d1e | 74 | |
98612240 MD |
75 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
76 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; | |
63371d1e DG |
77 | } |
78 | ||
98612240 | 79 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
63371d1e | 80 | |
98612240 | 81 | return random_string; |
63371d1e DG |
82 | } |
83 | ||
84 | /* | |
85 | * Return 0 if session name is found, else -1 | |
86 | */ | |
87 | static int find_session_name(char *name) | |
88 | { | |
89 | struct ltt_session *iter; | |
90 | ||
91 | cds_list_for_each_entry(iter, &session_list->head, list) { | |
92 | if (strcmp(iter->name, name) == 0) { | |
93 | return 0; | |
94 | } | |
95 | } | |
96 | ||
97 | return -1; | |
98 | } | |
99 | ||
cc305a0b MD |
100 | static int session_list_count(void) |
101 | { | |
102 | int count = 0; | |
103 | struct ltt_session *iter; | |
104 | ||
105 | cds_list_for_each_entry(iter, &session_list->head, list) { | |
106 | count++; | |
107 | } | |
108 | return count; | |
109 | } | |
110 | ||
63371d1e DG |
111 | /* |
112 | * Empty session list manually. | |
113 | */ | |
114 | static void empty_session_list(void) | |
115 | { | |
116 | struct ltt_session *iter, *tmp; | |
117 | ||
118 | cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { | |
119 | cds_list_del(&iter->list); | |
63371d1e DG |
120 | free(iter); |
121 | } | |
122 | ||
123 | /* Session list must be 0 */ | |
cc305a0b | 124 | assert(!session_list_count()); |
63371d1e DG |
125 | } |
126 | ||
127 | /* | |
128 | * Test creation of 1 session | |
129 | */ | |
130 | static int create_one_session(char *name, char *path) | |
131 | { | |
132 | int ret; | |
133 | ||
6df2e2c9 | 134 | ret = session_create(name, path, geteuid(), getegid()); |
54d01ffb | 135 | if (ret == LTTCOMM_OK) { |
63371d1e DG |
136 | /* Validate */ |
137 | ret = find_session_name(name); | |
138 | if (ret < 0) { | |
139 | /* Session not found by name */ | |
140 | printf("session not found after creation\n"); | |
141 | return -1; | |
142 | } else { | |
143 | /* Success */ | |
144 | return 0; | |
145 | } | |
54d01ffb DG |
146 | } else { |
147 | if (ret == LTTCOMM_EXIST_SESS) { | |
63371d1e DG |
148 | printf("(session already exists) "); |
149 | } | |
150 | return -1; | |
151 | } | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Test deletion of 1 session | |
158 | */ | |
271933a4 | 159 | static int destroy_one_session(struct ltt_session *session) |
63371d1e DG |
160 | { |
161 | int ret; | |
162 | ||
271933a4 | 163 | ret = session_destroy(session); |
54d01ffb DG |
164 | |
165 | if (ret == LTTCOMM_OK) { | |
63371d1e | 166 | /* Validate */ |
271933a4 DG |
167 | if (session == NULL) { |
168 | return 0; | |
169 | } | |
170 | ret = find_session_name(session->name); | |
63371d1e DG |
171 | if (ret < 0) { |
172 | /* Success, -1 means that the sesion is NOT found */ | |
173 | return 0; | |
174 | } else { | |
175 | /* Fail */ | |
176 | return -1; | |
177 | } | |
63371d1e DG |
178 | } |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
183 | static int fuzzing_create_args(void) | |
184 | { | |
185 | int ret; | |
186 | ||
187 | ret = create_one_session(NULL, NULL); | |
54d01ffb | 188 | if (ret > 0) { |
63371d1e DG |
189 | printf("Session created with (null),(null)\n"); |
190 | return -1; | |
191 | } | |
192 | ||
193 | ret = create_one_session(NULL, PATH1); | |
54d01ffb | 194 | if (ret > 0) { |
63371d1e DG |
195 | printf("Session created with (null), %s)\n", PATH1); |
196 | return -1; | |
197 | } | |
198 | ||
63371d1e | 199 | /* Session list must be 0 */ |
cc305a0b | 200 | assert(!session_list_count()); |
63371d1e DG |
201 | |
202 | return 0; | |
203 | } | |
204 | ||
205 | static int fuzzing_destroy_args(void) | |
206 | { | |
207 | int ret; | |
208 | ||
209 | ret = destroy_one_session(NULL); | |
54d01ffb | 210 | if (ret > 0) { |
63371d1e DG |
211 | printf("Session destroyed with (null)\n"); |
212 | return -1; | |
213 | } | |
214 | ||
63371d1e | 215 | /* Session list must be 0 */ |
cc305a0b | 216 | assert(!session_list_count()); |
63371d1e DG |
217 | |
218 | return 0; | |
219 | } | |
220 | ||
221 | /* | |
222 | * This test is supposed to fail at the second create call. If so, return 0 for | |
223 | * test success, else -1. | |
224 | */ | |
225 | static int two_session_same_name(void) | |
226 | { | |
227 | int ret; | |
00e2e675 | 228 | struct ltt_session *sess; |
63371d1e DG |
229 | |
230 | ret = create_one_session(SESSION1, PATH1); | |
231 | if (ret < 0) { | |
232 | /* Fail */ | |
233 | return -1; | |
234 | } | |
235 | ||
00e2e675 DG |
236 | sess = session_find_by_name(SESSION1); |
237 | if (sess) { | |
63371d1e DG |
238 | /* Success */ |
239 | return 0; | |
240 | } | |
241 | ||
242 | /* Fail */ | |
243 | return -1; | |
244 | } | |
245 | ||
246 | int main(int argc, char **argv) | |
247 | { | |
248 | int ret, i; | |
63371d1e DG |
249 | struct ltt_session *iter, *tmp; |
250 | ||
251 | srand(time(NULL)); | |
252 | ||
253 | printf("\nTesting Sessions:\n-----------\n"); | |
254 | ||
54d01ffb | 255 | session_list = session_get_list(); |
63371d1e DG |
256 | if (session_list == NULL) { |
257 | return -1; | |
258 | } | |
259 | ||
260 | printf("Create 1 session %s: ", SESSION1); | |
bcf480b0 | 261 | fflush(stdout); |
63371d1e DG |
262 | ret = create_one_session(SESSION1, PATH1); |
263 | if (ret < 0) { | |
264 | return -1; | |
265 | } | |
897b8e23 | 266 | PRINT_OK(); |
63371d1e DG |
267 | |
268 | printf("Validating created session %s: ", SESSION1); | |
bcf480b0 | 269 | fflush(stdout); |
54d01ffb | 270 | tmp = session_find_by_name(SESSION1); |
63371d1e DG |
271 | if (tmp == NULL) { |
272 | return -1; | |
273 | } | |
274 | /* Basic init session values */ | |
275 | assert(tmp->kernel_session == NULL); | |
63371d1e DG |
276 | assert(strlen(tmp->path)); |
277 | assert(strlen(tmp->name)); | |
54d01ffb DG |
278 | session_lock(tmp); |
279 | session_unlock(tmp); | |
63371d1e | 280 | |
897b8e23 | 281 | PRINT_OK(); |
63371d1e DG |
282 | |
283 | printf("Destroy 1 session %s: ", SESSION1); | |
bcf480b0 | 284 | fflush(stdout); |
271933a4 | 285 | ret = destroy_one_session(tmp); |
63371d1e DG |
286 | if (ret < 0) { |
287 | return -1; | |
288 | } | |
897b8e23 | 289 | PRINT_OK(); |
63371d1e DG |
290 | |
291 | printf("Two session with same name: "); | |
bcf480b0 | 292 | fflush(stdout); |
63371d1e DG |
293 | ret = two_session_same_name(); |
294 | if (ret < 0) { | |
295 | return -1; | |
296 | } | |
897b8e23 | 297 | PRINT_OK(); |
63371d1e DG |
298 | |
299 | empty_session_list(); | |
300 | ||
301 | printf("Fuzzing create_session arguments: "); | |
bcf480b0 | 302 | fflush(stdout); |
63371d1e DG |
303 | ret = fuzzing_create_args(); |
304 | if (ret < 0) { | |
305 | return -1; | |
306 | } | |
897b8e23 | 307 | PRINT_OK(); |
63371d1e DG |
308 | |
309 | printf("Fuzzing destroy_session argument: "); | |
bcf480b0 | 310 | fflush(stdout); |
63371d1e DG |
311 | ret = fuzzing_destroy_args(); |
312 | if (ret < 0) { | |
313 | return -1; | |
314 | } | |
897b8e23 | 315 | PRINT_OK(); |
63371d1e DG |
316 | |
317 | printf("Creating %d sessions: ", MAX_SESSIONS); | |
bcf480b0 | 318 | fflush(stdout); |
63371d1e | 319 | for (i = 0; i < MAX_SESSIONS; i++) { |
e6dd5671 MD |
320 | char *tmp_name = get_random_string(); |
321 | ||
322 | ret = create_one_session(tmp_name, PATH1); | |
63371d1e DG |
323 | if (ret < 0) { |
324 | printf("session %d (name: %s) creation failed\n", i, tmp_name); | |
325 | return -1; | |
326 | } | |
db9b8b88 DG |
327 | |
328 | if ((i % 1000) == 0) { | |
329 | fprintf(stdout, "%d..", i); | |
330 | fflush(stdout); | |
331 | } | |
63371d1e | 332 | } |
897b8e23 | 333 | PRINT_OK(); |
63371d1e DG |
334 | |
335 | printf("Destroying %d sessions: ", MAX_SESSIONS); | |
bcf480b0 | 336 | fflush(stdout); |
63371d1e DG |
337 | for (i = 0; i < MAX_SESSIONS; i++) { |
338 | cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { | |
271933a4 | 339 | ret = destroy_one_session(iter); |
63371d1e DG |
340 | if (ret < 0) { |
341 | printf("session %d (name: %s) creation failed\n", i, iter->name); | |
342 | return -1; | |
343 | } | |
344 | } | |
345 | } | |
897b8e23 | 346 | PRINT_OK(); |
63371d1e DG |
347 | |
348 | /* Session list must be 0 */ | |
cc305a0b | 349 | assert(!session_list_count()); |
63371d1e DG |
350 | |
351 | /* Success */ | |
352 | return 0; | |
353 | } |