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