*/
struct VasumException: public std::runtime_error {
- VasumException(const std::string& error = "") : std::runtime_error(error) {}
+ VasumException(const std::string& error) : std::runtime_error(error) {}
};
/**
*/
struct UtilsException: public VasumException {
- UtilsException(const std::string& error = "") : VasumException(error) {}
+ UtilsException(const std::string& error) : VasumException(error) {}
};
{
std::string result;
if (!readFileContent(path, result)) {
- throw UtilsException();
+ throw UtilsException("Read failed");
}
return result;
}
{
std::unique_lock<std::mutex> lock(mMutex);
if (mValue) {
- throw UtilsException("Cannot set ValueLatch multiple times!");
+ throw UtilsException("Cannot set value multiple times");
}
mValue.reset(new T(value));
mCondition.notify_one();
{
std::unique_lock<std::mutex> lock(mMutex);
if (mValue) {
- throw UtilsException("Cannot set ValueLatch multiple times!");
+ throw UtilsException("Cannot set value multiple times");
}
mValue.reset(new T(std::move(value)));
mCondition.notify_one();
std::unique_ptr<T> retValue(std::move(mValue));
return T(std::move(*retValue));
} else {
- throw UtilsException("Timeout occured.");
+ throw UtilsException("Timeout occured");
}
}
*/
struct ServerException: public VasumException {
- ServerException(const std::string& error = "") : VasumException(error) {}
+ ServerException(const std::string& error) : VasumException(error) {}
};
/**
*/
struct ZoneOperationException: public ServerException {
- ZoneOperationException(const std::string& error = "") : ServerException(error) {}
+ ZoneOperationException(const std::string& error) : ServerException(error) {}
};
/**
*/
struct InvalidZoneIdException : public ServerException {
- InvalidZoneIdException(const std::string& error = "") : ServerException(error) {}
+ InvalidZoneIdException(const std::string& error) : ServerException(error) {}
};
/**
*/
struct ZoneConnectionException: public ServerException {
- ZoneConnectionException(const std::string& error = "") : ServerException(error) {}
+ ZoneConnectionException(const std::string& error) : ServerException(error) {}
};
/**
*/
struct HostConnectionException: public ServerException {
- HostConnectionException(const std::string& error = "") : ServerException(error) {}
+ HostConnectionException(const std::string& error) : ServerException(error) {}
};
/**
*/
struct InputMonitorException: public ServerException {
- InputMonitorException(const std::string& error = "") : ServerException(error) {}
+ InputMonitorException(const std::string& error) : ServerException(error) {}
};
{
if (mConfig.timeWindowMs > MAX_TIME_WINDOW_SEC * 1000L) {
LOGE("Time window exceeds maximum: " << MAX_TIME_WINDOW_SEC);
- throw InputMonitorException();
+ throw InputMonitorException("Time window exceeds maximum");
}
if (mConfig.numberOfEvents > MAX_NUMBER_OF_EVENTS) {
LOGE("Number of events exceeds maximum: " << MAX_NUMBER_OF_EVENTS);
- throw InputMonitorException();
+ throw InputMonitorException("Number of events exceeds maximum");
}
std::string devicePath = getDevicePath();
if (it == end) {
LOGE("None of the files under '" << DEVICE_DIR <<
"' represents device named: " << device);
- throw InputMonitorException();
+ throw InputMonitorException("Cannot find a device");
}
return it->path().string();
if (fd < 0) {
LOGE("Cannot create input monitor channel. Device file: " <<
devicePath << " doesn't exist");
- throw InputMonitorException();
+ throw InputMonitorException("Device does not exist");
}
mChannelPtr = g_io_channel_unix_new(fd);
NULL,
NULL) != G_IO_STATUS_NORMAL) {
LOGE("Cannot set encoding for input monitor channel ");
- throw InputMonitorException();
+ throw InputMonitorException("Cannot set encoding");
}
using namespace std::placeholders;
&utils::deleteCallbackWrapper<ReadDeviceCallback>);
if (!mSourceId) {
LOGE("Cannot add watch on device input file");
- throw InputMonitorException();
+ throw InputMonitorException("Cannot add watch");
}
}
BOOST_CHECK_EQUAL("Processed: arg", client.process("arg"));
BOOST_CHECK_NO_THROW(client.throwException(0));
- auto checkException = [](const DbusCustomException& e) {
- return e.what() == std::string("Argument: 666");
- };
- BOOST_CHECK_EXCEPTION(client.throwException(666), DbusCustomException, checkException);
+ BOOST_CHECK_EXCEPTION(client.throwException(666),
+ DbusCustomException,
+ WhatEquals("Argument: 666"));
}
BOOST_AUTO_TEST_CASE(DbusApiNotifyTest)
#include <stdexcept>
-BOOST_AUTO_TEST_SUITE(LogSuite)
+BOOST_AUTO_TEST_SUITE(LoggerSuite)
using namespace logger;
Logger::setLogLevel("ERROR");
BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
- BOOST_REQUIRE_THROW(Logger::setLogLevel("UNKNOWN"), std::runtime_error);
+ BOOST_REQUIRE_EXCEPTION(Logger::setLogLevel("UNKNOWN"),
+ std::runtime_error,
+ WhatEquals("Invalid LogLevel to parse")); //TODO change message
}
BOOST_AUTO_TEST_CASE(TestLogsError)
BOOST_CHECK(lxc.isDefined());
BOOST_CHECK_EQUAL(lxc.getConfigItem("lxc.rootfs"), LXC_PATH + ZONE_NAME + "/rootfs");
- BOOST_CHECK_THROW(lxc.getConfigItem("xxx"), LxcException);
+ BOOST_CHECK_EXCEPTION(lxc.getConfigItem("xxx"), LxcException, WhatEquals("Key not found"));
BOOST_CHECK(lxc.destroy());
BOOST_AUTO_TEST_CASE(Config_OK)
{
- BOOST_REQUIRE_NO_THROW(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()));
+ InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback());
}
BOOST_AUTO_TEST_CASE(Config_timeWindowMsTooHigh)
{
inputConfig.timeWindowMs = 50000;
- BOOST_REQUIRE_THROW(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
- InputMonitorException);
+ BOOST_REQUIRE_EXCEPTION(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
+ InputMonitorException,
+ WhatEquals("Time window exceeds maximum"));
}
BOOST_AUTO_TEST_CASE(Config_deviceFilePathNotExisting)
{
inputConfig.device = TEST_INPUT_DEVICE + "notExisting";
- BOOST_REQUIRE_THROW(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
- InputMonitorException);
+ BOOST_REQUIRE_EXCEPTION(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
+ InputMonitorException,
+ WhatEquals("Cannot find a device"));
}
namespace {
{
Latch eventLatch;
- std::unique_ptr<InputMonitor> inputMonitor;
- BOOST_REQUIRE_NO_THROW(inputMonitor.reset(new InputMonitor(f.inputConfig, [&] {eventLatch.set();})));
+ InputMonitor inputMonitor(f.inputConfig, [&] {eventLatch.set();});
int fd = ::open(TEST_INPUT_DEVICE.c_str(), O_WRONLY);
BOOST_REQUIRE(fd >= 0);
{
Latch eventLatch;
- std::unique_ptr<InputMonitor> inputMonitor;
- BOOST_REQUIRE_NO_THROW(inputMonitor.reset(new InputMonitor(f.inputConfig, [&] {eventLatch.set();})));
+ InputMonitor inputMonitor(f.inputConfig, [&] {eventLatch.set();});
int fd = ::open(TEST_INPUT_DEVICE.c_str(), O_WRONLY);
BOOST_REQUIRE(fd >= 0);
BOOST_AUTO_TEST_CASE(MissingConfigTest)
{
- BOOST_REQUIRE_THROW(Server(MISSING_CONFIG_PATH).run(AS_ROOT), ConfigException);//TODO check message
+ BOOST_REQUIRE_EXCEPTION(Server(MISSING_CONFIG_PATH).run(AS_ROOT),
+ ConfigException,
+ WhatEquals("Could not load " + MISSING_CONFIG_PATH));
}
BOOST_AUTO_TEST_CASE(TerminateTest)
BOOST_AUTO_TEST_CASE(MissingConfigTest)
{
- BOOST_REQUIRE_THROW(create(MISSING_CONFIG_PATH), ZoneOperationException);//TODO check message
+ BOOST_REQUIRE_EXCEPTION(create(MISSING_CONFIG_PATH),
+ ZoneOperationException,
+ WhatEquals("Could not create zone"));
}
BOOST_AUTO_TEST_CASE(StartTest)
BOOST_AUTO_TEST_CASE(StartBuggyTest)
{
auto admin = create(BUGGY_CONFIG_PATH);
- BOOST_REQUIRE_THROW(admin->start(), ZoneOperationException);//TODO check message
+ BOOST_REQUIRE_EXCEPTION(admin->start(),
+ ZoneOperationException,
+ WhatEquals("Could not start zone"));
}
BOOST_AUTO_TEST_CASE(StopShutdownTest)
//
// admin->start();
// ensureStarted();
-// BOOST_REQUIRE_NO_THROW(admin->setSchedulerLevel(SchedulerLevel::FOREGROUND));
+// admin->setSchedulerLevel(SchedulerLevel::FOREGROUND);
// BOOST_REQUIRE(admin->getSchedulerQuota() == config.cpuQuotaForeground);
-// BOOST_REQUIRE_NO_THROW(admin->setSchedulerLevel(SchedulerLevel::BACKGROUND));
+// admin->setSchedulerLevel(SchedulerLevel::BACKGROUND);
// BOOST_REQUIRE(admin->getSchedulerQuota() == config.cpuQuotaBackground);
//}
ScopedGlibLoop loop;
ScopedDbusDaemon dbus;
- BOOST_REQUIRE_NO_THROW(ZoneConnection(dbus.acquireAddress(), nullptr));
+ ZoneConnection(dbus.acquireAddress(), nullptr);
}
BOOST_AUTO_TEST_CASE(NotifyActiveZoneApiTest)
ScopedDbusDaemon dbus;
Latch notifyCalled;
- std::unique_ptr<ZoneConnection> connection;
-
- BOOST_REQUIRE_NO_THROW(connection.reset(new ZoneConnection(dbus.acquireAddress(), nullptr)));
+ ZoneConnection connection(dbus.acquireAddress(), nullptr);
auto callback = [&](const std::string& application, const std::string& message) {
if (application == "testapp" && message == "testmessage") {
notifyCalled.set();
}
};
- connection->setNotifyActiveZoneCallback(callback);
+ connection.setNotifyActiveZoneCallback(callback);
DbusConnection::Pointer client = DbusConnection::create(dbus.acquireAddress());
client->callMethod(api::zone::BUS_NAME,
ScopedDbusDaemon dbus;
Latch signalEmitted;
- std::unique_ptr<ZoneConnection> connection;
-
- BOOST_REQUIRE_NO_THROW(connection.reset(new ZoneConnection(dbus.acquireAddress(), nullptr)));
+ ZoneConnection connection(dbus.acquireAddress(), nullptr);
DbusConnection::Pointer client = DbusConnection::create(dbus.acquireAddress());
};
client->signalSubscribe(handler, api::zone::BUS_NAME);
- connection->sendNotification("testzone", "testapp", "testmessage");
+ connection.sendNotification("testzone", "testapp", "testmessage");
BOOST_CHECK(signalEmitted.wait(EVENT_TIMEOUT));
}
ScopedDbusDaemon dbus;
Latch displayOffCalled;
- std::unique_ptr<ZoneConnection> connection;
-
- BOOST_REQUIRE_NO_THROW(connection.reset(new ZoneConnection(dbus.acquireAddress(),
- nullptr)));
+ ZoneConnection connection(dbus.acquireAddress(), nullptr);
DbusConnection::Pointer client = DbusConnection::create(dbus.acquireAddress());
displayOffCalled.set();
};
- connection->setDisplayOffCallback(callback);
+ connection.setDisplayOffCallback(callback);
client->emitSignal(fake_power_manager_api::OBJECT_PATH,
fake_power_manager_api::INTERFACE,
}
};
-std::function<bool(const std::exception&)> expectedMessage(const std::string& message) {
- return [=](const std::exception& e) {
- return e.what() == message;
- };
-}
-
} // namespace
zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake");
BOOST_CHECK_EXCEPTION(zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake"),
UtilsException,
- expectedMessage("Provision already exists"));
+ WhatEquals("Provision already exists"));
ZoneProvisioningConfig config;
load(config);
zoneProvision.remove("link /fake/path2 /fake/path4");
BOOST_CHECK_EXCEPTION(zoneProvision.remove("link /fake/path_fake /fake/path2"),
UtilsException,
- expectedMessage("Can't find provision"));
+ WhatEquals("Can't find provision"));
const std::vector<std::string> provisions = zoneProvision.list();
BOOST_REQUIRE_EQUAL(provisions.size(), expected.size());
BOOST_AUTO_TEST_CASE(BuggyConfigTest)
{
- BOOST_REQUIRE_THROW(create(BUGGY_CONFIG_PATH), ZoneOperationException);//TODO check message
+ BOOST_REQUIRE_EXCEPTION(create(BUGGY_CONFIG_PATH),
+ ZoneOperationException,
+ WhatEquals("Could not create zone"));
}
BOOST_AUTO_TEST_CASE(MissingConfigTest)
{
- BOOST_REQUIRE_THROW(create(MISSING_CONFIG_PATH), ConfigException);//TODO check message
+ BOOST_REQUIRE_EXCEPTION(create(MISSING_CONFIG_PATH),
+ ConfigException,
+ WhatEquals("Could not load " + MISSING_CONFIG_PATH));
}
BOOST_AUTO_TEST_CASE(StartStopTest)
}
};
-std::function<bool(const std::exception&)> expectedMessage(const std::string& message)
-{
- return [=](const std::exception& e) {
- return e.what() == message;
- };
-}
-
template<class Predicate>
bool spinWaitFor(int timeoutMs, Predicate pred)
{
BOOST_AUTO_TEST_CASE(MissingConfigTest)
{
- BOOST_REQUIRE_THROW(ZonesManager cm(MISSING_CONFIG_PATH), ConfigException);
+ BOOST_REQUIRE_EXCEPTION(ZonesManager{MISSING_CONFIG_PATH},
+ ConfigException,
+ WhatEquals("Could not load " + MISSING_CONFIG_PATH));
}
BOOST_AUTO_TEST_CASE(CreateTest)
// host -> unknown
BOOST_CHECK_EXCEPTION(dbuses.at(0)->testApiProxyCall("unknown", "param"),
DbusCustomException,
- expectedMessage("Unknown proxy call target"));
+ WhatEquals("Unknown proxy call target"));
// forwarding error
BOOST_CHECK_EXCEPTION(dbuses.at(0)->testApiProxyCall("host", ""),
DbusCustomException,
- expectedMessage("Test error"));
+ WhatEquals("Test error"));
// forbidden call
BOOST_CHECK_EXCEPTION(dbuses.at(0)->proxyCall("host",
"foo",
g_variant_new("(s)", "arg")),
DbusCustomException,
- expectedMessage("Proxy call forbidden"));
+ WhatEquals("Proxy call forbidden"));
}
namespace {
BOOST_CHECK(dbus.callMethodGetActiveZoneId() == zoneId);
}
- BOOST_REQUIRE_THROW(dbus.callMethodSetActiveZone(NON_EXISTANT_ZONE_ID),
- DbusException);
+ BOOST_REQUIRE_EXCEPTION(dbus.callMethodSetActiveZone(NON_EXISTANT_ZONE_ID),
+ DbusException,
+ WhatEquals("No such zone id"));
cm.stopAll();
- BOOST_REQUIRE_THROW(dbus.callMethodSetActiveZone("zone1"),
- DbusException);
+ BOOST_REQUIRE_EXCEPTION(dbus.callMethodSetActiveZone("zone1"),
+ DbusException,
+ WhatEquals("Could not activate stopped or paused zone"));
}
BOOST_AUTO_TEST_CASE(CreateDestroyZoneTest)
BOOST_CHECK(cm.isRunning(zoneId));
}
- BOOST_REQUIRE_THROW(dbus.callMethodLockZone(NON_EXISTANT_ZONE_ID),
- DbusException);
- BOOST_REQUIRE_THROW(dbus.callMethodUnlockZone(NON_EXISTANT_ZONE_ID),
- DbusException);
+ BOOST_REQUIRE_EXCEPTION(dbus.callMethodLockZone(NON_EXISTANT_ZONE_ID),
+ DbusException,
+ WhatEquals("No such zone id"));
+ BOOST_REQUIRE_EXCEPTION(dbus.callMethodUnlockZone(NON_EXISTANT_ZONE_ID),
+ DbusException,
+ WhatEquals("No such zone id"));
cm.stopAll();
- BOOST_REQUIRE_THROW(dbus.callMethodLockZone("zone1"),
- DbusException);
- BOOST_REQUIRE_THROW(dbus.callMethodUnlockZone("zone1"),
- DbusException);
+ BOOST_REQUIRE_EXCEPTION(dbus.callMethodLockZone("zone1"),
+ DbusException,
+ WhatEquals("Zone is not running"));
+ BOOST_REQUIRE_EXCEPTION(dbus.callMethodUnlockZone("zone1"),
+ DbusException,
+ WhatEquals("Zone is not paused"));
}
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
+#include <string>
+
+/**
+ * An exception message checker
+ *
+ * Usage example:
+ * BOOST_CHECK_EXCEPTION(foo(), SomeException, WhatEquals("oops"))
+ */
+class WhatEquals {
+public:
+ explicit WhatEquals(const std::string& message)
+ : mMessage(message) {}
+
+ template <typename T>
+ bool operator()(const T& e)
+ {
+ BOOST_WARN_EQUAL(e.what(), mMessage); // additional failure info
+ return e.what() == mMessage;
+ }
+private:
+ std::string mMessage;
+};
+
#endif // UNIT_TESTS_UT_HPP
BOOST_AUTO_TEST_CASE(ReadFileContentTest)
{
BOOST_CHECK_EQUAL(FILE_CONTENT, readFileContent(FILE_PATH));
- BOOST_CHECK_THROW(readFileContent(BUGGY_FILE_PATH), UtilsException);
+ BOOST_CHECK_EXCEPTION(readFileContent(BUGGY_FILE_PATH),
+ UtilsException,
+ WhatEquals("Read failed"));
}
BOOST_AUTO_TEST_CASE(SaveFileContentTest)
BOOST_CHECK_EQUAL(readFileContent(dst_inner2 + "/" + FILE_NAME_RANDOM_1), FILE_CONTENT_3);
BOOST_CHECK_EQUAL(readFileContent(dst_inner2 + "/" + FILE_NAME_RANDOM_2), FILE_CONTENT_2);
- fs::file_status st;
- BOOST_REQUIRE_NO_THROW(st = fs::status(fs::path(dst_inner2)));
+ fs::file_status st = fs::status(fs::path(dst_inner2));
BOOST_CHECK(fs::owner_read == st.permissions());
}
{
ValueLatch<int> testLatch;
- BOOST_REQUIRE_THROW(testLatch.get(EXPECTED_TIMEOUT), vasum::UtilsException);
+ BOOST_REQUIRE_EXCEPTION(testLatch.get(EXPECTED_TIMEOUT),
+ vasum::UtilsException,
+ WhatEquals("Timeout occured"));
}
BOOST_AUTO_TEST_CASE(MultipleSetTest)
ValueLatch<int> testLatch;
testLatch.set(3);
- BOOST_REQUIRE_THROW(testLatch.set(2), vasum::UtilsException);
+ BOOST_REQUIRE_EXCEPTION(testLatch.set(2),
+ vasum::UtilsException,
+ WhatEquals("Cannot set value multiple times"));
}
BOOST_AUTO_TEST_CASE(MultipleGetTest)
testLatch.set(3);
testLatch.get(TIMEOUT);
- BOOST_REQUIRE_THROW(testLatch.get(EXPECTED_TIMEOUT), vasum::UtilsException);
+ BOOST_REQUIRE_EXCEPTION(testLatch.get(EXPECTED_TIMEOUT),
+ vasum::UtilsException,
+ WhatEquals("Timeout occured"));
}
BOOST_AUTO_TEST_SUITE_END()
*/
struct ZoneDaemonException: public VasumException {
- ZoneDaemonException(const std::string& error = "") : VasumException(error) {}
+ ZoneDaemonException(const std::string& error) : VasumException(error) {}
};