test: Add assert mock to avoid death tests (#1947)

Fixes #1750
This commit is contained in:
Sergey Kuznetsov
2025-03-07 18:11:52 +00:00
committed by GitHub
parent 8a08c5e6ce
commit c57fe1e6e4
30 changed files with 411 additions and 295 deletions

67
src/util/Assert.cpp Normal file
View File

@@ -0,0 +1,67 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2025, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "util/Assert.hpp"
#include "util/log/Logger.hpp"
#include <boost/log/core/core.hpp>
#include <cstdlib>
#include <iostream>
#include <string_view>
#include <utility>
namespace util::impl {
OnAssert::ActionType OnAssert::action;
void
OnAssert::call(std::string_view message)
{
if (not OnAssert::action) {
resetAction();
}
OnAssert::action(message);
}
void
OnAssert::setAction(ActionType newAction)
{
OnAssert::action = std::move(newAction);
}
void
OnAssert::resetAction()
{
OnAssert::action = [](std::string_view m) { OnAssert::defaultAction(m); };
}
void
OnAssert::defaultAction(std::string_view message)
{
if (LogService::enabled()) {
LOG(LogService::fatal()) << message;
} else {
std::cerr << message;
}
std::exit(EXIT_FAILURE); // std::abort does not flush gcovr output and causes uncovered lines
}
} // namespace util::impl

View File

@@ -20,9 +20,11 @@
#pragma once #pragma once
#include "util/SourceLocation.hpp" #include "util/SourceLocation.hpp"
#include "util/log/Logger.hpp"
#include <boost/log/core/core.hpp> #include <boost/log/core/core.hpp>
#include <functional>
#include <string_view>
#ifndef CLIO_WITHOUT_STACKTRACE #ifndef CLIO_WITHOUT_STACKTRACE
#include <boost/stacktrace.hpp> #include <boost/stacktrace.hpp>
#include <boost/stacktrace/stacktrace.hpp> #include <boost/stacktrace/stacktrace.hpp>
@@ -31,9 +33,30 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <cstdlib> #include <cstdlib>
#include <iostream>
namespace util { namespace util::impl {
class OnAssert {
public:
using ActionType = std::function<void(std::string_view)>;
private:
static ActionType action;
public:
static void
call(std::string_view message);
static void
setAction(ActionType newAction);
static void
resetAction();
private:
static void
defaultAction(std::string_view message);
};
/** /**
* @brief Assert that a condition is true * @brief Assert that a condition is true
@@ -75,16 +98,12 @@ assertImpl(
fmt::format(format, std::forward<Args>(args)...) fmt::format(format, std::forward<Args>(args)...)
); );
#endif #endif
if (boost::log::core::get()->get_logging_enabled()) {
LOG(LogService::fatal()) << resultMessage; OnAssert::call(resultMessage);
} else {
std::cerr << resultMessage;
}
std::exit(EXIT_FAILURE); // std::abort does not flush gcovr output and causes uncovered lines
} }
} }
} // namespace util } // namespace util::impl
#define ASSERT(condition, ...) \ #define ASSERT(condition, ...) \
util::assertImpl(CURRENT_SRC_LOCATION, #condition, static_cast<bool>(condition), __VA_ARGS__) util::impl::assertImpl(CURRENT_SRC_LOCATION, #condition, static_cast<bool>(condition), __VA_ARGS__)

View File

@@ -2,7 +2,8 @@ add_library(clio_util)
target_sources( target_sources(
clio_util clio_util
PRIVATE build/Build.cpp PRIVATE Assert.cpp
build/Build.cpp
config/Config.cpp config/Config.cpp
CoroutineGroup.cpp CoroutineGroup.cpp
log/Logger.cpp log/Logger.cpp

View File

@@ -77,7 +77,7 @@ public:
* Used to cancel the timer for scheduled operations and request the operation to be stopped as soon as possible * Used to cancel the timer for scheduled operations and request the operation to be stopped as soon as possible
*/ */
void void
abort() noexcept abort()
{ {
operation_.abort(); operation_.abort();
} }

View File

@@ -199,6 +199,12 @@ LogService::init(config::ClioConfigDefinition const& config)
return {}; return {};
} }
bool
LogService::enabled()
{
return boost::log::core::get()->get_logging_enabled();
}
Logger::Pump Logger::Pump
Logger::trace(SourceLocationType const& loc) const Logger::trace(SourceLocationType const& loc) const
{ {

View File

@@ -367,6 +367,14 @@ public:
{ {
return alertLog.warn(loc); return alertLog.warn(loc);
} }
/**
* @brief Whether the LogService is enabled or not
*
* @return true if the LogService is enabled, false otherwise
*/
[[nodiscard]] static bool
enabled();
}; };
}; // namespace util }; // namespace util

View File

@@ -2,7 +2,8 @@ add_library(clio_testing_common)
target_sources( target_sources(
clio_testing_common clio_testing_common
PRIVATE util/AssignRandomPort.cpp PRIVATE util/MockAssert.cpp
util/AssignRandomPort.cpp
util/CallWithTimeout.cpp util/CallWithTimeout.cpp
util/StringUtils.cpp util/StringUtils.cpp
util/TestHttpClient.cpp util/TestHttpClient.cpp

View File

@@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2025, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "util/MockAssert.hpp"
#include "util/Assert.hpp"
#include <string>
#include <string_view>
namespace common::util {
WithMockAssert::WithMockAssert()
{
::util::impl::OnAssert::setAction([](std::string_view m) { WithMockAssert::throwOnAssert(m); });
}
WithMockAssert::~WithMockAssert()
{
::util::impl::OnAssert::resetAction();
}
void
WithMockAssert::throwOnAssert(std::string_view m)
{
throw MockAssertException{.message = std::string{m}};
}
} // namespace common::util

View File

@@ -0,0 +1,59 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2025, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#pragma once
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <string_view>
namespace common::util {
class WithMockAssert : virtual public testing::Test {
public:
struct MockAssertException {
std::string message;
};
WithMockAssert();
~WithMockAssert() override;
private:
static void
throwOnAssert(std::string_view m);
};
} // namespace common::util
#define EXPECT_CLIO_ASSERT_FAIL(statement) EXPECT_THROW(statement, MockAssertException)
#define EXPECT_CLIO_ASSERT_FAIL_WITH_MESSAGE(statement, message_regex) \
EXPECT_THROW( \
{ \
try { \
statement; \
} catch (common::util::WithMockAssert::MockAssertException const& e) { \
EXPECT_THAT(e.message, testing::ContainsRegex(message_regex)); \
throw; \
} \
}, \
common::util::WithMockAssert::MockAssertException \
)

View File

@@ -20,6 +20,7 @@
#include "data/AmendmentCenter.hpp" #include "data/AmendmentCenter.hpp"
#include "data/Types.hpp" #include "data/Types.hpp"
#include "util/AsioContextTestFixture.hpp" #include "util/AsioContextTestFixture.hpp"
#include "util/MockAssert.hpp"
#include "util/MockBackendTestFixture.hpp" #include "util/MockBackendTestFixture.hpp"
#include "util/MockPrometheus.hpp" #include "util/MockPrometheus.hpp"
#include "util/TestObject.hpp" #include "util/TestObject.hpp"
@@ -149,12 +150,12 @@ TEST(AmendmentTest, GenerateAmendmentId)
); );
} }
struct AmendmentCenterDeathTest : AmendmentCenterTest {}; struct AmendmentCenterAssertTest : common::util::WithMockAssert, AmendmentCenterTest {};
TEST_F(AmendmentCenterDeathTest, GetInvalidAmendmentAsserts) TEST_F(AmendmentCenterAssertTest, GetInvalidAmendmentAsserts)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto _ = amendmentCenter.getAmendment("invalidAmendmentKey"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = amendmentCenter.getAmendment("invalidAmendmentKey"); });
EXPECT_DEATH({ [[maybe_unused]] auto _ = amendmentCenter["invalidAmendmentKey"]; }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = amendmentCenter["invalidAmendmentKey"]; });
} }
struct AmendmentKeyTest : testing::Test {}; struct AmendmentKeyTest : testing::Test {};

View File

@@ -24,6 +24,7 @@
#include "etlng/impl/Extraction.hpp" #include "etlng/impl/Extraction.hpp"
#include "util/BinaryTestObject.hpp" #include "util/BinaryTestObject.hpp"
#include "util/LoggerFixtures.hpp" #include "util/LoggerFixtures.hpp"
#include "util/MockAssert.hpp"
#include "util/TestObject.hpp" #include "util/TestObject.hpp"
#include <gmock/gmock.h> #include <gmock/gmock.h>
@@ -359,21 +360,17 @@ TEST_F(ExtractionNgTests, SuccessorsWithNoNeighborsIncluded)
ASSERT_FALSE(res.has_value()); ASSERT_FALSE(res.has_value());
} }
struct ExtractionDeathTest : NoLoggerFixture {}; struct ExtractionAssertTest : common::util::WithMockAssert, NoLoggerFixture {};
TEST_F(ExtractionDeathTest, InvalidModTypeAsserts) TEST_F(ExtractionAssertTest, InvalidModTypeAsserts)
{ {
using namespace etlng::impl; using namespace etlng::impl;
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL({
{ [[maybe_unused]] auto _ = extractModType(
[[maybe_unused]] auto _ = extractModType( PBModType::RawLedgerObject_ModificationType_RawLedgerObject_ModificationType_INT_MIN_SENTINEL_DO_NOT_USE_
PBModType:: );
RawLedgerObject_ModificationType_RawLedgerObject_ModificationType_INT_MIN_SENTINEL_DO_NOT_USE_ });
);
},
".*"
);
} }
struct MockFetcher : etl::LedgerFetcherInterface { struct MockFetcher : etl::LedgerFetcherInterface {

View File

@@ -24,6 +24,7 @@
#include "etlng/impl/Loading.hpp" #include "etlng/impl/Loading.hpp"
#include "rpc/RPCHelpers.hpp" #include "rpc/RPCHelpers.hpp"
#include "util/BinaryTestObject.hpp" #include "util/BinaryTestObject.hpp"
#include "util/MockAssert.hpp"
#include "util/MockBackendTestFixture.hpp" #include "util/MockBackendTestFixture.hpp"
#include "util/MockETLServiceTestFixture.hpp" #include "util/MockETLServiceTestFixture.hpp"
#include "util/MockPrometheus.hpp" #include "util/MockPrometheus.hpp"
@@ -71,7 +72,7 @@ protected:
Loader loader_{backend_, mockLedgerFetcherPtr_, mockRegistryPtr_, mockAmendmentBlockHandlerPtr_}; Loader loader_{backend_, mockLedgerFetcherPtr_, mockRegistryPtr_, mockAmendmentBlockHandlerPtr_};
}; };
struct LoadingDeathTest : LoadingTests {}; struct LoadingAssertTest : common::util::WithMockAssert, LoadingTests {};
auto auto
createTestData() createTestData()
@@ -146,7 +147,7 @@ TEST_F(LoadingTests, OnInitialLoadGotMoreObjectsWithoutKey)
loader_.onInitialLoadGotMoreObjects(kSEQ, data.objects, lastKey); loader_.onInitialLoadGotMoreObjects(kSEQ, data.objects, lastKey);
} }
TEST_F(LoadingDeathTest, LoadInitialLedgerHasDataInDB) TEST_F(LoadingAssertTest, LoadInitialLedgerHasDataInDB)
{ {
auto const data = createTestData(); auto const data = createTestData();
auto const range = LedgerRange{.minSequence = kSEQ - 1, .maxSequence = kSEQ}; auto const range = LedgerRange{.minSequence = kSEQ - 1, .maxSequence = kSEQ};
@@ -156,5 +157,5 @@ TEST_F(LoadingDeathTest, LoadInitialLedgerHasDataInDB)
testing::Mock::AllowLeak(&*backend_); testing::Mock::AllowLeak(&*backend_);
ON_CALL(*backend_, hardFetchLedgerRange(testing::_)).WillByDefault(testing::Return(range)); ON_CALL(*backend_, hardFetchLedgerRange(testing::_)).WillByDefault(testing::Return(range));
EXPECT_DEATH({ [[maybe_unused]] auto unused = loader_.loadInitialLedger(data); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = loader_.loadInitialLedger(data); });
} }

View File

@@ -19,6 +19,7 @@
#include "data/Types.hpp" #include "data/Types.hpp"
#include "migration/MigrationInspectorFactory.hpp" #include "migration/MigrationInspectorFactory.hpp"
#include "util/MockAssert.hpp"
#include "util/MockBackendTestFixture.hpp" #include "util/MockBackendTestFixture.hpp"
#include "util/MockPrometheus.hpp" #include "util/MockPrometheus.hpp"
#include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigDefinition.hpp"
@@ -32,18 +33,18 @@
using namespace testing; using namespace testing;
struct MigrationInspectorFactoryTests : public util::prometheus::WithPrometheus, public MockBackendTest { struct MigrationInspectorFactoryTests : util::prometheus::WithPrometheus,
common::util::WithMockAssert,
MockBackendTest {
protected: protected:
util::config::ClioConfigDefinition const readerConfig_ = util::config::ClioConfigDefinition{ util::config::ClioConfigDefinition const readerConfig_ = util::config::ClioConfigDefinition{
{"read_only", util::config::ConfigValue{util::config::ConfigType::Boolean}.defaultValue(true)} {"read_only", util::config::ConfigValue{util::config::ConfigType::Boolean}.defaultValue(true)}
}; };
}; };
struct MigrationInspectorFactoryTestsDeathTest : public MigrationInspectorFactoryTests {}; TEST_F(MigrationInspectorFactoryTests, NullBackend)
TEST_F(MigrationInspectorFactoryTestsDeathTest, NullBackend)
{ {
EXPECT_DEATH(migration::makeMigrationInspector(readerConfig_, nullptr), ".*"); EXPECT_CLIO_ASSERT_FAIL(migration::makeMigrationInspector(readerConfig_, nullptr));
} }
TEST_F(MigrationInspectorFactoryTests, NotInitMigrationTableIfReader) TEST_F(MigrationInspectorFactoryTests, NotInitMigrationTableIfReader)

View File

@@ -19,6 +19,7 @@
#include "migration/cassandra/impl/FullTableScanner.hpp" #include "migration/cassandra/impl/FullTableScanner.hpp"
#include "util/LoggerFixtures.hpp" #include "util/LoggerFixtures.hpp"
#include "util/MockAssert.hpp"
#include <boost/asio/spawn.hpp> #include <boost/asio/spawn.hpp>
#include <gmock/gmock.h> #include <gmock/gmock.h>
@@ -51,30 +52,32 @@ struct TestScannerAdaper {
}; };
} // namespace } // namespace
struct FullTableScannerTests : public NoLoggerFixture {}; struct FullTableScannerAssertTest : common::util::WithMockAssert {};
TEST_F(FullTableScannerTests, workerNumZero) TEST_F(FullTableScannerAssertTest, workerNumZero)
{ {
testing::MockFunction<void(migration::cassandra::impl::TokenRange const&, boost::asio::yield_context)> mockCallback; testing::MockFunction<void(migration::cassandra::impl::TokenRange const&, boost::asio::yield_context)> mockCallback;
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL_WITH_MESSAGE(
migration::cassandra::impl::FullTableScanner<TestScannerAdaper>( migration::cassandra::impl::FullTableScanner<TestScannerAdaper>(
{.ctxThreadsNum = 1, .jobsNum = 0, .cursorsPerJob = 100}, TestScannerAdaper(mockCallback) {.ctxThreadsNum = 1, .jobsNum = 0, .cursorsPerJob = 100}, TestScannerAdaper(mockCallback)
), ),
"jobsNum for full table scanner must be greater than 0" ".*jobsNum for full table scanner must be greater than 0"
); );
} }
TEST_F(FullTableScannerTests, cursorsPerWorkerZero) TEST_F(FullTableScannerAssertTest, cursorsPerWorkerZero)
{ {
testing::MockFunction<void(migration::cassandra::impl::TokenRange const&, boost::asio::yield_context)> mockCallback; testing::MockFunction<void(migration::cassandra::impl::TokenRange const&, boost::asio::yield_context)> mockCallback;
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL_WITH_MESSAGE(
migration::cassandra::impl::FullTableScanner<TestScannerAdaper>( migration::cassandra::impl::FullTableScanner<TestScannerAdaper>(
{.ctxThreadsNum = 1, .jobsNum = 1, .cursorsPerJob = 0}, TestScannerAdaper(mockCallback) {.ctxThreadsNum = 1, .jobsNum = 1, .cursorsPerJob = 0}, TestScannerAdaper(mockCallback)
), ),
"cursorsPerJob for full table scanner must be greater than 0" ".*cursorsPerJob for full table scanner must be greater than 0"
); );
} }
struct FullTableScannerTests : NoLoggerFixture {};
TEST_F(FullTableScannerTests, SingleThreadCtx) TEST_F(FullTableScannerTests, SingleThreadCtx)
{ {
testing::MockFunction<void(migration::cassandra::impl::TokenRange const&, boost::asio::yield_context)> mockCallback; testing::MockFunction<void(migration::cassandra::impl::TokenRange const&, boost::asio::yield_context)> mockCallback;

View File

@@ -50,6 +50,7 @@
#include "util/Assert.hpp" #include "util/Assert.hpp"
#include "util/HandlerBaseTestFixture.hpp" #include "util/HandlerBaseTestFixture.hpp"
#include "util/MockAmendmentCenter.hpp" #include "util/MockAmendmentCenter.hpp"
#include "util/MockAssert.hpp"
#include "util/MockCounters.hpp" #include "util/MockCounters.hpp"
#include "util/MockCountersFixture.hpp" #include "util/MockCountersFixture.hpp"
#include "util/MockETLService.hpp" #include "util/MockETLService.hpp"
@@ -113,11 +114,12 @@ using AnyHandlerType = Types<
TransactionEntryHandler>; TransactionEntryHandler>;
template <typename HandlerType> template <typename HandlerType>
struct AllHandlersDeathTest : HandlerBaseTest, struct AllHandlersAssertTest : common::util::WithMockAssert,
MockLoadBalancerTest, HandlerBaseTest,
MockCountersTest, MockLoadBalancerTest,
testing::WithParamInterface<std::string> { MockCountersTest,
AllHandlersDeathTest() : handler_{initHandler()} testing::WithParamInterface<std::string> {
AllHandlersAssertTest() : handler_{initHandler()}
{ {
ASSERT(mockAmendmentCenterPtr_.amendmentCenterMock != nullptr, "mockAmendmentCenterPtr is not initialized."); ASSERT(mockAmendmentCenterPtr_.amendmentCenterMock != nullptr, "mockAmendmentCenterPtr is not initialized.");
ASSERT( ASSERT(
@@ -244,9 +246,9 @@ createInput<SubscribeHandler>()
return input; return input;
} }
TYPED_TEST_CASE(AllHandlersDeathTest, AnyHandlerType); TYPED_TEST_CASE(AllHandlersAssertTest, AnyHandlerType);
TYPED_TEST(AllHandlersDeathTest, NoRangeAvailable) TYPED_TEST(AllHandlersAssertTest, NoRangeAvailable)
{ {
// doesn't work without 'this' // doesn't work without 'this'
this->runSpawn( this->runSpawn(
@@ -256,7 +258,7 @@ TYPED_TEST(AllHandlersDeathTest, NoRangeAvailable)
auto const input = createInput<TypeParam>(); auto const input = createInput<TypeParam>();
auto const context = Context{yield, this->session_}; auto const context = Context{yield, this->session_};
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL_WITH_MESSAGE(
{ [[maybe_unused]] auto unused = handler.process(input, context); }, "Assertion .* failed at .*" { [[maybe_unused]] auto unused = handler.process(input, context); }, "Assertion .* failed at .*"
); );
}, },

View File

@@ -18,15 +18,18 @@
//============================================================================== //==============================================================================
#include "util/Assert.hpp" #include "util/Assert.hpp"
#include "util/MockAssert.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
TEST(AssertTests, assertTrue) struct AssertTest : common::util::WithMockAssert {};
TEST_F(AssertTest, assertTrue)
{ {
EXPECT_NO_THROW(ASSERT(true, "Should not fail")); EXPECT_NO_THROW(ASSERT(true, "Should not fail"));
} }
TEST(AssertDeathTest, assertFalse) TEST_F(AssertTest, assertFalse)
{ {
EXPECT_DEATH({ ASSERT(false, "failure"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ ASSERT(false, "failure"); });
} }

View File

@@ -18,6 +18,7 @@
//============================================================================== //==============================================================================
#include "util/LoggerFixtures.hpp" #include "util/LoggerFixtures.hpp"
#include "util/MockAssert.hpp"
#include "util/SignalsHandler.hpp" #include "util/SignalsHandler.hpp"
#include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp" #include "util/newconfig/ConfigValue.hpp"
@@ -64,7 +65,9 @@ protected:
bool testCanBeFinished_{false}; bool testCanBeFinished_{false};
}; };
TEST(SignalsHandlerDeathTest, CantCreateTwoSignalsHandlers) struct SignalsHandlerAssertTest : common::util::WithMockAssert {};
TEST_F(SignalsHandlerAssertTest, CantCreateTwoSignalsHandlers)
{ {
auto makeHandler = []() { auto makeHandler = []() {
return SignalsHandler{ return SignalsHandler{
@@ -72,7 +75,7 @@ TEST(SignalsHandlerDeathTest, CantCreateTwoSignalsHandlers)
}; };
}; };
auto const handler = makeHandler(); auto const handler = makeHandler();
EXPECT_DEATH({ makeHandler(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ makeHandler(); });
} }
struct SignalsHandlerTests : SignalsHandlerTestsBase { struct SignalsHandlerTests : SignalsHandlerTestsBase {

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/MockOperation.hpp" #include "util/MockOperation.hpp"
#include "util/async/AnyOperation.hpp" #include "util/async/AnyOperation.hpp"
#include "util/async/Error.hpp" #include "util/async/Error.hpp"
@@ -32,7 +33,7 @@
using namespace util::async; using namespace util::async;
using namespace ::testing; using namespace ::testing;
struct AnyOperationTests : Test { struct AnyOperationTests : virtual Test {
using OperationType = MockOperation<std::expected<std::any, ExecutionError>>; using OperationType = MockOperation<std::expected<std::any, ExecutionError>>;
using StoppableOperationType = MockStoppableOperation<std::expected<std::any, ExecutionError>>; using StoppableOperationType = MockStoppableOperation<std::expected<std::any, ExecutionError>>;
using ScheduledOperationType = MockScheduledOperation<std::expected<std::any, ExecutionError>>; using ScheduledOperationType = MockScheduledOperation<std::expected<std::any, ExecutionError>>;
@@ -49,7 +50,6 @@ struct AnyOperationTests : Test {
AnyOperation<void> scheduledVoidOp{impl::ErasedOperation(static_cast<ScheduledOperationType&>(mockScheduledOp))}; AnyOperation<void> scheduledVoidOp{impl::ErasedOperation(static_cast<ScheduledOperationType&>(mockScheduledOp))};
AnyOperation<void> repeatingOp{impl::ErasedOperation(static_cast<RepeatingOperationType&>(mockRepeatingOp))}; AnyOperation<void> repeatingOp{impl::ErasedOperation(static_cast<RepeatingOperationType&>(mockRepeatingOp))};
}; };
using AnyOperationDeathTest = AnyOperationTests;
TEST_F(AnyOperationTests, Move) TEST_F(AnyOperationTests, Move)
{ {
@@ -134,12 +134,14 @@ TEST_F(AnyOperationTests, RepeatingOpInvokeCallPropagated)
repeatingOp.invoke(); repeatingOp.invoke();
} }
TEST_F(AnyOperationDeathTest, CallAbortOnNonStoppableOrCancellableOperation) struct AnyOperationAssertTest : common::util::WithMockAssert, AnyOperationTests {};
TEST_F(AnyOperationAssertTest, CallAbortOnNonStoppableOrCancellableOperation)
{ {
EXPECT_DEATH(voidOp.abort(), ".*"); EXPECT_CLIO_ASSERT_FAIL(voidOp.abort());
} }
TEST_F(AnyOperationDeathTest, CallInvokeOnNonForceInvocableOperation) TEST_F(AnyOperationAssertTest, CallInvokeOnNonForceInvocableOperation)
{ {
EXPECT_DEATH(voidOp.invoke(), ".*"); EXPECT_CLIO_ASSERT_FAIL(voidOp.invoke());
} }

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/async/AnyStopToken.hpp" #include "util/async/AnyStopToken.hpp"
#include <boost/asio/spawn.hpp> #include <boost/asio/spawn.hpp>
@@ -38,7 +39,6 @@ struct FakeStopToken {
} // namespace } // namespace
struct AnyStopTokenTests : public TestWithParam<bool> {}; struct AnyStopTokenTests : public TestWithParam<bool> {};
using AnyStopTokenDeathTest = AnyStopTokenTests;
INSTANTIATE_TEST_CASE_P(AnyStopTokenGroup, AnyStopTokenTests, ValuesIn({true, false}), [](auto const& info) { INSTANTIATE_TEST_CASE_P(AnyStopTokenGroup, AnyStopTokenTests, ValuesIn({true, false}), [](auto const& info) {
return info.param ? "true" : "false"; return info.param ? "true" : "false";
@@ -61,9 +61,11 @@ TEST_P(AnyStopTokenTests, IsStopRequestedCallPropagated)
EXPECT_EQ(stopToken, flag); EXPECT_EQ(stopToken, flag);
} }
TEST_F(AnyStopTokenDeathTest, ConversionToYieldContextAssertsIfUnsupported) struct AnyStopTokenAssertTest : common::util::WithMockAssert {};
TEST_F(AnyStopTokenAssertTest, ConversionToYieldContextAssertsIfUnsupported)
{ {
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL(
[[maybe_unused]] auto unused = static_cast<boost::asio::yield_context>(AnyStopToken{FakeStopToken{}}), ".*" [[maybe_unused]] auto unused = static_cast<boost::asio::yield_context>(AnyStopToken{FakeStopToken{}})
); );
} }

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/newconfig/Array.hpp" #include "util/newconfig/Array.hpp"
#include "util/newconfig/ConfigConstraints.hpp" #include "util/newconfig/ConfigConstraints.hpp"
#include "util/newconfig/ConfigValue.hpp" #include "util/newconfig/ConfigValue.hpp"
@@ -38,9 +39,11 @@ TEST(ArrayTest, prefix)
EXPECT_EQ(Array::prefix("foo.bar.[].baz"), "foo.bar.[]"); EXPECT_EQ(Array::prefix("foo.bar.[].baz"), "foo.bar.[]");
} }
TEST(ArrayDeathTest, prefix) struct ArrayAssertTest : common::util::WithMockAssert {};
TEST_F(ArrayAssertTest, prefix)
{ {
EXPECT_DEATH(Array::prefix("foo.bar"), ".*"); EXPECT_CLIO_ASSERT_FAIL(Array::prefix("foo.bar"));
} }
TEST(ArrayTest, addSingleValue) TEST(ArrayTest, addSingleValue)

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/newconfig/ArrayView.hpp" #include "util/newconfig/ArrayView.hpp"
#include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigFileJson.hpp" #include "util/newconfig/ConfigFileJson.hpp"
@@ -33,7 +34,7 @@
using namespace util::config; using namespace util::config;
struct ArrayViewTest : testing::Test { struct ArrayViewTest : virtual testing::Test {
ArrayViewTest() ArrayViewTest()
{ {
ConfigFileJson const jsonFileObj{boost::json::parse(kJSON_DATA).as_object()}; ConfigFileJson const jsonFileObj{boost::json::parse(kJSON_DATA).as_object()};
@@ -147,31 +148,31 @@ TEST_F(ArrayViewTest, IterateObject)
EXPECT_EQ(it, arr.end<ObjectView>()); EXPECT_EQ(it, arr.end<ObjectView>());
} }
struct ArrayViewDeathTest : ArrayViewTest {}; struct ArrayViewAssertTest : common::util::WithMockAssert, ArrayViewTest {};
TEST_F(ArrayViewDeathTest, AccessArrayOutOfBounce) TEST_F(ArrayViewAssertTest, AccessArrayOutOfBounce)
{ {
// dies because higher only has 1 object (trying to access 2nd element) // dies because higher only has 1 object (trying to access 2nd element)
EXPECT_DEATH({ [[maybe_unused]] auto _ = configData.getArray("higher").objectAt(1); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = configData.getArray("higher").objectAt(1); });
} }
TEST_F(ArrayViewDeathTest, AccessIndexOfWrongType) TEST_F(ArrayViewAssertTest, AccessIndexOfWrongType)
{ {
auto const& arrVals2 = configData.getArray("array.[].sub2"); auto const& arrVals2 = configData.getArray("array.[].sub2");
auto const& tempVal = arrVals2.valueAt(0); auto const& tempVal = arrVals2.valueAt(0);
// dies as value is not of type int // dies as value is not of type int
EXPECT_DEATH({ [[maybe_unused]] auto _ = tempVal.asIntType<int>(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = tempVal.asIntType<int>(); });
} }
TEST_F(ArrayViewDeathTest, GetValueWhenItIsObject) TEST_F(ArrayViewAssertTest, GetValueWhenItIsObject)
{ {
ArrayView const arr = configData.getArray("higher"); ArrayView const arr = configData.getArray("higher");
EXPECT_DEATH({ [[maybe_unused]] auto _ = arr.begin<ValueView>(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = arr.begin<ValueView>(); });
} }
TEST_F(ArrayViewDeathTest, GetObjectWhenItIsValue) TEST_F(ArrayViewAssertTest, GetObjectWhenItIsValue)
{ {
ArrayView const dosguardWhitelist = configData.getArray("dosguard.whitelist"); ArrayView const dosguardWhitelist = configData.getArray("dosguard.whitelist");
EXPECT_DEATH({ [[maybe_unused]] auto _ = dosguardWhitelist.begin<ObjectView>(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = dosguardWhitelist.begin<ObjectView>(); });
} }

View File

@@ -18,6 +18,7 @@
//============================================================================== //==============================================================================
#include "util/LoggerFixtures.hpp" #include "util/LoggerFixtures.hpp"
#include "util/MockAssert.hpp"
#include "util/newconfig/Array.hpp" #include "util/newconfig/Array.hpp"
#include "util/newconfig/ArrayView.hpp" #include "util/newconfig/ArrayView.hpp"
#include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigDefinition.hpp"
@@ -45,7 +46,7 @@
using namespace util::config; using namespace util::config;
struct NewConfigTest : testing::Test { struct NewConfigTest : virtual testing::Test {
ClioConfigDefinition const configData = generateConfig(); ClioConfigDefinition const configData = generateConfig();
}; };
@@ -129,41 +130,41 @@ TEST_F(NewConfigTest, CheckAllKeys)
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
struct NewConfigDeathTest : NewConfigTest {}; struct NewConfigAssertTest : common::util::WithMockAssert, NewConfigTest {};
TEST_F(NewConfigDeathTest, GetNonExistentKeys) TEST_F(NewConfigAssertTest, GetNonExistentKeys)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getValueView("head."); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getValueView("head."); });
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getValueView("asdf"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getValueView("asdf"); });
} }
TEST_F(NewConfigDeathTest, GetValueButIsArray) TEST_F(NewConfigAssertTest, GetValueButIsArray)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getValueView("dosguard.whitelist"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getValueView("dosguard.whitelist"); });
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getValueView("dosguard.whitelist.[]"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getValueView("dosguard.whitelist.[]"); });
} }
TEST_F(NewConfigDeathTest, GetNonExistentObjectKey) TEST_F(NewConfigAssertTest, GetNonExistentObjectKey)
{ {
ASSERT_FALSE(configData.contains("head")); ASSERT_FALSE(configData.contains("head"));
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getObject("head"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getObject("head"); });
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getObject("doesNotExist"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getObject("doesNotExist"); });
} }
TEST_F(NewConfigDeathTest, GetObjectButIsArray) TEST_F(NewConfigAssertTest, GetObjectButIsArray)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getObject("array"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getObject("array"); });
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getObject("array", 2); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getObject("array", 2); });
} }
TEST_F(NewConfigDeathTest, GetArrayButIsValue) TEST_F(NewConfigAssertTest, GetArrayButIsValue)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getArray("header.text1"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getArray("header.text1"); });
} }
TEST_F(NewConfigDeathTest, GetNonExistentArrayKey) TEST_F(NewConfigAssertTest, GetNonExistentArrayKey)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto unused = configData.getArray("asdf"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = configData.getArray("asdf"); });
} }
TEST(ConfigDescription, GetValues) TEST(ConfigDescription, GetValues)
@@ -178,12 +179,14 @@ TEST(ConfigDescription, GetValues)
EXPECT_EQ(definition.get("prometheus.enabled"), "Enable or disable Prometheus metrics."); EXPECT_EQ(definition.get("prometheus.enabled"), "Enable or disable Prometheus metrics.");
} }
TEST(ConfigDescriptionAssertDeathTest, NonExistingKeyTest) struct ConfigDescriptionAssertTest : common::util::WithMockAssert {};
TEST_F(ConfigDescriptionAssertTest, NonExistingKeyTest)
{ {
ClioConfigDescription const definition{}; ClioConfigDescription const definition{};
EXPECT_DEATH({ [[maybe_unused]] auto a = definition.get("data"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto a = definition.get("data"); });
EXPECT_DEATH({ [[maybe_unused]] auto a = definition.get("etl_sources.[]"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto a = definition.get("etl_sources.[]"); });
} }
/** @brief Testing override the default values with the ones in Json */ /** @brief Testing override the default values with the ones in Json */

View File

@@ -18,6 +18,7 @@
//============================================================================== //==============================================================================
#include "util/LoggerFixtures.hpp" #include "util/LoggerFixtures.hpp"
#include "util/MockAssert.hpp"
#include "util/NameGenerator.hpp" #include "util/NameGenerator.hpp"
#include "util/OverloadSet.hpp" #include "util/OverloadSet.hpp"
#include "util/TmpFile.hpp" #include "util/TmpFile.hpp"
@@ -338,30 +339,30 @@ TEST_F(ConfigFileJsonTest, getValue)
EXPECT_FALSE(jsonFileObj.containsKey("object.int")); EXPECT_FALSE(jsonFileObj.containsKey("object.int"));
} }
struct ConfigFileJsonDeathTest : ConfigFileJsonTest {}; struct ConfigFileJsonAssertTest : common::util::WithMockAssert, ConfigFileJsonTest {};
TEST_F(ConfigFileJsonDeathTest, getValueInvalidKey) TEST_F(ConfigFileJsonAssertTest, getValueInvalidKey)
{ {
auto const jsonFileObj = ConfigFileJson{boost::json::parse("{}").as_object()}; auto const jsonFileObj = ConfigFileJson{boost::json::parse("{}").as_object()};
EXPECT_DEATH([[maybe_unused]] auto a = jsonFileObj.getValue("some_key"), ".*"); EXPECT_CLIO_ASSERT_FAIL([[maybe_unused]] auto a = jsonFileObj.getValue("some_key"));
} }
TEST_F(ConfigFileJsonDeathTest, getValueOfArray) TEST_F(ConfigFileJsonAssertTest, getValueOfArray)
{ {
auto const jsonStr = R"json({ auto const jsonStr = R"json({
"array": [1, 2, 3] "array": [1, 2, 3]
})json"; })json";
auto const jsonFileObj = ConfigFileJson{boost::json::parse(jsonStr).as_object()}; auto const jsonFileObj = ConfigFileJson{boost::json::parse(jsonStr).as_object()};
EXPECT_DEATH([[maybe_unused]] auto a = jsonFileObj.getValue("array"), ".*"); EXPECT_CLIO_ASSERT_FAIL([[maybe_unused]] auto a = jsonFileObj.getValue("array"));
} }
TEST_F(ConfigFileJsonDeathTest, nullIsNotSupported) TEST_F(ConfigFileJsonAssertTest, nullIsNotSupported)
{ {
auto const jsonStr = R"json({ auto const jsonStr = R"json({
"null": null "null": null
})json"; })json";
auto const jsonFileObj = ConfigFileJson{boost::json::parse(jsonStr).as_object()}; auto const jsonFileObj = ConfigFileJson{boost::json::parse(jsonStr).as_object()};
EXPECT_DEATH([[maybe_unused]] auto a = jsonFileObj.getValue("null"), ".*"); EXPECT_CLIO_ASSERT_FAIL([[maybe_unused]] auto a = jsonFileObj.getValue("null"));
} }
TEST_F(ConfigFileJsonTest, getArray) TEST_F(ConfigFileJsonTest, getArray)
@@ -444,19 +445,19 @@ TEST_F(ConfigFileJsonTest, getArrayOptionalInArray)
EXPECT_EQ(std::get<bool>(bools.at(1).value()), true); EXPECT_EQ(std::get<bool>(bools.at(1).value()), true);
} }
TEST_F(ConfigFileJsonDeathTest, getArrayInvalidKey) TEST_F(ConfigFileJsonAssertTest, getArrayInvalidKey)
{ {
auto const jsonFileObj = ConfigFileJson{boost::json::parse("{}").as_object()}; auto const jsonFileObj = ConfigFileJson{boost::json::parse("{}").as_object()};
EXPECT_DEATH([[maybe_unused]] auto a = jsonFileObj.getArray("some_key"), ".*"); EXPECT_CLIO_ASSERT_FAIL([[maybe_unused]] auto a = jsonFileObj.getArray("some_key"));
} }
TEST_F(ConfigFileJsonDeathTest, getArrayNotArray) TEST_F(ConfigFileJsonAssertTest, getArrayNotArray)
{ {
auto const jsonStr = R"json({ auto const jsonStr = R"json({
"int": 42 "int": 42
})json"; })json";
auto const jsonFileObj = ConfigFileJson{boost::json::parse(jsonStr).as_object()}; auto const jsonFileObj = ConfigFileJson{boost::json::parse(jsonStr).as_object()};
EXPECT_DEATH([[maybe_unused]] auto a = jsonFileObj.getArray("int"), ".*"); EXPECT_CLIO_ASSERT_FAIL([[maybe_unused]] auto a = jsonFileObj.getArray("int"));
} }
TEST_F(ConfigFileJsonTest, containsKey) TEST_F(ConfigFileJsonTest, containsKey)

View File

@@ -18,6 +18,7 @@
//============================================================================== //==============================================================================
#include "util/LoggerFixtures.hpp" #include "util/LoggerFixtures.hpp"
#include "util/MockAssert.hpp"
#include "util/newconfig/ConfigConstraints.hpp" #include "util/newconfig/ConfigConstraints.hpp"
#include "util/newconfig/ConfigValue.hpp" #include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Error.hpp" #include "util/newconfig/Error.hpp"
@@ -34,8 +35,7 @@
using namespace util::config; using namespace util::config;
struct ConfigValueTest : NoLoggerFixture {}; struct ConfigValueTest : common::util::WithMockAssert, NoLoggerFixture {};
struct ConfigValueDeathTest : ConfigValueTest {};
TEST_F(ConfigValueTest, construct) TEST_F(ConfigValueTest, construct)
{ {
@@ -69,9 +69,9 @@ TEST_F(ConfigValueTest, defaultValueWithDescription)
EXPECT_EQ(cv.type(), ConfigType::String); EXPECT_EQ(cv.type(), ConfigType::String);
} }
TEST_F(ConfigValueDeathTest, invalidDefaultValue) TEST_F(ConfigValueTest, invalidDefaultValue)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto const a = ConfigValue{ConfigType::String}.defaultValue(33); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto const a = ConfigValue{ConfigType::String}.defaultValue(33); });
} }
TEST_F(ConfigValueTest, setValueWrongType) TEST_F(ConfigValueTest, setValueWrongType)
@@ -128,19 +128,13 @@ TEST_F(ConfigValueConstraintTest, defaultValueWithConstraint)
EXPECT_EQ(cv.getValue(), Value{123}); EXPECT_EQ(cv.getValue(), Value{123});
} }
struct ConfigValueConstraintDeathTest : ConfigValueConstraintTest {}; TEST_F(ConfigValueConstraintTest, defaultValueWithConstraintCheckError)
TEST_F(ConfigValueConstraintDeathTest, defaultValueWithConstraintCheckError)
{ {
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL({
{ EXPECT_CALL(constraint, checkTypeImpl).WillOnce(testing::Return(std::nullopt));
EXPECT_CALL(constraint, checkTypeImpl).WillOnce(testing::Return(std::nullopt)); EXPECT_CALL(constraint, checkValueImpl).WillOnce(testing::Return(Error{"value error"}));
EXPECT_CALL(constraint, checkValueImpl).WillOnce(testing::Return(Error{"value error"})); [[maybe_unused]] auto const cv = ConfigValue{ConfigType::Integer}.defaultValue(123).withConstraint(constraint);
[[maybe_unused]] auto const cv = });
ConfigValue{ConfigType::Integer}.defaultValue(123).withConstraint(constraint);
},
".*"
);
} }
// A test for each constraint so it's easy to change in the future // A test for each constraint so it's easy to change in the future
@@ -259,11 +253,11 @@ struct ConstraintTestBundle {
Constraint const& constraint; Constraint const& constraint;
}; };
struct ConstraintDeathTest : testing::TestWithParam<ConstraintTestBundle> {}; struct ConstraintAssertTest : common::util::WithMockAssert, testing::WithParamInterface<ConstraintTestBundle> {};
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
EachConstraints, EachConstraints,
ConstraintDeathTest, ConstraintAssertTest,
testing::Values( testing::Values(
ConstraintTestBundle{"logTagConstraint", gValidateLogTag}, ConstraintTestBundle{"logTagConstraint", gValidateLogTag},
ConstraintTestBundle{"portConstraint", gValidatePort}, ConstraintTestBundle{"portConstraint", gValidatePort},
@@ -281,43 +275,28 @@ INSTANTIATE_TEST_SUITE_P(
[](testing::TestParamInfo<ConstraintTestBundle> const& info) { return info.param.name; } [](testing::TestParamInfo<ConstraintTestBundle> const& info) { return info.param.name; }
); );
TEST_P(ConstraintDeathTest, TestEachConstraint) TEST_P(ConstraintAssertTest, TestEachConstraint)
{ {
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL({
{ [[maybe_unused]] auto const a =
[[maybe_unused]] auto const a = ConfigValue{ConfigType::Boolean}.defaultValue(true).withConstraint(GetParam().constraint);
ConfigValue{ConfigType::Boolean}.defaultValue(true).withConstraint(GetParam().constraint); });
},
".*"
);
} }
TEST(ConstraintDeathTest, SetInvalidValueTypeStringAndBool) TEST_F(ConstraintAssertTest, SetInvalidValueTypeStringAndBool)
{ {
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL({
{ [[maybe_unused]] auto a = ConfigValue{ConfigType::String}.defaultValue(33).withConstraint(gValidateLoadMode);
[[maybe_unused]] auto a = });
ConfigValue{ConfigType::String}.defaultValue(33).withConstraint(gValidateLoadMode); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto a = ConfigValue{ConfigType::Boolean}.defaultValue(-66); });
},
".*"
);
EXPECT_DEATH({ [[maybe_unused]] auto a = ConfigValue{ConfigType::Boolean}.defaultValue(-66); }, ".*");
} }
TEST(ConstraintDeathTest, OutOfBounceIntegerConstraint) TEST_F(ConstraintAssertTest, OutOfBounceIntegerConstraint)
{ {
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL({
{ [[maybe_unused]] auto a = ConfigValue{ConfigType::Integer}.defaultValue(999999).withConstraint(gValidateUint16);
[[maybe_unused]] auto a = });
ConfigValue{ConfigType::Integer}.defaultValue(999999).withConstraint(gValidateUint16); EXPECT_CLIO_ASSERT_FAIL({
}, [[maybe_unused]] auto a = ConfigValue{ConfigType::Integer}.defaultValue(-66).withConstraint(gValidateUint32);
".*" });
);
EXPECT_DEATH(
{
[[maybe_unused]] auto a =
ConfigValue{ConfigType::Integer}.defaultValue(-66).withConstraint(gValidateUint32);
},
".*"
);
} }

View File

@@ -1,98 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "util/TmpFile.hpp"
#include "util/newconfig/ConfigFileJson.hpp"
#include "util/newconfig/FakeConfigData.hpp"
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdint>
#include <string>
#include <variant>
#include <vector>
struct JsonFromTempFile : testing::Test {
JsonFromTempFile() : jsonFileObj{util::config::ConfigFileJson::makeConfigFileJson(TmpFile(kJSON_DATA).path).value()}
{
}
ConfigFileJson jsonFileObj;
};
TEST_F(JsonFromTempFile, validateKeys)
{
EXPECT_TRUE(jsonFileObj.containsKey("header.text1"));
EXPECT_TRUE(jsonFileObj.containsKey("header.sub.sub2Value"));
EXPECT_TRUE(jsonFileObj.containsKey("dosguard.port"));
EXPECT_FALSE(jsonFileObj.containsKey("idk"));
EXPECT_FALSE(jsonFileObj.containsKey("optional.withNoDefault"));
}
TEST_F(JsonFromTempFile, validateValues)
{
EXPECT_EQ(std::get<std::string>(jsonFileObj.getValue("header.text1")), "value");
EXPECT_EQ(std::get<std::string>(jsonFileObj.getValue("header.sub.sub2Value")), "TSM");
EXPECT_EQ(std::get<int64_t>(jsonFileObj.getValue("dosguard.port")), 44444);
}
TEST_F(JsonFromTempFile, validateArrayValue)
{
// validate array.[].sub matches expected values
EXPECT_TRUE(jsonFileObj.containsKey("array.[].sub"));
auto const arrSub = jsonFileObj.getArray("array.[].sub");
EXPECT_EQ(arrSub.size(), 3);
std::vector<double> expectedArrSubVal{111.11, 4321.55, 5555.44};
std::vector<double> actualArrSubVal{};
for (auto it = arrSub.begin(); it != arrSub.end(); ++it) {
ASSERT_TRUE(std::holds_alternative<double>(*it));
actualArrSubVal.emplace_back(std::get<double>(*it));
}
EXPECT_TRUE(std::ranges::equal(expectedArrSubVal, actualArrSubVal));
// validate array.[].sub2 matches expected values
EXPECT_TRUE(jsonFileObj.containsKey("array.[].sub2"));
auto const arrSub2 = jsonFileObj.getArray("array.[].sub2");
EXPECT_EQ(arrSub2.size(), 3);
std::vector<std::string> expectedArrSub2Val{"subCategory", "temporary", "london"};
std::vector<std::string> actualArrSub2Val{};
for (auto it = arrSub2.begin(); it != arrSub2.end(); ++it) {
ASSERT_TRUE(std::holds_alternative<std::string>(*it));
actualArrSub2Val.emplace_back(std::get<std::string>(*it));
}
EXPECT_TRUE(std::ranges::equal(expectedArrSub2Val, actualArrSub2Val));
EXPECT_TRUE(jsonFileObj.containsKey("dosguard.whitelist.[]"));
auto const whitelistArr = jsonFileObj.getArray("dosguard.whitelist.[]");
EXPECT_EQ(whitelistArr.size(), 2);
EXPECT_EQ("125.5.5.1", std::get<std::string>(whitelistArr.at(0)));
EXPECT_EQ("204.2.2.1", std::get<std::string>(whitelistArr.at(1)));
}
struct ConfigValueJsonGetArrayDeathTest : JsonFromTempFile {};
TEST_F(ConfigValueJsonGetArrayDeathTest, invalidGetValues)
{
// not possible for json value to call a value that doesn't exist
EXPECT_DEATH([[maybe_unused]] auto a = jsonFileObj.getArray("header.text1"), ".*");
}

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/newconfig/ArrayView.hpp" #include "util/newconfig/ArrayView.hpp"
#include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigFileJson.hpp" #include "util/newconfig/ConfigFileJson.hpp"
@@ -31,7 +32,7 @@
using namespace util::config; using namespace util::config;
struct ObjectViewTest : testing::Test { struct ObjectViewTest : virtual testing::Test {
ObjectViewTest() ObjectViewTest()
{ {
ConfigFileJson const jsonFileObj{boost::json::parse(kJSON_DATA).as_object()}; ConfigFileJson const jsonFileObj{boost::json::parse(kJSON_DATA).as_object()};
@@ -124,27 +125,29 @@ TEST_F(ObjectViewTest, getArrayInObject)
EXPECT_EQ("204.2.2.1", arr.valueAt(1).asString()); EXPECT_EQ("204.2.2.1", arr.valueAt(1).asString());
} }
struct ObjectViewDeathTest : ObjectViewTest {}; struct ObjectViewAssertTest : common::util::WithMockAssert, ObjectViewTest {};
TEST_F(ObjectViewDeathTest, KeyDoesNotExist) TEST_F(ObjectViewAssertTest, KeyDoesNotExist)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto _ = configData.getObject("head"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = configData.getObject("head"); });
} }
TEST_F(ObjectViewDeathTest, KeyIsValueView) TEST_F(ObjectViewAssertTest, KeyIsValueView)
{ {
EXPECT_DEATH({ [[maybe_unused]] auto _ = configData.getObject("header.text1"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = configData.getObject("header.text1"); });
EXPECT_DEATH({ [[maybe_unused]] auto _ = configData.getArray("header"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = configData.getArray("header"); });
} }
TEST_F(ObjectViewDeathTest, KeyisArrayView) TEST_F(ObjectViewAssertTest, KeyisArrayView)
{ {
// dies because only 1 object in higher.[].low // dies because only 1 object in higher.[].low
EXPECT_DEATH({ [[maybe_unused]] auto _ = configData.getObject("higher.[].low", 1); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto _ = configData.getObject("higher.[].low", 1); });
} }
TEST_F(ObjectViewDeathTest, KeyisNotOptional) TEST_F(ObjectViewAssertTest, KeyisNotOptional)
{ {
// dies because not an optional // dies because not an optional
EXPECT_DEATH({ [[maybe_unused]] auto _ = configData.getObject("header").maybeValue<std::string>("text1"); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({
[[maybe_unused]] auto _ = configData.getObject("header").maybeValue<std::string>("text1");
});
} }

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/newconfig/ConfigConstraints.hpp" #include "util/newconfig/ConfigConstraints.hpp"
#include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp" #include "util/newconfig/ConfigValue.hpp"
@@ -32,7 +33,7 @@
using namespace util::config; using namespace util::config;
struct ValueViewTest : testing::Test { struct ValueViewTest : virtual testing::Test {
ClioConfigDefinition const configData = generateConfig(); ClioConfigDefinition const configData = generateConfig();
}; };
@@ -106,20 +107,20 @@ TEST_F(ValueViewTest, OptionalValues)
EXPECT_EQ(vv4.asOptional<std::string>(), "hello"); EXPECT_EQ(vv4.asOptional<std::string>(), "hello");
} }
struct ValueDeathTest : ValueViewTest {}; struct ValueViewAssertTest : common::util::WithMockAssert, ValueViewTest {};
TEST_F(ValueDeathTest, WrongTypes) TEST_F(ValueViewAssertTest, WrongTypes)
{ {
auto const vv = configData.getValueView("header.port"); auto const vv = configData.getValueView("header.port");
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv.asBool(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = vv.asBool(); });
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv.asString(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = vv.asString(); });
auto const cv = ConfigValue{ConfigType::Integer}.defaultValue(-5); auto const cv = ConfigValue{ConfigType::Integer}.defaultValue(-5);
auto const vv2 = ValueView(cv); auto const vv2 = ValueView(cv);
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv2.asIntType<uint32_t>(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = vv2.asIntType<uint32_t>(); });
auto const cv2 = ConfigValue{ConfigType::String}.defaultValue("asdf"); auto const cv2 = ConfigValue{ConfigType::String}.defaultValue("asdf");
auto const vv3 = ValueView(cv2); auto const vv3 = ValueView(cv2);
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv3.asDouble(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = vv3.asDouble(); });
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv3.asFloat(); }, ".*"); EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto unused = vv3.asFloat(); });
} }

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/prometheus/Counter.hpp" #include "util/prometheus/Counter.hpp"
#include "util/prometheus/OStream.hpp" #include "util/prometheus/OStream.hpp"
@@ -30,7 +31,7 @@
using namespace util::prometheus; using namespace util::prometheus;
struct AnyCounterTests : ::testing::Test { struct AnyCounterTests : virtual ::testing::Test {
struct MockCounterImpl { struct MockCounterImpl {
using ValueType = std::uint64_t; using ValueType = std::uint64_t;
MOCK_METHOD(void, add, (ValueType)); MOCK_METHOD(void, add, (ValueType));
@@ -89,18 +90,15 @@ TEST_F(AnyCounterTests, value)
EXPECT_EQ(counter.value(), 42); EXPECT_EQ(counter.value(), 42);
} }
struct AnyCounterDeathTest : AnyCounterTests {}; struct AnyCounterAssertTest : common::util::WithMockAssert, AnyCounterTests {};
TEST_F(AnyCounterDeathTest, setLowerValue) TEST_F(AnyCounterAssertTest, setLowerValue)
{ {
testing::Mock::AllowLeak(&mockCounterImpl); testing::Mock::AllowLeak(&mockCounterImpl);
EXPECT_DEATH( EXPECT_CLIO_ASSERT_FAIL({
{ EXPECT_CALL(mockCounterImpl, value()).WillOnce(::testing::Return(50));
EXPECT_CALL(mockCounterImpl, value()).WillOnce(::testing::Return(50)); counter.set(42);
counter.set(42); });
},
".*"
);
} }
struct CounterIntTests : ::testing::Test { struct CounterIntTests : ::testing::Test {

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/prometheus/Counter.hpp" #include "util/prometheus/Counter.hpp"
#include "util/prometheus/Gauge.hpp" #include "util/prometheus/Gauge.hpp"
#include "util/prometheus/Histogram.hpp" #include "util/prometheus/Histogram.hpp"
@@ -32,7 +33,9 @@
using namespace util::prometheus; using namespace util::prometheus;
TEST(MetricBuilderDeathTest, build) struct MetricBuilderAssertTest : common::util::WithMockAssert {};
TEST_F(MetricBuilderAssertTest, build)
{ {
std::string const name = "name"; std::string const name = "name";
std::string const labelsString = "{label1=\"value1\"}"; std::string const labelsString = "{label1=\"value1\"}";
@@ -80,5 +83,5 @@ TEST(MetricBuilderDeathTest, build)
EXPECT_EQ(metric->labelsString(), labelsString); EXPECT_EQ(metric->labelsString(), labelsString);
} }
} }
EXPECT_DEATH({ builder(name, labelsString, MetricType::Summary, std::vector<std::int64_t>{}); }, ""); EXPECT_CLIO_ASSERT_FAIL({ builder(name, labelsString, MetricType::Summary, std::vector<std::int64_t>{}); });
} }

View File

@@ -17,6 +17,7 @@
*/ */
//============================================================================== //==============================================================================
#include "util/MockAssert.hpp"
#include "util/Taggable.hpp" #include "util/Taggable.hpp"
#include "util/build/Build.hpp" #include "util/build/Build.hpp"
#include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigDefinition.hpp"
@@ -45,21 +46,21 @@ using namespace web::ng;
namespace http = boost::beast::http; namespace http = boost::beast::http;
using namespace util::config; using namespace util::config;
struct ResponseDeathTest : testing::Test {}; struct ResponseAssertTest : common::util::WithMockAssert {};
TEST_F(ResponseDeathTest, intoHttpResponseWithoutHttpData) TEST_F(ResponseAssertTest, intoHttpResponseWithoutHttpData)
{ {
Request::HttpHeaders const headers{}; Request::HttpHeaders const headers{};
Request const request{"some message", headers}; Request const request{"some message", headers};
Response response{boost::beast::http::status::ok, "message", request}; Response response{boost::beast::http::status::ok, "message", request};
EXPECT_DEATH(std::move(response).intoHttpResponse(), ""); EXPECT_CLIO_ASSERT_FAIL(std::move(response).intoHttpResponse());
} }
TEST_F(ResponseDeathTest, asConstBufferWithHttpData) TEST_F(ResponseAssertTest, asConstBufferWithHttpData)
{ {
Request const request{http::request<http::string_body>{http::verb::get, "/", 11}}; Request const request{http::request<http::string_body>{http::verb::get, "/", 11}};
Response const response{boost::beast::http::status::ok, "message", request}; Response const response{boost::beast::http::status::ok, "message", request};
EXPECT_DEATH(response.asWsResponse(), ""); EXPECT_CLIO_ASSERT_FAIL(response.asWsResponse());
} }
struct ResponseTest : testing::Test { struct ResponseTest : testing::Test {