Fix tests: NULL pointer dereference in ltt_session unit tests
[lttng-tools.git] / tests / unit / test_ust_data.c
CommitLineData
d3e8f6bb
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
d3e8f6bb
DG
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>
8273250b 26#include <urcu.h>
d3e8f6bb 27
10a8a223
DG
28#include <lttng/lttng.h>
29#include <bin/lttng-sessiond/lttng-ust-abi.h>
990570ed 30#include <common/defaults.h>
10a8a223 31#include <bin/lttng-sessiond/trace-ust.h>
7972aab2 32#include <bin/lttng-sessiond/ust-app.h>
83b45089 33#include <bin/lttng-sessiond/notification-thread.h>
10a8a223 34
657270a4
CB
35#include <tap/tap.h>
36
d3e8f6bb
DG
37/* This path will NEVER be created in this test */
38#define PATH1 "/tmp/.test-junk-lttng"
39
98612240
MD
40#define RANDOM_STRING_LEN 11
41
657270a4 42/* Number of TAP tests in this file */
1cda50f2 43#define NUM_TESTS 16
657270a4 44
ad7c9c18 45/* For error.h */
97e19046
DG
46int lttng_opt_quiet = 1;
47int lttng_opt_verbose;
c7e35b03 48int lttng_opt_mi;
d3e8f6bb 49
7972aab2
DG
50int ust_consumerd32_fd;
51int ust_consumerd64_fd;
52
83b45089 53/* Global variables required by sessiond objects being linked-in */
7c1d2758 54struct lttng_ht *agent_apps_ht_by_sock;
83b45089 55struct notification_thread_handle *notification_thread_handle;
7c1d2758 56
d3e8f6bb
DG
57static const char alphanum[] =
58 "0123456789"
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
60 "abcdefghijklmnopqrstuvwxyz";
98612240 61static char random_string[RANDOM_STRING_LEN];
d3e8f6bb
DG
62
63static struct ltt_ust_session *usess;
64static struct lttng_domain dom;
65
66/*
67 * Return random string of 10 characters.
98612240 68 * Not thread-safe.
d3e8f6bb
DG
69 */
70static char *get_random_string(void)
71{
72 int i;
d3e8f6bb 73
98612240
MD
74 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
75 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
d3e8f6bb
DG
76 }
77
98612240 78 random_string[RANDOM_STRING_LEN - 1] = '\0';
d3e8f6bb 79
98612240 80 return random_string;
d3e8f6bb
DG
81}
82
657270a4 83static void test_create_one_ust_session(void)
d3e8f6bb 84{
d3e8f6bb
DG
85 dom.type = LTTNG_DOMAIN_UST;
86
dec56f6c 87 usess = trace_ust_create_session(42);
657270a4
CB
88 ok(usess != NULL, "Create UST session");
89
67b2f51c
JR
90 if (!usess) {
91 skip(1, "UST session is null");
92 return;
93 }
94
657270a4 95 ok(usess->id == 42 &&
14fb1ebe 96 usess->active == 0 &&
657270a4 97 usess->domain_global.channels != NULL &&
657270a4
CB
98 usess->uid == 0 &&
99 usess->gid == 0,
100 "Validate UST session");
d3e8f6bb
DG
101
102 trace_ust_destroy_session(usess);
103}
104
657270a4 105static void test_create_ust_channel(void)
d3e8f6bb
DG
106{
107 struct ltt_ust_channel *uchan;
108 struct lttng_channel attr;
82b4ebce 109 struct lttng_channel_extended extended;
d3e8f6bb 110
441c16a7 111 memset(&attr, 0, sizeof(attr));
82b4ebce
JG
112 memset(&extended, 0, sizeof(extended));
113 attr.attr.extended.ptr = &extended;
441c16a7 114
1b0eb865
MD
115 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
116 "Validate channel name length");
51755dc8 117 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
657270a4
CB
118 ok(uchan != NULL, "Create UST channel");
119
67b2f51c
JR
120 if (!usess) {
121 skip(1, "UST session is null");
122 return;
123 }
124
657270a4 125 ok(uchan->enabled == 0 &&
657270a4
CB
126 strncmp(uchan->name, "channel0", 8) == 0 &&
127 uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0' &&
128 uchan->ctx != NULL &&
129 uchan->events != NULL &&
130 uchan->attr.overwrite == attr.attr.overwrite,
131 "Validate UST channel");
d3e8f6bb
DG
132
133 trace_ust_destroy_channel(uchan);
134}
135
657270a4 136static void test_create_ust_event(void)
d3e8f6bb
DG
137{
138 struct ltt_ust_event *event;
139 struct lttng_event ev;
39687410 140 enum lttng_error_code ret;
d3e8f6bb 141
441c16a7 142 memset(&ev, 0, sizeof(ev));
a84aca51
MD
143 ok(lttng_strncpy(ev.name, get_random_string(),
144 LTTNG_SYMBOL_NAME_LEN) == 0,
145 "Validate string length");
d3e8f6bb 146 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 147 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
d3e8f6bb 148
39687410 149 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
d3e8f6bb 150
39687410 151 ok(ret == LTTNG_OK, "Create UST event");
657270a4 152
67b2f51c
JR
153 if (!event) {
154 skip(1, "UST event is null");
155 return;
156 }
157
657270a4
CB
158 ok(event->enabled == 0 &&
159 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
160 strcmp(event->attr.name, ev.name) == 0 &&
161 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
162 "Validate UST event");
d3e8f6bb
DG
163
164 trace_ust_destroy_event(event);
165}
166
41d7b959
JI
167static void test_create_ust_event_exclusion(void)
168{
39687410 169 enum lttng_error_code ret;
41d7b959
JI
170 struct ltt_ust_event *event;
171 struct lttng_event ev;
172 char *name;
88329be5 173 char *random_name;
e196f4f4 174 struct lttng_event_exclusion *exclusion = NULL;
4b6816b6 175 struct lttng_event_exclusion *exclusion_copy = NULL;
88329be5 176 const int exclusion_count = 2;
41d7b959
JI
177
178 memset(&ev, 0, sizeof(ev));
179
180 /* make a wildcarded event name */
181 name = get_random_string();
182 name[strlen(name) - 1] = '*';
61a046d9
MD
183 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
184 "Validate string length");
41d7b959
JI
185
186 ev.type = LTTNG_EVENT_TRACEPOINT;
187 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
188
189 /* set up an exclusion set */
88329be5
PP
190 exclusion = zmalloc(sizeof(*exclusion) +
191 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 192 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 193 if (!exclusion) {
67b2f51c
JR
194 skip(4, "zmalloc failed");
195 goto end;
c710ece7
MD
196 }
197
88329be5
PP
198 exclusion->count = exclusion_count;
199 random_name = get_random_string();
200 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
201 LTTNG_SYMBOL_NAME_LEN);
202 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
203 LTTNG_SYMBOL_NAME_LEN);
41d7b959 204
39687410 205 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
3111dcc4 206 exclusion = NULL;
41d7b959 207
39687410 208 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
88329be5
PP
209
210 exclusion = zmalloc(sizeof(*exclusion) +
211 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 212 ok(exclusion != NULL, "Create UST exclusion");
88329be5 213 if (!exclusion) {
67b2f51c
JR
214 skip(2, "zmalloc failed");
215 goto end;
88329be5
PP
216 }
217
4b6816b6
FD
218 exclusion_copy = zmalloc(sizeof(*exclusion) +
219 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
220 if (!exclusion_copy) {
221 skip(2, "zmalloc failed");
222 goto end;
223 }
224
225 /*
226 * We are giving ownership of the exclusion struct to the
227 * trace_ust_create_event() function. Make a copy of the exclusion struct
228 * so we can compare it later.
229 */
230
88329be5
PP
231 exclusion->count = exclusion_count;
232 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
233 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
234 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
235 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
236
4b6816b6
FD
237 exclusion_copy->count = exclusion_count;
238 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
239 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN);
240 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
241 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN);
242
39687410 243 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
e196f4f4 244 exclusion = NULL;
39687410 245 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
41d7b959 246
67b2f51c
JR
247 if (!event) {
248 skip(1, "UST event with exclusion is null");
249 goto end;
250 }
251
41d7b959 252 ok(event->enabled == 0 &&
881fa57f
JG
253 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
254 strcmp(event->attr.name, ev.name) == 0 &&
255 event->exclusion != NULL &&
256 event->exclusion->count == exclusion_count &&
257 !memcmp(event->exclusion->names, exclusion_copy->names,
258 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
259 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
260 "Validate UST event and exclusion");
41d7b959 261
41d7b959 262 trace_ust_destroy_event(event);
67b2f51c 263end:
e196f4f4 264 free(exclusion);
4b6816b6 265 free(exclusion_copy);
67b2f51c 266 return;
41d7b959
JI
267}
268
269
657270a4 270static void test_create_ust_context(void)
d3e8f6bb 271{
e38021f8 272 struct lttng_event_context ectx;
d3e8f6bb
DG
273 struct ltt_ust_context *uctx;
274
e38021f8
DG
275 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
276
e38021f8 277 uctx = trace_ust_create_context(&ectx);
657270a4 278 ok(uctx != NULL, "Create UST context");
d3e8f6bb 279
657270a4
CB
280 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
281 "Validate UST context");
f949b23e 282 free(uctx);
d3e8f6bb
DG
283}
284
285int main(int argc, char **argv)
286{
657270a4 287 plan_tests(NUM_TESTS);
d3e8f6bb 288
e3bef725
CB
289 diag("UST data structures unit test");
290
8273250b
JR
291 rcu_register_thread();
292
657270a4 293 test_create_one_ust_session();
657270a4
CB
294 test_create_ust_channel();
295 test_create_ust_event();
296 test_create_ust_context();
41d7b959 297 test_create_ust_event_exclusion();
d3e8f6bb 298
8273250b
JR
299 rcu_unregister_thread();
300
657270a4 301 return exit_status();
d3e8f6bb 302}
This page took 0.072192 seconds and 5 git commands to generate.