|
6 | 6 | INCLUDE
|
7 | 7 | **************************************************************************************/
|
8 | 8 |
|
| 9 | +#include <algorithm> |
| 10 | + |
9 | 11 | #include <catch.hpp>
|
10 | 12 |
|
| 13 | +#include <OTATestData.h> |
11 | 14 | #include <OTAStorage_Mock.h>
|
12 | 15 |
|
13 | 16 | #include <OTALogic.h>
|
14 | 17 |
|
15 | 18 | /**************************************************************************************
|
16 |
| - TEST CODE |
| 19 | + TEST HELPER |
17 | 20 | **************************************************************************************/
|
18 | 21 |
|
19 |
| -TEST_CASE("A OTALogic object is instantiated", "[OTALogic::OTALogic]") |
| 22 | +void simulateOTABinaryReception(OTALogic & ota_logic, OTAStorage_Mock & ota_storage, OTAData const & ota_test_data) |
20 | 23 | {
|
21 |
| - OTALogic ota_logic; |
22 |
| - |
23 |
| - REQUIRE(ota_logic.state() == OTAState::Init); |
| 24 | + uint32_t bytes_written = 0; |
| 25 | + uint32_t const bytes_to_write = sizeof(uint32_t) + sizeof(uint32_t) + ota_test_data.data.len; |
| 26 | + for(; bytes_written < (bytes_to_write - MQTT_OTA_BUF_SIZE); bytes_written += MQTT_OTA_BUF_SIZE) |
| 27 | + { |
| 28 | + ota_logic.onOTADataReceived(ota_test_data.buf + bytes_written, MQTT_OTA_BUF_SIZE); |
| 29 | + ota_logic.update(&ota_storage); |
| 30 | + } |
24 | 31 |
|
25 |
| - WHEN("update(nullptr) is run") |
| 32 | + if(bytes_written < bytes_to_write) |
26 | 33 | {
|
27 |
| - ota_logic.update(nullptr); |
28 |
| - THEN("the logic shall transitition to the Error state") { |
29 |
| - REQUIRE(ota_logic.state() == OTAState::Error); |
30 |
| - } |
| 34 | + uint32_t const remaining_bytes = (bytes_to_write - bytes_written); |
| 35 | + ota_logic.onOTADataReceived(ota_test_data.buf + bytes_written, remaining_bytes); |
| 36 | + ota_logic.update(&ota_storage); |
31 | 37 | }
|
32 | 38 | }
|
33 | 39 |
|
34 |
| -TEST_CASE("A OTALogic object checks is valid OTAStorage is available", "[OTALogic::update-1]") |
| 40 | +/************************************************************************************** |
| 41 | + TEST CODE |
| 42 | + **************************************************************************************/ |
| 43 | + |
| 44 | +TEST_CASE("A valid OTA binary is received ", "[OTALogic - valid data]") |
35 | 45 | {
|
36 | 46 | OTALogic ota_logic;
|
| 47 | + OTAData valid_ota_test_data; |
37 | 48 | OTAStorage_Mock ota_storage;
|
38 | 49 |
|
39 |
| - WHEN("update(&ota_storage) is run") |
| 50 | + ota_storage._init_return_val = true; |
| 51 | + ota_storage._open_return_val = true; |
| 52 | + |
| 53 | + generate_valid_ota_data(valid_ota_test_data); |
| 54 | + |
| 55 | + simulateOTABinaryReception(ota_logic, ota_storage, valid_ota_test_data); |
| 56 | + |
| 57 | + THEN("the complete binary file should have been written to the OTA storage") |
| 58 | + { |
| 59 | + REQUIRE(ota_storage._binary.size() == valid_ota_test_data.data.len); |
| 60 | + REQUIRE(std::equal(ota_storage._binary.begin(), |
| 61 | + ota_storage._binary.end(), |
| 62 | + valid_ota_test_data.data.bin)); |
| 63 | + } |
| 64 | + |
| 65 | + THEN("The OTA logic should be in the 'Reset' state") |
40 | 66 | {
|
41 |
| - WHEN("OTAStorage::init() succeeds") |
42 |
| - { |
43 |
| - ota_storage._init_return_val = true; |
44 |
| - ota_logic.update(&ota_storage); |
45 |
| - THEN("the logic shall transitition to the Idle state") REQUIRE(ota_logic.state() == OTAState::Idle); |
46 |
| - } |
47 |
| - WHEN("OTAStorage::init() fails") |
48 |
| - { |
49 |
| - ota_storage._init_return_val = false; |
50 |
| - ota_logic.update(&ota_storage); |
51 |
| - THEN("the logic shall transitition to the Error state") REQUIRE(ota_logic.state() == OTAState::Error); |
52 |
| - } |
| 67 | + REQUIRE(ota_logic.state() == OTAState::Reset); |
53 | 68 | }
|
54 | 69 | }
|
0 commit comments