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