Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers close.cpp Source File

close.cpp

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