Tests: Fix first line of output to follow TAP guidelines
[lttng-tools.git] / tests / unit / test_kernel_data.c
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
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <time.h>
27
28 #include <bin/lttng-sessiond/trace-kernel.h>
29 #include <common/defaults.h>
30
31 #include <tap/tap.h>
32
33 /* This path will NEVER be created in this test */
34 #define PATH1 "/tmp/.test-junk-lttng"
35
36 #define RANDOM_STRING_LEN 11
37
38 /* Number of TAP tests in this file */
39 #define NUM_TESTS 10
40
41 /* For lttngerr.h */
42 int lttng_opt_quiet = 1;
43 int lttng_opt_verbose;
44
45 int ust_consumerd32_fd;
46 int ust_consumerd64_fd;
47
48 static const char alphanum[] =
49 "0123456789"
50 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51 "abcdefghijklmnopqrstuvwxyz";
52
53 static struct ltt_kernel_session *kern;
54 static char random_string[RANDOM_STRING_LEN];
55
56 /*
57 * Return random string of 10 characters.
58 * Not thread-safe.
59 */
60 static char *get_random_string(void)
61 {
62 int i;
63
64 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
65 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
66 }
67
68 random_string[RANDOM_STRING_LEN - 1] = '\0';
69
70 return random_string;
71 }
72
73 static void test_create_one_kernel_session(void)
74 {
75 kern = trace_kernel_create_session(PATH1);
76 ok(kern != NULL, "Create kernel session");
77
78 ok(kern->fd == -1 &&
79 kern->metadata_stream_fd == -1 &&
80 kern->consumer_fds_sent == 0 &&
81 kern->channel_count == 0 &&
82 kern->stream_count_global == 0 &&
83 kern->metadata == NULL,
84 "Validate kernel session");
85
86 /* Init list in order to avoid sefaults from cds_list_del */
87 trace_kernel_destroy_session(kern);
88 }
89
90 static void test_create_kernel_metadata(void)
91 {
92 assert(kern != NULL);
93
94 kern->metadata = trace_kernel_create_metadata();
95 ok(kern->metadata != NULL, "Create kernel metadata");
96
97 ok(kern->metadata->fd == -1 &&
98 kern->metadata->conf != NULL &&
99 kern->metadata->conf->attr.overwrite
100 == DEFAULT_CHANNEL_OVERWRITE &&
101 kern->metadata->conf->attr.subbuf_size
102 == default_get_metadata_subbuf_size() &&
103 kern->metadata->conf->attr.num_subbuf
104 == DEFAULT_METADATA_SUBBUF_NUM &&
105 kern->metadata->conf->attr.switch_timer_interval
106 == DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER &&
107 kern->metadata->conf->attr.read_timer_interval
108 == DEFAULT_KERNEL_CHANNEL_READ_TIMER &&
109 kern->metadata->conf->attr.output
110 == DEFAULT_KERNEL_CHANNEL_OUTPUT,
111 "Validate kernel session metadata");
112
113 trace_kernel_destroy_metadata(kern->metadata);
114 }
115
116 static void test_create_kernel_channel(void)
117 {
118 struct ltt_kernel_channel *chan;
119 struct lttng_channel attr;
120
121 memset(&attr, 0, sizeof(attr));
122
123 chan = trace_kernel_create_channel(&attr);
124 ok(chan != NULL, "Create kernel channel");
125
126 ok(chan->fd == -1 &&
127 chan->enabled == 1 &&
128 chan->stream_count == 0 &&
129 chan->ctx == NULL &&
130 chan->channel->attr.overwrite == attr.attr.overwrite,
131 "Validate kernel channel");
132
133 /* Init list in order to avoid sefaults from cds_list_del */
134 CDS_INIT_LIST_HEAD(&chan->list);
135 trace_kernel_destroy_channel(chan);
136 }
137
138 static void test_create_kernel_event(void)
139 {
140 struct ltt_kernel_event *event;
141 struct lttng_event ev;
142
143 memset(&ev, 0, sizeof(ev));
144 strncpy(ev.name, get_random_string(), LTTNG_KERNEL_SYM_NAME_LEN);
145 ev.type = LTTNG_EVENT_TRACEPOINT;
146 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
147
148 event = trace_kernel_create_event(&ev);
149 ok(event != NULL, "Create kernel event");
150
151 ok(event->fd == -1 &&
152 event->enabled == 1 &&
153 event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT &&
154 strlen(event->event->name),
155 "Validate kernel event");
156
157 /* Init list in order to avoid sefaults from cds_list_del */
158 CDS_INIT_LIST_HEAD(&event->list);
159 trace_kernel_destroy_event(event);
160 }
161
162 static void test_create_kernel_stream(void)
163 {
164 struct ltt_kernel_stream *stream;
165
166 stream = trace_kernel_create_stream("stream1", 0);
167 ok(stream != NULL, "Create kernel stream");
168
169 ok(stream->fd == -1 &&
170 stream->state == 0,
171 "Validate kernel stream");
172
173 /* Init list in order to avoid sefaults from cds_list_del */
174 CDS_INIT_LIST_HEAD(&stream->list);
175 trace_kernel_destroy_stream(stream);
176 }
177
178 int main(int argc, char **argv)
179 {
180 plan_tests(NUM_TESTS);
181
182 diag("Kernel data structure unit test");
183
184 test_create_one_kernel_session();
185 test_create_kernel_metadata();
186 test_create_kernel_channel();
187 test_create_kernel_event();
188 test_create_kernel_stream();
189
190 /* Success */
191 return 0;
192 }
This page took 0.035314 seconds and 6 git commands to generate.