Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_update_test.c Source File

pal_update_test.c

00001 /*******************************************************************************
00002  * Copyright 2016, 2017 ARM Ltd.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *******************************************************************************/
00016 
00017 #include "pal.h"
00018 #include "unity.h"
00019 #include "unity_fixture.h"
00020 #include "mbed_trace.h"
00021 
00022 #include <string.h>
00023 
00024 #define TRACE_GROUP "PAL"
00025 
00026 #define KILOBYTE 1024
00027 
00028 #define FIRST_IMAGE_INDEX        0
00029 
00030 TEST_GROUP(pal_update);
00031 
00032 
00033 palBuffer_t g_writeBuffer = {0};
00034 palBuffer_t g_readBuffer = {0};
00035 palImageHeaderDeails_t g_imageHeader = {0};
00036 uint8_t g_isTestDone;
00037 
00038 
00039 uint8_t numberofBlocks = 0;
00040 
00041 /*! \brief Sanity test the update test are running.
00042 */
00043 TEST_SETUP(pal_update)
00044 {
00045     PAL_LOG_INFO("running new test\r\n");
00046 
00047 }
00048 
00049 typedef enum _updateTestState
00050 {
00051     test_init = 1,
00052     test_write,
00053     test_commit,
00054     test_read
00055 }updateTestState;
00056 
00057 PAL_PRIVATE void stateAdvance(palImageEvents_t state)
00058 {
00059     PAL_PRINTF("Finished event %d\r\n",state);
00060     state++;
00061     PAL_PRINTF("Starting event %d\r\n",state);
00062     int rc = PAL_SUCCESS;
00063     PAL_PRINTF("Write ptr = (%p - %p) read ptr = (%p - %p)\r\n",
00064             g_writeBuffer.buffer,g_writeBuffer.buffer + g_writeBuffer.maxBufferLength,
00065             g_readBuffer.buffer, g_readBuffer.buffer + g_readBuffer.maxBufferLength);
00066     switch (state)
00067     {
00068     case PAL_IMAGE_EVENT_PREPARE:
00069           rc = pal_imagePrepare(FIRST_IMAGE_INDEX, &g_imageHeader);
00070           PAL_PRINTF("pal_imagePrepare returned %d \r\n",rc);
00071           TEST_ASSERT_EQUAL(0, rc);
00072           break;
00073     case PAL_IMAGE_EVENT_WRITE:
00074           rc = pal_imageWrite(FIRST_IMAGE_INDEX, 0, (palConstBuffer_t*)&g_writeBuffer);
00075           PAL_PRINTF("pal_imageWrite returned %d \r\n",rc);
00076           TEST_ASSERT_TRUE(rc >= 0);
00077           break;
00078     case PAL_IMAGE_EVENT_FINALIZE:
00079           rc = pal_imageFinalize(FIRST_IMAGE_INDEX);
00080           PAL_PRINTF("pal_imageFinalize returned %d \r\n",rc);
00081           TEST_ASSERT_TRUE(rc >= 0);
00082           break;
00083     case PAL_IMAGE_EVENT_READTOBUFFER:
00084           rc = pal_imageReadToBuffer(FIRST_IMAGE_INDEX,0,&g_readBuffer);
00085           TEST_ASSERT_TRUE(rc >= 0);
00086           PAL_PRINTF("pal_imageReadToBuffer  with offset %d return %d \r\n",0,rc);
00087           break;
00088     case PAL_IMAGE_EVENT_ACTIVATE:
00089           PAL_PRINTF("Checking the output\r\n");
00090           PAL_PRINTF("\r\ng_readBuffer bufferLength=%" PRIu32 "\r\n",g_readBuffer.maxBufferLength);
00091 
00092 
00093           TEST_ASSERT_EQUAL_MEMORY(g_writeBuffer.buffer,g_readBuffer.buffer,g_readBuffer.maxBufferLength);
00094           PAL_PRINTF("write ptr = %p read ptr = %p\r\n",g_writeBuffer.buffer,g_readBuffer.buffer);
00095 
00096           free(g_readBuffer.buffer);
00097           free(g_writeBuffer.buffer);
00098           pal_imageDeInit();
00099           g_isTestDone = 1;
00100           break;
00101     default:
00102         PAL_PRINTF("Error - this should not happen\r\n");
00103         PAL_PRINTF("Write ptr = %p read ptr = %p\r\n",g_writeBuffer.buffer,g_readBuffer.buffer);
00104         free(g_readBuffer.buffer);
00105         free(g_writeBuffer.buffer);
00106         pal_imageDeInit();
00107         g_isTestDone = 1;
00108     }
00109 }
00110 
00111 
00112 
00113 
00114 
00115 
00116 void printBuffer(uint8_t* buffer, size_t bufSize)
00117 {
00118     size_t i = 0;
00119     PAL_PRINTF("0x");
00120     for (i=0;i < bufSize;i++)
00121     {
00122         PAL_PRINTF("%x",buffer[i]);
00123     }
00124     PAL_PRINTF("\r\n");
00125 }
00126 
00127 
00128 PAL_PRIVATE void fillBuffer(uint8_t* buffer, size_t bufSize)
00129 {
00130     size_t  i = 0;
00131     uint8_t value = 0;
00132     int8_t step = -1;
00133     PAL_PRINTF("Filling buffer size %zu\r\n",bufSize);
00134     for(i=0; i < bufSize ; i++)
00135     {
00136         buffer[i] = value;
00137         if ((0 == value) || (255 == value))
00138         {
00139             step*=-1;
00140         }
00141         value+=step;
00142     }
00143     PAL_PRINTF("Buffer is full\r\n");
00144 
00145 }
00146 
00147 
00148 TEST_TEAR_DOWN(pal_update)
00149 {
00150 }
00151 
00152 
00153 
00154 void pal_update_xK(int sizeInK)
00155 {
00156 
00157   palStatus_t rc = PAL_SUCCESS;
00158   if (!(sizeInK % KILOBYTE))
00159   {
00160       PAL_PRINTF("\n-====== PAL_UPDATE_%dKb ======- \n",sizeInK / KILOBYTE);
00161   }
00162   else
00163   {
00164       PAL_PRINTF("\n-====== PAL_UPDATE_%db ======- \n",sizeInK);
00165   }
00166   uint8_t *writeData = (uint8_t*)malloc(sizeInK);
00167   uint8_t *readData  = (uint8_t*)malloc(sizeInK);
00168 
00169   TEST_ASSERT_TRUE(writeData != NULL);
00170   TEST_ASSERT_TRUE(readData != NULL);
00171 
00172   uint64_t version = 11111111;
00173 
00174   // XXX: the pal_imagePrepare() has hardcoded requirement of hash being SHA256, so it must be given here too
00175   uint8_t hash[SIZEOF_SHA256] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
00176                                   0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
00177                                   0x0c, 0x1c, 0x2c, 0x3c, 0x4c, 0x5c, 0x6c, 0x7c,
00178                                   0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7
00179                                   };
00180 
00181   g_isTestDone = 0;
00182 
00183   g_imageHeader.version = version;
00184 
00185   g_imageHeader.hash.buffer =(uint8_t*)&hash;
00186   g_imageHeader.hash.bufferLength = sizeof(hash);
00187   g_imageHeader.hash.maxBufferLength = sizeof(hash);
00188 
00189   g_imageHeader.imageSize = sizeInK;
00190 
00191   g_writeBuffer.buffer = writeData;
00192   g_writeBuffer.bufferLength = sizeInK;
00193   g_writeBuffer.maxBufferLength = sizeInK;
00194 
00195   PAL_PRINTF("write buffer length %" PRIu32 " max length %" PRIu32 "\r\n",g_writeBuffer.bufferLength,g_writeBuffer.maxBufferLength);
00196   fillBuffer(g_writeBuffer.buffer,g_writeBuffer.bufferLength);
00197 
00198   g_readBuffer.buffer = readData;
00199   g_readBuffer.maxBufferLength = sizeInK;
00200 
00201 
00202   rc =pal_imageInitAPI(stateAdvance);
00203   PAL_PRINTF("pal_imageInitAPI returned %" PRIu32 " \r\n",rc);
00204   TEST_ASSERT_TRUE(rc >= 0);
00205 
00206   /*Wait until the async test finishes*/
00207   while (!g_isTestDone)
00208       pal_osDelay(5); //Make the OS switch context
00209 
00210 }
00211 
00212 /*! \brief Writing a 1Kb image and verifying its value.
00213  * \test
00214 *  This test simulates a state machine for writing and reading a 1Kb image.
00215 *
00216 * | # |    Step                        |   Expected  |
00217 * |---|--------------------------------|-------------|
00218 * | 1 | Initialize the test.                                      | Success |
00219 * | 2 | `pal_imagePrepare`                               | PAL_SUCCESS |
00220 * | 3 | `pal_imageWrite`                                 | PAL_SUCCESS |
00221 * | 4 | `pal_imageFinalize`                              | PAL_SUCCESS |
00222 * | 5 | `pal_imageReadToBuffer`                          | PAL_SUCCESS |
00223 * | 6 | Compare the written image with the read image.            | memcmp == 0 |
00224 */
00225 
00226 TEST(pal_update, pal_update_1k)
00227 {
00228     pal_update_xK(1*KILOBYTE);
00229 }
00230 
00231 
00232 /*! \brief Writing a 2Kb image and verifying its value.
00233  * \test
00234 *  This test simulates a state machine for writing and reading a 2Kb image.
00235 *
00236 * | # |    Step                        |   Expected  |
00237 * |---|--------------------------------|-------------|
00238 * | 1 | Initialize the test.                                      | Success |
00239 * | 2 | `pal_imagePrepare`                               | PAL_SUCCESS |
00240 * | 3 | `pal_imageWrite`                                 | PAL_SUCCESS |
00241 * | 4 | `pal_imageFinalize`                              | PAL_SUCCESS |
00242 * | 5 | `pal_imageReadToBuffer`                          | PAL_SUCCESS |
00243 * | 6 | Compare the written image with the read image.            | memcmp == 0 |
00244 */
00245 
00246 TEST(pal_update, pal_update_2k)
00247 {
00248     pal_update_xK(2*KILOBYTE);
00249 }
00250 
00251 /*! \brief Writing a 4Kb image and verifying its value.
00252  * \test
00253 *  This test simulates a state machine for writing and reading a 4Kb image.
00254 *
00255 * | # |    Step                        |   Expected  |
00256 * |---|--------------------------------|-------------|
00257 * | 1 | Initialize the test.                                     | Success |
00258 * | 2 | `pal_imagePrepare`                               | PAL_SUCCESS |
00259 * | 3 | `pal_imageWrite`                                 | PAL_SUCCESS |
00260 * | 4 | `pal_imageFinalize`                              | PAL_SUCCESS |
00261 * | 5 | `pal_imageReadToBuffer`                          | PAL_SUCCESS |
00262 * | 6 | Compare the written image with the read image.            | memcmp == 0 |
00263 */
00264 
00265 TEST(pal_update, pal_update_4k)
00266 {
00267     pal_update_xK(4*KILOBYTE);
00268 }
00269 
00270 /*! \brief Writing an 8Kb image and verifying its value.
00271  * \test
00272 *  This test simulates a state machine for writing and reading an 8Kb image.
00273 *
00274 * | # |    Step                        |   Expected  |
00275 * |---|--------------------------------|-------------|
00276 * | 1 | Initialize the test.                              | Success |
00277 * | 2 | `pal_imagePrepare`                      | PAL_SUCCESS |
00278 * | 3 | `pal_imageWrite`                        | PAL_SUCCESS |
00279 * | 4 | `pal_imageFinalize`                     | PAL_SUCCESS |
00280 * | 5 | `pal_imageReadToBuffer`                 | PAL_SUCCESS |
00281 * | 6 | Compare the written image with the read image.   | memcmp == 0 |
00282 */
00283 
00284 TEST(pal_update, pal_update_8k)
00285 {
00286     pal_update_xK(8*KILOBYTE);
00287 }
00288 
00289 /*! \brief Writing a 16Kb image and verifying its value.
00290  * \test
00291 *  This test simulates a state machine for writing and reading a 16Kb image.
00292 *
00293 * | # |    Step                        |   Expected  |
00294 * |---|--------------------------------|-------------|
00295 * | 1 | Initialize the test.                                 | Success |
00296 * | 2 | `pal_imagePrepare`                           | PAL_SUCCESS |
00297 * | 3 | `pal_imageWrite`                             | PAL_SUCCESS |
00298 * | 4 | `pal_imageFinalize`                          | PAL_SUCCESS |
00299 * | 5 | `pal_imageReadToBuffer`                      | PAL_SUCCESS |
00300 * | 6 | Compare the written image with the read image.        | memcmp == 0 |
00301 */
00302 
00303 TEST(pal_update, pal_update_16k)
00304 {
00305     pal_update_xK(16*KILOBYTE);
00306 }
00307 
00308 
00309 /*! \brief Writing a small image (5b) and verifying its value.
00310  * \test
00311 *  This test simulates a state machine for writing and reading a 5b image.
00312 *
00313 * | # |    Step                        |   Expected  |
00314 * |---|--------------------------------|-------------|
00315 * | 1 | Initialize the test.                             | Success |
00316 * | 2 | `pal_imagePrepare`                       | PAL_SUCCESS |
00317 * | 3 | `pal_imageWrite`                         | PAL_SUCCESS |
00318 * | 4 | `pal_imageFinalize`                      | PAL_SUCCESS |
00319 * | 5 | `pal_imageReadToBuffer`                  | PAL_SUCCESS |
00320 * | 6 | Compare the written image with the read image.    | memcmp == 0 |
00321 */
00322 
00323 
00324 TEST(pal_update,pal_update_writeSmallChunk_5b)
00325 {
00326     pal_update_xK(5);
00327 }
00328 
00329 
00330 /*! \brief Writing an unaligned image of 1001b and verifying its value.
00331  * \test
00332 *  This test simulates a state machine for writing and reading a 51001b image.
00333 *
00334 * | # |    Step                        |   Expected  |
00335 * |---|--------------------------------|-------------|
00336 * | 1 | Initialize the test.                                   | Success |
00337 * | 2 | `pal_imagePrepare`                            | PAL_SUCCESS |
00338 * | 3 | `pal_imageWrite`                              | PAL_SUCCESS |
00339 * | 4 | `pal_imageFinalize`                           | PAL_SUCCESS |
00340 * | 5 | `pal_imageReadToBuffer`                       | PAL_SUCCESS |
00341 * | 6 | Compare the written image with the read image.         | memcmp == 0 |
00342 */
00343 
00344 
00345 TEST(pal_update,pal_update_writeUnaligned_1001b)
00346 {
00347     //1039 is a prime number so probably never aligned.
00348     pal_update_xK(1039);
00349 }
00350 
00351 
00352 
00353 
00354 PAL_PRIVATE void multiWriteMultiRead(palImageEvents_t state)
00355 {
00356     int rc = PAL_SUCCESS;
00357     static uint8_t counter = 0;
00358     static uint8_t *writeBase = NULL;
00359     static uint8_t *readBase = NULL;
00360     if (NULL == writeBase)
00361     {
00362         writeBase = g_writeBuffer.buffer;
00363         g_writeBuffer.maxBufferLength = g_writeBuffer.maxBufferLength/numberofBlocks;
00364         g_writeBuffer.bufferLength = g_writeBuffer.bufferLength/numberofBlocks;
00365 
00366         readBase = g_readBuffer.buffer;
00367         g_readBuffer.maxBufferLength = g_readBuffer.maxBufferLength/numberofBlocks;
00368     }
00369     PAL_PRINTF("Finished event %d\r\n",state);
00370     if (PAL_IMAGE_EVENT_WRITE == state) // Just wrote data
00371     {
00372         counter++;
00373         if (numberofBlocks == counter) // Wrote all needed blocks
00374         {
00375             state++; // Advance to next state
00376             counter = 0; //Initialize counter
00377         }
00378     }
00379     else if (PAL_IMAGE_EVENT_READTOBUFFER == state) // Just read data
00380     {
00381         counter++ ;
00382         if (numberofBlocks == counter) // Read all needed blocks
00383         {
00384             state++; // Advance to next state
00385             counter = 0;
00386         }
00387     }
00388     else
00389     {
00390         state++; // Advance to next state
00391     }
00392 
00393     PAL_PRINTF("Starting event %d\r\n",state);
00394 
00395     switch (state)
00396     {
00397     case PAL_IMAGE_EVENT_PREPARE:
00398           rc = pal_imagePrepare(FIRST_IMAGE_INDEX, &g_imageHeader);
00399           PAL_PRINTF("pal_imagePrepare returned %d \r\n",rc);
00400           TEST_ASSERT_EQUAL(0, rc);
00401           break;
00402     case PAL_IMAGE_EVENT_WRITE:
00403           PAL_PRINTF("Write KILOBYTE * %d = %d\r\n",counter,KILOBYTE*(counter));
00404           g_writeBuffer.buffer = &writeBase[KILOBYTE*(counter)];// Writing 1k every time
00405           rc = pal_imageWrite(FIRST_IMAGE_INDEX, KILOBYTE*(counter),(palConstBuffer_t*)&g_writeBuffer);
00406           PAL_PRINTF("pal_imageWrite returned %d \r\n",rc);
00407           TEST_ASSERT_TRUE(rc >= 0);
00408           break;
00409     case PAL_IMAGE_EVENT_FINALIZE:
00410           rc = pal_imageFinalize(FIRST_IMAGE_INDEX);
00411           PAL_PRINTF("pal_imageFinalize returned %d \r\n",rc);
00412           TEST_ASSERT_TRUE(rc >= 0);
00413           break;
00414     case PAL_IMAGE_EVENT_READTOBUFFER:
00415           PAL_PRINTF("Read KILOBYTE * %d = %d\r\n",counter, KILOBYTE*(counter));
00416           g_readBuffer.buffer = &readBase[KILOBYTE*(counter)];// Writing 1k every time
00417           g_readBuffer.bufferLength = 0;
00418           rc = pal_imageReadToBuffer(FIRST_IMAGE_INDEX, KILOBYTE*(counter),&g_readBuffer);
00419           PAL_PRINTF("pal_imageReadToBuffer  with offset %d return %d \r\n",0,rc);
00420           break;
00421     case PAL_IMAGE_EVENT_ACTIVATE:
00422 
00423           PAL_PRINTF("Checking the output\r\n");
00424 
00425           TEST_ASSERT_TRUE(rc >= 0);
00426           TEST_ASSERT_TRUE(!memcmp(readBase,writeBase,numberofBlocks*KILOBYTE));
00427           free(writeBase);
00428           free(readBase);
00429           pal_imageDeInit();
00430           g_isTestDone = 1;
00431           break;
00432     default:
00433         PAL_PRINTF("Error\r\n");
00434         free(writeBase);
00435         free(readBase);
00436         pal_imageDeInit();
00437         g_isTestDone = 1;
00438         break;
00439     }
00440 }
00441 
00442 
00443 /*! \brief Writing a 4Kb image and reading it with a 1Kb buffer.
00444  * \test
00445 *  This test simulates a state machine for writing and reading a 4Kb image.
00446 *
00447 * | # |    Step                        |   Expected  |
00448 * |---|--------------------------------|-------------|
00449 * | 1 | Initialize the test.                                             | Success |
00450 * | 2 | `pal_imagePrepare`                                     | PAL_SUCCESS |
00451 * | 3 | `pal_imageWrite`                                       | PAL_SUCCESS |
00452 * | 4 | `pal_imageFinalize`                                    | PAL_SUCCESS |
00453 * | 5 | `pal_imageReadToBuffer`                                | PAL_SUCCESS |
00454 * | 6 | Compare the written image with the read image.                  | memcmp == 0 |
00455 */
00456 
00457 TEST(pal_update, pal_update_4k_write_1k_4_times)
00458 {
00459   palStatus_t rc = PAL_SUCCESS;
00460 
00461   uint8_t *writeData = (uint8_t*)malloc(4*KILOBYTE);
00462   uint8_t *readData  = (uint8_t*)malloc(4*KILOBYTE);
00463 
00464 
00465   TEST_ASSERT_TRUE(writeData != NULL);
00466   TEST_ASSERT_TRUE(readData != NULL);
00467 
00468   uint64_t version = 11111111;
00469   // XXX: the pal_imagePrepare() has hardcoded requirement of hash being SHA256, so it must be given here too
00470   uint8_t hash[SIZEOF_SHA256] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
00471                                   0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
00472                                   0x0c, 0x1c, 0x2c, 0x3c, 0x4c, 0x5c, 0x6c, 0x7c,
00473                                   0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7
00474                                   };
00475   g_isTestDone = 0;
00476 
00477   g_imageHeader.version = version;
00478 
00479   g_imageHeader.hash.buffer =(uint8_t*)&hash;
00480   g_imageHeader.hash.bufferLength = sizeof(hash);
00481   g_imageHeader.hash.maxBufferLength = sizeof(hash);
00482 
00483   g_imageHeader.imageSize = 4*KILOBYTE;
00484 
00485   fillBuffer(writeData,4*KILOBYTE);
00486 
00487   g_writeBuffer.buffer = writeData;
00488   g_writeBuffer.bufferLength = 4*KILOBYTE;
00489   g_writeBuffer.maxBufferLength = 4*KILOBYTE;
00490   PAL_PRINTF("pal_update_4k");
00491   PAL_PRINTF("Write buffer length %" PRIu32 " max length %" PRIu32 "\r\n",g_writeBuffer.bufferLength,g_writeBuffer.maxBufferLength);
00492   fillBuffer(g_writeBuffer.buffer,g_writeBuffer.bufferLength);
00493 
00494   g_readBuffer.buffer = readData;
00495   g_readBuffer.maxBufferLength =  4*KILOBYTE;
00496 
00497   numberofBlocks = 4;
00498 
00499   rc = pal_imageInitAPI(multiWriteMultiRead);
00500   PAL_PRINTF("pal_imageInitAPI returned %" PRIu32 " \r\n",rc);
00501   TEST_ASSERT_TRUE(rc >= 0);
00502   /*Wait until the async test finishes*/
00503   while (!g_isTestDone)
00504       pal_osDelay(5); // Make the OS switch context
00505 
00506 }
00507 
00508 /*! \brief Writing a different image with incrementing size.
00509  * \test
00510 *
00511 * | # |    Step                        |   Expected  |
00512 * |---|--------------------------------|-------------|
00513 * | 1 | Initialize the test.                           | Success |
00514 * | 2 | `1kb_image`                          | Success |
00515 * | 3 | `2kb_image`                          | Success |
00516 * | 4 | `4kb_image`                          | Success |
00517 * | 5 | `8kb_image`                          | Success |
00518 * | 6 | `16kb_image`                         | Success |
00519 * | 7 | `32kb_image`                         | Success |
00520 */
00521 TEST(pal_update, pal_update_stressTest)
00522 {
00523     uint8_t it,j;
00524     PAL_PRINTF("****************************************************\r\n");
00525     PAL_PRINTF("******* Testing multiple writes sequentially *******\r\n");
00526     PAL_PRINTF("****************************************************\r\n");
00527     for (j=0; j < 5; j++)
00528     {
00529         PAL_PRINTF("1\r\n");
00530         for (it = 1; it < 32; it*=2)
00531         {
00532             pal_update_xK(it*KILOBYTE);
00533 
00534         }
00535     }
00536 }
00537 
00538 
00539 
00540 
00541 
00542 
00543 PAL_PRIVATE void readStateMachine(palImageEvents_t state)
00544 {
00545     static uint8_t *readData = NULL ;
00546     static uint32_t bytesWasRead = 0;
00547     int rc = PAL_SUCCESS;
00548     PAL_PRINTF("Finished event %d\r\n",state);
00549     /*If just finished reading*/
00550     if (PAL_IMAGE_EVENT_READTOBUFFER == state)
00551     {
00552         PAL_PRINTF("g_readBuffer.bufferLength %" PRIu32 "\r\n",g_readBuffer.bufferLength);
00553         if (0 < g_readBuffer.bufferLength)
00554         {
00555             PAL_PRINTF("Writing %" PRIu32 " bytes to readData[%" PRIu32 "]\r\n",g_readBuffer.bufferLength,bytesWasRead);
00556             memcpy(&readData[bytesWasRead],g_readBuffer.buffer,g_readBuffer.bufferLength);
00557             bytesWasRead+=g_readBuffer.bufferLength;
00558         }
00559         else
00560         {
00561             state++;
00562         }
00563     }
00564     else
00565     {
00566         state++;
00567     }
00568     PAL_PRINTF("Starting event %d\r\n",state);
00569     switch (state)
00570     {
00571     case PAL_IMAGE_EVENT_PREPARE:
00572           bytesWasRead = 0;
00573           PAL_PRINTF("Allocating %" PRIu32 " byes for test \r\n",g_writeBuffer.maxBufferLength);
00574           readData = (uint8_t*)malloc(g_writeBuffer.maxBufferLength);
00575           TEST_ASSERT_TRUE(readData != NULL);
00576           rc = pal_imagePrepare(FIRST_IMAGE_INDEX,&g_imageHeader);
00577           PAL_PRINTF("pal_imagePrepare returned %d \r\n",rc);
00578           TEST_ASSERT_EQUAL(0, rc);
00579           break;
00580     case PAL_IMAGE_EVENT_WRITE:
00581           rc = pal_imageWrite(FIRST_IMAGE_INDEX,0,(palConstBuffer_t*)&g_writeBuffer);
00582           PAL_PRINTF("pal_imageWrite returned %d \r\n",rc);
00583           TEST_ASSERT_TRUE(rc >= 0);
00584           break;
00585     case PAL_IMAGE_EVENT_FINALIZE:
00586           rc = pal_imageFinalize(FIRST_IMAGE_INDEX);
00587           PAL_PRINTF("pal_imageFinalize returned %d \r\n",rc);
00588           TEST_ASSERT_TRUE(rc >= 0);
00589           break;
00590     case PAL_IMAGE_EVENT_READTOBUFFER:
00591           g_readBuffer.bufferLength = 0;
00592           memset(g_readBuffer.buffer,0,g_readBuffer.maxBufferLength);
00593           rc = pal_imageReadToBuffer(FIRST_IMAGE_INDEX,bytesWasRead,&g_readBuffer);
00594           PAL_PRINTF("pal_imageReadToBuffer  with offset %" PRIu32 " return %d \r\n",bytesWasRead,rc);
00595           //TEST_ASSERT_TRUE((rc >= 0) || (rc != -10));
00596           break;
00597     case PAL_IMAGE_EVENT_ACTIVATE:
00598           PAL_PRINTF("Checking the output\r\n");
00599           PAL_PRINTF("\r\ng_readBuffer bufferLength=%" PRIu32 "\r\n",g_readBuffer.maxBufferLength);
00600           TEST_ASSERT_TRUE(!memcmp(readData,g_writeBuffer.buffer,g_writeBuffer.bufferLength));
00601           PAL_PRINTF("write ptr = %p read ptr = %p\r\n",g_writeBuffer.buffer,g_readBuffer.buffer);
00602           free(g_readBuffer.buffer);
00603           free(g_writeBuffer.buffer);
00604           free(readData);
00605           pal_imageDeInit();
00606           g_isTestDone = 1;
00607           break;
00608     default:
00609         PAL_PRINTF("Error - this should not happen\r\n");
00610         PAL_PRINTF("write ptr = %p read ptr = %p\r\n",g_writeBuffer.buffer,g_readBuffer.buffer);
00611         free(g_readBuffer.buffer);
00612         free(g_writeBuffer.buffer);
00613         free(readData);
00614         pal_imageDeInit();
00615         g_isTestDone = 1;
00616         TEST_ASSERT_TRUE(rc >= 0);
00617     }
00618 }
00619 
00620 
00621 
00622 /*! \brief Writing an image and verifying its value by multiple reads.
00623  * \test
00624 *  This test simulates a state machine for writing and reading an image.
00625 *
00626 * | # |    Step                        |   Expected  |
00627 * |---|--------------------------------|-------------|
00628 * | 1 | Initialize the test.                                        | Success |
00629 * | 2 | `pal_imagePrepare`                                 | PAL_SUCCESS |
00630 * | 3 | `pal_imageWrite`                                   | PAL_SUCCESS |
00631 * | 4 | `pal_imageFinalize`                                | PAL_SUCCESS |
00632 * | 5 | `pal_imageReadToBuffer`                            | PAL_SUCCESS |
00633 * | 6 | Compare the written image with the read image.              | memcmp == 0 |
00634 */
00635 
00636 TEST(pal_update, pal_update_Read)
00637 {
00638       uint32_t sizeIn=1500;
00639       palStatus_t rc = PAL_SUCCESS;
00640       PAL_PRINTF("\n-====== PAL_UPDATE_READ TEST %" PRIu32 " b ======- \n",sizeIn);
00641       uint8_t *writeData = (uint8_t*)malloc(sizeIn);
00642       uint8_t *readData  = (uint8_t*)malloc(sizeIn/5);
00643 
00644       TEST_ASSERT_TRUE(writeData != NULL);
00645       TEST_ASSERT_TRUE(readData != NULL);
00646 
00647       uint64_t version = 11111111;
00648       // XXX: the pal_imagePrepare() has hardcoded requirement of hash being SHA256, so it must be given here too
00649       uint8_t hash[SIZEOF_SHA256] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
00650                                       0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
00651                                       0x0c, 0x1c, 0x2c, 0x3c, 0x4c, 0x5c, 0x6c, 0x7c,
00652                                       0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7
00653                                       };
00654       g_isTestDone = 0;
00655 
00656       g_imageHeader.version = version;
00657 
00658       g_imageHeader.hash.buffer = (uint8_t*)&hash;
00659       g_imageHeader.hash.bufferLength = sizeof(hash);
00660       g_imageHeader.hash.maxBufferLength = sizeof(hash);
00661 
00662       g_imageHeader.imageSize = sizeIn;
00663 
00664       g_writeBuffer.buffer = writeData;
00665       g_writeBuffer.bufferLength = sizeIn;
00666       g_writeBuffer.maxBufferLength = sizeIn;
00667 
00668       PAL_PRINTF("write buffer length %" PRIu32 " max length %" PRIu32 "\r\n",g_writeBuffer.bufferLength,g_writeBuffer.maxBufferLength);
00669       fillBuffer(g_writeBuffer.buffer,g_writeBuffer.bufferLength);
00670 
00671       g_readBuffer.buffer = readData;
00672       g_readBuffer.maxBufferLength = sizeIn/5;
00673       g_readBuffer.bufferLength = 0;
00674 
00675       rc =pal_imageInitAPI(readStateMachine);
00676       PAL_PRINTF("pal_imageInitAPI returned %" PRIu32 " \r\n",rc);
00677       TEST_ASSERT_TRUE(rc >= 0);
00678 
00679       /*Wait until the async test finishes*/
00680       while (!g_isTestDone)
00681           pal_osDelay(5); // Make the OS switch context
00682 
00683 }