Fix: test_kernel_data dereference of null pointer
[lttng-tools.git] / tests / unit / test_kernel_data.c
CommitLineData
897b8e23
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
897b8e23
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 27#include <bin/lttng-sessiond/trace-kernel.h>
990570ed 28#include <common/defaults.h>
10a8a223 29
23aaa19e 30#include <tap/tap.h>
897b8e23 31
98612240
MD
32#define RANDOM_STRING_LEN 11
33
23aaa19e 34/* Number of TAP tests in this file */
1c0733db 35#define NUM_TESTS 11
23aaa19e 36
ad7c9c18 37/* For error.h */
97e19046
DG
38int lttng_opt_quiet = 1;
39int lttng_opt_verbose;
c7e35b03 40int lttng_opt_mi;
897b8e23 41
7972aab2
DG
42int ust_consumerd32_fd;
43int ust_consumerd64_fd;
44
897b8e23
DG
45static const char alphanum[] =
46 "0123456789"
47 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48 "abcdefghijklmnopqrstuvwxyz";
49
50static struct ltt_kernel_session *kern;
98612240 51static char random_string[RANDOM_STRING_LEN];
897b8e23
DG
52
53/*
54 * Return random string of 10 characters.
98612240 55 * Not thread-safe.
897b8e23
DG
56 */
57static char *get_random_string(void)
58{
59 int i;
897b8e23 60
98612240
MD
61 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
62 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
897b8e23
DG
63 }
64
98612240 65 random_string[RANDOM_STRING_LEN - 1] = '\0';
897b8e23 66
98612240 67 return random_string;
897b8e23
DG
68}
69
23aaa19e 70static void test_create_one_kernel_session(void)
897b8e23 71{
dec56f6c 72 kern = trace_kernel_create_session();
23aaa19e 73 ok(kern != NULL, "Create kernel session");
897b8e23 74
84a7eb73
JR
75 if (!kern) {
76 skip(1, "Kernel session is null");
77 return;
78 }
23aaa19e
CB
79 ok(kern->fd == -1 &&
80 kern->metadata_stream_fd == -1 &&
81 kern->consumer_fds_sent == 0 &&
82 kern->channel_count == 0 &&
83 kern->stream_count_global == 0 &&
84 kern->metadata == NULL,
85 "Validate kernel session");
897b8e23
DG
86}
87
23aaa19e 88static void test_create_kernel_metadata(void)
897b8e23
DG
89{
90 assert(kern != NULL);
91
a4b92340 92 kern->metadata = trace_kernel_create_metadata();
23aaa19e
CB
93 ok(kern->metadata != NULL, "Create kernel metadata");
94
95 ok(kern->metadata->fd == -1 &&
96 kern->metadata->conf != NULL &&
97 kern->metadata->conf->attr.overwrite
98 == DEFAULT_CHANNEL_OVERWRITE &&
99 kern->metadata->conf->attr.subbuf_size
100 == default_get_metadata_subbuf_size() &&
101 kern->metadata->conf->attr.num_subbuf
102 == DEFAULT_METADATA_SUBBUF_NUM &&
103 kern->metadata->conf->attr.switch_timer_interval
5d2e1e66 104 == DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER &&
23aaa19e 105 kern->metadata->conf->attr.read_timer_interval
5d2e1e66 106 == DEFAULT_KERNEL_CHANNEL_READ_TIMER &&
23aaa19e
CB
107 kern->metadata->conf->attr.output
108 == DEFAULT_KERNEL_CHANNEL_OUTPUT,
109 "Validate kernel session metadata");
897b8e23 110
3f43a221 111 trace_kernel_destroy_metadata(kern->metadata);
897b8e23
DG
112}
113
23aaa19e 114static void test_create_kernel_channel(void)
897b8e23
DG
115{
116 struct ltt_kernel_channel *chan;
117 struct lttng_channel attr;
118
441c16a7
MD
119 memset(&attr, 0, sizeof(attr));
120
fdd9eb17 121 chan = trace_kernel_create_channel(&attr);
23aaa19e 122 ok(chan != NULL, "Create kernel channel");
897b8e23 123
84a7eb73
JR
124 if (!chan) {
125 skip(1, "Channel is null");
126 return;
127 }
128
23aaa19e
CB
129 ok(chan->fd == -1 &&
130 chan->enabled == 1 &&
131 chan->stream_count == 0 &&
23aaa19e
CB
132 chan->channel->attr.overwrite == attr.attr.overwrite,
133 "Validate kernel channel");
897b8e23
DG
134
135 /* Init list in order to avoid sefaults from cds_list_del */
136 CDS_INIT_LIST_HEAD(&chan->list);
3f43a221 137 trace_kernel_destroy_channel(chan);
897b8e23
DG
138}
139
23aaa19e 140static void test_create_kernel_event(void)
897b8e23
DG
141{
142 struct ltt_kernel_event *event;
143 struct lttng_event ev;
144
441c16a7 145 memset(&ev, 0, sizeof(ev));
72265d8a 146 ok(!lttng_strncpy(ev.name, get_random_string(),
1c0733db
MD
147 LTTNG_KERNEL_SYM_NAME_LEN),
148 "Validate string length");
897b8e23 149 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 150 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
897b8e23 151
00a62084 152 event = trace_kernel_create_event(&ev, NULL, NULL);
23aaa19e 153 ok(event != NULL, "Create kernel event");
897b8e23 154
84a7eb73
JR
155 if (!event) {
156 skip(1, "Event is null");
157 return;
158 }
159
23aaa19e
CB
160 ok(event->fd == -1 &&
161 event->enabled == 1 &&
162 event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT &&
163 strlen(event->event->name),
164 "Validate kernel event");
897b8e23
DG
165
166 /* Init list in order to avoid sefaults from cds_list_del */
167 CDS_INIT_LIST_HEAD(&event->list);
3f43a221 168 trace_kernel_destroy_event(event);
897b8e23
DG
169}
170
23aaa19e 171static void test_create_kernel_stream(void)
897b8e23
DG
172{
173 struct ltt_kernel_stream *stream;
174
00e2e675 175 stream = trace_kernel_create_stream("stream1", 0);
23aaa19e 176 ok(stream != NULL, "Create kernel stream");
897b8e23 177
84a7eb73
JR
178 if (!stream) {
179 skip(1, "Stream is null");
180 return;
181 }
182
23aaa19e
CB
183 ok(stream->fd == -1 &&
184 stream->state == 0,
185 "Validate kernel stream");
897b8e23
DG
186
187 /* Init list in order to avoid sefaults from cds_list_del */
188 CDS_INIT_LIST_HEAD(&stream->list);
3f43a221 189 trace_kernel_destroy_stream(stream);
897b8e23
DG
190}
191
192int main(int argc, char **argv)
193{
23aaa19e 194 plan_tests(NUM_TESTS);
897b8e23 195
e3bef725
CB
196 diag("Kernel data structure unit test");
197
23aaa19e
CB
198 test_create_one_kernel_session();
199 test_create_kernel_metadata();
200 test_create_kernel_channel();
201 test_create_kernel_event();
202 test_create_kernel_stream();
897b8e23
DG
203
204 /* Success */
205 return 0;
206}
This page took 0.071153 seconds and 4 git commands to generate.