mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-03 17:59:27 +02:00
Merge pull request #1509 from jonny5532/feature/battery-can-aliveness-test
Add tests to make sure batteries aren't renewing liveness on bogus CAN frames
This commit is contained in:
commit
8354e554e4
3 changed files with 84 additions and 0 deletions
|
@ -70,6 +70,7 @@ add_executable(tests
|
||||||
tests.cpp
|
tests.cpp
|
||||||
safety_tests.cpp
|
safety_tests.cpp
|
||||||
battery/NissanLeafTest.cpp
|
battery/NissanLeafTest.cpp
|
||||||
|
battery/still_alive_tests.cpp
|
||||||
can_log_based/canlog_safety_tests.cpp
|
can_log_based/canlog_safety_tests.cpp
|
||||||
utils/utils.cpp
|
utils/utils.cpp
|
||||||
../Software/src/communication/can/obd.cpp
|
../Software/src/communication/can/obd.cpp
|
||||||
|
|
81
test/battery/still_alive_tests.cpp
Normal file
81
test/battery/still_alive_tests.cpp
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
|
||||||
|
#include "../../Software/src/battery/BATTERIES.h"
|
||||||
|
#include "../../Software/src/devboard/utils/events.h"
|
||||||
|
|
||||||
|
class BatteryTestFixture : public testing::Test {
|
||||||
|
public:
|
||||||
|
BatteryTestFixture(BatteryType type) : type(type) {}
|
||||||
|
// Optional:
|
||||||
|
// static void SetUpTestSuite() { ... }
|
||||||
|
// static void TearDownTestSuite() { ... }
|
||||||
|
|
||||||
|
void SetUp() override {
|
||||||
|
// Reset the datalayer and events before each test
|
||||||
|
datalayer = DataLayer();
|
||||||
|
reset_all_events();
|
||||||
|
if (battery) {
|
||||||
|
delete battery;
|
||||||
|
battery = nullptr;
|
||||||
|
}
|
||||||
|
init_hal();
|
||||||
|
|
||||||
|
user_selected_battery_type = type;
|
||||||
|
setup_battery();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override {
|
||||||
|
if (battery) {
|
||||||
|
delete battery;
|
||||||
|
battery = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
BatteryType type;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check that the parsed logs correctly trigger an overvoltage event.
|
||||||
|
class StillAliveTimeoutTest : public BatteryTestFixture {
|
||||||
|
public:
|
||||||
|
explicit StillAliveTimeoutTest(BatteryType type) : BatteryTestFixture(type) {}
|
||||||
|
void TestBody() override {
|
||||||
|
// check if battery is a CanBattery subclass
|
||||||
|
auto* battery = dynamic_cast<CanBattery*>(::battery);
|
||||||
|
if (battery == nullptr) {
|
||||||
|
GTEST_SKIP() << "Battery is not a CanBattery subclass";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the still-alive counter to 0 (ie, not alive)
|
||||||
|
datalayer.battery.status.CAN_battery_still_alive = 0;
|
||||||
|
|
||||||
|
// A random fake CAN frame
|
||||||
|
CAN_frame frame = {
|
||||||
|
.ID = 0x7ff,
|
||||||
|
.data = {.u8 = {0x00, 0x64, 0x00, 0x64, 0x0F, 0xA0, 0x27, 0x10}},
|
||||||
|
};
|
||||||
|
for (int i = 0; i < 50; i++) {
|
||||||
|
battery->handle_incoming_can_frame(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check it's still not alive (ie, the CAN frame handling didn't renew the counter)
|
||||||
|
EXPECT_EQ(datalayer.battery.status.CAN_battery_still_alive, 0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void RegisterStillAliveTests() {
|
||||||
|
for (int i = 2; i < 42; i++) {
|
||||||
|
if ((BatteryType)i == BatteryType::TestFake) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ((BatteryType)i == BatteryType::DalyBms) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string test_name = ("TestStillAliveTimeout" + snake_case_to_camel_case(name_for_battery_type((BatteryType)i)));
|
||||||
|
testing::RegisterTest("StillAliveTests", test_name.c_str(), nullptr, nullptr, __FILE__, __LINE__,
|
||||||
|
[=]() -> BatteryTestFixture* { return new StillAliveTimeoutTest((BatteryType)i); });
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,10 +6,12 @@
|
||||||
#include "../Software/src/devboard/utils/events.h"
|
#include "../Software/src/devboard/utils/events.h"
|
||||||
|
|
||||||
void RegisterCanLogTests(void);
|
void RegisterCanLogTests(void);
|
||||||
|
void RegisterStillAliveTests(void);
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
testing::InitGoogleTest(&argc, argv);
|
testing::InitGoogleTest(&argc, argv);
|
||||||
RegisterCanLogTests();
|
RegisterCanLogTests();
|
||||||
|
RegisterStillAliveTests();
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue