| 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 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <tap/tap.h> |
| 34 | |
| 35 | #include <lttng/action/action.h> |
| 36 | #include <lttng/action/notify.h> |
| 37 | #include <lttng/condition/buffer-usage.h> |
| 38 | #include <lttng/condition/condition.h> |
| 39 | #include <lttng/domain.h> |
| 40 | #include <lttng/notification/notification.h> |
| 41 | #include <lttng/trigger/trigger.h> |
| 42 | |
| 43 | /* For error.h */ |
| 44 | int lttng_opt_quiet = 1; |
| 45 | int lttng_opt_verbose; |
| 46 | int lttng_opt_mi; |
| 47 | |
| 48 | #define NUM_TESTS 182 |
| 49 | |
| 50 | void test_condition_buffer_usage(struct lttng_condition *buffer_usage_condition) |
| 51 | { |
| 52 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; |
| 53 | const char *session_name = NULL; |
| 54 | const char *channel_name = NULL; |
| 55 | enum lttng_domain_type domain_type; |
| 56 | /* Start at a non zero value to validate initialization */ |
| 57 | double threshold_ratio; |
| 58 | uint64_t threshold_bytes; |
| 59 | |
| 60 | assert(buffer_usage_condition); |
| 61 | |
| 62 | diag("Validating initialization"); |
| 63 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 64 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 65 | |
| 66 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 67 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold byte is unset"); |
| 68 | |
| 69 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 70 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 71 | ok(!session_name, "Session name is null"); |
| 72 | |
| 73 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 74 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 75 | ok(!session_name, "Channel name is null"); |
| 76 | |
| 77 | status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type); |
| 78 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain name is unset"); |
| 79 | |
| 80 | diag("Testing session name set/get"); |
| 81 | status = lttng_condition_buffer_usage_set_session_name(NULL, "Test"); |
| 82 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set session name"); |
| 83 | status = lttng_condition_buffer_usage_get_session_name(NULL, &session_name); |
| 84 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get session name with null condition"); |
| 85 | ok(!session_name, "Session name is null"); |
| 86 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 87 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 88 | ok(!session_name, "Session name is null"); |
| 89 | |
| 90 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, NULL); |
| 91 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null session name"); |
| 92 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 93 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 94 | ok(!session_name, "Session name is null"); |
| 95 | |
| 96 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, ""); |
| 97 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty session name"); |
| 98 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 99 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset"); |
| 100 | ok(!session_name, "Session name is null"); |
| 101 | |
| 102 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "session420"); |
| 103 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set session name session420"); |
| 104 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 105 | ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is set"); |
| 106 | ok(session_name, "Session name has a value"); |
| 107 | ok(strcmp("session420", session_name) == 0, "Session name is %s", "session420"); |
| 108 | |
| 109 | /* |
| 110 | * Test second set on session_name. Test invalid set and validate that |
| 111 | * the value is still the previous good one. |
| 112 | */ |
| 113 | |
| 114 | status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, ""); |
| 115 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set session name to empty"); |
| 116 | status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name); |
| 117 | ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is still set"); |
| 118 | ok(session_name, "Session name has a value"); |
| 119 | ok(strcmp("session420", session_name) == 0, "Session is still name is %s", "session420"); |
| 120 | |
| 121 | diag("Testing channel name set/get"); |
| 122 | status = lttng_condition_buffer_usage_set_channel_name(NULL, "Test"); |
| 123 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set channel name"); |
| 124 | status = lttng_condition_buffer_usage_get_channel_name(NULL, &channel_name); |
| 125 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get channel name with null condition"); |
| 126 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 127 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 128 | ok(!channel_name, "Channel name is null"); |
| 129 | |
| 130 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, NULL); |
| 131 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null channel name"); |
| 132 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 133 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 134 | ok(!channel_name, "Channel name is null"); |
| 135 | |
| 136 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, ""); |
| 137 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty channel name"); |
| 138 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 139 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset"); |
| 140 | ok(!channel_name, "Channel name is null"); |
| 141 | |
| 142 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "channel420"); |
| 143 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set channel name channel420"); |
| 144 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 145 | ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is set"); |
| 146 | ok(channel_name, "Channel name has a value"); |
| 147 | ok(strcmp("channel420", channel_name) == 0, "Channel name is %s", "channel420"); |
| 148 | |
| 149 | /* |
| 150 | * Test second set on channel_name. Test invalid set and validate that |
| 151 | * the value is still the previous good one. |
| 152 | */ |
| 153 | |
| 154 | status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, ""); |
| 155 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set channel name to empty"); |
| 156 | status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name); |
| 157 | ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is still set"); |
| 158 | ok(channel_name, "Channel name has a value"); |
| 159 | ok(strcmp("channel420", channel_name) == 0, "Channel is still name is %s", "channel420"); |
| 160 | |
| 161 | diag("Testing threshold ratio set/get"); |
| 162 | status = lttng_condition_buffer_usage_set_threshold_ratio(NULL, 0.420); |
| 163 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio with null condition"); |
| 164 | status = lttng_condition_buffer_usage_get_threshold_ratio(NULL, &threshold_ratio); |
| 165 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold ratio with null condition"); |
| 166 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 167 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 168 | |
| 169 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, -100.0); |
| 170 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio < 0"); |
| 171 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 172 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 173 | |
| 174 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 200.0); |
| 175 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set Threshold ratio > 1"); |
| 176 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 177 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 178 | |
| 179 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 1.0); |
| 180 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 1.0"); |
| 181 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 182 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 183 | ok(threshold_ratio == 1.0, "Threshold ratio is 1.0"); |
| 184 | |
| 185 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.0); |
| 186 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.0"); |
| 187 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 188 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 189 | ok(threshold_ratio == 0.0, "Threshold ratio is 0.0"); |
| 190 | |
| 191 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420); |
| 192 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420"); |
| 193 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 194 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 195 | ok(threshold_ratio == 0.420, "Threshold ratio is 0.420"); |
| 196 | |
| 197 | diag("Testing threshold bytes set/get"); |
| 198 | status = lttng_condition_buffer_usage_set_threshold(NULL, 100000); |
| 199 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold with null condition"); |
| 200 | status = lttng_condition_buffer_usage_get_threshold(NULL, &threshold_bytes); |
| 201 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold value with null condition "); |
| 202 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 203 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset"); |
| 204 | |
| 205 | status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 100000); |
| 206 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold > 0"); |
| 207 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 208 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set"); |
| 209 | ok(threshold_bytes == 100000, "Threshold is %" PRIu64 , 100000); |
| 210 | |
| 211 | status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, UINT64_MAX); |
| 212 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold UINT64_MAX"); |
| 213 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 214 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set"); |
| 215 | ok(threshold_bytes == UINT64_MAX, "Threshold is UINT64_MAX"); |
| 216 | |
| 217 | status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 0); |
| 218 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold == 0"); |
| 219 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 220 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set"); |
| 221 | ok(threshold_bytes == 0, "Threshold is %d", 0); |
| 222 | |
| 223 | /* |
| 224 | * Test value of threshold ration, since we overwrote it with a byte |
| 225 | * threshold. Make sure it gets squashed. |
| 226 | */ |
| 227 | diag("Testing interaction between byte and ratio thresholds"); |
| 228 | |
| 229 | threshold_ratio = -1.0; |
| 230 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 231 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset"); |
| 232 | ok(threshold_ratio == -1.0, "Threshold ratio is untouched"); |
| 233 | |
| 234 | /* Set a ratio to validate that the byte threshold is now unset */ |
| 235 | status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420); |
| 236 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420"); |
| 237 | status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio); |
| 238 | ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set"); |
| 239 | ok(threshold_ratio == 0.420, "Threshold ratio is 0.420"); |
| 240 | |
| 241 | threshold_bytes = 420; |
| 242 | status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes); |
| 243 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset"); |
| 244 | ok(threshold_bytes == 420, "Threshold is untouched"); |
| 245 | |
| 246 | diag("Testing domain type set/get"); |
| 247 | status = lttng_condition_buffer_usage_set_domain_type(NULL, LTTNG_DOMAIN_UST); |
| 248 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type with null condition"); |
| 249 | status = lttng_condition_buffer_usage_get_domain_type(NULL, &domain_type); |
| 250 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get domain type with null condition"); |
| 251 | |
| 252 | status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_NONE); |
| 253 | ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type as LTTNG_DOMAIN_NONE"); |
| 254 | status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type); |
| 255 | ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain type is unset"); |
| 256 | |
| 257 | status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_UST); |
| 258 | ok(status == LTTNG_CONDITION_STATUS_OK, "Set domain type as LTTNG_DOMAIN_UST"); |
| 259 | status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type); |
| 260 | ok(status == LTTNG_CONDITION_STATUS_OK, "Domain type is set"); |
| 261 | ok(domain_type == LTTNG_DOMAIN_UST, "Domain type is LTTNG_DOMAIN_UST"); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | void test_condition_buffer_usage_low(void) |
| 266 | { |
| 267 | struct lttng_condition *buffer_usage_low = NULL; |
| 268 | |
| 269 | diag("Testing lttng_condition_buffer_usage_low_create"); |
| 270 | buffer_usage_low = lttng_condition_buffer_usage_low_create(); |
| 271 | ok(buffer_usage_low, "Condition allocated"); |
| 272 | |
| 273 | ok(lttng_condition_get_type(buffer_usage_low) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, "Condition is of type \"low buffer usage\""); |
| 274 | |
| 275 | test_condition_buffer_usage(buffer_usage_low); |
| 276 | |
| 277 | lttng_condition_destroy(buffer_usage_low); |
| 278 | } |
| 279 | |
| 280 | void test_condition_buffer_usage_high(void) |
| 281 | { |
| 282 | struct lttng_condition *buffer_usage_high = NULL; |
| 283 | |
| 284 | diag("Testing lttng_condition_buffer_usage_high_create"); |
| 285 | buffer_usage_high = lttng_condition_buffer_usage_high_create(); |
| 286 | ok(buffer_usage_high, "High buffer usage condition allocated"); |
| 287 | |
| 288 | ok(lttng_condition_get_type(buffer_usage_high) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, "Condition is of type \"high buffer usage\""); |
| 289 | |
| 290 | test_condition_buffer_usage(buffer_usage_high); |
| 291 | |
| 292 | lttng_condition_destroy(buffer_usage_high); |
| 293 | } |
| 294 | |
| 295 | void test_action(void) |
| 296 | { |
| 297 | struct lttng_action *notify_action = NULL; |
| 298 | |
| 299 | notify_action = lttng_action_notify_create(); |
| 300 | ok(notify_action, "Create notify action"); |
| 301 | ok(lttng_action_get_type(notify_action) == LTTNG_ACTION_TYPE_NOTIFY, "Action has type LTTNG_ACTION_TYPE_NOTIFY"); |
| 302 | lttng_action_destroy(notify_action); |
| 303 | } |
| 304 | |
| 305 | void test_trigger(void) |
| 306 | { |
| 307 | struct lttng_action *notify_action = NULL; |
| 308 | struct lttng_condition *buffer_usage_high = NULL; |
| 309 | struct lttng_trigger *trigger = NULL; |
| 310 | |
| 311 | notify_action = lttng_action_notify_create(); |
| 312 | buffer_usage_high = lttng_condition_buffer_usage_high_create(); |
| 313 | |
| 314 | trigger = lttng_trigger_create(NULL, NULL); |
| 315 | ok(!trigger, "lttng_trigger_create(NULL, NULL) returns null"); |
| 316 | trigger = lttng_trigger_create(buffer_usage_high, NULL); |
| 317 | ok(!trigger, "lttng_trigger_create(NON-NULL, NULL) returns null"); |
| 318 | trigger = lttng_trigger_create(NULL, notify_action); |
| 319 | ok(!trigger, "lttng_trigger_create(NULL, NON-NULL) returns null"); |
| 320 | |
| 321 | trigger = lttng_trigger_create(buffer_usage_high, notify_action); |
| 322 | ok(trigger, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object"); |
| 323 | |
| 324 | lttng_action_destroy(notify_action); |
| 325 | lttng_condition_destroy(buffer_usage_high); |
| 326 | lttng_trigger_destroy(trigger); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | int main(int argc, const char *argv[]) |
| 331 | { |
| 332 | plan_tests(NUM_TESTS); |
| 333 | test_condition_buffer_usage_low(); |
| 334 | test_condition_buffer_usage_high(); |
| 335 | test_action(); |
| 336 | test_trigger(); |
| 337 | return exit_status(); |
| 338 | } |