#include "TestActionHelper.h" #include #include #include #include #include "mocks/MessageQueueMockBase.h" TEST_CASE("Action Helper", "[ActionHelper]") { MessageQueueMockBase testMqMock; ActionHelperOwnerMockBase testDhMock(&testMqMock); CommandMessage actionMessage; ActionId_t testActionId = (ActionId_t) TestActions::TEST_ACTION; std::array testParams{1, 2, 3}; store_address_t paramAddress; StorageManagerIF* ipcStore = tglob::getIpcStoreHandle(); REQUIRE(ipcStore != nullptr); ipcStore->addData(¶mAddress, testParams.data(), 3); REQUIRE(testDhMock.getActionHelper()->initialize() == retval::CATCH_OK); SECTION("Simple tests") { ActionMessage::setCommand(&actionMessage, testActionId, paramAddress); CHECK(not testDhMock.executeActionCalled); REQUIRE(testDhMock.getActionHelper()->handleActionMessage(&actionMessage) == retval::CATCH_OK); CHECK(testDhMock.executeActionCalled); // No message is sent if everything is alright. CHECK(not testMqMock.wasMessageSent()); store_address_t invalidAddress; ActionMessage::setCommand(&actionMessage, testActionId, invalidAddress); testDhMock.getActionHelper()->handleActionMessage(&actionMessage); CHECK(testMqMock.wasMessageSent()); const uint8_t* ptr = nullptr; size_t size = 0; REQUIRE(ipcStore->getData(paramAddress, &ptr, &size) == static_cast(StorageManagerIF::DATA_DOES_NOT_EXIST)); REQUIRE(ptr == nullptr); testDhMock.getBuffer(&ptr); for (uint8_t i = 0; i < 3; i++) { REQUIRE(ptr[i] == (i + 1)); } testDhMock.clearBuffer(); } SECTION("Handle failures") { actionMessage.setCommand(1234); REQUIRE(testDhMock.getActionHelper()->handleActionMessage(&actionMessage) == static_cast(CommandMessage::UNKNOWN_COMMAND)); CHECK(not testMqMock.wasMessageSent()); uint16_t step = 5; ReturnValue_t status = 0x1234; testDhMock.getActionHelper()->step(step, testMqMock.getId(), testActionId, status); step += 1; CHECK(testMqMock.wasMessageSent()); CommandMessage testMessage; REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::STEP_FAILED)); REQUIRE(testMessage.getParameter() == static_cast(testActionId)); uint32_t parameter2 = ((uint32_t)step << 16) | (uint32_t)status; REQUIRE(testMessage.getParameter2() == parameter2); REQUIRE(ActionMessage::getStep(&testMessage) == step); } SECTION("Handle finish") { CHECK(not testMqMock.wasMessageSent()); ReturnValue_t status = 0x9876; testDhMock.getActionHelper()->finish(false, testMqMock.getId(), testActionId, status); CHECK(testMqMock.wasMessageSent()); CommandMessage testMessage; REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::COMPLETION_FAILED)); REQUIRE(ActionMessage::getActionId(&testMessage) == testActionId); REQUIRE(ActionMessage::getReturnCode(&testMessage) == static_cast(status)); } SECTION("Handle failed") { store_address_t toLongParamAddress = StorageManagerIF::INVALID_ADDRESS; std::array toLongData = {5, 4, 3, 2, 1}; REQUIRE(ipcStore->addData(&toLongParamAddress, toLongData.data(), 5) == retval::CATCH_OK); ActionMessage::setCommand(&actionMessage, testActionId, toLongParamAddress); CHECK(not testDhMock.executeActionCalled); REQUIRE(testDhMock.getActionHelper()->handleActionMessage(&actionMessage) == retval::CATCH_OK); REQUIRE(ipcStore->getData(toLongParamAddress).first == static_cast(StorageManagerIF::DATA_DOES_NOT_EXIST)); CommandMessage testMessage; REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::STEP_FAILED)); REQUIRE(ActionMessage::getReturnCode(&testMessage) == HasActionsIF::INVALID_PARAMETERS); REQUIRE(ActionMessage::getStep(&testMessage) == 0); REQUIRE(ActionMessage::getActionId(&testMessage) == testActionId); } SECTION("Missing IPC Data") { ActionMessage::setCommand(&actionMessage, testActionId, StorageManagerIF::INVALID_ADDRESS); CHECK(not testDhMock.executeActionCalled); REQUIRE(testDhMock.getActionHelper()->handleActionMessage(&actionMessage) == retval::CATCH_OK); CommandMessage testMessage; REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::STEP_FAILED)); REQUIRE(ActionMessage::getReturnCode(&testMessage) == static_cast(StorageManagerIF::ILLEGAL_STORAGE_ID)); REQUIRE(ActionMessage::getStep(&testMessage) == 0); } SECTION("Data Reply") {} }