Fix: leak in error handling of userspace param parsing
[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
5c408ad8
JD
66/*
67 * Stub to prevent an undefined reference in this test without having to link
68 * the entire tree because of a cascade of dependencies. This is not used,
69 * it is just there to prevent GCC from complaining.
70 */
71int rotate_add_channel_pending(uint64_t key, enum lttng_domain_type domain,
72 struct ltt_session *session)
73{
74 ERR("Stub called instead of the real function");
75 abort();
76 return -1;
77}
78
d3e8f6bb
DG
79/*
80 * Return random string of 10 characters.
98612240 81 * Not thread-safe.
d3e8f6bb
DG
82 */
83static char *get_random_string(void)
84{
85 int i;
d3e8f6bb 86
98612240
MD
87 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
88 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
d3e8f6bb
DG
89 }
90
98612240 91 random_string[RANDOM_STRING_LEN - 1] = '\0';
d3e8f6bb 92
98612240 93 return random_string;
d3e8f6bb
DG
94}
95
657270a4 96static void test_create_one_ust_session(void)
d3e8f6bb 97{
d3e8f6bb
DG
98 dom.type = LTTNG_DOMAIN_UST;
99
dec56f6c 100 usess = trace_ust_create_session(42);
657270a4
CB
101 ok(usess != NULL, "Create UST session");
102
67b2f51c
JR
103 if (!usess) {
104 skip(1, "UST session is null");
105 return;
106 }
107
657270a4 108 ok(usess->id == 42 &&
14fb1ebe 109 usess->active == 0 &&
657270a4 110 usess->domain_global.channels != NULL &&
657270a4
CB
111 usess->uid == 0 &&
112 usess->gid == 0,
113 "Validate UST session");
d3e8f6bb
DG
114
115 trace_ust_destroy_session(usess);
116}
117
657270a4 118static void test_create_ust_channel(void)
d3e8f6bb
DG
119{
120 struct ltt_ust_channel *uchan;
121 struct lttng_channel attr;
82b4ebce 122 struct lttng_channel_extended extended;
d3e8f6bb 123
441c16a7 124 memset(&attr, 0, sizeof(attr));
82b4ebce
JG
125 memset(&extended, 0, sizeof(extended));
126 attr.attr.extended.ptr = &extended;
441c16a7 127
1b0eb865
MD
128 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
129 "Validate channel name length");
51755dc8 130 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
657270a4
CB
131 ok(uchan != NULL, "Create UST channel");
132
67b2f51c
JR
133 if (!usess) {
134 skip(1, "UST session is null");
135 return;
136 }
137
657270a4 138 ok(uchan->enabled == 0 &&
657270a4
CB
139 strncmp(uchan->name, "channel0", 8) == 0 &&
140 uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0' &&
141 uchan->ctx != NULL &&
142 uchan->events != NULL &&
143 uchan->attr.overwrite == attr.attr.overwrite,
144 "Validate UST channel");
d3e8f6bb
DG
145
146 trace_ust_destroy_channel(uchan);
147}
148
657270a4 149static void test_create_ust_event(void)
d3e8f6bb
DG
150{
151 struct ltt_ust_event *event;
152 struct lttng_event ev;
39687410 153 enum lttng_error_code ret;
d3e8f6bb 154
441c16a7 155 memset(&ev, 0, sizeof(ev));
a84aca51
MD
156 ok(lttng_strncpy(ev.name, get_random_string(),
157 LTTNG_SYMBOL_NAME_LEN) == 0,
158 "Validate string length");
d3e8f6bb 159 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 160 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
d3e8f6bb 161
39687410 162 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
d3e8f6bb 163
39687410 164 ok(ret == LTTNG_OK, "Create UST event");
657270a4 165
67b2f51c
JR
166 if (!event) {
167 skip(1, "UST event is null");
168 return;
169 }
170
657270a4
CB
171 ok(event->enabled == 0 &&
172 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
173 strcmp(event->attr.name, ev.name) == 0 &&
174 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
175 "Validate UST event");
d3e8f6bb
DG
176
177 trace_ust_destroy_event(event);
178}
179
41d7b959
JI
180static void test_create_ust_event_exclusion(void)
181{
39687410 182 enum lttng_error_code ret;
41d7b959
JI
183 struct ltt_ust_event *event;
184 struct lttng_event ev;
185 char *name;
88329be5 186 char *random_name;
41d7b959 187 struct lttng_event_exclusion *exclusion;
88329be5 188 const int exclusion_count = 2;
41d7b959
JI
189
190 memset(&ev, 0, sizeof(ev));
191
192 /* make a wildcarded event name */
193 name = get_random_string();
194 name[strlen(name) - 1] = '*';
61a046d9
MD
195 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
196 "Validate string length");
41d7b959
JI
197
198 ev.type = LTTNG_EVENT_TRACEPOINT;
199 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
200
201 /* set up an exclusion set */
88329be5
PP
202 exclusion = zmalloc(sizeof(*exclusion) +
203 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 204 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 205 if (!exclusion) {
67b2f51c
JR
206 skip(4, "zmalloc failed");
207 goto end;
c710ece7
MD
208 }
209
88329be5
PP
210 exclusion->count = exclusion_count;
211 random_name = get_random_string();
212 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
213 LTTNG_SYMBOL_NAME_LEN);
214 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
215 LTTNG_SYMBOL_NAME_LEN);
41d7b959 216
39687410 217 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
3111dcc4 218 exclusion = NULL;
41d7b959 219
39687410 220 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
88329be5
PP
221
222 exclusion = zmalloc(sizeof(*exclusion) +
223 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 224 ok(exclusion != NULL, "Create UST exclusion");
88329be5 225 if (!exclusion) {
67b2f51c
JR
226 skip(2, "zmalloc failed");
227 goto end;
88329be5
PP
228 }
229
230 exclusion->count = exclusion_count;
231 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
232 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
233 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
234 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
235
39687410
FD
236 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
237 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
41d7b959 238
67b2f51c
JR
239 if (!event) {
240 skip(1, "UST event with exclusion is null");
241 goto end;
242 }
243
41d7b959
JI
244 ok(event->enabled == 0 &&
245 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
246 strcmp(event->attr.name, ev.name) == 0 &&
247 event->exclusion != NULL &&
88329be5
PP
248 event->exclusion->count == exclusion_count &&
249 !memcmp(event->exclusion->names, exclusion->names,
250 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
41d7b959
JI
251 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
252 "Validate UST event and exclusion");
253
41d7b959 254 trace_ust_destroy_event(event);
67b2f51c
JR
255end:
256 return;
41d7b959
JI
257}
258
259
657270a4 260static void test_create_ust_context(void)
d3e8f6bb 261{
e38021f8 262 struct lttng_event_context ectx;
d3e8f6bb
DG
263 struct ltt_ust_context *uctx;
264
e38021f8
DG
265 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
266
e38021f8 267 uctx = trace_ust_create_context(&ectx);
657270a4 268 ok(uctx != NULL, "Create UST context");
d3e8f6bb 269
657270a4
CB
270 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
271 "Validate UST context");
f949b23e 272 free(uctx);
d3e8f6bb
DG
273}
274
275int main(int argc, char **argv)
276{
657270a4 277 plan_tests(NUM_TESTS);
d3e8f6bb 278
e3bef725
CB
279 diag("UST data structures unit test");
280
8273250b
JR
281 rcu_register_thread();
282
657270a4 283 test_create_one_ust_session();
657270a4
CB
284 test_create_ust_channel();
285 test_create_ust_event();
286 test_create_ust_context();
41d7b959 287 test_create_ust_event_exclusion();
d3e8f6bb 288
8273250b
JR
289 rcu_unregister_thread();
290
657270a4 291 return exit_status();
d3e8f6bb 292}
This page took 0.068062 seconds and 5 git commands to generate.