|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +/* |
| 3 | +Copyright (C) 2024 The Falco Authors. |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | +#pragma once |
| 15 | +#include <libscap/scap.h> |
| 16 | +#include <gtest/gtest.h> |
| 17 | +#include <cstdarg> |
| 18 | +#include <memory> |
| 19 | +#include <stdexcept> |
| 20 | +#include <libscap/engine/savefile/converter/converter.h> |
| 21 | + |
| 22 | +typedef std::shared_ptr<scap_evt> safe_scap_evt_t; |
| 23 | +safe_scap_evt_t new_safe_scap_evt(scap_evt *evt) { |
| 24 | + return safe_scap_evt_t{evt, free}; |
| 25 | +} |
| 26 | +class convert_event_test : public testing::Test { |
| 27 | + static constexpr uint16_t safe_margin = 100; |
| 28 | + |
| 29 | +protected: |
| 30 | + virtual void TearDown() { |
| 31 | + // At every iteration we want to clear the storage in the converter |
| 32 | + scap_clear_converter_storage(); |
| 33 | + } |
| 34 | + safe_scap_evt_t create_safe_scap_event(uint64_t ts, |
| 35 | + uint64_t tid, |
| 36 | + ppm_event_code event_type, |
| 37 | + uint32_t n, |
| 38 | + ...) { |
| 39 | + char error[SCAP_LASTERR_SIZE] = {'\0'}; |
| 40 | + va_list args; |
| 41 | + va_start(args, n); |
| 42 | + scap_evt *evt = scap_create_event_v(error, ts, tid, event_type, n, args); |
| 43 | + va_end(args); |
| 44 | + if(evt == NULL) { |
| 45 | + throw std::runtime_error("Error creating event: " + std::string(error)); |
| 46 | + } |
| 47 | + return new_safe_scap_evt(evt); |
| 48 | + } |
| 49 | + // The expected result can be either CONVERSION_CONTINUE or CONVERSION_COMPLETED |
| 50 | + void assert_single_conversion_success(enum conversion_result expected_res, |
| 51 | + safe_scap_evt_t evt_to_convert, |
| 52 | + safe_scap_evt_t expected_evt) { |
| 53 | + char error[SCAP_LASTERR_SIZE] = {'\0'}; |
| 54 | + // We assume it's okay to create a new event with the same size as the expected event |
| 55 | + auto storage = new_safe_scap_evt((scap_evt *)calloc(1, expected_evt->len)); |
| 56 | + // First we check the conversion result matches the expected result |
| 57 | + ASSERT_EQ(scap_convert_event(storage.get(), evt_to_convert.get(), error), expected_res) |
| 58 | + << "Different conversion results: " << error; |
| 59 | + if(!scap_compare_events(storage.get(), expected_evt.get(), error)) { |
| 60 | + printf("\nExpected event:\n"); |
| 61 | + scap_print_event(expected_evt.get(), PRINT_FULL); |
| 62 | + printf("\nConverted event:\n"); |
| 63 | + scap_print_event(storage.get(), PRINT_FULL); |
| 64 | + FAIL() << error; |
| 65 | + } |
| 66 | + } |
| 67 | + void assert_single_conversion_failure(safe_scap_evt_t evt_to_convert) { |
| 68 | + char error[SCAP_LASTERR_SIZE] = {'\0'}; |
| 69 | + // We assume it's okay to create a new event with the same size as the expected event |
| 70 | + auto storage = new_safe_scap_evt((scap_evt *)calloc(1, evt_to_convert->len)); |
| 71 | + // First we check the conversion result matches the expected result |
| 72 | + ASSERT_EQ(scap_convert_event(storage.get(), evt_to_convert.get(), error), CONVERSION_ERROR) |
| 73 | + << "The conversion is not failed: " << error; |
| 74 | + } |
| 75 | + void assert_single_conversion_skip(safe_scap_evt_t evt_to_convert) { |
| 76 | + char error[SCAP_LASTERR_SIZE] = {'\0'}; |
| 77 | + // We assume it's okay to create a new event with the same size as the expected event |
| 78 | + auto storage = new_safe_scap_evt((scap_evt *)calloc(1, evt_to_convert->len)); |
| 79 | + // First we check the conversion result matches the expected result |
| 80 | + ASSERT_EQ(scap_convert_event(storage.get(), evt_to_convert.get(), error), CONVERSION_SKIP) |
| 81 | + << "The conversion is not skipped: " << error; |
| 82 | + } |
| 83 | + void assert_full_conversion(safe_scap_evt_t evt_to_convert, safe_scap_evt_t expected_evt) { |
| 84 | + char error[SCAP_LASTERR_SIZE] = {'\0'}; |
| 85 | + // Here we need to allocate more space than the expected event because in the middle we |
| 86 | + // could have larger events. We could also use `MAX_EVENT_SIZE` but probably it will just |
| 87 | + // slowdown tests. |
| 88 | + auto to_convert_evt = new_safe_scap_evt( |
| 89 | + (scap_evt *)calloc(1, expected_evt->len + convert_event_test::safe_margin)); |
| 90 | + auto new_evt = new_safe_scap_evt( |
| 91 | + (scap_evt *)calloc(1, expected_evt->len + convert_event_test::safe_margin)); |
| 92 | + // We copy the event to convert into the new larger storage since during the conversions it |
| 93 | + // could contain larger events than the initial one. |
| 94 | + // We copy it in the new event to match the for loop logic. |
| 95 | + memcpy(new_evt.get(), evt_to_convert.get(), evt_to_convert->len); |
| 96 | + int conv_num = 0; |
| 97 | + conversion_result conv_res = CONVERSION_CONTINUE; |
| 98 | + for(conv_num = 0; conv_num < MAX_CONVERSION_BOUNDARY && conv_res == CONVERSION_CONTINUE; |
| 99 | + conv_num++) { |
| 100 | + // Copy the new event into the one to convert for the next conversion. |
| 101 | + memcpy(to_convert_evt.get(), new_evt.get(), new_evt->len); |
| 102 | + conv_res = scap_convert_event((scap_evt *)new_evt.get(), |
| 103 | + (scap_evt *)to_convert_evt.get(), |
| 104 | + error); |
| 105 | + } |
| 106 | + switch(conv_res) { |
| 107 | + case CONVERSION_ERROR: |
| 108 | + FAIL() << "Unexpected CONVERSION_ERROR: " << error; |
| 109 | + case CONVERSION_SKIP: |
| 110 | + FAIL() << "Unexpected CONVERSION_SKIP"; |
| 111 | + case CONVERSION_CONTINUE: |
| 112 | + if(conv_num < MAX_CONVERSION_BOUNDARY) { |
| 113 | + FAIL() << "Unexpected CONVERSION_CONTINUE without reaching max boundary"; |
| 114 | + } else { |
| 115 | + FAIL() << "Unexpected CONVERSION_CONTINUE reaching max boundary"; |
| 116 | + } |
| 117 | + default: |
| 118 | + break; |
| 119 | + } |
| 120 | + if(!scap_compare_events(new_evt.get(), expected_evt.get(), error)) { |
| 121 | + printf("\nExpected event:\n"); |
| 122 | + scap_print_event(expected_evt.get(), PRINT_FULL); |
| 123 | + printf("\nConverted event:\n"); |
| 124 | + scap_print_event(new_evt.get(), PRINT_FULL); |
| 125 | + FAIL() << error; |
| 126 | + } |
| 127 | + } |
| 128 | + void assert_event_storage_presence(safe_scap_evt_t expected_evt) { |
| 129 | + char error[SCAP_LASTERR_SIZE] = {'\0'}; |
| 130 | + int64_t tid = expected_evt.get()->tid; |
| 131 | + auto event = scap_retrieve_evt_from_converter_storage(tid); |
| 132 | + if(!event) { |
| 133 | + FAIL() << "Event with tid " << tid << " not found in the storage"; |
| 134 | + } |
| 135 | + if(!scap_compare_events(event, expected_evt.get(), error)) { |
| 136 | + FAIL() << "Different events: " << error; |
| 137 | + } |
| 138 | + } |
| 139 | +}; |
0 commit comments