| 1 | /* |
| 2 | * Unit tests for the kernel probe location API. |
| 3 | * |
| 4 | * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: LGPL-2.1-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <common/payload-view.hpp> |
| 11 | #include <common/payload.hpp> |
| 12 | |
| 13 | #include <lttng/kernel-probe-internal.hpp> |
| 14 | #include <lttng/kernel-probe.h> |
| 15 | |
| 16 | #include <inttypes.h> |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <tap/tap.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | /* For error.h */ |
| 23 | int lttng_opt_quiet = 1; |
| 24 | int lttng_opt_verbose; |
| 25 | int lttng_opt_mi; |
| 26 | |
| 27 | #define NUM_TESTS 24 |
| 28 | |
| 29 | static void test_kernel_probe_location_address() |
| 30 | { |
| 31 | struct lttng_kernel_probe_location *location = nullptr; |
| 32 | struct lttng_kernel_probe_location *location_from_buffer = nullptr; |
| 33 | enum lttng_kernel_probe_location_status status; |
| 34 | enum lttng_kernel_probe_location_type type; |
| 35 | uint64_t address = 50, _address; |
| 36 | struct lttng_payload payload; |
| 37 | |
| 38 | diag("Testing kernel probe location address"); |
| 39 | |
| 40 | lttng_payload_init(&payload); |
| 41 | |
| 42 | location = lttng_kernel_probe_location_address_create(address); |
| 43 | ok(location, "Location object"); |
| 44 | |
| 45 | type = lttng_kernel_probe_location_get_type(location); |
| 46 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS == type, |
| 47 | "Location type got %d expected %d", |
| 48 | type, |
| 49 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS); |
| 50 | |
| 51 | status = lttng_kernel_probe_location_address_get_address(location, &_address); |
| 52 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting address"); |
| 53 | ok(address == _address, |
| 54 | "Address is equal. Got %" PRIu64 " expected %" PRIu64, |
| 55 | _address, |
| 56 | address); |
| 57 | |
| 58 | ok(lttng_kernel_probe_location_serialize(location, &payload) > 0, "Serializing"); |
| 59 | { |
| 60 | struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); |
| 61 | ok(lttng_kernel_probe_location_create_from_payload(&view, &location_from_buffer) > |
| 62 | 0, |
| 63 | "Deserializing"); |
| 64 | } |
| 65 | |
| 66 | type = lttng_kernel_probe_location_get_type(location_from_buffer); |
| 67 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS == type, |
| 68 | "Location from buffer type got %d expected %d", |
| 69 | type, |
| 70 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS); |
| 71 | |
| 72 | status = lttng_kernel_probe_location_address_get_address(location_from_buffer, &_address); |
| 73 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting address"); |
| 74 | ok(address == _address, |
| 75 | "Address from buffer is equal. Got %" PRIu64 " expected %" PRIu64, |
| 76 | _address, |
| 77 | address); |
| 78 | |
| 79 | ok(lttng_kernel_probe_location_is_equal(location, location_from_buffer), |
| 80 | "serialized and from buffer are equal"); |
| 81 | |
| 82 | lttng_payload_reset(&payload); |
| 83 | lttng_kernel_probe_location_destroy(location); |
| 84 | lttng_kernel_probe_location_destroy(location_from_buffer); |
| 85 | } |
| 86 | |
| 87 | static void test_kernel_probe_location_symbol() |
| 88 | { |
| 89 | struct lttng_kernel_probe_location *location = nullptr; |
| 90 | struct lttng_kernel_probe_location *location_from_buffer = nullptr; |
| 91 | enum lttng_kernel_probe_location_status status; |
| 92 | enum lttng_kernel_probe_location_type type; |
| 93 | uint64_t offset = 50, _offset; |
| 94 | const char *symbol = "Une_bonne", *_symbol; |
| 95 | struct lttng_payload payload; |
| 96 | |
| 97 | diag("Testing kernel probe location symbol"); |
| 98 | |
| 99 | lttng_payload_init(&payload); |
| 100 | |
| 101 | location = lttng_kernel_probe_location_symbol_create(symbol, offset); |
| 102 | ok(location, "Location object"); |
| 103 | |
| 104 | type = lttng_kernel_probe_location_get_type(location); |
| 105 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET == type, |
| 106 | "Location type got %d expected %d", |
| 107 | type, |
| 108 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET); |
| 109 | |
| 110 | _symbol = lttng_kernel_probe_location_symbol_get_name(location); |
| 111 | ok(_symbol, "Getting symbol name"); |
| 112 | ok(!strncmp(symbol, _symbol, strlen(symbol)), |
| 113 | "Symbol name is equal. Got %s, expected %s", |
| 114 | _symbol, |
| 115 | symbol); |
| 116 | |
| 117 | status = lttng_kernel_probe_location_symbol_get_offset(location, &_offset); |
| 118 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting offset"); |
| 119 | ok(offset == _offset, "Offset is equal. Got %" PRIu64 " expected %" PRIu64, _offset, offset); |
| 120 | |
| 121 | ok(lttng_kernel_probe_location_serialize(location, &payload) > 0, "Serializing"); |
| 122 | { |
| 123 | struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); |
| 124 | ok(lttng_kernel_probe_location_create_from_payload(&view, &location_from_buffer) > |
| 125 | 0, |
| 126 | "Deserializing"); |
| 127 | } |
| 128 | |
| 129 | type = lttng_kernel_probe_location_get_type(location_from_buffer); |
| 130 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET == type, |
| 131 | "Location from buffer type got %d expected %d", |
| 132 | type, |
| 133 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET); |
| 134 | |
| 135 | _symbol = lttng_kernel_probe_location_symbol_get_name(location_from_buffer); |
| 136 | ok(_symbol, "Getting symbol name"); |
| 137 | ok(!strncmp(symbol, _symbol, strlen(symbol)), |
| 138 | "Symbol name is equal. Got %s, expected %s", |
| 139 | _symbol, |
| 140 | symbol); |
| 141 | |
| 142 | status = lttng_kernel_probe_location_symbol_get_offset(location_from_buffer, &_offset); |
| 143 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting offset"); |
| 144 | ok(offset == _offset, "Offset is equal. Got %" PRIu64 " expected %" PRIu64, _offset, offset); |
| 145 | |
| 146 | ok(lttng_kernel_probe_location_is_equal(location, location_from_buffer), |
| 147 | "serialized and from buffer are equal"); |
| 148 | |
| 149 | lttng_payload_reset(&payload); |
| 150 | lttng_kernel_probe_location_destroy(location); |
| 151 | lttng_kernel_probe_location_destroy(location_from_buffer); |
| 152 | } |
| 153 | |
| 154 | int main() |
| 155 | { |
| 156 | plan_tests(NUM_TESTS); |
| 157 | test_kernel_probe_location_address(); |
| 158 | test_kernel_probe_location_symbol(); |
| 159 | return exit_status(); |
| 160 | } |