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.
close.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 close.cpp Test cases to close KVs in the CFSTORE using the drv->Close() API function. 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 "Driver_Common.h" 00027 #include "configuration_store.h" 00028 #include "cfstore_test.h" 00029 #include "cfstore_debug.h" 00030 #include "utest/utest.h" 00031 #include "unity/unity.h" 00032 #include "greentea-client/test_env.h" 00033 #include "cfstore_utest.h" 00034 00035 #include <stdio.h> 00036 #include <string.h> 00037 #include <inttypes.h> 00038 00039 using namespace utest::v1; 00040 00041 static char cfstore_close_utest_msg_g[CFSTORE_UTEST_MSG_BUF_SIZE]; 00042 00043 /* KV data for test_01 */ 00044 static cfstore_kv_data_t cfstore_close_test_01_kv_data[] = { 00045 { "yotta.hello-world.animal{wobbly-dog}{foot}frontLeft", "first_data_"}, 00046 { "yotta.hello-world.animal{wobbly-dog}{foot}frontLeft", "second_data"}, 00047 { NULL, NULL}, 00048 }; 00049 00050 00051 /* report whether built/configured for flash sync or async mode */ 00052 static control_t cfstore_close_test_00(const size_t call_count) 00053 { 00054 int32_t ret = ARM_DRIVER_ERROR; 00055 00056 (void) call_count; 00057 ret = cfstore_test_startup(); 00058 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret); 00059 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00060 return CaseNext; 00061 } 00062 00063 00064 /** @brief 00065 * 00066 * The is a basic test case which does the following: 00067 * - 01. create a key with handle hkey1 00068 * - 02. write data of hkey1 00069 * - 03. opens KV with 2nd handle, hkey2 00070 * - 04. read data with hkey2 and make sure its the same as that written with hkey1 00071 * - 05. write new data with hkey2 00072 * - 06. delete hkey2 00073 * - 07. close hkey2 00074 * - 08. read hkey1 and make sure the data is the newly written data i.e. the key hasnt 00075 * been deleted yet 00076 * - 09. try to open KV and make sure unable to do so, as KV is deleting 00077 * - 10. close hkey1 00078 * - 11. try to open KV and make sure unable to do so because its now been deleted 00079 * - 12. create new KV with same key_name to make sure can re-create the key again. 00080 * 00081 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00082 */ 00083 control_t cfstore_close_test_01_end(const size_t call_count) 00084 { 00085 char read_buf[CFSTORE_KEY_NAME_MAX_LENGTH]; 00086 int32_t ret = ARM_DRIVER_ERROR; 00087 ARM_CFSTORE_SIZE len = 0; 00088 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00089 ARM_CFSTORE_KEYDESC kdesc; 00090 ARM_CFSTORE_HANDLE_INIT(hkey1); 00091 ARM_CFSTORE_HANDLE_INIT(hkey2); 00092 cfstore_kv_data_t *node; 00093 ARM_CFSTORE_FMODE flags; 00094 00095 CFSTORE_DBGLOG("%s:entered\n", __func__); 00096 (void) call_count; 00097 node = &cfstore_close_test_01_kv_data[0]; 00098 memset(&kdesc, 0, sizeof(kdesc)); 00099 memset(&flags, 0, sizeof(flags)); 00100 memset(read_buf, 0, CFSTORE_KEY_NAME_MAX_LENGTH); 00101 00102 /* step 01 */ 00103 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00104 CFSTORE_DBGLOG("%s:About to create new node (key_name=\"%s\", value=\"%s\")\n", __func__, node->key_name, node->value); 00105 ret = drv->Create(node->key_name, strlen(node->value), &kdesc, hkey1); 00106 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create node (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00107 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00108 CFSTORE_DBGLOG("%s:length of KV=%d (key_name=\"%s\", value=\"%s\")\n", __func__, (int) len, node->key_name, node->value); 00109 00110 /* step 02 */ 00111 len = strlen(node->value); 00112 ret = drv->Write(hkey1, (char*) node->value, &len); 00113 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write key (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00114 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00115 00116 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write full value data (key_name=\"%s\", value=\"%s\"), len=%d, (ret=%d)\n", __func__, node->key_name, node->value, (int) len, (int) ret); 00117 TEST_ASSERT_MESSAGE(len == strlen(node->value), cfstore_close_utest_msg_g); 00118 /* revert to CFSTORE_LOG if more trace required */ 00119 CFSTORE_DBGLOG("Created KV successfully (key_name=\"%s\", value=\"%s\")\n", node->key_name, node->value); 00120 00121 /* step 03: Now open second handle while keeping first handle open */ 00122 flags.read = true; 00123 flags.write = true; 00124 ret = drv->Open(node->key_name, flags, hkey2); 00125 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open node (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00126 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00127 len = strlen(node->value) + 1; 00128 /* revert to CFSTORE_LOG if more trace required */ 00129 CFSTORE_DBGLOG("Opened KV successfully (key_name=\"%s\", value=\"%s\")\n", node->key_name, node->value); 00130 00131 /* step 04 */ 00132 ret = drv->Read(hkey2, read_buf, &len); 00133 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to read key (key_name=\"%s\", value=\"%s\")\n", __func__, node->key_name, node->value); 00134 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00135 00136 /* check read data is as expected */ 00137 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: read value data (%s) != KV value data (key_name=\"%s\", value=\"%s\")\n", __func__, read_buf, node->key_name, node->value); 00138 TEST_ASSERT_MESSAGE(strncmp(read_buf, node->value, strlen(node->value)) == 0, cfstore_close_utest_msg_g); 00139 00140 /* step 05 write new data using hkey2 */ 00141 node = &cfstore_close_test_01_kv_data[1]; 00142 len = strlen(node->value); 00143 ret = drv->Write(hkey2, (char*) node->value, &len); 00144 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write key with 2nd handle (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00145 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00146 00147 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write full value data (key_name=\"%s\", value=\"%s\"), len=%d, (ret=%d)\n", __func__, node->key_name, node->value, (int) len, (int) ret); 00148 TEST_ASSERT_MESSAGE(len == strlen(node->value), cfstore_close_utest_msg_g); 00149 /* revert to CFSTORE_LOG if more trace required */ 00150 CFSTORE_DBGLOG("Wrote KV successfully with new data (key_name=\"%s\", value=\"%s\")\n", node->key_name, node->value); 00151 00152 /* step 06 delete hkey2 */ 00153 ret = drv->Delete(hkey2); 00154 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to Delete KV with 2nd handle (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00155 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00156 00157 /* step 07 close hkey2 */ 00158 ret = drv->Close(hkey2); 00159 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to Close KV with 2nd handle (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00160 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00161 00162 /* step 08 read hkey1 for updated data */ 00163 len = strlen(node->value) + 1; 00164 memset(read_buf, 0, len); 00165 ret = drv->Read(hkey1, read_buf, &len); 00166 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to read key with hkey1 (key_name=\"%s\", value=\"%s\")\n", __func__, node->key_name, node->value); 00167 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00168 00169 /* check read data is as expected */ 00170 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: read value data (%s) != KV value data (key_name=\"%s\", value=\"%s\")\n", __func__, read_buf, node->key_name, node->value); 00171 TEST_ASSERT_MESSAGE(strncmp(read_buf, node->value, strlen(node->value)) == 0, cfstore_close_utest_msg_g); 00172 00173 /* step 09 */ 00174 ret = drv->Open(node->key_name, flags, hkey2); 00175 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Should not be able to open pre-existing key in deleting state (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00176 TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_PREEXISTING_KEY_DELETING || ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND, cfstore_close_utest_msg_g); 00177 00178 /* step 10 close hkey1 */ 00179 ret = drv->Close(hkey1); 00180 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to Close KV with 1st handle (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00181 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00182 00183 /* step 11 try to open KV and make sure unable to do so because its now been deleted */ 00184 ret = drv->Open(node->key_name, flags, hkey1); 00185 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Should not be able to open key as it should not be present in the CFSTORE (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00186 TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND, cfstore_close_utest_msg_g); 00187 00188 /* step 12. create new KV with same key_name to make sure can re-create the key again. */ 00189 node = &cfstore_close_test_01_kv_data[0]; 00190 len = strlen(node->value); 00191 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00192 CFSTORE_DBGLOG("%s:About to create new node (key_name=\"%s\", value=\"%s\")\n", __func__, node->key_name, node->value); 00193 ret = drv->Create(node->key_name, strlen(node->value), &kdesc, hkey1); 00194 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create node (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00195 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00196 CFSTORE_DBGLOG("%s:length of KV=%d (key_name=\"%s\", value=\"%s\")\n", __func__, (int) len, node->key_name, node->value); 00197 00198 len = strlen(node->value); 00199 ret = drv->Write(hkey1, (char*) node->value, &len); 00200 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write key (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, node->key_name, node->value, (int) ret); 00201 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00202 00203 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write full value data (key_name=\"%s\", value=\"%s\"), len=%d, (ret=%d)\n", __func__, node->key_name, node->value, (int) len, (int) ret); 00204 TEST_ASSERT_MESSAGE(len == strlen(node->value), cfstore_close_utest_msg_g); 00205 00206 /* revert to CFSTORE_LOG if more trace required */ 00207 CFSTORE_DBGLOG("Created KV successfully (key_name=\"%s\", value=\"%s\")\n", node->key_name, node->value); 00208 00209 ret = drv->Uninitialize(); 00210 CFSTORE_TEST_UTEST_MESSAGE(cfstore_close_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00211 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_close_utest_msg_g); 00212 return CaseNext; 00213 } 00214 00215 00216 /// @cond CFSTORE_DOXYGEN_DISABLE 00217 utest::v1::status_t greentea_setup(const size_t number_of_cases) 00218 { 00219 GREENTEA_SETUP(100, "default_auto"); 00220 return greentea_test_setup_handler(number_of_cases); 00221 } 00222 00223 Case cases[] = { 00224 /* 1 2 3 4 5 6 7 */ 00225 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ 00226 Case("CLOSE_test_00", cfstore_close_test_00), 00227 Case("CLOSE_test_01_start", cfstore_utest_default_start), 00228 Case("CLOSE_test_01_end", cfstore_close_test_01_end) 00229 }; 00230 00231 00232 /* Declare your test specification with a custom setup handler */ 00233 Specification specification(greentea_setup, cases); 00234 00235 int main() 00236 { 00237 return !Harness::run(specification); 00238 } 00239 /// @endcond 00240 00241
Generated on Tue Aug 9 2022 00:37:04 by
1.7.2