Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
misc.cpp
00001 /* 00002 * mbed Microcontroller Library 00003 * Copyright (c) 2006-2016 ARM Limited 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 /** @file misc.cpp Test cases for miscellaneous API drv->Xxx() functions. 00019 * 00020 * Please consult the documentation under the test-case functions for 00021 * a description of the individual test case. 00022 */ 00023 00024 #include "mbed.h" 00025 #include "cfstore_config.h" 00026 #include "cfstore_test.h" 00027 #include "cfstore_debug.h" 00028 #include "Driver_Common.h" 00029 #include "cfstore_config.h" 00030 #include "configuration_store.h" 00031 #include "utest/utest.h" 00032 #include "unity/unity.h" 00033 #include "greentea-client/test_env.h" 00034 #include "cfstore_utest.h" 00035 00036 #include <stdio.h> 00037 #include <string.h> 00038 #include <inttypes.h> 00039 00040 using namespace utest::v1; 00041 00042 static char cfstore_misc_utest_msg_g[CFSTORE_UTEST_MSG_BUF_SIZE]; 00043 00044 00045 00046 /* report whether built/configured for flash sync or async mode */ 00047 static control_t cfstore_misc_test_00(const size_t call_count) 00048 { 00049 int32_t ret = ARM_DRIVER_ERROR; 00050 00051 (void) call_count; 00052 ret = cfstore_test_startup(); 00053 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret); 00054 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00055 return CaseNext; 00056 } 00057 00058 00059 /** @brief basic PowerControl() test 00060 * 00061 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00062 */ 00063 control_t cfstore_misc_test_00_start(const size_t call_count) 00064 { 00065 int32_t ret = ARM_DRIVER_ERROR; 00066 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00067 ARM_POWER_STATE state = ARM_POWER_OFF; 00068 00069 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00070 (void) call_count; 00071 00072 /* try setting the power control state before initialised, which should fail */ 00073 ret = drv->PowerControl(state); 00074 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Eror: PowerControl() call should have failed as CFSTORE not initialised, but the call succeeded\r\n", __func__); 00075 TEST_ASSERT_MESSAGE(ret < ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00076 00077 ret = drv->Initialize(cfstore_utest_default_callback, NULL); 00078 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to initialize CFSTORE (ret=%d)\n", __func__, (int) ret); 00079 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00080 return CaseTimeout(CFSTORE_UTEST_DEFAULT_TIMEOUT_MS); 00081 } 00082 00083 static control_t cfstore_misc_test_00_end(const size_t call_count) 00084 { 00085 int32_t ret = ARM_DRIVER_ERROR; 00086 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00087 ARM_POWER_STATE state = ARM_POWER_OFF; 00088 00089 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00090 (void) call_count; 00091 00092 while(state <= ARM_POWER_FULL) 00093 { 00094 ret = drv->PowerControl(state); 00095 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: PowerControl() call failed\r\n", __func__); 00096 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00097 state = (ARM_POWER_STATE)((uint32_t) state + 1); 00098 } 00099 /*try invalid value which should fail*/ 00100 ret = drv->PowerControl((ARM_POWER_STATE ) 0xffffffff); 00101 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:ERror: PowerControl() did not fail with bad value 0xffffffff (not as expected)\r\n", __func__); 00102 TEST_ASSERT_MESSAGE(ret < ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00103 00104 ret = drv->Uninitialize(); 00105 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00106 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00107 return CaseNext; 00108 } 00109 00110 00111 /** @brief basic GetVersion() test 00112 * 00113 */ 00114 control_t cfstore_misc_test_01(const size_t call_count) 00115 { 00116 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00117 ARM_DRIVER_VERSION version; 00118 00119 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00120 (void) call_count; 00121 memset(&version, 0, sizeof(version)); 00122 00123 version = drv->GetVersion(); 00124 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:GetVersion() failed to return correct API version.\r\n", __func__); 00125 TEST_ASSERT_MESSAGE(version.api == ARM_CFSTORE_API_VERSION, cfstore_misc_utest_msg_g); 00126 00127 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:GetVersion() to return correct driver version.\r\n", __func__); 00128 TEST_ASSERT_MESSAGE(version.drv == ARM_CFSTORE_DRV_VERSION, cfstore_misc_utest_msg_g); 00129 return CaseNext; 00130 } 00131 00132 00133 /* KV data for test_03 */ 00134 static cfstore_kv_data_t cfstore_misc_test_03_kv_data[] = { 00135 /* 1 2 3 4 5 6 7 */ 00136 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ 00137 { "The.principles.of.least.action.in.quantum.mechanics", "DoctoralThesis"}, 00138 { "Space.Time.Approach.to.Quantum.Electrodynamic", " PhysicalReview766)"}, 00139 { "An.Operator.Calculus.Having.Applications.in.Quantum.Electrodynamics", "PhysicalReview84)"}, 00140 { NULL, NULL}, 00141 }; 00142 00143 00144 /** @brief basic GetKeyName() test 00145 * 00146 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00147 */ 00148 control_t cfstore_misc_test_02_end(const size_t call_count) 00149 { 00150 uint8_t key_name_len = 0; 00151 char key_name_buf[CFSTORE_KEY_NAME_MAX_LENGTH+1]; 00152 int32_t ret = ARM_DRIVER_ERROR; 00153 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00154 cfstore_kv_data_t* node = NULL; 00155 ARM_CFSTORE_HANDLE_INIT(hkey); 00156 ARM_CFSTORE_FMODE flags; 00157 00158 CFSTORE_FENTRYLOG("%s:entered\r\n", __func__); 00159 (void) call_count; 00160 memset(key_name_buf, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00161 /* dont set any flags to get default settings */ 00162 memset(&flags, 0, sizeof(flags)); 00163 00164 ret = cfstore_test_create_table(cfstore_misc_test_03_kv_data); 00165 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: unable to create keys from table.\r\n", __func__); 00166 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00167 00168 node = cfstore_misc_test_03_kv_data; 00169 while(node->key_name != NULL) 00170 { 00171 CFSTORE_DBGLOG("%s:About to open KV (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value); 00172 ret = drv->Open(node->key_name, flags, hkey); 00173 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Failed to open node (key_name=\"%s\", value=\"%s\")(ret=%d)\r\n", __func__, node->key_name, node->value, (int) ret); 00174 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00175 00176 key_name_len = CFSTORE_KEY_NAME_MAX_LENGTH+1; 00177 memset(key_name_buf, 0, key_name_len); 00178 drv->GetKeyName(hkey, key_name_buf, &key_name_len); 00179 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to GetKeyName() (key_name (expected)=\"%s\", key_name (returned)=\"%s\", value=\"%s\"), len return=%d, len expected=%d\r\n", __func__, node->key_name, key_name_buf, node->value, (int) key_name_len, (int) strlen(node->key_name)); 00180 TEST_ASSERT_MESSAGE(key_name_len == strlen(node->key_name)+1, cfstore_misc_utest_msg_g); 00181 00182 /* revert to CFSTORE_LOG if more trace required */ 00183 CFSTORE_DBGLOG("GetKeyName() successfully reported key_name (key_name=\"%s\")\r\n", key_name_buf); 00184 ret = drv->Close(hkey); 00185 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Failed to close key (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value); 00186 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00187 node++; 00188 } 00189 00190 cfstore_test_delete_all(); 00191 ret = drv->Uninitialize(); 00192 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00193 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00194 return CaseNext; 00195 } 00196 00197 00198 /** @brief basic GetValueLen() test 00199 * 00200 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00201 */ 00202 control_t cfstore_misc_test_03_end(const size_t call_count) 00203 { 00204 int32_t ret = ARM_DRIVER_ERROR; 00205 ARM_CFSTORE_SIZE len = 0; 00206 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00207 cfstore_kv_data_t* node = NULL; 00208 ARM_CFSTORE_HANDLE_INIT(hkey); 00209 ARM_CFSTORE_FMODE flags; 00210 00211 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00212 (void) call_count; 00213 /* dont set any flags to get default settings */ 00214 memset(&flags, 0, sizeof(flags)); 00215 00216 ret = cfstore_test_create_table(cfstore_misc_test_03_kv_data); 00217 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: unable to create keys from table.\r\n", __func__); 00218 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00219 00220 node = cfstore_misc_test_03_kv_data; 00221 while(node->key_name != NULL) 00222 { 00223 CFSTORE_DBGLOG("%s:About to open KV (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value); 00224 ret = drv->Open(node->key_name, flags, hkey); 00225 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Failed to open node (key_name=\"%s\", value=\"%s\")(ret=%d)\r\n", __func__, node->key_name, node->value, (int) ret); 00226 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00227 00228 drv->GetValueLen(hkey, &len); 00229 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Errro: GetValueLen() retrieve incorrect length of value blob(expected=%d, returned=%d)\r\n", __func__, (int) strlen(node->value), (int) len); 00230 TEST_ASSERT_MESSAGE(len == strlen(node->value), cfstore_misc_utest_msg_g); 00231 /* revert to CFSTORE_LOG if more trace required */ 00232 CFSTORE_DBGLOG("GetValueLen() successfully reported correct value blob length%s", "\r\n"); 00233 00234 ret = drv->Close(hkey); 00235 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Failed to close key (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value); 00236 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00237 node++; 00238 } 00239 cfstore_test_delete_all(); 00240 ret = drv->Uninitialize(); 00241 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00242 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00243 return CaseNext; 00244 } 00245 00246 00247 /** @brief basic GetStatus() test 00248 * 00249 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00250 */ 00251 control_t cfstore_misc_test_04_start(const size_t call_count) 00252 { 00253 int32_t ret = ARM_DRIVER_ERROR; 00254 ARM_CFSTORE_STATUS status; 00255 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00256 00257 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00258 (void) call_count; 00259 00260 status = drv->GetStatus(); 00261 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: GetStatus() before initialisation should have reported error, but reported no error.\r\n", __func__); 00262 TEST_ASSERT_MESSAGE(status.error == 1, cfstore_misc_utest_msg_g); 00263 00264 ret = drv->Initialize(cfstore_utest_default_callback, NULL); 00265 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to initialize CFSTORE (ret=%d)\n", __func__, (int) ret); 00266 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00267 return CaseTimeout(CFSTORE_UTEST_DEFAULT_TIMEOUT_MS); 00268 } 00269 00270 /** @brief basic GetStatus() test 00271 * 00272 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00273 */ 00274 control_t cfstore_misc_test_04_end(const size_t call_count) 00275 { 00276 int32_t ret = ARM_DRIVER_ERROR; 00277 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00278 ARM_CFSTORE_STATUS status; 00279 00280 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00281 (void) call_count; 00282 00283 status = drv->GetStatus(); 00284 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: GetStatus() but reported error.\r\n", __func__); 00285 TEST_ASSERT_MESSAGE(status.error == 0, cfstore_misc_utest_msg_g); 00286 00287 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: GetStatus() reported operation in progress.\r\n", __func__); 00288 TEST_ASSERT_MESSAGE(status.in_progress == 0, cfstore_misc_utest_msg_g); 00289 00290 ret = drv->Uninitialize(); 00291 CFSTORE_TEST_UTEST_MESSAGE(cfstore_misc_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00292 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_misc_utest_msg_g); 00293 return CaseNext; 00294 } 00295 00296 00297 /// @cond CFSTORE_DOXYGEN_DISABLE 00298 utest::v1::status_t greentea_setup(const size_t number_of_cases) 00299 { 00300 GREENTEA_SETUP(200, "default_auto"); 00301 return greentea_test_setup_handler(number_of_cases); 00302 } 00303 00304 Case cases[] = { 00305 /* 1 2 3 4 5 6 7 */ 00306 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ 00307 Case("MISC_test_00", cfstore_misc_test_00), 00308 Case("MISC_test_00_start", cfstore_misc_test_00_start), 00309 Case("MISC_test_00_end", cfstore_misc_test_00_end), 00310 Case("MISC_test_01", cfstore_misc_test_01), 00311 Case("MISC_test_02_start", cfstore_utest_default_start), 00312 Case("MISC_test_02_end", cfstore_misc_test_02_end), 00313 Case("MISC_test_03_start", cfstore_utest_default_start), 00314 Case("MISC_test_03_end", cfstore_misc_test_03_end), 00315 Case("MISC_test_04_start", cfstore_misc_test_04_start), 00316 Case("MISC_test_05_end", cfstore_misc_test_04_end), 00317 }; 00318 00319 00320 /* Declare your test specification with a custom setup handler */ 00321 Specification specification(greentea_setup, cases); 00322 00323 int main() 00324 { 00325 return !Harness::run(specification); 00326 } 00327 /// @endcond
Generated on Tue Aug 9 2022 00:37:15 by
1.7.2