Skip to content

Commit

Permalink
formatting and adding test with arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jbbjarnason committed Jun 24, 2024
1 parent 86c2c13 commit 29086ca
Showing 1 changed file with 57 additions and 33 deletions.
90 changes: 57 additions & 33 deletions exes/themis/tests/themis_integration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include <boost/asio.hpp>
#include <boost/ut.hpp>

#include "alarm_database.hpp"
#include "dbus_interface.hpp"
#include <tfc/dbus/sd_bus.hpp>
#include <tfc/progbase.hpp>
#include <tfc/snitch.hpp>
#include <tfc/snitch/common.hpp>
#include <tfc/snitch/details/dbus_client.hpp>
#include "alarm_database.hpp"
#include "dbus_interface.hpp"

namespace ut = boost::ut;
namespace asio = boost::asio;
Expand All @@ -20,12 +20,12 @@ using boost::ut::operator|;
using boost::ut::operator/;
using boost::ut::expect;
using boost::ut::throws;
using boost::ut::fatal;
using tfc::snitch::error;
using tfc::snitch::info;
using tfc::snitch::warning;
using tfc::snitch::warning_latched;
using tfc::snitch::warning_resettable;
using tfc::snitch::error;
using tfc::snitch::detail::dbus_client;
using tfc::themis::alarm_database;
using tfc::themis::interface;
Expand All @@ -51,6 +51,13 @@ struct test_setup {
}
};

struct formattable_type {
constexpr operator std::string_view() const noexcept { return "42"; }
};
constexpr auto format_as(formattable_type const& val) -> std::string_view {
return std::string_view{ val };
}

int main(int argc, char** argv) {
tfc::base::init(argc, argv);
"Test some dbus functions for signature errors and general correctness"_test = [] {
Expand All @@ -62,7 +69,7 @@ int main(int argc, char** argv) {
});
t.ctx.run_for(2ms);
expect(t.ran[0]);
t.client.list_alarms([&](const std::error_code& err, std::vector<tfc::snitch::api::alarm> alarms){
t.client.list_alarms([&](const std::error_code& err, std::vector<tfc::snitch::api::alarm> alarms) {
expect(!err) << err.message();
expect(alarms.size() == 1);
t.ran[5] = true;
Expand Down Expand Up @@ -112,14 +119,12 @@ int main(int argc, char** argv) {
expect(t.ran[3]);
expect(t.ran[4]);
};
"Test if the try_reset signal makes it in this world"_test = []{
"Test if the try_reset signal makes it in this world"_test = [] {
test_setup t;
warning_latched<"desc", "details"> w(t.connection, "first_test");
t.ctx.run_for(1ms);
w.on_try_reset([&]() {
t.ran[1] = true;
});
t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err){
w.on_try_reset([&]() { t.ran[1] = true; });
t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err) {
expect(!!err);
t.ran[2] = true;
});
Expand All @@ -134,32 +139,24 @@ int main(int argc, char** argv) {
t.ctx.run_for(2ms);
expect(t.ran[0]);
expect(w.alarm_id().has_value());
w.on_try_reset([&]() {
t.ran[3] = true;
});
t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err){
w.on_try_reset([&]() { t.ran[3] = true; });
t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err) {
expect(!err) << err.message();
t.ran[4] = true;
});
t.ctx.run_for(2ms);
expect(t.ran[3]);
expect(t.ran[4]);
};
"Test is the try_reset_all signal tickles these fancies"_test = []{
"Test is the try_reset_all signal tickles these fancies"_test = [] {
test_setup t;
warning_latched<"desc1", "details"> wl(t.connection, "first_test");
warning_resettable<"desc2", "details"> wr(t.connection, "first_test");
error<"desc3", "details"> e(t.connection, "first_test");
wl.on_try_reset([&]() {
t.ran[1] = true;
});
wr.on_try_reset([&]() {
t.ran[2] = true;
});
e.on_try_reset([&]() {
t.ran[3] = true;
});
t.client.try_reset_all_alarms([&](const std::error_code& err){
wl.on_try_reset([&]() { t.ran[1] = true; });
wr.on_try_reset([&]() { t.ran[2] = true; });
e.on_try_reset([&]() { t.ran[3] = true; });
t.client.try_reset_all_alarms([&](const std::error_code& err) {
expect(!!err);
t.ran[0] = true;
});
Expand All @@ -173,7 +170,7 @@ int main(int argc, char** argv) {
expect(!err) << err.message();
t.ran[4] = true;
});
t.client.try_reset_all_alarms([&](const std::error_code& err){
t.client.try_reset_all_alarms([&](const std::error_code& err) {
expect(!err) << err.message();
t.ran[5] = true;
});
Expand All @@ -191,7 +188,7 @@ int main(int argc, char** argv) {
expect(!err) << err.message();
t.ran[7] = true;
});
t.client.try_reset_all_alarms([&](const std::error_code& err){
t.client.try_reset_all_alarms([&](const std::error_code& err) {
expect(!err) << err.message();
t.ran[8] = true;
});
Expand All @@ -207,13 +204,40 @@ int main(int argc, char** argv) {
"alarm with declared params but no params provided"_test = [] {
test_setup t;
info<"desc {foo}", "details {bar} {foo}"> i(t.connection, "first_test");
try {
i.set();
expect(false) << "Should have thrown";
}
catch (std::exception&) {
expect(true);
}
expect(throws([&]{ i.set(); }));
};

"alarm with wrong named params"_test = [] {
test_setup t;
info<"desc {foo}", "details {bar} {foo}"> i(t.connection, "first_test");
expect(throws([&]{ i.set(fmt::arg("ababa", 1), fmt::arg("some_other name", 2)); }));
};

"alarm with params"_test = [] {
test_setup t;
info<"desc {foo}", "details {bar} {foo}"> i(t.connection, "first_test");
i.set(
[&](auto err) {
expect(!err) << fmt::format("Received error: {}", err.message());
t.ran[0] = true;

t.client.list_activations(
"en", 0, 10000, tfc::snitch::level_e::info, tfc::snitch::api::active_e::active,
tfc::themis::alarm_database::timepoint_from_milliseconds(0),
tfc::themis::alarm_database::timepoint_from_milliseconds(std::numeric_limits<std::int64_t>::max()),
[&](auto list_err, std::vector<tfc::snitch::api::activation> const& act) {
expect(!list_err) << list_err.message();
expect(fatal(act.size() == 1));
auto const& alarm = act.at(0);
expect(alarm.description == "desc 1337");
expect(alarm.details == "details 42 1337");
t.ran[1] = true;
});
},
fmt::arg("foo", 1337), fmt::arg("bar", formattable_type{}));
t.ctx.run_for(2ms);
expect(t.ran[0]);
expect(t.ran[1]);
};

return 0;
Expand Down

0 comments on commit 29086ca

Please sign in to comment.