Skip to content

Commit 03201de

Browse files
authored
OTA PAL test: allow more return values for otaPal_Abort and fix some test cases. (#53)
* Allow more possible return values for otaPal_Abort and fix otaPal_WriteBlock_WriteManyBlocks.
1 parent 989e3a6 commit 03201de

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/ota/ota_pal_test.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ TEST_TEAR_DOWN( Full_OTA_PAL )
8181
{
8282
OtaPalStatus_t xOtaStatus;
8383

84-
/* Abort the OTA file after every test. This closes the OTA file. */
84+
/* Abort the OTA file after every test. This closes the OTA file.
85+
* otaPal_Abort should only return OtaPalSuccess or OtaPalAbortFailed. */
8586
xOtaStatus = otaPal_Abort( &xOtaFile );
86-
87-
TEST_ASSERT_EQUAL_MESSAGE( OtaPalSuccess, xOtaStatus, "Failed to abort xOtaFile" );
87+
TEST_ASSERT( OTA_PAL_MAIN_ERR( xOtaStatus ) == OtaPalSuccess ||
88+
OTA_PAL_MAIN_ERR( xOtaStatus ) == OtaPalAbortFailed );
8889
}
8990

9091
TEST_GROUP_RUNNER( Full_OTA_PAL )
@@ -213,6 +214,7 @@ TEST( Full_OTA_PAL, otaPal_CloseFile_InvalidSignatureNoBlockWritten )
213214

214215
/* Create a local file using the PAL. */
215216
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
217+
xOtaFile.fileSize = sizeof( ucDummyData );
216218
xOtaStatus = otaPal_CreateFileForRx( &xOtaFile );
217219
TEST_ASSERT_EQUAL( OtaPalSuccess, OTA_PAL_MAIN_ERR( xOtaStatus ) );
218220

@@ -289,6 +291,7 @@ TEST( Full_OTA_PAL, otaPal_CreateFileForRx_CreateAnyFile )
289291

290292
/* TEST: Create a local file using the PAL. Verify error in close. */
291293
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
294+
xOtaFile.fileSize = sizeof( ucDummyData );
292295
xOtaStatus = otaPal_CreateFileForRx( &xOtaFile );
293296
TEST_ASSERT_EQUAL( OtaPalSuccess, OTA_PAL_MAIN_ERR( xOtaStatus ) );
294297
}
@@ -300,7 +303,10 @@ TEST( Full_OTA_PAL, otaPal_Abort_OpenFile )
300303
{
301304
OtaPalStatus_t xOtaStatus;
302305

306+
memset( &xOtaFile, 0, sizeof( OtaFileContext_t ) );
307+
303308
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
309+
xOtaFile.fileSize = sizeof( ucDummyData );
304310

305311
/* Create a local file using the PAL. */
306312
xOtaStatus = otaPal_CreateFileForRx( &xOtaFile );
@@ -356,8 +362,8 @@ TEST( Full_OTA_PAL, otaPal_Abort_NullFileHandle )
356362
OtaPalStatus_t xOtaStatus;
357363

358364
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
359-
360365
xOtaFile.pFile = 0;
366+
361367
xOtaStatus = otaPal_Abort( &xOtaFile );
362368
TEST_ASSERT_EQUAL_INT( OtaPalSuccess, OTA_PAL_MAIN_ERR( xOtaStatus ) );
363369
}
@@ -369,15 +375,14 @@ TEST( Full_OTA_PAL, otaPal_Abort_NonExistentFile )
369375
{
370376
OtaPalStatus_t xOtaStatus;
371377

372-
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
373-
374378
xOtaFile.pFilePath = ( uint8_t * ) ( "nonexistingfile.bin" );
379+
375380
xOtaStatus = otaPal_Abort( &xOtaFile );
376381
TEST_ASSERT_EQUAL_INT( OtaPalSuccess, OTA_PAL_MAIN_ERR( xOtaStatus ) );
377382
}
378383

379384
/**
380-
* Write one byte of data and verify success.
385+
* @brief Write one byte of data and verify success.
381386
*/
382387
TEST( Full_OTA_PAL, otaPal_WriteBlock_WriteSingleByte )
383388
{
@@ -387,6 +392,7 @@ TEST( Full_OTA_PAL, otaPal_WriteBlock_WriteSingleByte )
387392

388393
/* TEST: Write a byte of data. */
389394
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
395+
xOtaFile.fileSize = sizeof( ucDummyData );
390396
xOtaStatus = otaPal_CreateFileForRx( &xOtaFile );
391397
TEST_ASSERT_EQUAL( OtaPalSuccess, OTA_PAL_MAIN_ERR( xOtaStatus ) );
392398

@@ -399,14 +405,22 @@ TEST( Full_OTA_PAL, otaPal_WriteBlock_WriteSingleByte )
399405

400406
/**
401407
* @brief Write many blocks of data to a file opened in the device. Verify success.
408+
*
409+
* Because of Flash property, we might not able to write same page multiple times.
410+
* So we write one block into one page.
402411
*/
403412
TEST( Full_OTA_PAL, otaPal_WriteBlock_WriteManyBlocks )
404413
{
405414
OtaPalStatus_t xOtaStatus;
406415
int16_t bytesWritten;
407416

417+
/* The page size must >= dummy data size, so that we can write whole dummy data in one operation. */
418+
TEST_ASSERT_LESS_OR_EQUAL( testParam.pageSize, sizeof( ucDummyData ) );
419+
420+
/* Some platforms compare the offset and file size, and it's not legal to write when offset > filesize.
421+
* We just set file size to the number of block size to make sure file size is big enough for every write. */
408422
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
409-
xOtaFile.fileSize = sizeof( ucDummyData ) * testotapalNUM_WRITE_BLOCKS;
423+
xOtaFile.fileSize = testParam.pageSize * testotapalNUM_WRITE_BLOCKS;
410424
/* TEST: Write many bytes of data. */
411425

412426
xOtaFile.pFilePath = ( uint8_t * ) OTA_PAL_FIRMWARE_FILE;
@@ -416,14 +430,15 @@ TEST( Full_OTA_PAL, otaPal_WriteBlock_WriteManyBlocks )
416430
if( TEST_PROTECT() )
417431
{
418432
int lIndex = 0;
433+
uint32_t writeOffset = 0;
419434

420435
for( lIndex = 0; lIndex < testotapalNUM_WRITE_BLOCKS; lIndex++ )
421436
{
422-
uint32_t writeOffset = lIndex * sizeof( ucDummyData );
423-
/* Align the writeOff with page size */
424-
writeOffset = writeOffset + testParam.pageSize - ( writeOffset % testParam.pageSize );
425437
bytesWritten = otaPal_WriteBlock( &xOtaFile, writeOffset, ucDummyData, sizeof( ucDummyData ) );
426438
TEST_ASSERT_EQUAL_INT( sizeof( ucDummyData ), bytesWritten );
439+
440+
/* Align the writeOff with page size */
441+
writeOffset = writeOffset + testParam.pageSize;
427442
}
428443
}
429444
}

0 commit comments

Comments
 (0)