| 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 <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> |
| 20 | #include <lttng/event-rule/user-tracepoint.h> |
| 21 | #include <lttng/condition/condition-internal.h> |
| 22 | #include <lttng/condition/event-rule-matches.h> |
| 23 | #include <lttng/condition/event-rule-matches-internal.h> |
| 24 | #include <lttng/domain.h> |
| 25 | #include <lttng/log-level-rule.h> |
| 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 | |
| 34 | #define NUM_TESTS 13 |
| 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" }; |
| 49 | struct lttng_log_level_rule *log_level_rule_at_least_as_severe = NULL; |
| 50 | struct lttng_payload buffer; |
| 51 | |
| 52 | lttng_payload_init(&buffer); |
| 53 | |
| 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); |
| 58 | LTTNG_ASSERT(log_level_rule_at_least_as_severe); |
| 59 | |
| 60 | tracepoint = lttng_event_rule_user_tracepoint_create(); |
| 61 | ok(tracepoint, "user tracepoint"); |
| 62 | |
| 63 | status = lttng_event_rule_user_tracepoint_set_name_pattern(tracepoint, pattern); |
| 64 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting pattern"); |
| 65 | |
| 66 | status = lttng_event_rule_user_tracepoint_set_filter(tracepoint, filter); |
| 67 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting filter"); |
| 68 | |
| 69 | status = lttng_event_rule_user_tracepoint_set_log_level_rule( |
| 70 | tracepoint, log_level_rule_at_least_as_severe); |
| 71 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting log level range"); |
| 72 | |
| 73 | for (i = 0; i < 3; i++) { |
| 74 | status = lttng_event_rule_user_tracepoint_add_name_pattern_exclusion( |
| 75 | tracepoint, exclusions[i]); |
| 76 | ok(status == LTTNG_EVENT_RULE_STATUS_OK, |
| 77 | "Setting exclusion pattern"); |
| 78 | } |
| 79 | |
| 80 | condition = lttng_condition_event_rule_matches_create(tracepoint); |
| 81 | ok(condition, "Created condition"); |
| 82 | |
| 83 | condition_status = lttng_condition_event_rule_matches_get_rule( |
| 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); |
| 109 | lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe); |
| 110 | } |
| 111 | |
| 112 | int main(int argc, const char *argv[]) |
| 113 | { |
| 114 | plan_tests(NUM_TESTS); |
| 115 | test_condition_event_rule(); |
| 116 | return exit_status(); |
| 117 | } |