sessiond: enforce mmap output type for kernel metadata channel
[lttng-tools.git] / tests / unit / test_kernel_data.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <assert.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <time.h>
15
16 #include <bin/lttng-sessiond/trace-kernel.h>
17 #include <common/defaults.h>
18
19 #include <tap/tap.h>
20
21 #define RANDOM_STRING_LEN 11
22
23 /* Number of TAP tests in this file */
24 #define NUM_TESTS 11
25
26 /* For error.h */
27 int lttng_opt_quiet = 1;
28 int lttng_opt_verbose;
29 int lttng_opt_mi;
30
31 static const char alphanum[] =
32 "0123456789"
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34 "abcdefghijklmnopqrstuvwxyz";
35
36 static struct ltt_kernel_session *kern;
37 static char random_string[RANDOM_STRING_LEN];
38
39 /*
40 * Return random string of 10 characters.
41 * Not thread-safe.
42 */
43 static char *get_random_string(void)
44 {
45 int i;
46
47 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
48 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
49 }
50
51 random_string[RANDOM_STRING_LEN - 1] = '\0';
52
53 return random_string;
54 }
55
56 static void test_create_one_kernel_session(void)
57 {
58 kern = trace_kernel_create_session();
59 ok(kern != NULL, "Create kernel session");
60
61 if (!kern) {
62 skip(1, "Kernel session is null");
63 return;
64 }
65 ok(kern->fd == -1 &&
66 kern->metadata_stream_fd == -1 &&
67 kern->consumer_fds_sent == 0 &&
68 kern->channel_count == 0 &&
69 kern->stream_count_global == 0 &&
70 kern->metadata == NULL,
71 "Validate kernel session");
72 }
73
74 static void test_create_kernel_metadata(void)
75 {
76 assert(kern != NULL);
77
78 kern->metadata = trace_kernel_create_metadata();
79 ok(kern->metadata != NULL, "Create kernel metadata");
80
81 ok(kern->metadata->fd == -1 &&
82 kern->metadata->conf != NULL &&
83 kern->metadata->conf->attr.overwrite
84 == DEFAULT_METADATA_OVERWRITE &&
85 kern->metadata->conf->attr.subbuf_size
86 == default_get_metadata_subbuf_size() &&
87 kern->metadata->conf->attr.num_subbuf
88 == DEFAULT_METADATA_SUBBUF_NUM &&
89 kern->metadata->conf->attr.switch_timer_interval
90 == DEFAULT_METADATA_SWITCH_TIMER &&
91 kern->metadata->conf->attr.read_timer_interval
92 == DEFAULT_METADATA_READ_TIMER &&
93 kern->metadata->conf->attr.output
94 == LTTNG_EVENT_MMAP,
95 "Validate kernel session metadata");
96
97 trace_kernel_destroy_metadata(kern->metadata);
98 }
99
100 static void test_create_kernel_channel(void)
101 {
102 struct ltt_kernel_channel *chan;
103 struct lttng_channel attr;
104 struct lttng_channel_extended extended;
105
106 memset(&attr, 0, sizeof(attr));
107 memset(&extended, 0, sizeof(extended));
108 attr.attr.extended.ptr = &extended;
109
110 chan = trace_kernel_create_channel(&attr);
111 ok(chan != NULL, "Create kernel channel");
112
113 if (!chan) {
114 skip(1, "Channel is null");
115 return;
116 }
117
118 ok(chan->fd == -1 &&
119 chan->enabled == 1 &&
120 chan->stream_count == 0 &&
121 chan->channel->attr.overwrite == attr.attr.overwrite,
122 "Validate kernel channel");
123
124 /* Init list in order to avoid sefaults from cds_list_del */
125 CDS_INIT_LIST_HEAD(&chan->list);
126 trace_kernel_destroy_channel(chan);
127 }
128
129 static void test_create_kernel_event(void)
130 {
131 enum lttng_error_code ret;
132 struct ltt_kernel_event *event;
133 struct lttng_event ev;
134
135 memset(&ev, 0, sizeof(ev));
136 ok(!lttng_strncpy(ev.name, get_random_string(),
137 RANDOM_STRING_LEN),
138 "Validate string length");
139 ev.type = LTTNG_EVENT_TRACEPOINT;
140 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
141
142 ret = trace_kernel_create_event(&ev, NULL, NULL, &event);
143 ok(ret == LTTNG_OK, "Create kernel event");
144
145 if (!event) {
146 skip(1, "Event is null");
147 return;
148 }
149
150 ok(event->fd == -1 &&
151 event->enabled == 1 &&
152 event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT &&
153 strlen(event->event->name),
154 "Validate kernel event");
155
156 /* Init list in order to avoid sefaults from cds_list_del */
157 CDS_INIT_LIST_HEAD(&event->list);
158 trace_kernel_destroy_event(event);
159 }
160
161 static void test_create_kernel_stream(void)
162 {
163 struct ltt_kernel_stream *stream;
164
165 stream = trace_kernel_create_stream("stream1", 0);
166 ok(stream != NULL, "Create kernel stream");
167
168 if (!stream) {
169 skip(1, "Stream is null");
170 return;
171 }
172
173 ok(stream->fd == -1 &&
174 stream->state == 0,
175 "Validate kernel stream");
176
177 /* Init list in order to avoid sefaults from cds_list_del */
178 CDS_INIT_LIST_HEAD(&stream->list);
179 trace_kernel_destroy_stream(stream);
180 }
181
182 int main(int argc, char **argv)
183 {
184 plan_tests(NUM_TESTS);
185
186 diag("Kernel data structure unit test");
187
188 test_create_one_kernel_session();
189 test_create_kernel_metadata();
190 test_create_kernel_channel();
191 test_create_kernel_event();
192 test_create_kernel_stream();
193
194 /* Success */
195 return 0;
196 }
This page took 0.032035 seconds and 4 git commands to generate.