Tests: fix: log level rule: leak of rule after test
[lttng-tools.git] / tests / unit / test_log_level_rule.c
CommitLineData
85b05318
JR
1/*
2 * Unit tests for the log level rule API.
3 *
4 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only
7 *
8 */
9
10#include <assert.h>
11#include <inttypes.h>
12#include <stdio.h>
13#include <string.h>
14#include <unistd.h>
15
16#include <tap/tap.h>
17
18#include <common/payload-view.h>
19#include <common/payload.h>
20#include <lttng/log-level-rule-internal.h>
21#include <lttng/log-level-rule.h>
22
23/* For error.h. */
24int lttng_opt_quiet = 1;
25int lttng_opt_verbose;
26int lttng_opt_mi;
27
28#define NUM_TESTS 29
29
30static void test_log_level_rule_error(void)
31{
32 int level = 9000;
33 struct lttng_log_level_rule *exactly =
34 lttng_log_level_rule_exactly_create(level);
35 struct lttng_log_level_rule *at_least_as_severe =
36 lttng_log_level_rule_at_least_as_severe_as_create(
37 level);
38
39 ok(lttng_log_level_rule_get_type(NULL) == LTTNG_LOG_LEVEL_RULE_TYPE_UNKNOWN, "Get type on invalid pointer");
40
41 ok(lttng_log_level_rule_exactly_get_level(NULL, NULL) == LTTNG_LOG_LEVEL_RULE_STATUS_INVALID, "lttng_log_level_rule_exactly_get_level (NULL, NULL) returns invalid");
42 ok(lttng_log_level_rule_exactly_get_level(exactly, NULL) == LTTNG_LOG_LEVEL_RULE_STATUS_INVALID, "lttng_log_level_rule_exactly_get_level (valid, NULL) returns invalid");
43 ok(lttng_log_level_rule_exactly_get_level(NULL, &level) == LTTNG_LOG_LEVEL_RULE_STATUS_INVALID, "lttng_log_level_rule_exactly_get_level (NULL, valid) returns invalid");
44
45 ok(lttng_log_level_rule_at_least_as_severe_as_get_level(NULL, NULL) == LTTNG_LOG_LEVEL_RULE_STATUS_INVALID, "lttng_log_level_rule_at_least_as_severe_as_get_level (NULL, NULL) returns invalid");
46 ok(lttng_log_level_rule_at_least_as_severe_as_get_level(exactly, NULL) == LTTNG_LOG_LEVEL_RULE_STATUS_INVALID, "lttng_log_level_rule_at_least_as_severe_as_get_level (valid, NULL) returns invalid");
47 ok(lttng_log_level_rule_at_least_as_severe_as_get_level(NULL, &level) == LTTNG_LOG_LEVEL_RULE_STATUS_INVALID, "lttng_log_level_rule_at_least_as_severe_as_get_level (NULL, valid) returns invalid");
48
49 lttng_log_level_rule_destroy(exactly);
50 lttng_log_level_rule_destroy(at_least_as_severe);
51}
52
53static
54void test_log_level_rule_serialize_deserialize(const struct lttng_log_level_rule *rule)
55{
56 struct lttng_log_level_rule *log_level_rule_from_buffer = NULL;
57 struct lttng_payload payload;
58
59 lttng_payload_init(&payload);
60
61 ok(lttng_log_level_rule_serialize(rule, &payload) == 0, "Serializing.");
62
63 {
64 struct lttng_payload_view view =
65 lttng_payload_view_from_payload(
66 &payload, 0, -1);
67
68 ok(lttng_log_level_rule_create_from_payload(
69 &view, &log_level_rule_from_buffer) > 0,
70 "Deserializing.");
71 }
72
73 ok(lttng_log_level_rule_is_equal(rule, log_level_rule_from_buffer), "Serialized and from buffer are equal");
74
75 lttng_log_level_rule_destroy(log_level_rule_from_buffer);
76}
77
78static
79void test_log_level_rule_is_equal_exactly(void)
80{
81 int level = 9000, no_eq_level = 420;
82 struct lttng_log_level_rule *a, *b, *different_level, *different_type;
83
84 /* Identical log level rules. */
85 a = lttng_log_level_rule_exactly_create(level);
86 b = lttng_log_level_rule_exactly_create(level);
87
88 /* Different level, same type. */
89 different_level = lttng_log_level_rule_exactly_create(no_eq_level);
90
91 /* Different type. */
92 different_type = lttng_log_level_rule_at_least_as_severe_as_create(level);
93
94 assert(a && b && different_level && different_type);
95
96 ok(lttng_log_level_rule_is_equal(a, a), "Same object is equal");
97 ok(lttng_log_level_rule_is_equal(a, b), "Object a and b are equal");
98 ok(!lttng_log_level_rule_is_equal(a, different_level), " Object of different levels are not equal");
99 ok(!lttng_log_level_rule_is_equal(a, different_type), " Object of different types are not equal");
100
101 lttng_log_level_rule_destroy(a);
102 lttng_log_level_rule_destroy(b);
103 lttng_log_level_rule_destroy(different_level);
104 lttng_log_level_rule_destroy(different_type);
105}
106
107static
108void test_log_level_rule_is_equal_at_least_as_severe_as(void)
109{
110 int level = 9000, no_eq_level = 420;
111 struct lttng_log_level_rule *a, *b, *different_level, *different_type;
112
113 /* Identical log level rules. */
114 a = lttng_log_level_rule_at_least_as_severe_as_create(level);
115 b = lttng_log_level_rule_at_least_as_severe_as_create(level);
116
117 /* Different level, same type. */
118 different_level = lttng_log_level_rule_at_least_as_severe_as_create(no_eq_level);
119
120 /* Different type. */
121 different_type = lttng_log_level_rule_exactly_create(level);
122
123 assert(a && b && different_level && different_type);
124
125 ok(lttng_log_level_rule_is_equal(a, a), "Same object is equal");
126 ok(lttng_log_level_rule_is_equal(a, b), "Object a and b are equal");
127 ok(!lttng_log_level_rule_is_equal(a, different_level), " Object of different levels are not equal");
128 ok(!lttng_log_level_rule_is_equal(a, different_type), " Object of different types are not equal");
129
130 lttng_log_level_rule_destroy(a);
131 lttng_log_level_rule_destroy(b);
132 lttng_log_level_rule_destroy(different_level);
133 lttng_log_level_rule_destroy(different_type);
134}
135
136static void test_log_level_rule_exactly(void)
137{
138 int level = 9000;
139 int _level;
140 struct lttng_log_level_rule *exactly = NULL;
141 enum lttng_log_level_rule_status status;
142
143 exactly = lttng_log_level_rule_exactly_create(level);
144
145 ok(exactly, "Log level exactly allocated");
146 ok(lttng_log_level_rule_get_type(exactly) ==
147 LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY,
148 "Log level rule exactly type");
149
150 status = lttng_log_level_rule_exactly_get_level(exactly, &_level);
151 ok(status == LTTNG_LOG_LEVEL_RULE_STATUS_OK, "Get the level");
152 ok(_level == level, "Level property is valid");
153
154 test_log_level_rule_is_equal_exactly();
155 test_log_level_rule_serialize_deserialize(exactly);
f6acdcb9 156 lttng_log_level_rule_destroy(exactly);
85b05318
JR
157}
158
159static void test_log_level_rule_at_least_as_severe_as(void)
160{
161 int level = 9000;
162 int _level;
163 struct lttng_log_level_rule *at_least_as_severe_as = NULL;
164 enum lttng_log_level_rule_status status;
165
166 at_least_as_severe_as = lttng_log_level_rule_at_least_as_severe_as_create(level);
167
168 ok(at_least_as_severe_as, "Log level at_least_as_severe_as allocated");
169 ok(lttng_log_level_rule_get_type(at_least_as_severe_as) ==
170 LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS,
171 "Log level rule at_least_as_severe_as type");
172
173 status = lttng_log_level_rule_at_least_as_severe_as_get_level(at_least_as_severe_as, &_level);
174 ok(status == LTTNG_LOG_LEVEL_RULE_STATUS_OK, "Get the level");
175 ok(_level == level, "Level property is valid");
176
177 test_log_level_rule_is_equal_at_least_as_severe_as();
178 test_log_level_rule_serialize_deserialize(at_least_as_severe_as);
f6acdcb9 179 lttng_log_level_rule_destroy(at_least_as_severe_as);
85b05318
JR
180}
181
182int main(int argc, const char *argv[])
183{
184 plan_tests(NUM_TESTS);
185 test_log_level_rule_exactly();
186 test_log_level_rule_at_least_as_severe_as();
187 test_log_level_rule_error();
188 return exit_status();
189}
This page took 0.042655 seconds and 4 git commands to generate.