Fix tests: NULL pointer dereference in ust channel unit tests
[lttng-tools.git] / tests / unit / test_ust_data.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 #include <assert.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <urcu.h>
27
28 #include <lttng/lttng.h>
29 #include <bin/lttng-sessiond/lttng-ust-abi.h>
30 #include <common/defaults.h>
31 #include <bin/lttng-sessiond/trace-ust.h>
32 #include <bin/lttng-sessiond/ust-app.h>
33
34 #include <tap/tap.h>
35
36 /* This path will NEVER be created in this test */
37 #define PATH1 "/tmp/.test-junk-lttng"
38
39 #define RANDOM_STRING_LEN 11
40
41 /* Number of TAP tests in this file */
42 #define NUM_TESTS 16
43
44 /* For error.h */
45 int lttng_opt_quiet = 1;
46 int lttng_opt_verbose;
47 int lttng_opt_mi;
48
49 int ust_consumerd32_fd;
50 int ust_consumerd64_fd;
51
52 /* Global variable required by sessiond objects being linked-in */
53 struct lttng_ht *agent_apps_ht_by_sock;
54
55 static const char alphanum[] =
56 "0123456789"
57 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
58 "abcdefghijklmnopqrstuvwxyz";
59 static char random_string[RANDOM_STRING_LEN];
60
61 /*
62 * Return random string of 10 characters.
63 * Not thread-safe.
64 */
65 static char *get_random_string(void)
66 {
67 int i;
68
69 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
70 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
71 }
72
73 random_string[RANDOM_STRING_LEN - 1] = '\0';
74
75 return random_string;
76 }
77
78 static void test_create_one_ust_session(void)
79 {
80 struct ltt_ust_session *usess =
81 trace_ust_create_session(42);
82
83 ok(usess != NULL, "Create UST session");
84
85 if (!usess) {
86 skip(1, "UST session is null");
87 return;
88 }
89
90 ok(usess->id == 42 &&
91 usess->active == 0 &&
92 usess->domain_global.channels != NULL &&
93 usess->uid == 0 &&
94 usess->gid == 0,
95 "Validate UST session");
96
97 trace_ust_destroy_session(usess);
98 }
99
100 static void test_create_ust_channel(void)
101 {
102 struct ltt_ust_channel *uchan;
103 struct lttng_channel attr;
104
105 memset(&attr, 0, sizeof(attr));
106
107 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
108 "Validate channel name length");
109 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
110 ok(uchan != NULL, "Create UST channel");
111
112 if (!uchan) {
113 skip(1, "UST channel is null");
114 return;
115 }
116
117 ok(uchan->enabled == 0 &&
118 strncmp(uchan->name, "channel0", 8) == 0 &&
119 uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0' &&
120 uchan->ctx != NULL &&
121 uchan->events != NULL &&
122 uchan->attr.overwrite == attr.attr.overwrite,
123 "Validate UST channel");
124
125 trace_ust_destroy_channel(uchan);
126 }
127
128 static void test_create_ust_event(void)
129 {
130 struct ltt_ust_event *event;
131 struct lttng_event ev;
132
133 memset(&ev, 0, sizeof(ev));
134 ok(lttng_strncpy(ev.name, get_random_string(),
135 LTTNG_SYMBOL_NAME_LEN) == 0,
136 "Validate string length");
137 ev.type = LTTNG_EVENT_TRACEPOINT;
138 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
139
140 event = trace_ust_create_event(&ev, NULL, NULL, NULL, false);
141
142 ok(event != NULL, "Create UST event");
143
144 if (!event) {
145 skip(1, "UST event is null");
146 return;
147 }
148
149 ok(event->enabled == 0 &&
150 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
151 strcmp(event->attr.name, ev.name) == 0 &&
152 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
153 "Validate UST event");
154
155 trace_ust_destroy_event(event);
156 }
157
158 static void test_create_ust_event_exclusion(void)
159 {
160 struct ltt_ust_event *event;
161 struct lttng_event ev;
162 char *name;
163 char *random_name;
164 struct lttng_event_exclusion *exclusion;
165 const int exclusion_count = 2;
166
167 memset(&ev, 0, sizeof(ev));
168
169 /* make a wildcarded event name */
170 name = get_random_string();
171 name[strlen(name) - 1] = '*';
172 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
173 "Validate string length");
174
175 ev.type = LTTNG_EVENT_TRACEPOINT;
176 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
177
178 /* set up an exclusion set */
179 exclusion = zmalloc(sizeof(*exclusion) +
180 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
181 ok(exclusion != NULL, "Create UST exclusion");
182 if (!exclusion) {
183 skip(4, "zmalloc failed");
184 goto end;
185 }
186
187 exclusion->count = exclusion_count;
188 random_name = get_random_string();
189 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
190 LTTNG_SYMBOL_NAME_LEN);
191 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
192 LTTNG_SYMBOL_NAME_LEN);
193
194 event = trace_ust_create_event(&ev, NULL, NULL, exclusion, false);
195 exclusion = NULL;
196
197 ok(!event, "Create UST event with identical exclusion names fails");
198
199 exclusion = zmalloc(sizeof(*exclusion) +
200 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
201 ok(exclusion != NULL, "Create UST exclusion");
202 if (!exclusion) {
203 skip(2, "zmalloc failed");
204 goto end;
205 }
206
207 exclusion->count = exclusion_count;
208 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
209 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
210 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
211 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
212
213 event = trace_ust_create_event(&ev, NULL, NULL, exclusion, false);
214 ok(event != NULL, "Create UST event with different exclusion names");
215
216 if (!event) {
217 skip(1, "UST event with exclusion is null");
218 goto end;
219 }
220
221 ok(event->enabled == 0 &&
222 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
223 strcmp(event->attr.name, ev.name) == 0 &&
224 event->exclusion != NULL &&
225 event->exclusion->count == exclusion_count &&
226 !memcmp(event->exclusion->names, exclusion->names,
227 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
228 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
229 "Validate UST event and exclusion");
230
231 trace_ust_destroy_event(event);
232 end:
233 return;
234 }
235
236
237 static void test_create_ust_context(void)
238 {
239 struct lttng_event_context ectx;
240 struct ltt_ust_context *uctx;
241
242 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
243
244 uctx = trace_ust_create_context(&ectx);
245 ok(uctx != NULL, "Create UST context");
246
247 if (uctx) {
248 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
249 "Validate UST context");
250 } else {
251 skip(1, "Skipping UST context validation as creation failed");
252 }
253 free(uctx);
254 }
255
256 int main(int argc, char **argv)
257 {
258 plan_tests(NUM_TESTS);
259
260 diag("UST data structures unit test");
261
262 rcu_register_thread();
263
264 test_create_one_ust_session();
265 test_create_ust_channel();
266 test_create_ust_event();
267 test_create_ust_context();
268 test_create_ust_event_exclusion();
269
270 rcu_unregister_thread();
271
272 return exit_status();
273 }
This page took 0.042709 seconds and 4 git commands to generate.