Commit | Line | Data |
---|---|---|
d17cfa5d JR |
1 | /* |
2 | * test_condition.c | |
3 | * | |
4 | * Unit tests for the condition API. | |
5 | * | |
6 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> | |
7 | * | |
8 | * SPDX-License-Identifier: GPL-2.0-only | |
9 | * | |
10 | */ | |
11 | ||
d17cfa5d JR |
12 | #include <inttypes.h> |
13 | #include <stdio.h> | |
14 | #include <string.h> | |
15 | #include <unistd.h> | |
16 | ||
17 | #include <tap/tap.h> | |
18 | ||
19 | #include <lttng/event.h> | |
695f7044 | 20 | #include <lttng/event-rule/user-tracepoint.h> |
d17cfa5d | 21 | #include <lttng/condition/condition-internal.h> |
670a26e4 JR |
22 | #include <lttng/condition/event-rule-matches.h> |
23 | #include <lttng/condition/event-rule-matches-internal.h> | |
d17cfa5d | 24 | #include <lttng/domain.h> |
85b05318 | 25 | #include <lttng/log-level-rule.h> |
d17cfa5d JR |
26 | #include <common/dynamic-buffer.h> |
27 | #include <common/buffer-view.h> | |
28 | ||
29 | /* For error.h */ | |
30 | int lttng_opt_quiet = 1; | |
31 | int lttng_opt_verbose; | |
32 | int lttng_opt_mi; | |
33 | ||
b006f85d | 34 | #define NUM_TESTS 13 |
d17cfa5d JR |
35 | |
36 | static | |
37 | void test_condition_event_rule(void) | |
38 | { | |
39 | int ret, i; | |
40 | struct lttng_event_rule *tracepoint = NULL; | |
41 | const struct lttng_event_rule *tracepoint_tmp = NULL; | |
42 | enum lttng_event_rule_status status; | |
43 | struct lttng_condition *condition = NULL; | |
44 | struct lttng_condition *condition_from_buffer = NULL; | |
45 | enum lttng_condition_status condition_status; | |
46 | const char *pattern="my_event_*"; | |
47 | const char *filter="msg_id == 23 && size >= 2048"; | |
48 | const char *exclusions[] = { "my_event_test1", "my_event_test2", "my_event_test3" }; | |
85b05318 | 49 | struct lttng_log_level_rule *log_level_rule_at_least_as_severe = NULL; |
d17cfa5d JR |
50 | struct lttng_payload buffer; |
51 | ||
52 | lttng_payload_init(&buffer); | |
53 | ||
85b05318 JR |
54 | /* Create log level rule. */ |
55 | log_level_rule_at_least_as_severe = | |
56 | lttng_log_level_rule_at_least_as_severe_as_create( | |
57 | LTTNG_LOGLEVEL_WARNING); | |
a0377dfe | 58 | LTTNG_ASSERT(log_level_rule_at_least_as_severe); |
85b05318 | 59 | |
695f7044 JR |
60 | tracepoint = lttng_event_rule_user_tracepoint_create(); |
61 | ok(tracepoint, "user tracepoint"); | |
d17cfa5d | 62 | |
695f7044 | 63 | status = lttng_event_rule_user_tracepoint_set_name_pattern(tracepoint, pattern); |
d17cfa5d JR |
64 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting pattern"); |
65 | ||
695f7044 | 66 | status = lttng_event_rule_user_tracepoint_set_filter(tracepoint, filter); |
d17cfa5d JR |
67 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting filter"); |
68 | ||
695f7044 | 69 | status = lttng_event_rule_user_tracepoint_set_log_level_rule( |
85b05318 | 70 | tracepoint, log_level_rule_at_least_as_severe); |
d17cfa5d JR |
71 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting log level range"); |
72 | ||
73 | for (i = 0; i < 3; i++) { | |
695f7044 | 74 | status = lttng_event_rule_user_tracepoint_add_name_pattern_exclusion( |
d17cfa5d JR |
75 | tracepoint, exclusions[i]); |
76 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, | |
77 | "Setting exclusion pattern"); | |
78 | } | |
79 | ||
8dbb86b8 | 80 | condition = lttng_condition_event_rule_matches_create(tracepoint); |
d17cfa5d JR |
81 | ok(condition, "Created condition"); |
82 | ||
8dbb86b8 | 83 | condition_status = lttng_condition_event_rule_matches_get_rule( |
d17cfa5d JR |
84 | condition, &tracepoint_tmp); |
85 | ok(condition_status == LTTNG_CONDITION_STATUS_OK, | |
86 | "Getting event rule from event rule condition"); | |
87 | ok(tracepoint == tracepoint_tmp, "lttng_condition_event_rule_get_rule provides a reference to the original rule"); | |
88 | ||
89 | ret = lttng_condition_serialize(condition, &buffer); | |
90 | ok(ret == 0, "Condition serialized"); | |
91 | ||
92 | { | |
93 | struct lttng_payload_view view = | |
94 | lttng_payload_view_from_payload(&buffer, 0, -1); | |
95 | ||
96 | (void) lttng_condition_create_from_payload( | |
97 | &view, &condition_from_buffer); | |
98 | } | |
99 | ||
100 | ok(condition_from_buffer, "Condition created from payload is non-null"); | |
101 | ||
102 | ok(lttng_condition_is_equal(condition, condition_from_buffer), | |
103 | "Serialized and de-serialized conditions are equal"); | |
104 | ||
105 | lttng_payload_reset(&buffer); | |
106 | lttng_event_rule_destroy(tracepoint); | |
107 | lttng_condition_destroy(condition); | |
108 | lttng_condition_destroy(condition_from_buffer); | |
85b05318 | 109 | lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe); |
d17cfa5d JR |
110 | } |
111 | ||
f46376a1 | 112 | int main(void) |
d17cfa5d JR |
113 | { |
114 | plan_tests(NUM_TESTS); | |
115 | test_condition_event_rule(); | |
116 | return exit_status(); | |
117 | } |