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.
add_del.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 00019 /** @file add_del.cpp Test cases to add and delete key-value pairs in the CFSTORE. 00020 * 00021 * Please consult the documentation under the test-case functions for 00022 * a description of the individual test case. 00023 */ 00024 00025 #include "mbed.h" 00026 #include "cfstore_config.h" 00027 #include "Driver_Common.h" 00028 #include "cfstore_debug.h" 00029 #include "cfstore_test.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 <stdlib.h> 00038 #include <string.h> 00039 #include <inttypes.h> 00040 00041 using namespace utest::v1; 00042 00043 #define CFSTORE_ADD_DEL_MALLOC_SIZE 1024 00044 00045 static char cfstore_add_del_utest_msg_g[CFSTORE_UTEST_MSG_BUF_SIZE]; 00046 00047 static cfstore_kv_data_t cfstore_add_del_test_07_data[] = { 00048 CFSTORE_INIT_1_TABLE_MID_NODE, 00049 { NULL, NULL}, 00050 }; 00051 00052 00053 /* report whether built/configured for flash sync or async mode */ 00054 static control_t cfstore_add_del_test_00(const size_t call_count) 00055 { 00056 int32_t ret = ARM_DRIVER_ERROR; 00057 00058 (void) call_count; 00059 ret = cfstore_test_startup(); 00060 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret); 00061 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00062 return CaseNext; 00063 } 00064 00065 /** @brief 00066 * 00067 * This test case does the following: 00068 * - creates a KV. 00069 * - deletes the KV. 00070 * - checks that the deleted KV can no longer be found in the store. 00071 * 00072 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00073 */ 00074 control_t cfstore_add_del_test_01_end(const size_t call_count) 00075 { 00076 bool bfound = false; 00077 int32_t ret = ARM_DRIVER_ERROR; 00078 ARM_CFSTORE_SIZE len = 0; 00079 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00080 ARM_CFSTORE_KEYDESC kdesc; 00081 ARM_CFSTORE_HANDLE_INIT(hkey); 00082 ARM_CFSTORE_FMODE flags; 00083 00084 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00085 (void) call_count; 00086 memset(&kdesc, 0, sizeof(kdesc)); 00087 memset(&flags, 0, sizeof(flags)); 00088 00089 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00090 len = strlen(cfstore_add_del_test_07_data[0].value); 00091 00092 ret = cfstore_test_create(cfstore_add_del_test_07_data[0].key_name, (char*) cfstore_add_del_test_07_data[0].value, &len, &kdesc); 00093 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create KV in store (ret=%d).\n", __func__, (int) ret); 00094 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00095 00096 /* now delete KV*/ 00097 ret = drv->Open(cfstore_add_del_test_07_data[0].key_name, flags, hkey); 00098 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to Open() (ret=%d).\n", __func__, (int) ret); 00099 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00100 00101 if(hkey != NULL){ 00102 ret = drv->Delete(hkey); 00103 drv->Close(hkey); 00104 hkey = NULL; 00105 } 00106 /* check that the KV has been deleted */ 00107 /* revert to CFSTORE_LOG if more trace required */ 00108 CFSTORE_DBGLOG("LOG: WARNING: About to look for non-existent key (key_name=%s) (which will generate internal trace reporting errors if debug trace enabled).\n", cfstore_add_del_test_07_data[0].key_name); 00109 ret = cfstore_test_kv_is_found(cfstore_add_del_test_07_data[0].key_name, &bfound); 00110 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error failed to delete a key (ret=%d).\n", __func__, (int) ret); 00111 TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND, cfstore_add_del_utest_msg_g); 00112 00113 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Test failed: found KV that was previously deleted (key_name=%s)\n", __func__, cfstore_add_del_test_07_data[0].key_name); 00114 TEST_ASSERT_MESSAGE(bfound == false, cfstore_add_del_utest_msg_g); 00115 00116 ret = drv->Uninitialize(); 00117 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00118 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00119 return CaseNext; 00120 } 00121 00122 00123 static cfstore_kv_data_t cfstore_add_del_test_08_data[] = { 00124 CFSTORE_INIT_1_TABLE_HEAD, 00125 CFSTORE_INIT_1_TABLE_MID_NODE, 00126 CFSTORE_INIT_1_TABLE_TAIL, 00127 { NULL, NULL}, 00128 }; 00129 00130 00131 /** @brief 00132 * 00133 * This test case adds a small number of KVs (~3), and then delete them. 00134 * - add key(s) 00135 * - delete key(s) 00136 * - make sure can't find key in cfstore 00137 * - loop over the above a number of times. 00138 * 00139 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00140 */ 00141 control_t cfstore_add_del_test_02_end(const size_t call_count) 00142 { 00143 bool bResult = true; // We'll do "&=" cumulative checking. 00144 int32_t ret = ARM_DRIVER_ERROR; 00145 ARM_CFSTORE_SIZE len = 0; 00146 ARM_CFSTORE_KEYDESC kdesc; 00147 cfstore_kv_data_t* node = NULL; 00148 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00149 00150 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00151 (void) call_count; 00152 memset(&kdesc, 0, sizeof(kdesc)); 00153 00154 /* create */ 00155 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00156 node = cfstore_add_del_test_08_data; 00157 while(node->key_name != NULL) 00158 { 00159 len = strlen(node->value); 00160 ret = cfstore_test_create(node->key_name, (char*) node->value, &len, &kdesc); 00161 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create kv (key_name=%s.\n", __func__, node->key_name); 00162 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00163 /* revert CFSTORE_LOG for more trace */ 00164 CFSTORE_DBGLOG("Created KV successfully (key_name=\"%s\", value=\"%s\")\n", node->key_name, node->value); 00165 node++; 00166 } 00167 00168 /* test delete all */ 00169 ret = cfstore_test_delete_all(); 00170 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to delete all KVs.\n", __func__); 00171 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00172 00173 /* check there are no KVs present as expected */ 00174 node = cfstore_add_del_test_08_data; 00175 while(node->key_name != NULL) 00176 { 00177 ret = cfstore_test_kv_is_found(node->key_name, &bResult); 00178 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: found key when should not be present.\n", __func__); 00179 TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND && bResult == false, cfstore_add_del_utest_msg_g); 00180 /* revert CFSTORE_LOG for more trace */ 00181 CFSTORE_DBGLOG("Found KV successfully (key_name=\"%s\")\n", node->key_name); 00182 node++; 00183 } 00184 ret = drv->Uninitialize(); 00185 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00186 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00187 return CaseNext; 00188 } 00189 00190 /** @brief 00191 * 00192 * This test case adds ~50 KVs, and then delete entries at the start, 00193 * middle and end of list. 00194 * 00195 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00196 */ 00197 control_t cfstore_add_del_test_03_end(const size_t call_count) 00198 { 00199 bool bfound = false; 00200 int32_t ret = ARM_DRIVER_ERROR; 00201 ARM_CFSTORE_FMODE flags; 00202 cfstore_kv_data_t *node; 00203 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00204 00205 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00206 (void) call_count; 00207 memset(&flags, 0, sizeof(flags)); 00208 00209 ret = cfstore_test_init_1(); 00210 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to initialise cfstore area with entries\n", __func__); 00211 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00212 00213 /* delete some keys */ 00214 node = cfstore_add_del_test_08_data; 00215 while(node->key_name != NULL) 00216 { 00217 CFSTORE_DBGLOG("%s:about to delete key (key_name=%s).\n", __func__, node->key_name); 00218 cfstore_test_delete(node->key_name); 00219 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error failed to delete a key (ret=%d).\n", __func__, (int) ret); 00220 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00221 /* revert CFSTORE_LOG for more trace */ 00222 CFSTORE_DBGLOG("Deleted KV successfully (key_name=\"%s\")\n", node->key_name); 00223 node++; 00224 } 00225 /* check the keys have been deleted */ 00226 node = cfstore_add_del_test_08_data; 00227 while(node->key_name != NULL) 00228 { 00229 ret = cfstore_test_kv_is_found(node->key_name, &bfound); 00230 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to delete a key (ret=%d).\n", __func__, (int) ret); 00231 TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND, cfstore_add_del_utest_msg_g); 00232 00233 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Test failed: found KV that was previously deleted (key_name=%s)\n", __func__, node->key_name); 00234 TEST_ASSERT_MESSAGE(bfound == false, cfstore_add_del_utest_msg_g); 00235 node++; 00236 } 00237 00238 /* clean up by deleting all remaining KVs. this is not part of the test */ 00239 ret = cfstore_test_delete_all(); 00240 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error failed to delete a all KVs (ret=%d).\n", __func__, (int) ret); 00241 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00242 00243 ret = drv->Uninitialize(); 00244 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to initialize CFSTORE (ret=%d)\n", __func__, (int) ret); 00245 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00246 return CaseNext; 00247 } 00248 00249 00250 /** @brief 00251 * 00252 * This test case is as per test_03 but using delete_all() on all init_1 data. 00253 * This test case is yet to be implemented. 00254 * 00255 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00256 */ 00257 control_t cfstore_add_del_test_04(const size_t call_count) 00258 { 00259 (void) call_count; 00260 /*todo: implement test */ 00261 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Warn: Not implemented\n", __func__); 00262 CFSTORE_DBGLOG("%s: WARN: requires implementation\n", __func__); 00263 TEST_ASSERT_MESSAGE(true, cfstore_add_del_utest_msg_g); 00264 return CaseNext; 00265 } 00266 00267 /** @brief Delete an attribute after an internal realloc of the cfstore memory area 00268 * 00269 * This test case goes through the following steps: 00270 * 1. Creates attribute att_1 of size x, and write some data. This causes an internal 00271 * cfstore realloc to allocate heap memory for the attribute. 00272 * 2. Allocates some memory on the heap. Typically, this will be immediately after the 00273 * memory used by cfstore for the KV area. This means that if any cfstore reallocs are 00274 * made to increase size the memory area will have to move. 00275 * 3. Creates attribute att_2 of size y. This causes an internal cfstore realloc to move 00276 * the KV memory area to a new location. 00277 * 4. Delete att_1. This causes an internal realloc to shrink the area and tests that the 00278 * internal data structures that contain pointers to different parts of the KV area 00279 * are updated correctly. 00280 * 5. Allocates some memory on the heap. If the heap has been corrupted, this will likely trigger 00281 * a crash 00282 * 00283 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00284 */ 00285 control_t cfstore_add_del_test_05_end(const size_t call_count) 00286 { 00287 char data[] = "some test data"; 00288 char filename[] = "file1"; 00289 char filename2[] = "file2"; 00290 int32_t ret = ARM_DRIVER_ERROR; 00291 void *test_buf1 = NULL; 00292 void *test_buf2 = NULL; 00293 ARM_CFSTORE_DRIVER *cfstoreDriver = &cfstore_driver; 00294 ARM_CFSTORE_KEYDESC keyDesc1; 00295 ARM_CFSTORE_HANDLE_INIT(hkey1); 00296 ARM_CFSTORE_KEYDESC keyDesc2; 00297 ARM_CFSTORE_HANDLE_INIT(hkey2); 00298 ARM_CFSTORE_SIZE count = sizeof(data); 00299 00300 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00301 (void) call_count; 00302 00303 /* step 1 */ 00304 memset(&keyDesc1, 0, sizeof(keyDesc1)); 00305 keyDesc1.drl = ARM_RETENTION_NVM; 00306 keyDesc1.flags.read = true; 00307 keyDesc1.flags.write = true; 00308 ret = cfstoreDriver->Create(filename, 1024, &keyDesc1, hkey1); 00309 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create attribute 1 (ret=%d)\n", __func__, (int) ret); 00310 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00311 00312 /* Write data to file */ 00313 ret = cfstoreDriver->Write(hkey1, (const char *)data, &count); 00314 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write to attribute 1 (ret=%d)\n", __func__, (int) ret); 00315 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00316 00317 /* step 2 */ 00318 test_buf1 = malloc(CFSTORE_ADD_DEL_MALLOC_SIZE); 00319 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to allocate memory (test_buf1=%p)\n", __func__, test_buf1); 00320 TEST_ASSERT_MESSAGE(test_buf1 != NULL, cfstore_add_del_utest_msg_g); 00321 00322 /* step 3 */ 00323 memset(&keyDesc2, 0, sizeof(keyDesc2)); 00324 keyDesc2.drl = ARM_RETENTION_NVM; 00325 keyDesc2.flags.read = true; 00326 keyDesc2.flags.write = true; 00327 ret = cfstoreDriver->Create(filename2, 1024, &keyDesc2, hkey2); 00328 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create attribute 2 (ret=%d)\n", __func__, (int) ret); 00329 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00330 00331 /* Write data to file */ 00332 count = sizeof(data); 00333 ret = cfstoreDriver->Write(hkey2, (const char *)data, &count); 00334 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write to attribute 2 (ret=%d)\n", __func__, (int) ret); 00335 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00336 00337 /* step 4 */ 00338 ret = cfstoreDriver->Delete(hkey1); 00339 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to delete to attribute 1 (ret=%d)\n", __func__, (int) ret); 00340 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00341 00342 ret = cfstoreDriver->Close(hkey1); 00343 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close to attribute 1 (ret=%d)\n", __func__, (int) ret); 00344 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00345 00346 /* step 5 */ 00347 test_buf2 = malloc(CFSTORE_ADD_DEL_MALLOC_SIZE); 00348 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to allocate memory (test_buf2=%p)\n", __func__, test_buf2); 00349 TEST_ASSERT_MESSAGE(test_buf2 != NULL, cfstore_add_del_utest_msg_g); 00350 00351 /* clean up */ 00352 ret = cfstoreDriver->Close(hkey2); 00353 CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close to attribute 2 (ret=%d)\n", __func__, (int) ret); 00354 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); 00355 free(test_buf2); 00356 free(test_buf1); 00357 00358 return CaseNext; 00359 } 00360 00361 /// @cond CFSTORE_DOXYGEN_DISABLE 00362 utest::v1::status_t greentea_setup(const size_t number_of_cases) 00363 { 00364 GREENTEA_SETUP(300, "default_auto"); 00365 return greentea_test_setup_handler(number_of_cases); 00366 } 00367 00368 Case cases[] = { 00369 /* 1 2 3 4 5 6 7 */ 00370 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ 00371 Case("ADD_DEL_test_00", cfstore_add_del_test_00), 00372 Case("ADD_DEL_test_01_start", cfstore_utest_default_start), 00373 Case("ADD_DEL_test_01_end", cfstore_add_del_test_01_end), 00374 Case("ADD_DEL_test_02_start", cfstore_utest_default_start), 00375 Case("ADD_DEL_test_02_end", cfstore_add_del_test_02_end), 00376 Case("ADD_DEL_test_03_start", cfstore_utest_default_start), 00377 Case("ADD_DEL_test_03_end", cfstore_add_del_test_03_end), 00378 Case("ADD_DEL_test_04", cfstore_add_del_test_04), 00379 Case("ADD_DEL_test_05_start", cfstore_utest_default_start), 00380 Case("ADD_DEL_test_05_end", cfstore_add_del_test_05_end), 00381 }; 00382 00383 00384 /* Declare your test specification with a custom setup handler */ 00385 Specification specification(greentea_setup, cases); 00386 00387 int main() 00388 { 00389 return !Harness::run(specification); 00390 } 00391 /// @endcond
Generated on Tue Aug 9 2022 00:37:00 by
1.7.2