2 * Unit tests for the log level rule API.
4 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
17 #include <common/payload-view.hpp>
18 #include <common/payload.hpp>
19 #include <lttng/log-level-rule-internal.hpp>
20 #include <lttng/log-level-rule.h>
23 int lttng_opt_quiet
= 1;
24 int lttng_opt_verbose
;
29 static void test_log_level_rule_error(void)
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(
38 ok(lttng_log_level_rule_get_type(NULL
) == LTTNG_LOG_LEVEL_RULE_TYPE_UNKNOWN
, "Get type on invalid pointer");
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");
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");
48 lttng_log_level_rule_destroy(exactly
);
49 lttng_log_level_rule_destroy(at_least_as_severe
);
53 void test_log_level_rule_serialize_deserialize(const struct lttng_log_level_rule
*rule
)
55 struct lttng_log_level_rule
*log_level_rule_from_buffer
= NULL
;
56 struct lttng_payload payload
;
58 lttng_payload_init(&payload
);
60 ok(lttng_log_level_rule_serialize(rule
, &payload
) == 0, "Serializing.");
63 struct lttng_payload_view view
=
64 lttng_payload_view_from_payload(
67 ok(lttng_log_level_rule_create_from_payload(
68 &view
, &log_level_rule_from_buffer
) > 0,
72 ok(lttng_log_level_rule_is_equal(rule
, log_level_rule_from_buffer
), "Serialized and from buffer are equal");
74 lttng_log_level_rule_destroy(log_level_rule_from_buffer
);
78 void test_log_level_rule_is_equal_exactly(void)
80 int level
= 9000, no_eq_level
= 420;
81 struct lttng_log_level_rule
*a
, *b
, *different_level
, *different_type
;
83 /* Identical log level rules. */
84 a
= lttng_log_level_rule_exactly_create(level
);
85 b
= lttng_log_level_rule_exactly_create(level
);
87 /* Different level, same type. */
88 different_level
= lttng_log_level_rule_exactly_create(no_eq_level
);
91 different_type
= lttng_log_level_rule_at_least_as_severe_as_create(level
);
93 LTTNG_ASSERT(a
&& b
&& different_level
&& different_type
);
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");
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
);
107 void test_log_level_rule_is_equal_at_least_as_severe_as(void)
109 int level
= 9000, no_eq_level
= 420;
110 struct lttng_log_level_rule
*a
, *b
, *different_level
, *different_type
;
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
);
116 /* Different level, same type. */
117 different_level
= lttng_log_level_rule_at_least_as_severe_as_create(no_eq_level
);
119 /* Different type. */
120 different_type
= lttng_log_level_rule_exactly_create(level
);
122 LTTNG_ASSERT(a
&& b
&& different_level
&& different_type
);
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");
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
);
135 static void test_log_level_rule_exactly(void)
139 struct lttng_log_level_rule
*exactly
= NULL
;
140 enum lttng_log_level_rule_status status
;
142 exactly
= lttng_log_level_rule_exactly_create(level
);
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");
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");
153 test_log_level_rule_is_equal_exactly();
154 test_log_level_rule_serialize_deserialize(exactly
);
155 lttng_log_level_rule_destroy(exactly
);
158 static void test_log_level_rule_at_least_as_severe_as(void)
162 struct lttng_log_level_rule
*at_least_as_severe_as
= NULL
;
163 enum lttng_log_level_rule_status status
;
165 at_least_as_severe_as
= lttng_log_level_rule_at_least_as_severe_as_create(level
);
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");
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");
176 test_log_level_rule_is_equal_at_least_as_severe_as();
177 test_log_level_rule_serialize_deserialize(at_least_as_severe_as
);
178 lttng_log_level_rule_destroy(at_least_as_severe_as
);
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();