SoW-2020-0003: Trace Hit Counters
[lttng-tools.git] / tests / unit / test_action.c
1 /*
2 * test_action.c
3 *
4 * Unit tests for the action API.
5 *
6 * Copyright (C) 2021 Francis Deslauriers <francis.deslauriers@efficios.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0-only
9 *
10 */
11
12 #include <assert.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <tap/tap.h>
19
20 #include <lttng/action/action.h>
21 #include <lttng/action/action-internal.h>
22 #include <lttng/action/incr-value.h>
23 #include <lttng/action/incr-value-internal.h>
24
25 #include <lttng/map-key-internal.h>
26
27 #include <common/dynamic-buffer.h>
28 #include <common/buffer-view.h>
29 #include <common/payload.h>
30
31 /* For error.h */
32 int lttng_opt_quiet = 1;
33 int lttng_opt_verbose;
34 int lttng_opt_mi;
35
36 #define NUM_TESTS 14
37
38 static
39 void test_action_incr_value(void)
40 {
41 int ret;
42 struct lttng_action *action = NULL;
43 struct lttng_action *action_from_buffer = NULL;
44 const char *map_name = "my_map_name";
45 const char *session_name = "my_session_name";
46 const char *first_part_key = "first_part_🥇_";
47 const char *second_part_key = "_🥈_second_part";
48 struct lttng_map_key *key = NULL;
49 enum lttng_action_status action_status;
50 enum lttng_map_key_status key_status;
51 struct lttng_payload buffer;
52 const struct lttng_map_key *key_from_buffer;
53 const struct lttng_map_key_token *token;
54
55 lttng_payload_init(&buffer);
56
57 /* Test key creation */
58 key = lttng_map_key_create();
59 ok(key, "Key created");
60
61 key_status = lttng_map_key_append_token_string(key, first_part_key);
62 ok(key_status == LTTNG_MAP_KEY_STATUS_OK, "Key append first string");
63
64 key_status = lttng_map_key_append_token_variable(key, LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME);
65 ok(key_status == LTTNG_MAP_KEY_STATUS_OK, "Key append event name variable");
66
67 key_status = lttng_map_key_append_token_string(key, second_part_key);
68 ok(key_status == LTTNG_MAP_KEY_STATUS_OK, "Key append second string");
69
70 /*Test incr value action creation */
71 action = lttng_action_incr_value_create();
72 ok(action, "Incr-value action created");
73
74 action_status = lttng_action_incr_value_set_session_name(action, session_name);
75 ok(action_status == LTTNG_ACTION_STATUS_OK, "incr-value set session name");
76
77 action_status = lttng_action_incr_value_set_map_name(action, map_name);
78 ok(action_status == LTTNG_ACTION_STATUS_OK, "incr-value set map name");
79
80 action_status = lttng_action_incr_value_set_key(action, key);
81 ok(action_status == LTTNG_ACTION_STATUS_OK, "incr-value set key");
82
83 /* Test incr value action serialization */
84 ret = lttng_action_serialize(action, &buffer);
85 ok(ret == 0, "Incr value action serialized");
86
87 {
88 struct lttng_payload_view view =
89 lttng_payload_view_from_payload(&buffer, 0, -1);
90
91 (void) lttng_action_create_from_payload(
92 &view, &action_from_buffer);
93 }
94 ok(action_from_buffer, "Incr value action created from payload is non-null");
95
96 action_status = lttng_action_incr_value_get_key(action, &key_from_buffer);
97 ok(key_from_buffer, "Retrived key from incr value action");
98
99 token = lttng_map_key_get_token_at_index(key_from_buffer, 0);
100 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "First key token is a string");
101
102 token = lttng_map_key_get_token_at_index(key_from_buffer, 1);
103 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE, "Second key token is a variable");
104
105 token = lttng_map_key_get_token_at_index(key_from_buffer, 2);
106 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "Third key token is a string");
107
108 lttng_payload_reset(&buffer);
109
110 lttng_action_destroy(action);
111 lttng_action_destroy(action_from_buffer);
112 lttng_map_key_destroy(key);
113 }
114
115 int main(int argc, const char *argv[])
116 {
117 plan_tests(NUM_TESTS);
118 test_action_incr_value();
119 return exit_status();
120 }
This page took 0.033034 seconds and 5 git commands to generate.