Fix: runas check fd value before calling close()
[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;
4b6816b6 188 struct lttng_event_exclusion *exclusion_copy = NULL;
88329be5 189 const int exclusion_count = 2;
41d7b959
JI
190
191 memset(&ev, 0, sizeof(ev));
192
193 /* make a wildcarded event name */
194 name = get_random_string();
195 name[strlen(name) - 1] = '*';
61a046d9
MD
196 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
197 "Validate string length");
41d7b959
JI
198
199 ev.type = LTTNG_EVENT_TRACEPOINT;
200 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
201
202 /* set up an exclusion set */
88329be5
PP
203 exclusion = zmalloc(sizeof(*exclusion) +
204 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 205 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 206 if (!exclusion) {
67b2f51c
JR
207 skip(4, "zmalloc failed");
208 goto end;
c710ece7
MD
209 }
210
88329be5
PP
211 exclusion->count = exclusion_count;
212 random_name = get_random_string();
213 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
214 LTTNG_SYMBOL_NAME_LEN);
215 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
216 LTTNG_SYMBOL_NAME_LEN);
41d7b959 217
39687410 218 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
3111dcc4 219 exclusion = NULL;
41d7b959 220
39687410 221 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
88329be5
PP
222
223 exclusion = zmalloc(sizeof(*exclusion) +
224 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 225 ok(exclusion != NULL, "Create UST exclusion");
88329be5 226 if (!exclusion) {
67b2f51c
JR
227 skip(2, "zmalloc failed");
228 goto end;
88329be5
PP
229 }
230
4b6816b6
FD
231 exclusion_copy = zmalloc(sizeof(*exclusion) +
232 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
233 if (!exclusion_copy) {
234 skip(2, "zmalloc failed");
235 goto end;
236 }
237
238 /*
239 * We are giving ownership of the exclusion struct to the
240 * trace_ust_create_event() function. Make a copy of the exclusion struct
241 * so we can compare it later.
242 */
243
88329be5
PP
244 exclusion->count = exclusion_count;
245 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
246 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
247 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
248 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
249
4b6816b6
FD
250 exclusion_copy->count = exclusion_count;
251 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
252 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN);
253 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
254 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN);
255
39687410
FD
256 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
257 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
41d7b959 258
67b2f51c
JR
259 if (!event) {
260 skip(1, "UST event with exclusion is null");
261 goto end;
262 }
263
41d7b959
JI
264 ok(event->enabled == 0 &&
265 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
266 strcmp(event->attr.name, ev.name) == 0 &&
267 event->exclusion != NULL &&
88329be5 268 event->exclusion->count == exclusion_count &&
4b6816b6 269 !memcmp(event->exclusion->names, exclusion_copy->names,
88329be5 270 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
41d7b959
JI
271 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
272 "Validate UST event and exclusion");
273
41d7b959 274 trace_ust_destroy_event(event);
67b2f51c 275end:
4b6816b6 276 free(exclusion_copy);
67b2f51c 277 return;
41d7b959
JI
278}
279
280
657270a4 281static void test_create_ust_context(void)
d3e8f6bb 282{
e38021f8 283 struct lttng_event_context ectx;
d3e8f6bb
DG
284 struct ltt_ust_context *uctx;
285
e38021f8
DG
286 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
287
e38021f8 288 uctx = trace_ust_create_context(&ectx);
657270a4 289 ok(uctx != NULL, "Create UST context");
d3e8f6bb 290
657270a4
CB
291 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
292 "Validate UST context");
f949b23e 293 free(uctx);
d3e8f6bb
DG
294}
295
296int main(int argc, char **argv)
297{
657270a4 298 plan_tests(NUM_TESTS);
d3e8f6bb 299
e3bef725
CB
300 diag("UST data structures unit test");
301
8273250b
JR
302 rcu_register_thread();
303
657270a4 304 test_create_one_ust_session();
657270a4
CB
305 test_create_ust_channel();
306 test_create_ust_event();
307 test_create_ust_context();
41d7b959 308 test_create_ust_event_exclusion();
d3e8f6bb 309
8273250b
JR
310 rcu_unregister_thread();
311
657270a4 312 return exit_status();
d3e8f6bb 313}
This page took 0.069449 seconds and 5 git commands to generate.