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