Tests: Fix: Use '.logfile' instead of '.log' for test app output
[lttng-tools.git] / tests / unit / test_condition.cpp
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
12 #include <common/buffer-view.hpp>
13 #include <common/dynamic-buffer.hpp>
14
15 #include <lttng/condition/condition-internal.hpp>
16 #include <lttng/condition/event-rule-matches-internal.hpp>
17 #include <lttng/condition/event-rule-matches.h>
18 #include <lttng/domain.h>
19 #include <lttng/event-rule/user-tracepoint.h>
20 #include <lttng/event.h>
21 #include <lttng/log-level-rule.h>
22
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <tap/tap.h>
27 #include <unistd.h>
28
29 /* For error.h */
30 int lttng_opt_quiet = 1;
31 int lttng_opt_verbose;
32 int lttng_opt_mi;
33
34 #define NUM_TESTS 13
35
36 static void test_condition_event_rule()
37 {
38 int ret, i;
39 struct lttng_event_rule *tracepoint = nullptr;
40 const struct lttng_event_rule *tracepoint_tmp = nullptr;
41 enum lttng_event_rule_status status;
42 struct lttng_condition *condition = nullptr;
43 struct lttng_condition *condition_from_buffer = nullptr;
44 enum lttng_condition_status condition_status;
45 const char *pattern = "my_event_*";
46 const char *filter = "msg_id == 23 && size >= 2048";
47 const char *exclusions[] = { "my_event_test1", "my_event_test2", "my_event_test3" };
48 struct lttng_log_level_rule *log_level_rule_at_least_as_severe = nullptr;
49 struct lttng_payload buffer;
50
51 lttng_payload_init(&buffer);
52
53 /* Create log level rule. */
54 log_level_rule_at_least_as_severe =
55 lttng_log_level_rule_at_least_as_severe_as_create(LTTNG_LOGLEVEL_WARNING);
56 LTTNG_ASSERT(log_level_rule_at_least_as_severe);
57
58 tracepoint = lttng_event_rule_user_tracepoint_create();
59 ok(tracepoint, "user tracepoint");
60
61 status = lttng_event_rule_user_tracepoint_set_name_pattern(tracepoint, pattern);
62 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting pattern");
63
64 status = lttng_event_rule_user_tracepoint_set_filter(tracepoint, filter);
65 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting filter");
66
67 status = lttng_event_rule_user_tracepoint_set_log_level_rule(
68 tracepoint, log_level_rule_at_least_as_severe);
69 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting log level range");
70
71 for (i = 0; i < 3; i++) {
72 status = lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(tracepoint,
73 exclusions[i]);
74 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting exclusion pattern");
75 }
76
77 condition = lttng_condition_event_rule_matches_create(tracepoint);
78 ok(condition, "Created condition");
79
80 condition_status = lttng_condition_event_rule_matches_get_rule(condition, &tracepoint_tmp);
81 ok(condition_status == LTTNG_CONDITION_STATUS_OK,
82 "Getting event rule from event rule condition");
83 ok(tracepoint == tracepoint_tmp,
84 "lttng_condition_event_rule_get_rule provides a reference to the original rule");
85
86 ret = lttng_condition_serialize(condition, &buffer);
87 ok(ret == 0, "Condition serialized");
88
89 {
90 struct lttng_payload_view view = lttng_payload_view_from_payload(&buffer, 0, -1);
91
92 (void) lttng_condition_create_from_payload(&view, &condition_from_buffer);
93 }
94
95 ok(condition_from_buffer, "Condition created from payload is non-null");
96
97 ok(lttng_condition_is_equal(condition, condition_from_buffer),
98 "Serialized and de-serialized conditions are equal");
99
100 lttng_payload_reset(&buffer);
101 lttng_event_rule_destroy(tracepoint);
102 lttng_condition_destroy(condition);
103 lttng_condition_destroy(condition_from_buffer);
104 lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe);
105 }
106
107 int main()
108 {
109 plan_tests(NUM_TESTS);
110 test_condition_event_rule();
111 return exit_status();
112 }
This page took 0.03286 seconds and 4 git commands to generate.