Skip to content

Commit aa454bb

Browse files
committed
When the writing to the file on the OTA storage device for the update file fails then the OTA logic should transition to the OTAState::Error state and set OTAError::StorageWritedFailed
1 parent 539ace2 commit aa454bb

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

extras/test/src/test_OTALogic.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ TEST_CASE("OTAStorage opening of storage file fails ", "[OTAStorage::open() -> r
114114
}
115115
}
116116

117+
118+
/**************************************************************************************/
119+
120+
TEST_CASE("OTAStorage writing to storage file fails ", "[OTAStorage::write() -> fails]")
121+
{
122+
Mock<OTAStorage> ota_storage;
123+
124+
/* Configure mock object */
125+
When(Method(ota_storage, init)).Return(true);
126+
When(Method(ota_storage, open)).Return(true);
127+
When(Method(ota_storage, write)).AlwaysDo([](uint8_t const * const /* buf */, size_t const /* num_bytes */) -> size_t { return 0 /* should return num_bytes in case of succes */;});
128+
Fake(Method(ota_storage, close));
129+
Fake(Method(ota_storage, remove));
130+
Fake(Method(ota_storage, deinit));
131+
132+
133+
/* Perform test */
134+
OTALogic ota_logic(ota_storage.get());
135+
136+
WHEN("OTALogic::update() is called and some bytes have been received")
137+
{
138+
uint8_t const SOME_FAKE_DATA[16] = {0};
139+
ota_logic.onOTADataReceived(SOME_FAKE_DATA, sizeof(SOME_FAKE_DATA));
140+
ota_logic.update();
141+
142+
THEN("The OTA logic should be in the 'Error' state")
143+
{
144+
REQUIRE(ota_logic.state() == OTAState::Error);
145+
}
146+
THEN("The OTA error should be set to OTAError::StorageWriteFailed")
147+
{
148+
REQUIRE(ota_logic.error() == OTAError::StorageWriteFailed);
149+
}
150+
}
151+
}
152+
117153
/**************************************************************************************/
118154

119155
TEST_CASE("Valid OTA data is received ", "[OTALogic]")

src/utility/ota/OTALogic.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ OTAState OTALogic::handle_WaitForBinary()
174174
OTAState OTALogic::handle_BinaryReceived()
175175
{
176176
/* Write to OTA storage */
177-
if(_ota_storage.write(_mqtt_ota_buf.buf, _mqtt_ota_buf.num_bytes) != _mqtt_ota_buf.num_bytes) {
177+
if(_ota_storage.write(_mqtt_ota_buf.buf, _mqtt_ota_buf.num_bytes) != _mqtt_ota_buf.num_bytes)
178+
{
179+
_ota_error = OTAError::StorageWriteFailed;
178180
return OTAState::Error;
179181
}
180182

src/utility/ota/OTALogic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum class OTAState
4343

4444
enum class OTAError
4545
{
46-
None, StorageInitFailed, StorageOpenFailed, ChecksumMismatch
46+
None, StorageInitFailed, StorageOpenFailed, StorageWriteFailed, ChecksumMismatch
4747
};
4848

4949
/******************************************************************************

0 commit comments

Comments
 (0)