eive-obsw/unittest/rebootLogic/main.cpp

117 lines
3.8 KiB
C++
Raw Normal View History

2022-02-28 14:13:31 +01:00
#include "CoreController.h"
2022-02-28 19:52:43 +01:00
#include "libxiphos.h"
#include "HasActionsIF.h"
#include "event.h"
2022-02-28 16:05:32 +01:00
#include <catch2/catch_test_macros.hpp>
#include <iostream>
2022-02-28 16:47:36 +01:00
#include <fstream>
#include <filesystem>
2022-02-28 19:52:43 +01:00
static constexpr bool CAT_FILE_TO_CONSOLE = false;
2022-02-28 16:47:36 +01:00
const std::string CONF_PATH = "/tmp/conf";
const std::string REBOOT_FILE = CONF_PATH + "/reboot.txt";
void catFileToConsole();
2022-02-28 16:05:32 +01:00
TEST_CASE( "Core Controller Reboot File Handling", "[reboot-file]" ) {
2022-02-28 19:52:43 +01:00
if(not std::filesystem::exists(CONF_PATH)) {
std::filesystem::create_directory(CONF_PATH);
}
CoreController ctrl;
2022-02-28 16:47:36 +01:00
2022-02-28 19:52:43 +01:00
SECTION ("Primary") {
xsc_libnor_chip_t chip;
xsc_libnor_copy_t copy;
RebootFile rf = {};
ctrl.rewriteRebootFile(rf);
REQUIRE(rf.enabled == 0);
REQUIRE(rf.maxCount == RebootFile::DEFAULT_MAX_BOOT_CNT);
REQUIRE(rf.img00Cnt == 0);
REQUIRE(rf.img01Cnt == 0);
REQUIRE(rf.img10Cnt == 0);
REQUIRE(rf.img11Cnt == 0);
REQUIRE(rf.bootFlag == 0);
REQUIRE(rf.lastChip == 0);
REQUIRE(rf.lastCopy == 0);
// This recreates the file but should also increment the boot counter of the current image
REQUIRE(ctrl.CURRENT_CHIP == xsc::CHIP_0);
REQUIRE(ctrl.CURRENT_COPY == xsc::COPY_0);
ctrl.performRebootFileHandling(true);
2022-02-28 16:47:36 +01:00
catFileToConsole();
ctrl.parseRebootFile(REBOOT_FILE, rf);
REQUIRE(rf.enabled == 1);
2022-02-28 19:52:43 +01:00
REQUIRE(rf.maxCount == RebootFile::DEFAULT_MAX_BOOT_CNT);
REQUIRE(rf.img00Cnt == 1);
REQUIRE(rf.img01Cnt == 0);
REQUIRE(rf.img10Cnt == 0);
REQUIRE(rf.img11Cnt == 0);
REQUIRE(rf.bootFlag == 0);
REQUIRE(rf.lastChip == 0);
REQUIRE(rf.lastCopy == 0);
uint8_t newRebootCnt = 3;
CHECK(ctrl.executeAction(CoreController::SET_MAX_REBOOT_CNT, 0, &newRebootCnt, 1) == HasActionsIF::EXECUTION_FINISHED);
ctrl.parseRebootFile(REBOOT_FILE, rf);
REQUIRE(rf.enabled == 1);
REQUIRE(rf.maxCount == 3);
REQUIRE(not getRebootWasCalled(chip, copy));
ctrl.performRebootFileHandling(false);
ctrl.performRebootFileHandling(false);
ctrl.parseRebootFile(REBOOT_FILE, rf);
2022-02-28 16:47:36 +01:00
REQUIRE(rf.enabled == 1);
2022-02-28 19:52:43 +01:00
REQUIRE(rf.img00Cnt == 3);
REQUIRE(rf.img01Cnt == 0);
REQUIRE(rf.img10Cnt == 0);
REQUIRE(rf.img11Cnt == 0);
REQUIRE(rf.bootFlag == true);
REQUIRE(rf.lastChip == xsc::CHIP_0);
REQUIRE(rf.lastCopy == xsc::COPY_0);
REQUIRE(getRebootWasCalled(chip, copy));
REQUIRE(chip == XSC_LIBNOR_CHIP_0);
REQUIRE(copy == XSC_LIBNOR_COPY_GOLD);
EventInfo info = {};
uint32_t numEvents = 0;
ctrl.performRebootFileHandling(false);
eventWasCalled(info, numEvents);
CHECK(numEvents == 1);
CHECK(info.event == CoreController::REBOOT_MECHANISM_TRIGGERED);
CHECK(static_cast<xsc::Chip>((info.p1 >> 16) & 0xFFFF) == xsc::CHIP_0);
CHECK(static_cast<xsc::Copy>(info.p1 & 0xFFFF) == xsc::COPY_0);
CHECK(((info.p2 >> 24) & 0xFF) == 3);
CHECK(((info.p2 >> 16) & 0xFF) == 1);
CHECK(((info.p2 >> 8) & 0xFF) == 0);
CHECK((info.p2 & 0xFF) == 0);
ctrl.parseRebootFile(REBOOT_FILE, rf);
REQUIRE(rf.enabled == 1);
REQUIRE(rf.img00Cnt == 3);
REQUIRE(rf.img01Cnt == 1);
REQUIRE(rf.img10Cnt == 0);
REQUIRE(rf.img11Cnt == 0);
// Flag was cleared when event was thrown
REQUIRE(rf.bootFlag == false);
REQUIRE(rf.lastChip == xsc::CHIP_0);
REQUIRE(rf.lastCopy == xsc::COPY_0);
ctrl.performRebootFileHandling(false);
ctrl.performRebootFileHandling(false);
ctrl.parseRebootFile(REBOOT_FILE, rf);
REQUIRE(rf.enabled == 1);
REQUIRE(rf.img00Cnt == 3);
REQUIRE(rf.img01Cnt == 3);
REQUIRE(rf.img10Cnt == 0);
REQUIRE(rf.img11Cnt == 0);
}
if(std::filesystem::exists(CONF_PATH)) {
std::uintmax_t n = std::filesystem::remove_all(CONF_PATH);
CHECK(n == 2);
}
}
2022-02-28 16:47:36 +01:00
void catFileToConsole() {
if(CAT_FILE_TO_CONSOLE) {
std::ifstream file(REBOOT_FILE);
if (file.is_open()) {
std::cout << file.rdbuf();
}
}
}