Skip to content

Commit 248ce9c

Browse files
committed
Extending unit test code to incorporate the extra step
1 parent 44acce9 commit 248ce9c

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

extras/test/src/test_OTALogic.cpp

+54-4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ TEST_CASE("OTAStorage initialisation fails", "[OTAStorage::init() -> returns fal
5959
Fake(Method(ota_storage, write));
6060
Fake(Method(ota_storage, close));
6161
Fake(Method(ota_storage, remove));
62+
Fake(Method(ota_storage, rename));
6263
Fake(Method(ota_storage, deinit));
6364

6465

@@ -92,6 +93,7 @@ TEST_CASE("OTAStorage opening of storage file fails", "[OTAStorage::open() -> re
9293
Fake(Method(ota_storage, write));
9394
Fake(Method(ota_storage, close));
9495
Fake(Method(ota_storage, remove));
96+
Fake(Method(ota_storage, rename));
9597
Fake(Method(ota_storage, deinit));
9698

9799

@@ -129,6 +131,7 @@ TEST_CASE("OTAStorage writing to storage file fails", "[OTAStorage::write() -> f
129131
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 success */;});
130132
Fake(Method(ota_storage, close));
131133
Fake(Method(ota_storage, remove));
134+
Fake(Method(ota_storage, rename));
132135
Fake(Method(ota_storage, deinit));
133136

134137

@@ -165,6 +168,7 @@ TEST_CASE("Data overrun due to receiving too much data", "[OTALogic - Data Overr
165168
When(Method(ota_storage, write)).AlwaysDo([](uint8_t const * const /* buf */, size_t const num_bytes) -> size_t { return num_bytes; });
166169
Fake(Method(ota_storage, close));
167170
Fake(Method(ota_storage, remove));
171+
Fake(Method(ota_storage, rename));
168172
Fake(Method(ota_storage, deinit));
169173

170174

@@ -206,9 +210,10 @@ TEST_CASE("Valid OTA data is received ", "[OTALogic]")
206210
std::copy(buf, buf + num_bytes, std::back_inserter(ota_binary_data));
207211
return num_bytes;
208212
});
209-
Fake(Method(ota_storage, close));
210-
Fake(Method(ota_storage, remove));
211-
Fake(Method(ota_storage, deinit));
213+
Fake(Method(ota_storage, close));
214+
Fake(Method(ota_storage, remove));
215+
When(Method(ota_storage, rename)).Return(true);
216+
Fake(Method(ota_storage, deinit));
212217

213218

214219
/* Generate test data */
@@ -231,6 +236,11 @@ TEST_CASE("Valid OTA data is received ", "[OTALogic]")
231236
valid_ota_test_data.data.bin));
232237
}
233238

239+
THEN("The temporary file UPDATE.BIN.TMP should have been renamed to UPDATE.BIN")
240+
{
241+
Verify(Method(ota_storage, rename)).Once();
242+
}
243+
234244
THEN("The OTA logic should be in the 'Reset' state")
235245
{
236246
REQUIRE(ota_logic.state() == OTAState::Reset);
@@ -244,6 +254,45 @@ TEST_CASE("Valid OTA data is received ", "[OTALogic]")
244254

245255
/**************************************************************************************/
246256

257+
TEST_CASE("Valid OTA data is received but the rename step failed (identical too device being turned off during writing of file)", "[OTALogic - Rename fail]")
258+
{
259+
Mock<OTAStorage> ota_storage;
260+
261+
/* Configure mock object */
262+
When(Method(ota_storage, init)).Return(true);
263+
When(Method(ota_storage, open)).Return(true);
264+
When(Method(ota_storage, write)).AlwaysDo([](uint8_t const * const /* buf */, size_t const num_bytes) -> size_t { return num_bytes; });
265+
Fake(Method(ota_storage, close));
266+
Fake(Method(ota_storage, remove));
267+
When(Method(ota_storage, rename)).Return(false);
268+
Fake(Method(ota_storage, deinit));
269+
270+
271+
/* Generate test data */
272+
ota::OTAData valid_ota_test_data;
273+
ota::generate_valid_ota_data(valid_ota_test_data);
274+
275+
276+
/* Perform test */
277+
OTALogic ota_logic;
278+
ota_logic.setOTAStorage(ota_storage.get());
279+
simulateOTABinaryReception(ota_logic, valid_ota_test_data);
280+
281+
282+
/* Perform checks */
283+
THEN("The OTA logic should be in the 'Error' state")
284+
{
285+
REQUIRE(ota_logic.state() == OTAState::Error);
286+
}
287+
288+
THEN("The OTA error should be set to OTAError::RenameOfTempFileFailed")
289+
{
290+
REQUIRE(ota_logic.error() == OTAError::RenameOfTempFileFailed);
291+
}
292+
}
293+
294+
/**************************************************************************************/
295+
247296
TEST_CASE("Invalid OTA data is received ", "[OTALogic - CRC wrong]")
248297
{
249298
Mock<OTAStorage> ota_storage;
@@ -255,6 +304,7 @@ TEST_CASE("Invalid OTA data is received ", "[OTALogic - CRC wrong]")
255304
When(Method(ota_storage, write)).AlwaysDo([](uint8_t const * const /* buf */, size_t const num_bytes) -> size_t { return num_bytes; });
256305
Fake(Method(ota_storage, close));
257306
Fake(Method(ota_storage, remove));
307+
Fake(Method(ota_storage, rename));
258308
Fake(Method(ota_storage, deinit));
259309

260310

@@ -268,7 +318,7 @@ TEST_CASE("Invalid OTA data is received ", "[OTALogic - CRC wrong]")
268318
ota_logic.setOTAStorage(ota_storage.get());
269319
simulateOTABinaryReception(ota_logic, invalid_valid_ota_test_data_crc_wrong);
270320

271-
321+
272322
/* Perform checks */
273323
THEN("there should be no binary file be stored on the OTA storage")
274324
{

0 commit comments

Comments
 (0)