| 1 | /* |
| 2 | * test_notification.c |
| 3 | * |
| 4 | * Unit tests for the notification API. |
| 5 | * |
| 6 | * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 7 | * |
| 8 | * SPDX-License-Identifier: MIT |
| 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/notify.h> |
| 22 | #include <lttng/condition/buffer-usage.h> |
| 23 | #include <lttng/condition/condition.h> |
| 24 | #include <lttng/domain.h> |
| 25 | #include <lttng/notification/notification.h> |
| 26 | #include <lttng/trigger/trigger.h> |
| 27 | |
| 28 | /* For error.h */ |
| 29 | int lttng_opt_quiet = 1; |
| 30 | int lttng_opt_verbose; |
| 31 | int lttng_opt_mi; |
| 32 | |
| 33 | #define NUM_TESTS 180 |
| 34 | |
| 35 | static void test_condition_buffer_usage( |
| 36 | struct lttng_condition *buffer_usage_condition) |
| 37 | { |
| 38 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; |
| 39 | const char *session_name = NULL; |
| 40 | const char *channel_name = NULL; |
| 41 | enum lttng_domain_type domain_type; |
| 42 | /* Start at a non zero value to validate initialization */ |
| 43 | double threshold_ratio; |
| 44 | uint64_t threshold_bytes; |
| 45 | |
| 46 | assert(buffer_usage_condition); |
| 47 | |
| 48 | diag("Validating initialization"); |
| 49 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 50 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 51 | |
| 52 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 53 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold byte is unset"); |
| 54 | |
| 55 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 56 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 57 | ok(!session_name, "Session name is null"); |
| 58 | |
| 59 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 60 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 61 | ok(!session_name, "Channel name is null"); |
| 62 | |
| 63 | status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type); |
| 64 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain name is unset"); |
| 65 | |
| 66 | diag("Testing session name set/get"); |
| 67 | status = lttng_condition_buffer_usage_set_session_name(NULL, "Test"); |
| 68 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set session name"); |
| 69 | status = lttng_condition_buffer_usage_get_session_name(NULL, &session_name); |
| 70 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get session name with null condition"); |
| 71 | ok(!session_name, "Session name is null"); |
| 72 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 73 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 74 | ok(!session_name, "Session name is null"); |
| 75 | |
| 76 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, NULL); |
| 77 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null session name"); |
| 78 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 79 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 80 | ok(!session_name, "Session name is null"); |
| 81 | |
| 82 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, ""); |
| 83 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty session name"); |
| 84 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 85 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 86 | ok(!session_name, "Session name is null"); |
| 87 | |
| 88 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "session420"); |
| 89 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set session name session420"); |
| 90 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 91 | ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is set"); |
| 92 | ok(session_name, "Session name has a value"); |
| 93 | ok(strcmp("session420", session_name) == 0, "Session name is %s", "session420"); |
| 94 | |
| 95 | /* |
| 96 | * Test second set on session_name. Test invalid set and validate that |
| 97 | * the value is still the previous good one. |
| 98 | */ |
| 99 | |
| 100 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, ""); |
| 101 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set session name to empty"); |
| 102 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 103 | ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is still set"); |
| 104 | ok(session_name, "Session name has a value"); |
| 105 | ok(strcmp("session420", session_name) == 0, "Session is still name is %s", "session420"); |
| 106 | |
| 107 | diag("Testing channel name set/get"); |
| 108 | status = lttng_condition_buffer_usage_set_channel_name(NULL, "Test"); |
| 109 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set channel name"); |
| 110 | status = lttng_condition_buffer_usage_get_channel_name(NULL, &channel_name); |
| 111 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get channel name with null condition"); |
| 112 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 113 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 114 | ok(!channel_name, "Channel name is null"); |
| 115 | |
| 116 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, NULL); |
| 117 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null channel name"); |
| 118 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 119 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 120 | ok(!channel_name, "Channel name is null"); |
| 121 | |
| 122 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, ""); |
| 123 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty channel name"); |
| 124 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 125 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 126 | ok(!channel_name, "Channel name is null"); |
| 127 | |
| 128 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "channel420"); |
| 129 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set channel name channel420"); |
| 130 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 131 | ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is set"); |
| 132 | ok(channel_name, "Channel name has a value"); |
| 133 | ok(strcmp("channel420", channel_name) == 0, "Channel name is %s", "channel420"); |
| 134 | |
| 135 | /* |
| 136 | * Test second set on channel_name. Test invalid set and validate that |
| 137 | * the value is still the previous good one. |
| 138 | */ |
| 139 | |
| 140 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, ""); |
| 141 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set channel name to empty"); |
| 142 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 143 | ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is still set"); |
| 144 | ok(channel_name, "Channel name has a value"); |
| 145 | ok(strcmp("channel420", channel_name) == 0, "Channel is still name is %s", "channel420"); |
| 146 | |
| 147 | diag("Testing threshold ratio set/get"); |
| 148 | status = lttng_condition_buffer_usage_set_threshold_ratio(NULL, 0.420); |
| 149 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio with null condition"); |
| 150 | status = lttng_condition_buffer_usage_get_threshold_ratio(NULL, &threshold_ratio); |
| 151 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold ratio with null condition"); |
| 152 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 153 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 154 | |
| 155 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, -100.0); |
| 156 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio < 0"); |
| 157 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 158 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 159 | |
| 160 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 200.0); |
| 161 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set Threshold ratio > 1"); |
| 162 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 163 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 164 | |
| 165 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 1.0); |
| 166 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 1.0"); |
| 167 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 168 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 169 | ok(threshold_ratio == 1.0, "Threshold ratio is 1.0"); |
| 170 | |
| 171 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.0); |
| 172 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.0"); |
| 173 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 174 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 175 | ok(threshold_ratio == 0.0, "Threshold ratio is 0.0"); |
| 176 | |
| 177 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420); |
| 178 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420"); |
| 179 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 180 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 181 | ok(threshold_ratio == 0.420, "Threshold ratio is 0.420"); |
| 182 | |
| 183 | diag("Testing threshold bytes set/get"); |
| 184 | status = lttng_condition_buffer_usage_set_threshold(NULL, 100000); |
| 185 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold with null condition"); |
| 186 | status = lttng_condition_buffer_usage_get_threshold(NULL, &threshold_bytes); |
| 187 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold value with null condition "); |
| 188 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 189 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset"); |
| 190 | |
| 191 | status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 100000); |
| 192 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold > 0"); |
| 193 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 194 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set"); |
| 195 | ok(threshold_bytes == 100000, "Threshold is %" PRIu64 , 100000); |
| 196 | |
| 197 | status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, UINT64_MAX); |
| 198 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold UINT64_MAX"); |
| 199 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 200 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set"); |
| 201 | ok(threshold_bytes == UINT64_MAX, "Threshold is UINT64_MAX"); |
| 202 | |
| 203 | status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 0); |
| 204 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold == 0"); |
| 205 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 206 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set"); |
| 207 | ok(threshold_bytes == 0, "Threshold is %d", 0); |
| 208 | |
| 209 | /* |
| 210 | * Test value of threshold ration, since we overwrote it with a byte |
| 211 | * threshold. Make sure it gets squashed. |
| 212 | */ |
| 213 | diag("Testing interaction between byte and ratio thresholds"); |
| 214 | |
| 215 | threshold_ratio = -1.0; |
| 216 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 217 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 218 | ok(threshold_ratio == -1.0, "Threshold ratio is untouched"); |
| 219 | |
| 220 | /* Set a ratio to validate that the byte threshold is now unset */ |
| 221 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420); |
| 222 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420"); |
| 223 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 224 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 225 | ok(threshold_ratio == 0.420, "Threshold ratio is 0.420"); |
| 226 | |
| 227 | threshold_bytes = 420; |
| 228 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 229 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset"); |
| 230 | ok(threshold_bytes == 420, "Threshold is untouched"); |
| 231 | |
| 232 | diag("Testing domain type set/get"); |
| 233 | status = lttng_condition_buffer_usage_set_domain_type(NULL, LTTNG_DOMAIN_UST); |
| 234 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type with null condition"); |
| 235 | status = lttng_condition_buffer_usage_get_domain_type(NULL, &domain_type); |
| 236 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get domain type with null condition"); |
| 237 | |
| 238 | status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_NONE); |
| 239 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type as LTTNG_DOMAIN_NONE"); |
| 240 | status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type); |
| 241 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain type is unset"); |
| 242 | |
| 243 | status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_UST); |
| 244 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set domain type as LTTNG_DOMAIN_UST"); |
| 245 | status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type); |
| 246 | ok(status == LTTNG_CONDITION_STATUS_OK, "Domain type is set"); |
| 247 | ok(domain_type == LTTNG_DOMAIN_UST, "Domain type is LTTNG_DOMAIN_UST"); |
| 248 | } |
| 249 | |
| 250 | static void test_condition_buffer_usage_low(void) |
| 251 | { |
| 252 | struct lttng_condition *buffer_usage_low = NULL; |
| 253 | |
| 254 | diag("Testing lttng_condition_buffer_usage_low_create"); |
| 255 | buffer_usage_low = lttng_condition_buffer_usage_low_create(); |
| 256 | ok(buffer_usage_low, "Condition allocated"); |
| 257 | |
| 258 | ok(lttng_condition_get_type(buffer_usage_low) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, "Condition is of type \"low buffer usage\""); |
| 259 | |
| 260 | test_condition_buffer_usage(buffer_usage_low); |
| 261 | |
| 262 | lttng_condition_destroy(buffer_usage_low); |
| 263 | } |
| 264 | |
| 265 | static void test_condition_buffer_usage_high(void) |
| 266 | { |
| 267 | struct lttng_condition *buffer_usage_high = NULL; |
| 268 | |
| 269 | diag("Testing lttng_condition_buffer_usage_high_create"); |
| 270 | buffer_usage_high = lttng_condition_buffer_usage_high_create(); |
| 271 | ok(buffer_usage_high, "High buffer usage condition allocated"); |
| 272 | |
| 273 | ok(lttng_condition_get_type(buffer_usage_high) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, "Condition is of type \"high buffer usage\""); |
| 274 | |
| 275 | test_condition_buffer_usage(buffer_usage_high); |
| 276 | |
| 277 | lttng_condition_destroy(buffer_usage_high); |
| 278 | } |
| 279 | |
| 280 | static void test_trigger(void) |
| 281 | { |
| 282 | struct lttng_action *notify_action = NULL; |
| 283 | struct lttng_condition *buffer_usage_high = NULL; |
| 284 | struct lttng_trigger *trigger = NULL; |
| 285 | |
| 286 | notify_action = lttng_action_notify_create(); |
| 287 | buffer_usage_high = lttng_condition_buffer_usage_high_create(); |
| 288 | |
| 289 | trigger = lttng_trigger_create(NULL, NULL); |
| 290 | ok(!trigger, "lttng_trigger_create(NULL, NULL) returns null"); |
| 291 | trigger = lttng_trigger_create(buffer_usage_high, NULL); |
| 292 | ok(!trigger, "lttng_trigger_create(NON-NULL, NULL) returns null"); |
| 293 | trigger = lttng_trigger_create(NULL, notify_action); |
| 294 | ok(!trigger, "lttng_trigger_create(NULL, NON-NULL) returns null"); |
| 295 | |
| 296 | trigger = lttng_trigger_create(buffer_usage_high, notify_action); |
| 297 | ok(trigger, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object"); |
| 298 | |
| 299 | lttng_action_destroy(notify_action); |
| 300 | lttng_condition_destroy(buffer_usage_high); |
| 301 | lttng_trigger_destroy(trigger); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | int main(int argc, const char *argv[]) |
| 306 | { |
| 307 | plan_tests(NUM_TESTS); |
| 308 | test_condition_buffer_usage_low(); |
| 309 | test_condition_buffer_usage_high(); |
| 310 | test_trigger(); |
| 311 | return exit_status(); |
| 312 | } |