Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_fileSystem_test.c Source File

pal_fileSystem_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 
00021 
00022 #define TEST_DIR "dir1"
00023 #define TEST_DIR2 "dir2"
00024 #define TEST_WORKING_DIR "work1"
00025 #define TEST_DIR_FILE "dir1/test.txt"
00026 #define TEST_NUMBER_OF_FILE_TO_CREATE 20
00027 #define TEST_BUFFER_SIZE 100
00028 #define TEST_BUFFER_SMALL_SIZE 17
00029 //#define TEST_BYTES_TO_WRITE 300*4
00030 #define TEST_BYTES_TO_WRITE 100
00031 #define TEST_FILE_NAME "%s/test_f%d"
00032 #define BUFFER_TEST_SIZE 1123
00033 
00034 #if (false == PAL_PRIMARY_PARTITION_PRIVATE)
00035     #define PAL_TEST_PRIMARY_PATH "/pri"
00036 #else
00037     #define PAL_TEST_PRIMARY_PATH ""
00038 #endif
00039 
00040 #if (false == PAL_SECONDARY_PARTITION_PRIVATE)
00041     #define PAL_TEST_SECONDARY_PATH "/sec"
00042 #else
00043     #define PAL_TEST_SECONDARY_PATH ""
00044 #endif
00045 
00046 //out should in length be PAL_MAX_FILE_AND_FOLDER_LENGTH
00047 static char* addRootToPath(const char* in, char* out,pal_fsStorageID_t id)
00048 {
00049     char root[PAL_MAX_FILE_AND_FOLDER_LENGTH];
00050     size_t len = 0;
00051     palStatus_t status;
00052 
00053     status = pal_fsGetMountPoint(id, PAL_MAX_FILE_AND_FOLDER_LENGTH, root);
00054     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00055 
00056     strncpy(out, root, PAL_MAX_FILE_AND_FOLDER_LENGTH-1); //-1 for null terminator space
00057     len = strlen(out);
00058     if (PAL_FS_PARTITION_PRIMARY == id)
00059     {
00060         strncat(out, PAL_TEST_PRIMARY_PATH, PAL_MAX_FILE_AND_FOLDER_LENGTH - len);
00061     }
00062     else
00063     {
00064         strncat(out, PAL_TEST_SECONDARY_PATH, PAL_MAX_FILE_AND_FOLDER_LENGTH - len);
00065     }
00066     len = strlen(out);
00067     if (*in != '\0')
00068     {
00069         strncat(out,"/",PAL_MAX_FILE_AND_FOLDER_LENGTH - len);
00070         strncat(out,in,PAL_MAX_FILE_AND_FOLDER_LENGTH -len -1);
00071     }
00072     return(out);
00073 }
00074 
00075 
00076 
00077 PAL_PRIVATE uint8_t *bufferTest = NULL;
00078 PAL_PRIVATE uint8_t *bufferTest2  = NULL;
00079 
00080 PAL_PRIVATE palFileDescriptor_t g_fd1 = 0;
00081 PAL_PRIVATE palFileDescriptor_t g_fd2 = 0;
00082 
00083 // local copy of the primary root, needed as test may change it
00084 // and the cleanup needs a stable base to work on
00085 PAL_PRIVATE char rootPathPrimaryOriginal[PAL_MAX_FILE_AND_FOLDER_LENGTH];
00086 
00087 
00088 PAL_PRIVATE palStatus_t pal_fsClearAndInitialyze(pal_fsStorageID_t id)
00089 {
00090     palStatus_t status = PAL_SUCCESS;
00091 
00092     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00093     status = pal_fsRmFiles(addRootToPath("",buffer,id));
00094 
00095     return(status);
00096 }
00097 
00098 /*! \brief This function compare two files
00099 *
00100 * @param[in]    *pathCmp1 - pointer to the null-terminated string that specifies the first filename to be compare
00101 * @param[in]    *pathCmp2 - pointer to the null-terminated string that specifies the second filename to be compare
00102 *
00103 * \return PAL_SUCCESS upon successful operation.\n
00104 *
00105 */
00106 PAL_PRIVATE palStatus_t fileSystemCompareUtil(const char * pathCmp1, const char * pathCmp2)
00107 {
00108     palStatus_t status = PAL_SUCCESS;
00109 
00110     char bufferCmp1[TEST_BUFFER_SIZE];
00111     char bufferCmp2[TEST_BUFFER_SIZE];
00112     size_t numOfBytesCmp1 = 0;
00113     size_t numOfBytesCmp2 = 0;
00114     palFileDescriptor_t  fdCmp1 = 0;
00115     palFileDescriptor_t  fdCmp2 = 0;
00116 
00117     status =  pal_fsFopen(pathCmp1, PAL_FS_FLAG_READONLY, &fdCmp1);
00118     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00119 
00120     status =  pal_fsFopen(pathCmp2, PAL_FS_FLAG_READONLY, &fdCmp2);
00121     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00122 
00123     while(true)
00124     {
00125 
00126         status  = pal_fsFread(&fdCmp1, bufferCmp1, TEST_BUFFER_SIZE, &numOfBytesCmp1);
00127         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00128 
00129         status  = pal_fsFread(&fdCmp2, bufferCmp2, TEST_BUFFER_SIZE, &numOfBytesCmp2);
00130         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00131 
00132         if ((numOfBytesCmp2 == 0) && (numOfBytesCmp1 == 0))
00133         {//End of file reached
00134             break;
00135         }
00136 
00137         TEST_ASSERT_EQUAL(numOfBytesCmp1, numOfBytesCmp2);
00138         TEST_ASSERT_EQUAL_MEMORY(bufferCmp1, bufferCmp2, numOfBytesCmp1);
00139     }
00140 
00141     status =  pal_fsFclose(&fdCmp1);
00142     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00143 
00144     status =  pal_fsFclose(&fdCmp2);
00145     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00146 
00147     return status;
00148 }
00149 
00150 TEST_GROUP(pal_fileSystem);
00151 
00152 
00153 TEST_SETUP(pal_fileSystem)
00154 {
00155 
00156     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00157 
00158     // copy the root dir, so the deletion works also from teardown
00159     addRootToPath("", rootPathPrimaryOriginal, PAL_FS_PARTITION_PRIMARY);
00160 
00161     pal_fsRmFiles(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_PRIMARY));//Remove all files in the testing DIRECTORY
00162     pal_fsRmDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_PRIMARY)); //Delete Directory if exist
00163     pal_fsRmFiles(addRootToPath(TEST_WORKING_DIR,buffer,PAL_FS_PARTITION_PRIMARY));//Remove all files in the testing DIRECTORY
00164     pal_fsRmDir(addRootToPath(TEST_WORKING_DIR,buffer,PAL_FS_PARTITION_PRIMARY)); //Delete Directory if exist
00165     pal_fsRmFiles(addRootToPath(TEST_DIR2,buffer,PAL_FS_PARTITION_PRIMARY));//Remove all files in the testing DIRECTORY
00166     pal_fsRmDir(addRootToPath(TEST_DIR2,buffer,PAL_FS_PARTITION_PRIMARY)); //Delete Directory if exist
00167 
00168 
00169     pal_fsRmFiles(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_SECONDARY));//Remove all files in the testing DIRECTORY
00170     pal_fsRmDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_SECONDARY)); //Delete Directory if exist
00171     pal_fsRmFiles(addRootToPath(TEST_WORKING_DIR,buffer,PAL_FS_PARTITION_SECONDARY));//Remove all files in the testing DIRECTORY
00172     pal_fsRmDir(addRootToPath(TEST_WORKING_DIR,buffer,PAL_FS_PARTITION_SECONDARY)); //Delete Directory if exist
00173     pal_fsRmFiles(addRootToPath(TEST_DIR2,buffer,PAL_FS_PARTITION_SECONDARY));//Remove all files in the testing DIRECTORY
00174     pal_fsRmDir(addRootToPath(TEST_DIR2,buffer,PAL_FS_PARTITION_SECONDARY)); //Delete Directory if exist
00175 
00176     g_fd1 = 0;
00177     g_fd2 = 0;
00178     bufferTest = NULL;
00179     bufferTest2 = NULL;
00180     if(!pal_fsIsPrivatePartition(PAL_FS_PARTITION_PRIMARY))
00181     {
00182 
00183         addRootToPath("",buffer,PAL_FS_PARTITION_PRIMARY);
00184         pal_fsMkDir(buffer);
00185     }
00186     if(!pal_fsIsPrivatePartition(PAL_FS_PARTITION_SECONDARY))
00187     {
00188         addRootToPath("",buffer,PAL_FS_PARTITION_SECONDARY);
00189         pal_fsMkDir(buffer);
00190     }
00191 }
00192 
00193 TEST_TEAR_DOWN(pal_fileSystem)
00194 {
00195     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00196     if (g_fd1) 
00197     {
00198         pal_fsFclose(&g_fd1);
00199     }
00200     if (g_fd2) 
00201     {
00202         pal_fsFclose(&g_fd2);
00203     }
00204     g_fd1 = 0;
00205     g_fd2 = 0;
00206 
00207     if (bufferTest != NULL)
00208     {
00209         free(bufferTest);
00210         bufferTest = NULL;
00211     }
00212     if (bufferTest2 != NULL)
00213     {
00214         free(bufferTest2);
00215         bufferTest2 = NULL;
00216     }
00217     pal_fsClearAndInitialyze(PAL_FS_PARTITION_PRIMARY);
00218     pal_fsClearAndInitialyze(PAL_FS_PARTITION_SECONDARY);
00219 
00220     if(!pal_fsIsPrivatePartition(PAL_FS_PARTITION_PRIMARY))
00221     {
00222 
00223         addRootToPath("",buffer,PAL_FS_PARTITION_PRIMARY);
00224         pal_fsRmDir(buffer);
00225     }
00226     if(!pal_fsIsPrivatePartition(PAL_FS_PARTITION_SECONDARY))
00227     {
00228         addRootToPath("",buffer,PAL_FS_PARTITION_SECONDARY);
00229         pal_fsRmDir(buffer);
00230     }
00231 
00232     // If there is a primary root configured (eg. "/pri") and it was queried successfully
00233     // on setup, use it also to do cleanup. This is needed to clean up after test which
00234     // changes the root during test.
00235     // Perhaps the same could be achieved by restoring the root dir before this cleanup code above is ran?!
00236     if (strlen(rootPathPrimaryOriginal) > 1) {
00237 
00238         snprintf(buffer, PAL_MAX_FILE_AND_FOLDER_LENGTH, "%s/%s", rootPathPrimaryOriginal, TEST_DIR);
00239         pal_fsRmFiles(buffer);//Remove all files in the testing dir, eg (/pri/dir1
00240         pal_fsRmDir(buffer); //Delete Directory if exist
00241 
00242         snprintf(buffer, PAL_MAX_FILE_AND_FOLDER_LENGTH, "%s/%s", rootPathPrimaryOriginal, TEST_WORKING_DIR);
00243         pal_fsRmFiles(buffer);//Remove all files in the testing dir, eg (/pri/work1
00244         pal_fsRmDir(buffer); //Delete Directory if exist
00245 
00246         snprintf(buffer, PAL_MAX_FILE_AND_FOLDER_LENGTH, "%s/%s", rootPathPrimaryOriginal, TEST_DIR2);
00247         pal_fsRmFiles(buffer);//Remove all files in the testing dir, eg (/pri/dir2
00248         pal_fsRmDir(buffer); //Delete Directory if exist
00249 
00250     }
00251     // the pal_destroy may not necessarily do the full PAL shutdown, if this is ran from
00252     // a "live" environment, eg. testapp
00253     pal_fsCleanup();
00254 
00255 }
00256 
00257 /*! \brief /b SDFormat function tests formatting an SD card.
00258 *
00259 ** \test
00260 * | # |    Step                        |   Expected  |
00261 * |---|--------------------------------|-------------|
00262 * | 1 | create TEST_DIR with pal_fsMkDir                                                | PAL_SUCCESS |
00263 * | 2 | create TEST_DIR_FILE file with pal_fsOpen                                       | PAL_SUCCESS |
00264 * | 3 | close file TEST_DIR_FILE with pal_fsClose                                       | PAL_SUCCESS |
00265 * | 4 | Format SD card with pal_FormatSDPartition                                       | PAL_SUCCESS |
00266 * | 5 | TEST_DIR_FILE should not exist after format                                     | PAL_ERR_FS_NO_FILE |
00267 * | 6 | create TEST_DIR with pal_fsMkDir                                                | PAL_SUCCESS |
00268 */
00269 void SDFormat_1Partition()
00270 {
00271     palStatus_t status = PAL_SUCCESS;
00272     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00273 
00274     /*#1*/
00275     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_PRIMARY)); //Create Directory
00276     if (PAL_SUCCESS == status)
00277     {
00278         /*#2*/
00279         status = pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,PAL_FS_PARTITION_PRIMARY), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
00280         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00281         /*#3*/
00282         status = pal_fsFclose(&g_fd1);
00283         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00284     }
00285 
00286 
00287     /*#4*/
00288     status = pal_fsClearAndInitialyze(PAL_FS_PARTITION_PRIMARY); //Format SD
00289     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00290 
00291     /*#5*/
00292     status = pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,PAL_FS_PARTITION_PRIMARY), PAL_FS_FLAG_READONLY, &g_fd1);
00293     TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_FILE, status);    //Failed all files where deleted in previous step
00294     
00295     status = pal_fsClearAndInitialyze(PAL_FS_PARTITION_SECONDARY); //Format SD
00296     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00297 
00298 
00299     /*#6*/
00300     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_PRIMARY)); //Create Directory
00301     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00302 
00303 }
00304 
00305 /*! \brief /b SDFormat function tests formatting an SD card.
00306 *
00307 ** \test
00308 * | # |    Step                        |   Expected  |
00309 * |---|--------------------------------|-------------|
00310 * | 1 | create TEST_DIR with pal_fsMkDir primary partition                              | PAL_SUCCESS |
00311 * | 2 | create TEST_DIR_FILE file with pal_fsOpen primary partition                     | PAL_SUCCESS |
00312 * | 3 | close file TEST_DIR_FILE with pal_fsClose                                       | PAL_SUCCESS |
00313 * | 4 | create TEST_DIR with pal_fsMkDir secondary partition                            | PAL_SUCCESS |
00314 * | 5 | create TEST_DIR_FILE file with pal_fsOpen secondary partition                   | PAL_SUCCESS |
00315 * | 6 | close file TEST_DIR_FILE with pal_fsClose                                       | PAL_SUCCESS |
00316 * | 7 | Format SD card primary partition with pal_FormatSDPartition                     | PAL_SUCCESS |
00317 * | 8 | TEST_DIR_FILE in primary should not exist after format                          | PAL_ERR_FS_NO_FILE |
00318 * | 9 | TEST_DIR_FILE in secondary should  exist after format                           | PAL_SUCCESS |
00319 * | 10| Format SD card secondary partition with pal_FormatSDPartition                   | PAL_SUCCESS |
00320 * | 11| TEST_DIR_FILE in secondary should not exist after format                        | PAL_ERR_FS_NO_FILE |
00321 * | 12| create TEST_DIR with pal_fsMkDir in primary partition                           | PAL_SUCCESS |
00322 * | 13| create TEST_DIR with pal_fsMkDir in secondary partition                         | PAL_SUCCESS |
00323 */
00324 void SDFormat_2Partition()
00325 {
00326     palStatus_t status = PAL_SUCCESS;
00327     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00328 
00329     /*#1*/
00330     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_PRIMARY)); //Create Directory
00331     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00332     /*#2*/
00333     status = pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,PAL_FS_PARTITION_PRIMARY), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
00334     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00335     /*#3*/
00336     status = pal_fsFclose(&g_fd1);
00337     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00338 
00339     /*#4*/
00340     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_SECONDARY)); //Create Directory
00341     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00342 
00343     /*#5*/
00344     status = pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,PAL_FS_PARTITION_SECONDARY), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
00345     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00346     /*#6*/
00347     status = pal_fsFclose(&g_fd1);
00348     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00349 
00350 
00351     /*#7*/
00352     status = pal_fsClearAndInitialyze(PAL_FS_PARTITION_PRIMARY); //Format SD
00353     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00354 
00355     /*#8*/
00356     status = pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,PAL_FS_PARTITION_PRIMARY), PAL_FS_FLAG_READONLY, &g_fd1);
00357     TEST_ASSERT_EQUAL_HEX(PAL_ERR_FS_NO_FILE, status);    //Failed all files where deleted in previous step
00358 
00359     /*#9*/
00360     status = pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,PAL_FS_PARTITION_SECONDARY), PAL_FS_FLAG_READONLY, &g_fd1);
00361     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);    //the file still exists in secondary
00362 
00363     status = pal_fsFclose(&g_fd1);
00364     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00365 
00366     /*#10*/
00367     status = pal_fsClearAndInitialyze(PAL_FS_PARTITION_SECONDARY); //Format SD
00368     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00369     /*#11*/
00370     status = pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,PAL_FS_PARTITION_SECONDARY), PAL_FS_FLAG_READONLY, &g_fd1);
00371     TEST_ASSERT_EQUAL_HEX(PAL_ERR_FS_NO_FILE, status);    //Failed all files where deleted in previous step
00372 
00373     /*#12*/
00374     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_PRIMARY)); //Create Directory
00375     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00376     /*#13*/
00377     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,PAL_FS_PARTITION_SECONDARY)); //Create Directory
00378     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00379 }
00380 
00381 
00382 
00383 TEST(pal_fileSystem, SDFormat)
00384 {
00385 //#if (PAL_NUMBER_OF_PARTITIONS == 2)
00386     // There should be no reason why would the 2 partition code need to be tested if it is not configured for platform.
00387     SDFormat_2Partition();
00388 //#endif
00389     SDFormat_1Partition();
00390 }
00391 
00392 
00393 /*! \brief /b directoryTests function Tests  root Directory commands
00394 *
00395 ** \test
00396 * | # |    Step                        |   Expected  |
00397 * |---|--------------------------------|-------------|
00398 * | 1 | get root directory with pal_fsGetMountPoint                                    | PAL_SUCCESS |
00399 * | 2 | create TEST_WORKING_DIR with pal_fsMkDir                                       | PAL_SUCCESS |
00400 * | 3 | Change Root Directory to TEST_WORKING_DIR with     pal_fsSetMountPoint         | PAL_SUCCESS |
00401 * | 4 | get root directory with pal_fsGetMountPoint                                    | PAL_SUCCESS |
00402 * | 5 | create TEST_WORKING_DIR with pal_fsMkDir                                       | PAL_ERR_FS_NAME_ALREADY_EXIST |
00403 */
00404 void rootDirectoryTests(pal_fsStorageID_t storageId)
00405 {
00406     palStatus_t status = PAL_SUCCESS;
00407     char getRootPath[TEST_BUFFER_SIZE] = { 0 };
00408     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00409 /*#1*/
00410     status = pal_fsGetMountPoint(storageId, TEST_BUFFER_SIZE, getRootPath); //Setting New working Directory
00411     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00412     
00413 /*#2*/
00414     addRootToPath(TEST_WORKING_DIR, buffer, storageId);
00415     status = pal_fsMkDir(buffer); //Create Directory
00416     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00417 
00418 /*#3*/
00419     status = pal_fsSetMountPoint(storageId, buffer); //Setting New working Directory
00420     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00421 
00422 /*#4*/
00423     status = pal_fsGetMountPoint(storageId, TEST_BUFFER_SIZE, getRootPath); //Setting New working Directory
00424     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00425 
00426 /*#5*/
00427     status = pal_fsMkDir(getRootPath); //should fail because already exits and path is absolute
00428     TEST_ASSERT_EQUAL(PAL_ERR_FS_NAME_ALREADY_EXIST, status);
00429 
00430 
00431 }
00432 
00433 
00434 TEST(pal_fileSystem, rootDirectoryTests)
00435 {
00436 
00437     rootDirectoryTests(PAL_FS_PARTITION_PRIMARY);
00438 
00439 }
00440 
00441 
00442 /*! \brief /b directoryTests function Tests Directory commands
00443 *
00444 ** \test
00445 * | # |    Step                        |   Expected  |
00446 * |---|--------------------------------|-------------|
00447 * | 1 | create TEST_DIR with pal_fsMkDir                                                | PAL_SUCCESS |
00448 * | 2 | create TEST_DIR with pal_fsMkDir                                                | PAL_ERR_FS_NAME_ALREADY_EXIST |
00449 * | 3 | Create File TEST_DIR_FILE With PAL_ERR_FS_READWRITEEXCLUSIVE with pal_fsFopen   | PAL_SUCCESS |
00450 * | 4 | Create File TEST_DIR_FILE With PAL_ERR_FS_READWRITEEXCLUSIVE with pal_fsFopen   | PAL_ERR_FS_NAME_ALREADY_EXIST |
00451 * | 5 | Close file with uninitialized file descriptor                                   | PAL_ERR_FS_BAD_FD |
00452 * | 6 | Close file with initialized file descriptor                                     | PAL_SUCCESS |
00453 * | 7 | Delete directory with  pal_fsRmDir (directory not empty)                        | PAL_ERR_FS_ERROR |
00454 * | 8 | Delete file TEST_DIR_FILE with pal_fsUnlink                                     | PAL_SUCCESS |
00455 * | 9 | Delete file TEST_DIR_FILE with pal_fsUnlink                                     | PAL_SUCCESS |
00456 * | 10 | Delete a folder which not exists with pal_fsUnlink                             | PAL_ERR_FS_NO_PATH |
00457 *
00458 */
00459 void directoryTests(pal_fsStorageID_t storageId)
00460 {
00461     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00462     char *pathToFile = NULL;
00463     palStatus_t status = PAL_SUCCESS;
00464 
00465 /*#1*/
00466     pathToFile = addRootToPath(TEST_DIR,buffer,storageId);
00467     status = pal_fsMkDir(pathToFile); //Create Directory
00468     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00469 
00470 /*#2*/
00471     pathToFile = addRootToPath(TEST_DIR,buffer,storageId);
00472     status = pal_fsMkDir(pathToFile); //Create same Directory Shall failed
00473     TEST_ASSERT_EQUAL_HEX(PAL_ERR_FS_NAME_ALREADY_EXIST, status);
00474 
00475 /*#3*/
00476     pathToFile = addRootToPath(TEST_DIR_FILE,buffer,storageId);
00477     status =  pal_fsFopen(pathToFile, PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
00478     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00479 
00480 /*#4*/
00481     pathToFile = addRootToPath(TEST_DIR_FILE,buffer,storageId);
00482     status =  pal_fsFopen(pathToFile, PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd2); // Failed open Exclusively and file already created
00483     TEST_ASSERT_EQUAL_HEX(PAL_ERR_FS_NAME_ALREADY_EXIST, status);
00484 /*#5*/
00485     #ifdef DEBUG
00486         pal_fsFclose(&g_fd2);//Failed fd1 was not a valid File descriptor
00487     #endif
00488     //TEST_ASSERT_EQUAL(PAL_ERR_FS_BAD_FD, status); //TODO Pass on mbedOS
00489 
00490 /*#6*/    
00491     status =  pal_fsFclose(&g_fd1);
00492     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00493 
00494 /*#7*/
00495     pathToFile = addRootToPath(TEST_DIR,buffer,storageId);
00496     status = pal_fsRmDir(pathToFile); //Delete Directory Failed Directory not empty
00497     TEST_ASSERT_EQUAL_HEX(PAL_ERR_FS_DIR_NOT_EMPTY, status);
00498 
00499 /*#8*/
00500     pathToFile = addRootToPath(TEST_DIR_FILE,buffer,storageId);
00501     status = pal_fsUnlink(pathToFile); //Delete the file in a directory
00502     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00503 
00504 /*#9*/
00505     pathToFile = addRootToPath(TEST_DIR,buffer,storageId);
00506     status = pal_fsRmDir(pathToFile); //Delete Directory success
00507     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00508 
00509 /*#10*/
00510     
00511     pathToFile = addRootToPath(TEST_DIR,buffer,storageId);
00512     status = pal_fsRmDir(pathToFile); //Delete not existing Directory
00513     TEST_ASSERT_EQUAL_HEX(PAL_ERR_FS_NO_PATH, status);
00514 }
00515 
00516 TEST(pal_fileSystem, directoryTests)
00517 {
00518     directoryTests(PAL_FS_PARTITION_PRIMARY);
00519     directoryTests(PAL_FS_PARTITION_SECONDARY);
00520 }
00521 
00522 /*! \brief /b FilesTests function Tests files commands
00523 *
00524 ** \test
00525 * | # |    Step                        |   Expected  |
00526 * |---|--------------------------------|-------------|
00527 * | 1 | Init Test                                                                                                       | |
00528 * | 2 | create TEST_DIR with pal_fsMkDir                                                                                | PAL_SUCCESS |
00529 * | 3 | create TEST_DIR2 with pal_fsMkDir                                                                               | PAL_SUCCESS |
00530 * | 4 | Start Loop i from [0 - TEST_NUMBER_OF_FILE_TO_CREATE]                                                           | |
00531 * | 5 | Create File in DIR_FILE named f_i (i - index of loop)with PAL_ERR_FS_READWRITEEXCLUSIVE mode using pal_fsFopen  | PAL_SUCCESS |
00532 * | 6 | Write random buffer[TEST_BYTES_TO_WRITE] to file with pal_fsFwrite                                              | PAL_SUCCESS |
00533 * | 7 | close file handler with pal_fsFclose                                                                            | PAL_SUCCESS |
00534 * | 8 | End Loop                                                                                                        | |
00535 * | 9 | Copy TEST_DIR folder to TEST_DIR2 with pal_fsCpFolder                                                           | PAL_SUCCESS |
00536 * | 10 | Compare Folders                                                                                                | |
00537 * | 11 | remove all files from TEST_DIR2                                                                                | PAL_SUCCESS |
00538 * | 12 | remove all files from TEST_DIR                                                                                 | PAL_SUCCESS |
00539 * | 13 | Start Loop i from [0 - TEST_NUMBER_OF_FILE_TO_CREATE]                                                          | |
00540 * | 14 | open Files in DIR_FILE named f_i (i - index of loop) with PAL_ERR_FS_READONLY mode using pal_fsFopen           | PAL_ERR_FS_NO_FILE |
00541 * | 15 | open Files in DIR_FILE named f_i (i - index of loop) with PAL_ERR_FS_READWRITE mode using pal_fsFopen          | PAL_ERR_FS_NO_FILE |
00542 * | 16 | open Files in DIR_FILE2 named f_i (i - index of loop) with PAL_ERR_FS_READONLY mode using pal_fsFopen          | PAL_ERR_FS_NO_FILE |
00543 * | 17 | open Files in DIR_FILE2 named f_i (i - index of loop) with PAL_ERR_FS_READWRITE mode using pal_fsFopen         | PAL_ERR_FS_NO_FILE |
00544 * | 18 | remove TEST_DIR with pal_fsRmDir                                                                               | PAL_SUCCESS |
00545 * | 19 | remove TEST_DIR2 with pal_fsRmDir                                                                              | PAL_SUCCESS |
00546 * | 20 | try to remove a file that does not exist                                                                       | PAL_ERR_FS_NO_FILE |
00547 * | 21 | try to remove a a variety of files that does not exist                                                         | PAL_ERR_FS_NO_PATH |
00548 * | 22 | try to copy a non existing folder                                                                              | PAL_ERR_FS_NO_PATH |
00549 *
00550 */
00551 void FilesTests(pal_fsStorageID_t storageId)
00552 {
00553     palStatus_t status = PAL_SUCCESS;
00554     char rootPathBuffer1[PAL_MAX_FILE_AND_FOLDER_LENGTH];
00555     char rootPathBuffer2[PAL_MAX_FILE_AND_FOLDER_LENGTH];
00556     char *buffer1;
00557     char *buffer2;
00558     int i = 0;
00559     size_t numOfBytes;
00560 
00561 /*#1*/
00562     //---------------- INIT TESTS----------------------------//
00563 
00564     // code expects to bufferX to have at least PAL_MAX_FILE_AND_FOLDER_LENGTH and TEST_BYTES_TO_WRITE
00565     // bytes available, so it needs to be calculated on the fly.
00566     const size_t test_buffer_size = (PAL_MAX_FILE_AND_FOLDER_LENGTH > TEST_BYTES_TO_WRITE) ? PAL_MAX_FILE_AND_FOLDER_LENGTH : TEST_BYTES_TO_WRITE;
00567 
00568     buffer1 = malloc(test_buffer_size);
00569     TEST_ASSERT_NOT_NULL(buffer1);
00570 
00571     buffer2 = malloc(test_buffer_size);
00572     TEST_ASSERT_NOT_NULL(buffer2);
00573 
00574     memset(rootPathBuffer1, '1', PAL_MAX_FILE_AND_FOLDER_LENGTH);
00575     memset(rootPathBuffer2, '2', PAL_MAX_FILE_AND_FOLDER_LENGTH);
00576     memset(buffer1, '1', test_buffer_size);
00577     memset(buffer2, '2', test_buffer_size);
00578     //----------------END INIT TESTS-------------------------//
00579 
00580 
00581 /*#2*/
00582     status = pal_fsMkDir(addRootToPath(TEST_DIR,rootPathBuffer1,storageId)); //Create Directory
00583     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00584 
00585 /*#3*/
00586     status = pal_fsMkDir(addRootToPath(TEST_DIR2,rootPathBuffer2,storageId)); //Create Directory
00587     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00588 
00589 /*#4*/
00590     for(i = 0; i < TEST_NUMBER_OF_FILE_TO_CREATE; i++)
00591     {
00592 /*#5*/
00593         snprintf(rootPathBuffer1, PAL_MAX_FILE_AND_FOLDER_LENGTH, TEST_FILE_NAME, TEST_DIR, i);
00594         status =  pal_fsFopen(addRootToPath(rootPathBuffer1,buffer1,storageId), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
00595         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00596 
00597 /*#6*/
00598         status =  pal_fsFwrite(&g_fd1, (void *)buffer1, test_buffer_size, &numOfBytes);
00599         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00600 
00601 /*#7*/
00602         status =  pal_fsFclose(&g_fd1);
00603         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00604 /*#8*/
00605     }
00606 
00607 /*#9*/
00608     status = pal_fsCpFolder(addRootToPath(TEST_DIR,rootPathBuffer1,storageId), addRootToPath(TEST_DIR2,rootPathBuffer2,storageId));//Copy all files from TEST_DIR to TEST_DIR2
00609     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00610 /*#10*/
00611     for(i = 0; i < TEST_NUMBER_OF_FILE_TO_CREATE; i++)
00612     {
00613         snprintf(rootPathBuffer1, PAL_MAX_FILE_AND_FOLDER_LENGTH, TEST_FILE_NAME, TEST_DIR, i);
00614         snprintf(rootPathBuffer2, PAL_MAX_FILE_AND_FOLDER_LENGTH, TEST_FILE_NAME, TEST_DIR2, i);
00615 
00616         fileSystemCompareUtil(addRootToPath(rootPathBuffer1,buffer1,storageId), addRootToPath(rootPathBuffer2,buffer2,storageId));
00617     }
00618 
00619 /*#11*/
00620     status = pal_fsRmFiles(addRootToPath(TEST_DIR2,rootPathBuffer2,storageId));//Remove all files in the testing DIRECTORY
00621     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00622 
00623 /*#12*/
00624     status = pal_fsRmFiles(addRootToPath(TEST_DIR,rootPathBuffer1,storageId));//Remove all files in the testing DIRECTORY
00625     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00626 
00627 /*#13*/
00628     for(i = 0; i < TEST_NUMBER_OF_FILE_TO_CREATE; i++)
00629     {
00630 /*#14*/
00631         snprintf(buffer1, PAL_MAX_FILE_AND_FOLDER_LENGTH, TEST_FILE_NAME, TEST_DIR, i);
00632         status =  pal_fsFopen(addRootToPath(buffer1,rootPathBuffer1,storageId), PAL_FS_FLAG_READONLY, &g_fd1);
00633         TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_FILE, status);    //Failed all files where deleted in previous step
00634 
00635 /*#15*/
00636         status =  pal_fsFopen(addRootToPath(buffer1,rootPathBuffer1,storageId), PAL_FS_FLAG_READWRITE, &g_fd1);
00637         TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_FILE, status);    //Failed all files where deleted in previous step
00638 
00639 /*#16*/
00640         snprintf(buffer2, PAL_MAX_FILE_AND_FOLDER_LENGTH, TEST_FILE_NAME, TEST_DIR2, i);
00641         status =  pal_fsFopen(addRootToPath(buffer2,rootPathBuffer2,storageId), PAL_FS_FLAG_READONLY, &g_fd1);
00642         TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_FILE, status);    //Failed all files where deleted in previous step
00643 
00644 /*#17*/
00645         status =  pal_fsFopen(addRootToPath(buffer1,rootPathBuffer1,storageId), PAL_FS_FLAG_READWRITE, &g_fd1);
00646         TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_FILE, status);    //Failed all files where deleted in previous step
00647 
00648     }
00649 
00650 /*#18*/
00651     status = pal_fsRmDir(addRootToPath(TEST_DIR,buffer1,storageId));
00652     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00653 
00654 /*#19*/
00655     status = pal_fsRmDir(addRootToPath(TEST_DIR2,buffer2,storageId));
00656     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00657 
00658 /*#20*/
00659     status = pal_fsUnlink(addRootToPath("aaaa.t",rootPathBuffer1, storageId));//not existing file
00660     TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_FILE, status);
00661 
00662 /*#21*/
00663     status = pal_fsRmFiles(addRootToPath("aaaaa",rootPathBuffer1, storageId));//Remove all file in not existing directory
00664     TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_PATH, status);
00665 
00666 /*#22*/
00667 
00668     status = pal_fsCpFolder(addRootToPath("aaaaa", rootPathBuffer1, storageId), addRootToPath("bbbb" ,rootPathBuffer2,storageId)); //copy from not existing dir
00669     TEST_ASSERT_EQUAL(PAL_ERR_FS_NO_PATH, status);
00670 
00671     free(buffer1);
00672     free(buffer2);
00673 }
00674 
00675 TEST(pal_fileSystem, FilesTests)
00676 {
00677     FilesTests(PAL_FS_PARTITION_PRIMARY);
00678     FilesTests(PAL_FS_PARTITION_SECONDARY);
00679 }
00680 
00681 /*! \brief /b FilesTestsSeek function Tests \b fseek() , \b fteel() & \b fread() function
00682 *
00683 ** \test
00684 * | # |    Step                        |   Expected  |
00685 * |---|--------------------------------|-------------|
00686 * | 1 | create TEST_DIR with pal_fsMkDir                                                        | PAL_SUCCESS |
00687 * | 2 | Create File TEST_DIR_FILE With PAL_ERR_FS_READWRITETRUNC with pal_fsFopen               | PAL_SUCCESS |
00688 * | 3 | Create buffer[TEST_BUFFER_SIZE] with incremental data, Buffer size TEST_BUFFER_SIZE     | PAL_SUCCESS |
00689 * | 4 | Write buffer to file with pal_fsFwrite                                                  | PAL_SUCCESS |
00690 * | 5 | Start Loop     i from [0 - TEST_BUFFER_SIZE]                                            | |
00691 * | 6 | run pal_fsFseek with PAL_FS_OFFSET_SEEKSET option and incremental offset i              | PAL_SUCCESS |
00692 * | 7 | run    pal_fsFtell and compare offset to i                                              | PAL_SUCCESS |
00693 * | 8 | run    pal_fsFread, read one byte and compare it to the buffer[i]                       | PAL_SUCCESS |
00694 * | 9 | End Loop                                                                                | |
00695 * | 10 | Start Loop i from [0 - TEST_BUFFER_SIZE]                                               | |
00696 * | 11 | run pal_fsFseek with PAL_FS_OFFSET_SEEKEND option and incremental offset (-1)*i        | PAL_SUCCESS |
00697 * | 12 | run pal_fsFtell and compare offset to TEST_BUFFER_SIZE - i                             | PAL_SUCCESS |
00698 * | 13 | End Loop                                                                               | |
00699 * | 14 | run pal_fsFseek with PAL_FS_OFFSET_SEEKSET option offset TEST_BUFFER_SIZE/2            | PAL_SUCCESS |
00700 * | 15 | Start Loop i from [0 - TEST_BUFFER_SIZE/10]                                            | |
00701 * | 16 | run pal_fsFseek with PAL_FS_OFFSET_SEEKEND option and incremental offset i             | PAL_SUCCESS |
00702 * | 17 | run    pal_fsFtell and compare offset to i                                             | PAL_SUCCESS |
00703 * | 18 | End Loop                                                                               | |
00704 * | 19 | Cleanup                                                                                | PAL_SUCCESS |
00705 *
00706 */
00707 void FilesTestsSeek(pal_fsStorageID_t storageId)
00708 {
00709     palStatus_t status = PAL_SUCCESS;
00710     char buffer[TEST_BUFFER_SIZE];
00711     int i = 0;
00712     size_t numOfBytes;
00713     int32_t pos = 0;
00714     char read_buf = 1;
00715     size_t prePos = 0;
00716 /*#1*/
00717     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,storageId)); //Create Directory
00718     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00719 
00720 /*#2*/
00721     status =  pal_fsFopen(addRootToPath(TEST_DIR_FILE,buffer,storageId), PAL_FS_FLAG_READWRITETRUNC, &g_fd1);
00722     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00723 
00724 /*#3*/
00725     for(i = 0; i < TEST_BUFFER_SIZE; i++)
00726     {
00727         buffer[i] = i;
00728     }
00729 
00730 /*#4*/
00731     status =  pal_fsFwrite(&g_fd1, (void *)buffer, TEST_BUFFER_SIZE, &numOfBytes);  //Write incremental buffer for seek testing
00732     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00733     TEST_ASSERT_EQUAL(TEST_BUFFER_SIZE, numOfBytes);
00734 
00735 /*#5*/
00736     //Test Seek "PAL_FS_OFFSET_SEEKSET"
00737     for(i = 0; i < TEST_BUFFER_SIZE; i++)
00738     {
00739 
00740 /*#6*/    
00741         status = pal_fsFseek(&g_fd1, i, PAL_FS_OFFSET_SEEKSET); //Set position to start of the stream
00742         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00743 
00744 /*#7*/    
00745         status = pal_fsFtell(&g_fd1, &pos); //Check if position is in the start of the stream
00746         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00747         TEST_ASSERT_EQUAL(i, pos);
00748 
00749 /*#8*/    
00750         status  = pal_fsFread(&g_fd1, &read_buf, 1, &numOfBytes);
00751         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00752         TEST_ASSERT_EQUAL(1, numOfBytes);
00753         TEST_ASSERT_EQUAL(buffer[i], read_buf);
00754 
00755 /*#9*/
00756     }
00757     
00758 /*#10*/
00759     //Test Seek "PAL_FS_OFFSET_SEEKEND"
00760     for(i = 0; i < TEST_BUFFER_SIZE; i++)
00761     {
00762 /*#11*/    
00763         status = pal_fsFseek(&g_fd1, (-1)*i, PAL_FS_OFFSET_SEEKEND); //Set position to end  of the stream
00764         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00765 
00766 /*#12*/    
00767         status = pal_fsFtell(&g_fd1, &pos); //Get position
00768         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00769         TEST_ASSERT_EQUAL(TEST_BUFFER_SIZE - i, pos);
00770 
00771 /*#13*/
00772     }
00773 
00774 /*#14*/
00775     //Test Seek "PAL_ERR_FS_SEEKCUR"
00776     status = pal_fsFseek(&g_fd1, TEST_BUFFER_SIZE/2, PAL_FS_OFFSET_SEEKSET); //Set position to middle of the stream
00777     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00778     prePos = TEST_BUFFER_SIZE/2;
00779 
00780 /*#15*/
00781     for(i = 0; i < TEST_BUFFER_SIZE/10 ; i++)
00782     {
00783 
00784 /*#16*/    
00785         status = pal_fsFseek(&g_fd1, i, PAL_FS_OFFSET_SEEKCUR); //Set position to start of the stream
00786         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00787 
00788 /*17*/    
00789         status = pal_fsFtell(&g_fd1, &pos); //Get position
00790         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00791         TEST_ASSERT_EQUAL(prePos + i, pos);
00792         prePos = pos;
00793 
00794 /*#18*/
00795     }
00796 
00797 /*#19*/
00798     status =  pal_fsFclose(&g_fd1);
00799     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00800 }
00801 
00802 
00803 TEST(pal_fileSystem, FilesTestsSeek)
00804 {
00805     FilesTestsSeek(PAL_FS_PARTITION_PRIMARY);
00806 #if (PAL_NUMBER_OF_PARTITIONS == 2)
00807     FilesTestsSeek(PAL_FS_PARTITION_SECONDARY);
00808 #endif
00809 }
00810 
00811 /*! \brief /b FilesPermission function Tests \b fopen() with r
00812 *
00813 ** \test
00814 * | # |    Step                        |   Expected  |
00815 * |---|--------------------------------|-------------|
00816 * | 1 | Init Test                                                                               | Success |
00817 * | 2 | create TEST_DIR with pal_fsMkDir                                                        | PAL_SUCCESS |
00818 * | 3 | create new file using fopen with PAL_FS_FLAG_READWRITEEXCLUSIVE                         | PAL_SUCCESS |
00819 * | 4 | write buffer to file                                                                    | PAL_SUCCESS |
00820 * | 5 | close file                                                                              | PAL_SUCCESS |
00821 * | 6 | open file using fopen() with PAL_FS_FLAG_READONLY                                       | PAL_SUCCESS |
00822 * | 7 | write buffer to file with fwrite()                                                      | failed |
00823 * | 8 | read buffer from file with fread()                                                      | PAL_SUCCESS |
00824 * | 9 | close file                                                                              | PAL_SUCCESS |
00825 * | 10 | remove all files in folder                                                             | PAL_SUCCESS |
00826 * | 11 | remove folder                                                                          | PAL_SUCCESS |
00827 */
00828 void FilesPermission_read_only(pal_fsStorageID_t storageId)
00829 {
00830     palStatus_t status = PAL_SUCCESS;
00831     char readBuffer[TEST_BUFFER_SIZE];
00832     char readBuffer2[TEST_BUFFER_SIZE];
00833     char filename[TEST_BUFFER_SIZE];
00834     size_t numOfBytes = 0;
00835     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00836 
00837 /*#1*/
00838     //---------------- INIT TESTS----------------------------//
00839     memset(readBuffer, '1', TEST_BUFFER_SIZE);
00840     //----------------END INIT TESTS-------------------------//
00841 
00842 /*#2*/
00843     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,storageId)); //Create Directory
00844     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00845 
00846     snprintf(filename, TEST_BUFFER_SIZE, TEST_FILE_NAME, TEST_DIR, 1);
00847 /*#3*/
00848     status =  pal_fsFopen(addRootToPath(filename,buffer,storageId), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
00849     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00850 
00851 /*#4*/
00852     status =  pal_fsFwrite(&g_fd1, (void *)readBuffer, TEST_BYTES_TO_WRITE, &numOfBytes);
00853     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00854     TEST_ASSERT_EQUAL(TEST_BYTES_TO_WRITE, numOfBytes);
00855 
00856 /*#5*/
00857     status =  pal_fsFclose(&g_fd1);
00858     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00859 
00860 /*#6*/
00861     status =  pal_fsFopen(addRootToPath(filename,buffer,storageId), PAL_FS_FLAG_READONLY, &g_fd1);
00862     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00863 
00864 /*#7*/
00865     pal_fsFwrite(&g_fd1, (void *)readBuffer, TEST_BYTES_TO_WRITE, &numOfBytes);
00866     TEST_ASSERT_EQUAL(0, numOfBytes);
00867 
00868 /*#8*/
00869     status = pal_fsFread(&g_fd1, readBuffer2, TEST_BUFFER_SIZE, &numOfBytes);
00870     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00871     TEST_ASSERT_EQUAL(TEST_BUFFER_SIZE, numOfBytes);
00872     TEST_ASSERT_EQUAL_MEMORY(readBuffer, readBuffer2, TEST_BUFFER_SIZE);
00873 
00874 /*#9*/
00875     status =  pal_fsFclose(&g_fd1);
00876     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00877 
00878 /*#10*/
00879     status = pal_fsRmFiles(addRootToPath(TEST_DIR,buffer,storageId));//Remove all files in the testing DIRECTORY
00880     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00881 
00882 /*#11*/
00883     status = pal_fsRmDir(addRootToPath(TEST_DIR,buffer,storageId)); //Delete Directory if exist
00884     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00885 }
00886 
00887 TEST(pal_fileSystem, FilesPermission_read_only)
00888 {
00889     FilesPermission_read_only(PAL_FS_PARTITION_PRIMARY);
00890     FilesPermission_read_only(PAL_FS_PARTITION_SECONDARY);
00891 }
00892 
00893 
00894 /*! \brief /b FilesPermission function Tests \b fopen() with r+
00895 *
00896 ** \test
00897 * | # |    Step                        |   Expected  |
00898 * |---|--------------------------------|-------------|
00899 * | 1 | Init Test                                                                               | Success |
00900 * | 2 | create TEST_DIR with pal_fsMkDir                                                        | PAL_SUCCESS |
00901 * | 3 | create new file using fopen with PAL_FS_FLAG_READWRITEEXCLUSIVE                         | PAL_SUCCESS |
00902 * | 4 | write buffer to file                                                                    | PAL_SUCCESS |
00903 * | 5 | close file                                                                              | PAL_SUCCESS |
00904 * | 6 | open file using fopen() with PAL_FS_FLAG_READONLY                                       | PAL_SUCCESS |
00905 * | 7 | write buffer to file with fwrite()                                                      | PAL_SUCCESS |
00906 * | 8 | seek to the begining of the file                                                        | PAL_SUCCESS |
00907 * | 9 | read buffer from file with fread()                                                      | PAL_SUCCESS |
00908 * | 10 | close file                                                                             | PAL_SUCCESS |
00909 * | 11 | remove all files in folder                                                             | PAL_SUCCESS |
00910 * | 12 | remove folder                                                                          | PAL_SUCCESS |
00911 */
00912 void FilesPermission_read_write(pal_fsStorageID_t storageId)
00913 {
00914     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00915     palStatus_t status = PAL_SUCCESS;
00916     char readBuffer[TEST_BUFFER_SIZE];
00917     char readBuffer2[TEST_BUFFER_SIZE];
00918     char filename[TEST_BUFFER_SIZE];
00919     size_t numOfBytes = 0;
00920 
00921 /*#1*/
00922     //---------------- INIT TESTS----------------------------//
00923     memset(readBuffer, '1', TEST_BUFFER_SIZE);
00924     //----------------END INIT TESTS-------------------------//
00925 
00926 /*#2*/
00927     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,storageId)); //Create Directory
00928     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00929 
00930     snprintf(filename, TEST_BUFFER_SIZE, TEST_FILE_NAME, TEST_DIR, 1);
00931 /*#3*/
00932     status =  pal_fsFopen(addRootToPath(filename,buffer,storageId), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
00933     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00934 
00935 /*#4*/
00936     status =  pal_fsFwrite(&g_fd1, (void *)readBuffer, TEST_BYTES_TO_WRITE, &numOfBytes);
00937     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00938     TEST_ASSERT_EQUAL(TEST_BYTES_TO_WRITE, numOfBytes);
00939 
00940 /*#5*/
00941     status =  pal_fsFclose(&g_fd1);
00942     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00943 
00944 /*#6*/
00945     status =  pal_fsFopen(addRootToPath(filename,buffer,storageId), PAL_FS_FLAG_READWRITE, &g_fd1);
00946     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00947 
00948 /*#7*/
00949     status =  pal_fsFwrite(&g_fd1, (void *)readBuffer, TEST_BYTES_TO_WRITE, &numOfBytes);
00950     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00951     TEST_ASSERT_EQUAL(TEST_BYTES_TO_WRITE, numOfBytes);
00952 
00953 /*#8*/
00954     status = pal_fsFseek(&g_fd1, 0, PAL_FS_OFFSET_SEEKSET); //Set position to start of the stream
00955     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00956 
00957 /*#9*/
00958     status = pal_fsFread(&g_fd1, readBuffer2, TEST_BUFFER_SIZE, &numOfBytes);
00959     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00960     TEST_ASSERT_EQUAL(TEST_BYTES_TO_WRITE, numOfBytes);
00961     TEST_ASSERT_EQUAL_MEMORY(readBuffer, readBuffer2, TEST_BUFFER_SIZE);
00962 
00963 /*#10*/
00964     status =  pal_fsFclose(&g_fd1);
00965     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00966 
00967 /*#11*/
00968     status = pal_fsRmFiles(addRootToPath(TEST_DIR,buffer,storageId));//Remove all files in the testing DIRECTORY
00969     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00970 
00971 /*#12*/
00972     status = pal_fsRmDir(addRootToPath(TEST_DIR,buffer,storageId)); //Delete Directory if exist
00973     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00974 }
00975 
00976 
00977 TEST(pal_fileSystem, FilesPermission_read_write)
00978 {
00979     FilesPermission_read_write(PAL_FS_PARTITION_PRIMARY);
00980     FilesPermission_read_write(PAL_FS_PARTITION_SECONDARY);
00981 }
00982 
00983 
00984 /*! \brief /b FilesPermission function Tests \b fopen() with w+x
00985 *
00986 ** \test
00987 * | # |    Step                        |   Expected  |
00988 * |---|--------------------------------|-------------|
00989 * | 1 | Init Test                                                                               | Success |
00990 * | 2 | create TEST_DIR with pal_fsMkDir                                                        | PAL_SUCCESS |
00991 * | 3 | create new file using fopen with PAL_FS_FLAG_READWRITEEXCLUSIVE                         | PAL_SUCCESS |
00992 * | 4 | write buffer to file                                                                    | PAL_SUCCESS |
00993 * | 5 | close file                                                                              | PAL_SUCCESS |
00994 * | 6 | open file using fopen() withPAL_FS_FLAG_READWRITETRUNC                                  | PAL_SUCCESS |
00995 * | 7 | read buffer from file with fread()                                                      | PAL_SUCCESS with read length 0 |
00996 * | 8 | close file                                                                              | PAL_SUCCESS |
00997 * | 9 | remove all files in folder                                                              | PAL_SUCCESS |
00998 * | 10 | remove folder                                                                          | PAL_SUCCESS |
00999 */
01000 void FilesPermission_read_write_trunc(pal_fsStorageID_t storageId)
01001 {
01002     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
01003     palStatus_t status = PAL_SUCCESS;
01004     char readBuffer[TEST_BUFFER_SIZE];
01005     char filename[TEST_BUFFER_SIZE];
01006     size_t numOfBytes = 0;
01007 
01008 /*#1*/
01009     //---------------- INIT TESTS----------------------------//
01010     memset(readBuffer, '1', TEST_BUFFER_SIZE);
01011     //----------------END INIT TESTS-------------------------//
01012 
01013 /*#2*/
01014     status = pal_fsMkDir(addRootToPath(TEST_DIR,buffer,storageId)); //Create Directory
01015     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01016 
01017     snprintf(filename, TEST_BUFFER_SIZE, TEST_FILE_NAME, TEST_DIR, 1);
01018 /*#3*/
01019     status = pal_fsFopen(addRootToPath(filename,buffer,storageId), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
01020     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01021 
01022 /*#4*/
01023     status = pal_fsFwrite(&g_fd1, (void *)readBuffer, TEST_BYTES_TO_WRITE, &numOfBytes);
01024     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01025     TEST_ASSERT_EQUAL(TEST_BYTES_TO_WRITE, numOfBytes);
01026 
01027 /*#5*/
01028     status = pal_fsFclose(&g_fd1);
01029     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01030 
01031 /*#6*/
01032     status = pal_fsFopen(addRootToPath(filename,buffer,storageId), PAL_FS_FLAG_READWRITETRUNC, &g_fd1);
01033     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01034 
01035 /*#7*/
01036     status = pal_fsFread(&g_fd1, readBuffer, TEST_BUFFER_SIZE, &numOfBytes);
01037     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01038     TEST_ASSERT_EQUAL(0, numOfBytes); //nothing to read empty file
01039 
01040 /*#8*/
01041     status = pal_fsFclose(&g_fd1);
01042     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01043 
01044 /*#9*/
01045     status = pal_fsRmFiles(addRootToPath(TEST_DIR,buffer,storageId));//Remove all files in the testing DIRECTORY
01046     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01047 
01048 /*#10*/
01049     status = pal_fsRmDir(addRootToPath(TEST_DIR,buffer,storageId)); //Delete Directory if exist
01050     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
01051 }
01052 
01053 
01054 TEST(pal_fileSystem, FilesPermission_read_write_trunc)
01055 {
01056     FilesPermission_read_write_trunc(PAL_FS_PARTITION_PRIMARY);
01057     FilesPermission_read_write_trunc(PAL_FS_PARTITION_SECONDARY);
01058 }
01059 
01060 
01061 void create_write_and_read_pal_file(pal_fsStorageID_t storageId)
01062 {
01063     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
01064     char fileName[] = "fileName";
01065     palStatus_t res = PAL_SUCCESS;
01066     size_t num_bytes_write = 0;
01067     size_t num_bytes_read = 0;
01068 
01069     uint32_t i = 0;
01070 
01071     bufferTest = malloc(BUFFER_TEST_SIZE);
01072     TEST_ASSERT_NOT_EQUAL(bufferTest, NULL);
01073     bufferTest2 = malloc(BUFFER_TEST_SIZE);
01074     TEST_ASSERT_NOT_EQUAL(bufferTest2, NULL);
01075     memset(bufferTest, 0, BUFFER_TEST_SIZE);
01076     memset(bufferTest2, 0, BUFFER_TEST_SIZE);
01077 
01078     for (i = 0; i < BUFFER_TEST_SIZE; i++){
01079             bufferTest[i] = (uint8_t)(i % 256);
01080     }
01081 
01082     pal_fsUnlink(addRootToPath(fileName,buffer,storageId));
01083 
01084     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READWRITEEXCLUSIVE, &(g_fd1));
01085     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01086 
01087     res = pal_fsFwrite(&(g_fd1), bufferTest, BUFFER_TEST_SIZE, &num_bytes_write);
01088     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01089     TEST_ASSERT_EQUAL(BUFFER_TEST_SIZE, num_bytes_write);
01090 
01091     res = pal_fsFclose(&(g_fd1));
01092     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01093 
01094     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READONLY, &(g_fd1));
01095     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01096 
01097     res = pal_fsFread(&(g_fd1), bufferTest2, 223, &num_bytes_read);
01098     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01099     TEST_ASSERT_EQUAL(223, num_bytes_read);
01100 
01101     // split the reads
01102     res = pal_fsFread(&(g_fd1), bufferTest2, 900, &num_bytes_read);
01103     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01104     TEST_ASSERT_EQUAL(900, num_bytes_read);
01105 
01106 
01107     // Compare the buffers � here we have a mismatch in location buffer2[288]
01108     TEST_ASSERT_EQUAL_INT8_ARRAY(&(bufferTest[223]), bufferTest2, 900);
01109 
01110     res = pal_fsFclose(&(g_fd1));
01111     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01112 
01113     res = pal_fsUnlink(addRootToPath(fileName,buffer,storageId));
01114     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01115 
01116     free(bufferTest);
01117     bufferTest = NULL;
01118     free(bufferTest2);
01119     bufferTest2 = NULL;
01120     
01121 }
01122 
01123 
01124 TEST(pal_fileSystem, create_write_and_read_pal_file)
01125 {
01126     create_write_and_read_pal_file(PAL_FS_PARTITION_PRIMARY);
01127     create_write_and_read_pal_file(PAL_FS_PARTITION_SECONDARY);
01128 }
01129 
01130 void WriteInTheMiddle(pal_fsStorageID_t storageId)
01131 {
01132     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
01133     char fileName[] = "fileName";
01134     const uint32_t inputSizeSmall = (TEST_BUFFER_SIZE * 6) + 1;
01135     const uint32_t inputSizeBig = BUFFER_TEST_SIZE;
01136     palStatus_t res = PAL_SUCCESS;
01137     size_t num_bytes_write = 0;
01138     size_t num_bytes_read = 0;
01139     unsigned char *smallBuffer = NULL;
01140     const size_t offset = 200;
01141     uint32_t residue = inputSizeBig - (offset + inputSizeSmall);
01142 
01143     bufferTest = malloc(inputSizeBig);
01144     TEST_ASSERT_NOT_EQUAL(bufferTest, NULL);
01145     bufferTest2 = malloc(inputSizeBig);
01146     TEST_ASSERT_NOT_EQUAL(bufferTest2, NULL);
01147     smallBuffer = malloc(inputSizeSmall);
01148     TEST_ASSERT_NOT_EQUAL(smallBuffer, NULL);
01149     memset(bufferTest, 0, inputSizeBig);
01150     memset(bufferTest2, 0, inputSizeBig);
01151     memset(smallBuffer, 0, inputSizeSmall);
01152 
01153     // create 1123 bytes buffer filled with the numbers 0..255
01154     for (uint32_t i = 0; i < inputSizeBig; i++)
01155     {
01156         bufferTest[i] = (uint8_t)(i % 256);
01157     }
01158 
01159     // create 601 bytes buffer filled with only 0xCC
01160     for (uint32_t i = 0; i < inputSizeSmall; i++)
01161     {
01162         smallBuffer[i] = 0xCC;
01163     }
01164 
01165     pal_fsUnlink(addRootToPath(fileName,buffer,storageId));
01166     TEST_ASSERT((PAL_SUCCESS == res) || (PAL_ERR_FS_NO_FILE == res));
01167 
01168     /* 1. Write bufferTest data to file, read it and compare read content to bufferTest */
01169     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
01170     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01171 
01172     res = pal_fsFwrite(&g_fd1, bufferTest, inputSizeBig, &num_bytes_write);
01173     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01174     TEST_ASSERT_EQUAL(inputSizeBig, num_bytes_write);
01175 
01176     res = pal_fsFclose(&g_fd1);
01177     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01178 
01179     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READONLY, &g_fd1);
01180     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01181 
01182     res = pal_fsFread(&g_fd1, bufferTest2, inputSizeBig, &num_bytes_read);
01183     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01184     TEST_ASSERT_EQUAL(inputSizeBig, num_bytes_read);
01185     TEST_ASSERT_EQUAL_HEX8_ARRAY(bufferTest, bufferTest2, inputSizeBig);
01186 
01187     res = pal_fsFclose(&g_fd1);
01188     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01189 
01190     /* 
01191     2. Write smallBuffer data to offset 201 of the file and then compare each fragment:
01192         - offset 0..200 equal to bufferTest[0..200]
01193         - offset 201..801 equal to smallBuffer
01194         - offset 802..1122 equal to bufferTest[802..1122]
01195     */
01196     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READWRITE, &g_fd1);
01197     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01198 
01199     res = pal_fsFseek(&g_fd1, offset, PAL_FS_OFFSET_SEEKSET);
01200     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01201 
01202     res = pal_fsFwrite(&g_fd1, smallBuffer, inputSizeSmall, &num_bytes_write);
01203     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01204     TEST_ASSERT_EQUAL(inputSizeSmall, num_bytes_write);
01205 
01206     res = pal_fsFclose(&g_fd1);
01207     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01208 
01209     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READONLY, &g_fd1);
01210     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01211 
01212     // offset 0..200 equal to bufferTest[0..200]
01213     res = pal_fsFread(&g_fd1, bufferTest2, offset, &num_bytes_read);
01214     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01215     TEST_ASSERT_EQUAL(offset, num_bytes_read);
01216     TEST_ASSERT_EQUAL_HEX8_ARRAY(bufferTest, bufferTest2, offset);
01217 
01218     // offset 201..801 equal to smallBuffer
01219     res = pal_fsFread(&g_fd1, bufferTest2, inputSizeSmall, &num_bytes_read);
01220     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01221     TEST_ASSERT_EQUAL(inputSizeSmall, num_bytes_read);
01222     TEST_ASSERT_EQUAL_HEX8_ARRAY(smallBuffer, bufferTest2, inputSizeSmall);
01223 
01224     // offset 802..1122 equal to bufferTest[802..1122]
01225     res = pal_fsFread(&g_fd1, bufferTest2, residue, &num_bytes_read);
01226     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01227     TEST_ASSERT_EQUAL(residue, num_bytes_read);
01228     TEST_ASSERT_EQUAL_HEX8_ARRAY(bufferTest + offset + inputSizeSmall, bufferTest2, residue);
01229 
01230     res = pal_fsFclose(&g_fd1);
01231     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01232 
01233     res = pal_fsUnlink(addRootToPath(fileName,buffer,storageId));
01234     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01235 
01236     free(bufferTest);
01237     bufferTest = NULL;
01238     free(bufferTest2);
01239     bufferTest2 = NULL;
01240     free(smallBuffer);
01241     smallBuffer = NULL;
01242 }
01243 
01244 TEST(pal_fileSystem, WriteInTheMiddle)
01245 {
01246     WriteInTheMiddle(PAL_FS_PARTITION_PRIMARY);
01247     WriteInTheMiddle(PAL_FS_PARTITION_SECONDARY);
01248 }
01249 
01250 
01251 
01252 
01253 void SequentialWriteAndRead(pal_fsStorageID_t storageId)
01254 {
01255     char buffer[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
01256     char fileName[] = "fileName";
01257     palStatus_t res = PAL_SUCCESS;
01258     size_t num_bytes_write = 0;
01259     size_t num_bytes_read = 0;
01260     unsigned char small_write_buffer[TEST_BUFFER_SMALL_SIZE] = {
01261         0x2D, 0x6B, 0xAC, 0xCC, 0x08, 0x6B, 0x14, 0x82,
01262         0xF3, 0x0C, 0xF5, 0x67, 0x17, 0x23, 0x50, 0xB4,
01263         0xFF
01264     };
01265     unsigned char small_read_buffer[TEST_BUFFER_SMALL_SIZE] = { 0 };
01266     bufferTest = malloc(BUFFER_TEST_SIZE);
01267     TEST_ASSERT_NOT_EQUAL(bufferTest, NULL);
01268     bufferTest2 = malloc(BUFFER_TEST_SIZE);
01269     TEST_ASSERT_NOT_EQUAL(bufferTest2, NULL);
01270     memset(bufferTest, 0, BUFFER_TEST_SIZE);
01271     memset(bufferTest2, 0, BUFFER_TEST_SIZE);
01272 
01273     // create 1123 bytes buffer filled with the numbers 0..255
01274     for (uint32_t i = 0; i < BUFFER_TEST_SIZE; i++)
01275     {
01276         bufferTest[i] = (uint8_t)(i % 256);
01277     }
01278     res = pal_fsUnlink(addRootToPath(fileName,buffer,storageId));
01279     TEST_ASSERT((PAL_SUCCESS == res) || (PAL_ERR_FS_NO_FILE == res));
01280 
01281     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READWRITEEXCLUSIVE, &g_fd1);
01282     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01283 
01284     // split the writes
01285     res = pal_fsFwrite(&g_fd1, small_write_buffer, TEST_BUFFER_SMALL_SIZE, &num_bytes_write);
01286     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01287     TEST_ASSERT_EQUAL(TEST_BUFFER_SMALL_SIZE, num_bytes_write);
01288 
01289     res = pal_fsFwrite(&g_fd1, bufferTest, BUFFER_TEST_SIZE, &num_bytes_write);
01290     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01291     TEST_ASSERT_EQUAL(BUFFER_TEST_SIZE, num_bytes_write);
01292 
01293     res = pal_fsFclose(&g_fd1);
01294     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01295 
01296     res = pal_fsFopen(addRootToPath(fileName,buffer,storageId), PAL_FS_FLAG_READONLY, &g_fd1);
01297     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01298 
01299     // split the reads
01300     res = pal_fsFread(&g_fd1, small_read_buffer, TEST_BUFFER_SMALL_SIZE, &num_bytes_read);
01301     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01302     TEST_ASSERT_EQUAL(TEST_BUFFER_SMALL_SIZE, num_bytes_read);
01303 
01304     res = pal_fsFread(&g_fd1, bufferTest2, BUFFER_TEST_SIZE, &num_bytes_read);
01305     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01306     TEST_ASSERT_EQUAL(BUFFER_TEST_SIZE, num_bytes_read);
01307 
01308     TEST_ASSERT_EQUAL_INT8_ARRAY(bufferTest, bufferTest2, BUFFER_TEST_SIZE);
01309 
01310     res = pal_fsFclose(&g_fd1);
01311     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01312 
01313     res = pal_fsUnlink(addRootToPath(fileName,buffer,storageId));
01314     TEST_ASSERT_EQUAL(PAL_SUCCESS, res);
01315 
01316     free(bufferTest);
01317     bufferTest = NULL;
01318     free(bufferTest2);
01319     bufferTest2 = NULL;    
01320 }
01321 
01322 TEST(pal_fileSystem, SequentialWriteAndRead)
01323 {
01324     SequentialWriteAndRead(PAL_FS_PARTITION_PRIMARY);
01325     SequentialWriteAndRead(PAL_FS_PARTITION_SECONDARY);
01326 }