Simple interface for Mbed Cloud Client

Dependents:  

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_main.c Source File

test_main.c

00001 
00002 /*******************************************************************************
00003  * Copyright 2016, 2017 ARM Ltd.
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *******************************************************************************/
00017 
00018 #include "stdio.h"
00019 
00020 #include "PlatIncludes.h"
00021 #include "test_runners.h"
00022 #include "pal_BSP.h"
00023 #include "pal.h"
00024 #include "mbed_trace.h"
00025 #include "unity.h"
00026 #include "unity_fixture.h"
00027 
00028 extern struct _Unity Unity;
00029 
00030 #define PAL_TEST_STATUS_FILE_LOCATION "/tstSts"
00031 #define PAL_TEST_STATUS_FILE_DATA_MAX_SIZE 128
00032 
00033 void * g_palTestNetworkInterface = NULL;
00034 void * g_palTestTLSInterfaceCTX = NULL;
00035 
00036 
00037 pal_args_t g_args; // defiend as global so it could persist 
00038                    // during task execution on FreeRTOS
00039 
00040 #ifdef DEBUG
00041 #define PAL_TESTS_LOG_LEVEL ((uint8_t)((TRACE_MASK_LEVEL & TRACE_ACTIVE_LEVEL_ALL) | (TRACE_MASK_CONFIG & TRACE_CARRIAGE_RETURN)))
00042 #else
00043 #define PAL_TESTS_LOG_LEVEL ((uint8_t)((TRACE_MASK_LEVEL & TRACE_ACTIVE_LEVEL_ERROR) | (TRACE_MASK_CONFIG & TRACE_CARRIAGE_RETURN)))
00044 #endif
00045 
00046 
00047 palTestsStatusData_t palTestStatus = {-1,-1,-1,0,0,0};
00048 
00049 
00050 palStatus_t getPalTestStatus(void)
00051 {
00052     palStatus_t status = PAL_SUCCESS, status2 = PAL_SUCCESS;
00053     char filePath[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00054     palFileDescriptor_t fd = 0;
00055     size_t dataSizeWritten = 0;
00056     char data[128] = {0};
00057   
00058 
00059     status = pal_fsGetMountPoint(PAL_FS_PARTITION_PRIMARY, PAL_MAX_FILE_AND_FOLDER_LENGTH, filePath);
00060     if (PAL_SUCCESS == status) 
00061     {
00062         strncat(filePath,PAL_TEST_STATUS_FILE_LOCATION,PAL_MAX_FILE_AND_FOLDER_LENGTH - strlen(filePath));
00063         status = pal_fsFopen(filePath, PAL_FS_FLAG_READONLY, &fd);
00064          if (PAL_SUCCESS == status)
00065          {
00066 
00067              status =  pal_fsFread(&fd, (void *)data, PAL_TEST_STATUS_FILE_DATA_MAX_SIZE, &dataSizeWritten);
00068              if ((PAL_SUCCESS == status) && (dataSizeWritten > 0))
00069              {
00070                  printf("reading DATA into test\r\n");
00071                      sscanf(data,"%i %i %i %llu %llu %llu", &palTestStatus.module, &palTestStatus.test, &palTestStatus.inner, &palTestStatus.numOfTestsFailures, &palTestStatus.numberOfTests, &palTestStatus.numberOfIgnoredTests);
00072              }
00073              status2 = pal_fsFclose(&fd);
00074              if (PAL_SUCCESS != status2) 
00075              {
00076                  PAL_LOG(ERR,"Failed to close data file of test status after read");
00077              }
00078              status2 = pal_fsUnlink(filePath);
00079              if (PAL_SUCCESS != status2) 
00080              {
00081                  PAL_LOG(ERR,"Failed to delete data file of test status after read");
00082              }
00083          }
00084         else if (PAL_ERR_FS_NO_FILE == status) {
00085             //this is not an error... in most times there will be no file
00086             status = PAL_SUCCESS;
00087         }
00088     }
00089 
00090     PAL_LOG(DBG,"*********************************\n"
00091             "** Test status:                **\n"
00092             "** Module %d                   **\n"
00093             "** Test %d                     **\n"
00094             "** Inner %d                    **\n"
00095             "** num of tests failures %llu  **\n"
00096             "** num of tests %llu           **\n"
00097             "** num of ignored tests %llu   **\n"
00098             "*********************************\n", palTestStatus.module, palTestStatus.test, palTestStatus.inner, palTestStatus.numOfTestsFailures, palTestStatus.numberOfTests, palTestStatus.numberOfIgnoredTests);
00099     return status;
00100 }
00101 
00102 palStatus_t setPalTestStatus(palTestsStatusData_t palRebootTestStatus)
00103 {
00104     palStatus_t status = PAL_SUCCESS, status2 = PAL_SUCCESS;;
00105     char filePath[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00106     palFileDescriptor_t fd = 0;
00107     size_t dataSizeWritten = 0;
00108     char data[PAL_TEST_STATUS_FILE_DATA_MAX_SIZE] = {0};
00109     
00110 
00111     status = pal_fsGetMountPoint(PAL_FS_PARTITION_PRIMARY, PAL_MAX_FILE_AND_FOLDER_LENGTH, filePath);
00112     if (PAL_SUCCESS == status) 
00113     {
00114         strncat(filePath,PAL_TEST_STATUS_FILE_LOCATION,PAL_MAX_FILE_AND_FOLDER_LENGTH - strlen(filePath));
00115         status = pal_fsFopen(filePath, PAL_FS_FLAG_READWRITETRUNC, &fd);
00116          if (PAL_SUCCESS == status)
00117          {
00118              snprintf((char *)data,PAL_TEST_STATUS_FILE_DATA_MAX_SIZE, "%d %d %d %llu %llu %llu ", palRebootTestStatus.module, palRebootTestStatus.test, palRebootTestStatus.inner, palRebootTestStatus.numOfTestsFailures, palRebootTestStatus.numberOfTests, palRebootTestStatus.numberOfIgnoredTests);
00119              status =  pal_fsFwrite(&fd, (void *)data, PAL_TEST_STATUS_FILE_DATA_MAX_SIZE, &dataSizeWritten);
00120              pal_fsFclose(&fd);
00121              if (PAL_SUCCESS != status2) 
00122              {
00123                  PAL_LOG(ERR,"Failed to close data file of test status after write");
00124              }
00125          }
00126     }
00127     return status;
00128 }
00129 
00130 
00131 palStatus_t palTestReboot(palTestModules_t module ,palTestSOTPTests_t test )
00132 {
00133     palStatus_t status = PAL_SUCCESS;
00134     palTestsStatusData_t palRebootTestStatus;
00135     palRebootTestStatus.module = module;
00136     palRebootTestStatus.test = test;
00137     palRebootTestStatus.inner = 1;
00138     palRebootTestStatus.numberOfTests = Unity.NumberOfTests;
00139     palRebootTestStatus.numOfTestsFailures = Unity.TestFailures;
00140     palRebootTestStatus.numberOfIgnoredTests = Unity.CurrentTestIgnored;
00141 
00142     status = setPalTestStatus(palRebootTestStatus);
00143     if (PAL_SUCCESS != status)
00144     {
00145         PAL_LOG(ERR,"Failed to set test status before reboot");
00146     }
00147     else
00148     {
00149         pal_osReboot();
00150     }
00151     return status;
00152 }
00153 
00154 
00155 
00156 
00157 
00158 void TEST_pal_all_GROUPS_RUNNER(void)
00159 {
00160     PRINT_MEMORY_STATS;
00161     switch (palTestStatus.module) // fall through is in design
00162     {
00163         case -1:
00164             TEST_pal_sanity_GROUP_RUNNER(); // always run this at least once
00165         case PAL_TEST_MODULE_SOTP:
00166             PRINT_MEMORY_STATS;
00167             /**
00168              *  CAUTION:THIS TEST MUDOLE REBOOTS THE SYSTEM
00169              *  THIS TEST MUST BE 1ST!!!!!
00170              *  DO NOT MOVE THIS TEST!!!!!
00171             */
00172             #ifndef PAL_SKIP_TEST_MODULE_SOTP
00173                 TEST_pal_SOTP_GROUP_RUNNER();
00174             #endif
00175         case PAL_TEST_MODULE_RTOS:
00176             PRINT_MEMORY_STATS;
00177             #ifndef PAL_SKIP_TEST_MODULE_RTOS
00178                 TEST_pal_rtos_GROUP_RUNNER();
00179             #endif
00180         case PAL_TEST_MODULE_SOCKET:
00181             PRINT_MEMORY_STATS;
00182             #ifndef PAL_SKIP_TEST_MODULE_NETWORK
00183                 TEST_pal_socket_GROUP_RUNNER();
00184             #endif
00185         case PAL_TEST_MODULE_CRYPTO:
00186             PRINT_MEMORY_STATS;
00187             #ifndef PAL_SKIP_TEST_MODULE_CRYPTO
00188                 TEST_pal_crypto_GROUP_RUNNER();
00189             #endif
00190         case PAL_TEST_MODULE_FILESYSTEM:
00191             PRINT_MEMORY_STATS;
00192             #ifndef PAL_SKIP_TEST_MODULE_FILESYSTEM
00193                 TEST_pal_fileSystem_GROUP_RUNNER();
00194             #endif
00195         case PAL_TEST_MODULE_UPDATE:
00196             PRINT_MEMORY_STATS;
00197             #ifndef PAL_SKIP_TEST_MODULE_UPDATE
00198                 TEST_pal_update_GROUP_RUNNER();
00199             #endif
00200         case PAL_TEST_MODULE_INTERNALFLASH:
00201             PRINT_MEMORY_STATS;
00202             #ifndef PAL_SKIP_TEST_MODULE_INTERNALFLASH
00203                 TEST_pal_internalFlash_GROUP_RUNNER();
00204             #endif
00205         case PAL_TEST_MODULE_TLS:
00206             PRINT_MEMORY_STATS;
00207             #ifndef PAL_SKIP_TEST_MODULE_TLS
00208                 TEST_pal_tls_GROUP_RUNNER();
00209             #endif        
00210             PRINT_MEMORY_STATS;
00211             break;
00212         default:
00213             PAL_PRINTF("this should not happen!!! Error Error Error");
00214     }
00215 }
00216 
00217 
00218 
00219 void palTestMain(palTestModules_t modules,void* network)
00220 {
00221     const char * myargv[] = {"app","-v"};
00222     g_palTestNetworkInterface = network; 
00223     g_palTestTLSInterfaceCTX = network;
00224     mbed_trace_init();
00225     mbed_trace_config_set(PAL_TESTS_LOG_LEVEL);
00226 
00227     palStatus_t getTestStatusReturnValue = getPalTestStatus();
00228     if (PAL_SUCCESS != getTestStatusReturnValue) 
00229     {
00230         PAL_LOG(ERR,"%s: Failed to get current status of tests 0x%" PRIu32 "\r\n",__FUNCTION__,getTestStatusReturnValue);
00231     }
00232 
00233     UnityPrint("*****PAL_TEST_START*****");
00234     UNITY_PRINT_EOL();
00235     switch (modules) 
00236     {
00237         case PAL_TEST_MODULE_ALL:
00238         {
00239             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_all_GROUPS_RUNNER);
00240             break;
00241         }
00242         case PAL_TEST_MODULE_SOTP:
00243         {
00244             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_SOTP_GROUP_RUNNER);
00245             break;
00246         }
00247 
00248         case PAL_TEST_MODULE_RTOS:
00249         {
00250             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_rtos_GROUP_RUNNER);
00251             break;
00252         }
00253 
00254         case PAL_TEST_MODULE_SOCKET:
00255         {
00256             UnityMain(sizeof(myargv)/ sizeof(myargv[0]), myargv, TEST_pal_socket_GROUP_RUNNER);
00257             break;
00258         }
00259 
00260         case PAL_TEST_MODULE_CRYPTO:
00261         {
00262             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_crypto_GROUP_RUNNER);
00263             break;
00264         }
00265 
00266         case PAL_TEST_MODULE_FILESYSTEM:
00267         {
00268             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_fileSystem_GROUP_RUNNER);
00269             break;
00270         }
00271 
00272         case PAL_TEST_MODULE_UPDATE:
00273         {
00274             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_update_GROUP_RUNNER);
00275             break;
00276         }
00277 
00278         case PAL_TEST_MODULE_INTERNALFLASH:
00279         {
00280             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_internalFlash_GROUP_RUNNER);
00281             break;
00282         }
00283 
00284         case PAL_TEST_MODULE_TLS:
00285         {
00286             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_tls_GROUP_RUNNER);
00287             break;
00288         }
00289         
00290         case PAL_TEST_MODULE_SANITY:
00291         {
00292             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_sanity_GROUP_RUNNER);
00293             break;
00294         }
00295 
00296         default:
00297         {
00298             UnityPrint("*****ERROR WRONG TEST SUITE WAS CHOOSEN*****");                
00299             break;
00300         }
00301 
00302     }
00303     UnityPrint("*****PAL_TEST_END*****");
00304     UNITY_PRINT_EOL();
00305 
00306     mbed_trace_free();
00307 
00308 }
00309 
00310 void palAllTestMain(void* network)
00311 {
00312     palTestMain(PAL_TEST_MODULE_ALL, network);
00313 }
00314 
00315 void palFileSystemTestMain(void* network)
00316 {
00317     palTestMain(PAL_TEST_MODULE_FILESYSTEM, network);
00318 }
00319 
00320 void palNetworkTestMain(void* network)
00321 {
00322    palTestMain(PAL_TEST_MODULE_SOCKET, network); 
00323 }
00324 
00325 void palCryptoTestMain(void* network)
00326 {
00327    palTestMain(PAL_TEST_MODULE_CRYPTO, network); 
00328 }
00329 
00330 void palRTOSTestMain(void* network)
00331 {
00332    palTestMain(PAL_TEST_MODULE_RTOS, network); 
00333 }
00334 
00335 void palStorageTestMain(void* network)
00336 {
00337    palTestMain(PAL_TEST_MODULE_INTERNALFLASH, network); 
00338 }
00339 
00340 void palTLSTestMain(void* network)
00341 {
00342    palTestMain(PAL_TEST_MODULE_TLS, network); 
00343 }
00344 
00345 void palUpdateTestMain(void* network)
00346 {
00347    palTestMain(PAL_TEST_MODULE_UPDATE, network); 
00348 }
00349 
00350 void palSOTPTestMain(void* network)
00351 {
00352     palTestMain(PAL_TEST_MODULE_SOTP, network); 
00353 }
00354 
00355 void palSanityTestMain(void* network)
00356 {
00357      palTestMain(PAL_TEST_MODULE_SANITY, network); 
00358 }
00359 
00360 
00361