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