diff --git a/unit_tests/engine/test_add_source.cpp b/unit_tests/engine/test_add_source.cpp index 2670dd1bbbc..88a77a562cb 100644 --- a/unit_tests/engine/test_add_source.cpp +++ b/unit_tests/engine/test_add_source.cpp @@ -54,12 +54,9 @@ TEST(AddSource, basic) sinsp inspector; sinsp_filter_check_list filterchecks; - auto filter_factory = std::shared_ptr( - new sinsp_filter_factory(&inspector, filterchecks)); - auto formatter_factory = std::shared_ptr( - new sinsp_evt_formatter_factory(&inspector, filterchecks)); - test_ruleset_factory *test_factory = new test_ruleset_factory(filter_factory); - auto ruleset_factory = std::shared_ptr(test_factory); + auto filter_factory = std::make_shared(&inspector, filterchecks); + auto formatter_factory = std::make_shared(&inspector, filterchecks); + auto ruleset_factory = std::make_shared(filter_factory); falco_source syscall_source; syscall_source.name = syscall_source_name; @@ -84,6 +81,6 @@ TEST(AddSource, basic) ASSERT_EQ(engine.ruleset_factory_for_source(syscall_source_name), ruleset_factory); ASSERT_EQ(engine.ruleset_factory_for_source(source_idx), ruleset_factory); - ASSERT_EQ(engine.ruleset_for_source(syscall_source_name), test_factory->ruleset); - ASSERT_EQ(engine.ruleset_for_source(source_idx), test_factory->ruleset); + ASSERT_EQ(engine.ruleset_for_source(syscall_source_name), ruleset_factory->ruleset); + ASSERT_EQ(engine.ruleset_for_source(source_idx), ruleset_factory->ruleset); } diff --git a/unit_tests/engine/test_alt_rule_loader.cpp b/unit_tests/engine/test_alt_rule_loader.cpp index 3b2953bc203..713e5849d61 100644 --- a/unit_tests/engine/test_alt_rule_loader.cpp +++ b/unit_tests/engine/test_alt_rule_loader.cpp @@ -163,8 +163,7 @@ class test_ruleset_factory : public filter_ruleset_factory inline std::shared_ptr new_ruleset() override { - std::shared_ptr ret(new test_ruleset(m_filter_factory)); - return ret; + return std::make_shared(m_filter_factory); } std::shared_ptr m_filter_factory; @@ -235,12 +234,9 @@ static std::shared_ptr create_configuration(sinsp& i sinsp_filter_check_list& filterchecks, indexed_vector& sources) { - auto filter_factory = std::shared_ptr( - new sinsp_filter_factory(&inspector, filterchecks)); - auto formatter_factory = std::shared_ptr( - new sinsp_evt_formatter_factory(&inspector, filterchecks)); - auto ruleset_factory = std::shared_ptr( - new evttype_index_ruleset_factory(filter_factory)); + auto filter_factory = std::make_shared(&inspector, filterchecks); + auto formatter_factory = std::make_shared(&inspector, filterchecks); + auto ruleset_factory = std::make_shared(filter_factory); falco_source syscall_source; syscall_source.name = syscall_source_name; @@ -251,12 +247,9 @@ static std::shared_ptr create_configuration(sinsp& i sources.insert(syscall_source, syscall_source_name); - std::shared_ptr configuration = - std::make_shared(content, - sources, - "test configuration"); - - return configuration; + return std::make_shared(content, + sources, + "test configuration"); } static void load_rules(sinsp& inspector, @@ -330,22 +323,15 @@ TEST(engine_loader_alt_loader, falco_engine_alternate_loader) sinsp inspector; sinsp_filter_check_list filterchecks; - auto filter_factory = std::shared_ptr( - new sinsp_filter_factory(&inspector, filterchecks)); - auto formatter_factory = std::shared_ptr( - new sinsp_evt_formatter_factory(&inspector, filterchecks)); - auto ruleset_factory = std::shared_ptr( - new test_ruleset_factory(filter_factory)); + auto filter_factory = std::make_shared(&inspector, filterchecks); + auto formatter_factory = std::make_shared(&inspector, filterchecks); + auto ruleset_factory = std::make_shared(filter_factory); - engine.add_source(syscall_source_name, - filter_factory, - formatter_factory, - ruleset_factory); + engine.add_source(syscall_source_name, filter_factory, formatter_factory, ruleset_factory); - std::shared_ptr reader(new test_reader()); - test_collector* test_col = new test_collector(); - std::shared_ptr collector(test_col); - std::shared_ptr compiler(new test_compiler()); + auto reader = std::make_shared(); + auto collector = std::make_shared(); + auto compiler = std::make_shared(); engine.set_rule_reader(reader); engine.set_rule_collector(collector); @@ -357,7 +343,7 @@ TEST(engine_loader_alt_loader, falco_engine_alternate_loader) engine.load_rules(content, "test_rules.yaml"); - EXPECT_EQ(test_col->test_object_infos.size(), 2); + EXPECT_EQ(collector->test_object_infos.size(), 2); std::shared_ptr ruleset = engine.ruleset_for_source(syscall_source_name); std::set& defined_properties = std::dynamic_pointer_cast(ruleset)->defined_properties; diff --git a/unit_tests/engine/test_plugin_requirements.cpp b/unit_tests/engine/test_plugin_requirements.cpp index 74816086256..3fcde665893 100644 --- a/unit_tests/engine/test_plugin_requirements.cpp +++ b/unit_tests/engine/test_plugin_requirements.cpp @@ -23,15 +23,15 @@ static bool check_requirements(std::string& err, const std::vector& plugins, const std::string& ruleset_content) { - std::unique_ptr e(new falco_engine()); + falco_engine e; falco::load_result::rules_contents_t c = {{"test", ruleset_content}}; - auto res = e->load_rules(c.begin()->second, c.begin()->first); + auto res = e.load_rules(c.begin()->second, c.begin()->first); if(!res->successful()) { return false; } - return e->check_plugin_requirements(plugins, err); + return e.check_plugin_requirements(plugins, err); } TEST(PluginRequirements, check_plugin_requirements_success) diff --git a/unit_tests/engine/test_rulesets.cpp b/unit_tests/engine/test_rulesets.cpp index b33eb741b38..a06396323eb 100644 --- a/unit_tests/engine/test_rulesets.cpp +++ b/unit_tests/engine/test_rulesets.cpp @@ -25,30 +25,26 @@ limitations under the License. /* Helpers methods */ static std::shared_ptr create_factory(sinsp* inspector, filter_check_list& list) { - std::shared_ptr ret(new sinsp_filter_factory(inspector, list)); - return ret; + return std::make_shared(inspector, list); } static std::shared_ptr create_ruleset(std::shared_ptr f) { - std::shared_ptr ret(new evttype_index_ruleset(f)); - return ret; + return std::make_shared(f); } static std::shared_ptr create_ast(std::shared_ptr f) { libsinsp::filter::parser parser("evt.type=open"); - std::shared_ptr ret(parser.parse()); - return ret; + return parser.parse(); } static std::shared_ptr create_filter( std::shared_ptr f, - std::shared_ptr ast) + libsinsp::filter::ast::expr* ast) { - sinsp_filter_compiler compiler(f, ast.get()); - std::shared_ptr filter(compiler.compile()); - return filter; + sinsp_filter_compiler compiler(f, ast); + return std::shared_ptr(compiler.compile()); } TEST(Ruleset, enable_disable_rules_using_names) @@ -59,7 +55,7 @@ TEST(Ruleset, enable_disable_rules_using_names) auto f = create_factory(&inspector, filterlist); auto r = create_ruleset(f); auto ast = create_ast(f); - auto filter = create_filter(f, ast); + auto filter = create_filter(f, ast.get()); falco_rule rule_A = {}; rule_A.name = "rule_A"; @@ -128,7 +124,7 @@ TEST(Ruleset, enable_disable_rules_using_tags) auto f = create_factory(&inspector, filterlist); auto r = create_ruleset(f); auto ast = create_ast(f); - auto filter = create_filter(f, ast); + auto filter = create_filter(f, ast.get()); falco_rule rule_A = {}; rule_A.name = "rule_A"; diff --git a/unit_tests/falco/test_atomic_signal_handler.cpp b/unit_tests/falco/test_atomic_signal_handler.cpp index 5d08a06c94c..ea5530a0eed 100644 --- a/unit_tests/falco/test_atomic_signal_handler.cpp +++ b/unit_tests/falco/test_atomic_signal_handler.cpp @@ -15,15 +15,15 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include +#include + #include + +#include #include -#include -#include #include -#include - -#include -#include +#include TEST(AtomicSignalHandler, lock_free_implementation) { @@ -33,55 +33,50 @@ TEST(AtomicSignalHandler, lock_free_implementation) TEST(AtomicSignalHandler, handle_once_wait_consistency) { constexpr const auto thread_num = 10; - constexpr const auto thread_wait_sec = 2; - constexpr const auto handler_wait_sec = 1; + constexpr const std::chrono::seconds thread_wait_sec{2}; + constexpr const std::chrono::seconds handler_wait_sec{1}; // have a shared signal handler falco::atomic_signal_handler handler; // launch a bunch of threads all syncing on the same handler - typedef struct + struct task_result_t { bool handled; - uint64_t duration_secs; - } task_result_t; + std::chrono::seconds duration_secs; + }; + std::vector> futures; - std::vector> threads; for (int i = 0; i < thread_num; i++) { - std::packaged_task task([&handler, &thread_wait_sec]{ - auto start = std::chrono::high_resolution_clock::now(); - task_result_t res; - res.handled = false; - while (!handler.handled()) - { - if (handler.triggered()) + futures.emplace_back(std::async(std::launch::async, + [&handler, thread_wait_sec]() { + auto start = std::chrono::high_resolution_clock::now(); + task_result_t res; + res.handled = false; + while (!handler.handled()) { - res.handled = handler.handle([&thread_wait_sec]{ - std::this_thread::sleep_for (std::chrono::seconds(thread_wait_sec)); - }); + if (handler.triggered()) + { + res.handled = handler.handle([thread_wait_sec]() { + std::this_thread::sleep_for(thread_wait_sec); + }); + } } - } - auto diff = std::chrono::high_resolution_clock::now() - start; - res.duration_secs = std::chrono::duration_cast(diff).count(); - return res; - }); - futures.push_back(task.get_future()); - threads.emplace_back(); - threads[i].reset(new std::thread(std::move(task))); + auto diff = std::chrono::high_resolution_clock::now() - start; + res.duration_secs = std::chrono::duration_cast(diff); + return res; + })); } // wait a bit, then trigger the signal handler from the main thread auto total_handled = 0; auto start = std::chrono::high_resolution_clock::now(); - std::this_thread::sleep_for (std::chrono::seconds(handler_wait_sec)); + std::this_thread::sleep_for(handler_wait_sec); handler.trigger(); for (int i = 0; i < thread_num; i++) { - // we need to check that all threads didn't quit before - // the handle() function finished executing - futures[i].wait(); - threads[i]->join(); + // wait for all threads to finish and get the results from the futures auto res = futures[i].get(); if (res.handled) { @@ -92,7 +87,7 @@ TEST(AtomicSignalHandler, handle_once_wait_consistency) // check that the total time is consistent with the expectations auto diff = std::chrono::high_resolution_clock::now() - start; - auto secs = std::chrono::duration_cast(diff).count(); + auto secs = std::chrono::duration_cast(diff); ASSERT_GE(secs, thread_wait_sec + handler_wait_sec); // check that only one thread handled the signal diff --git a/userspace/engine/evttype_index_ruleset.cpp b/userspace/engine/evttype_index_ruleset.cpp index f1dfe4a9b6f..684afeefd5a 100644 --- a/userspace/engine/evttype_index_ruleset.cpp +++ b/userspace/engine/evttype_index_ruleset.cpp @@ -199,7 +199,7 @@ void evttype_index_ruleset::add( { try { - std::shared_ptr wrap(new filter_wrapper()); + auto wrap = std::make_shared(); wrap->rule = rule; wrap->filter = filter; if(rule.source == falco_common::syscall_source) @@ -369,7 +369,7 @@ libsinsp::events::set evttype_index_ruleset::enabled_sc_codes(uint1 } return m_rulesets[ruleset]->sc_codes(); } - + libsinsp::events::set evttype_index_ruleset::enabled_event_codes(uint16_t ruleset) { if(m_rulesets.size() < (size_t)ruleset + 1)