ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers write.cpp Source File

write.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 write.cpp Test cases to write KVs in the CFSTORE using the drv->Write() API call.
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 "configuration_store.h"
00030 #include "utest/utest.h"
00031 #include "unity/unity.h"
00032 #include "greentea-client/test_env.h"
00033 #include "cfstore_utest.h"
00034 #ifdef YOTTA_CFG_CFSTORE_UVISOR
00035 #include "uvisor-lib/uvisor-lib.h"
00036 #endif /* YOTTA_CFG_CFSTORE_UVISOR */
00037 
00038 #include <stdio.h>
00039 #include <string.h>
00040 #include <inttypes.h>
00041 
00042 using namespace utest::v1;
00043 
00044 static char cfstore_write_utest_msg_g[CFSTORE_UTEST_MSG_BUF_SIZE];
00045 
00046 /* Configure secure box. */
00047 #ifdef YOTTA_CFG_CFSTORE_UVISOR
00048 UVISOR_BOX_NAMESPACE("com.arm.mbed.cfstore.test.write.box1");
00049 UVISOR_BOX_CONFIG(cfstore_write_box1, UVISOR_BOX_STACK_SIZE);
00050 #endif /* YOTTA_CFG_CFSTORE_UVISOR */
00051 
00052 
00053 /*
00054  * write tests
00055  * cfstore_handle_t cfstore_write()
00056  */
00057 
00058 /* KV data for test_01 */
00059 static cfstore_kv_data_t cfstore_write_test_01_kv_data[] = {
00060         CFSTORE_INIT_1_TABLE_MID_NODE,
00061         { NULL, NULL},
00062 };
00063 
00064 
00065 /* report whether built/configured for flash sync or async mode */
00066 static control_t cfstore_write_test_00(const size_t call_count)
00067 {
00068     int32_t ret = ARM_DRIVER_ERROR;
00069 
00070     (void) call_count;
00071     ret = cfstore_test_startup();
00072     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret);
00073     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00074     return CaseNext;
00075 }
00076 
00077 /** @brief  test to write many times to an open KV to test data is appended
00078  *          sequentially to the end of the value blob
00079  *
00080  * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
00081  */
00082 control_t cfstore_write_test_01_end(const size_t call_count)
00083 {
00084     char read_buf[CFSTORE_KEY_NAME_MAX_LENGTH+1];
00085     uint32_t i = 0;
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(hkey);
00091     ARM_CFSTORE_FMODE flags;
00092 
00093     CFSTORE_DBGLOG("%s:entered\n", __func__);
00094     (void) call_count;
00095     memset(read_buf, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1);
00096     memset(&kdesc, 0, sizeof(kdesc));
00097     memset(&flags, 0, sizeof(flags));
00098 
00099     /* create an empty KV of the required length */
00100     kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE;
00101     len = strlen(cfstore_write_test_01_kv_data[0].value);
00102     ret = cfstore_test_create(cfstore_write_test_01_kv_data[0].key_name, "one", &len, &kdesc);
00103     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create new KV (key_name=%s, ret=%d).\n", __func__, cfstore_write_test_01_kv_data[0].key_name, (int) ret);
00104     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00105 
00106     /* now open the newly created key and write repeated to created the string */
00107     flags.write = true;
00108     ret = drv->Open(cfstore_write_test_01_kv_data[0].key_name, flags, hkey);
00109     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open node (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, cfstore_write_test_01_kv_data[0].key_name, cfstore_write_test_01_kv_data[0].value, (int) ret);
00110     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00111 
00112     for(i = 0; i < strlen(cfstore_write_test_01_kv_data[0].value); i++)
00113     {
00114         len = 1;
00115         ret = drv->Write(hkey, &cfstore_write_test_01_kv_data[0].value[i], &len);
00116         CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Write failed for char (\'%c\') (ret=%d)\n", __func__, cfstore_write_test_01_kv_data[0].value[i], (int) ret);
00117         TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00118     }
00119     /* check that the value created in the key is as expected*/
00120     len = CFSTORE_KEY_NAME_MAX_LENGTH+1;
00121     ret = drv->Read(hkey, read_buf, &len);
00122     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Read failed (ret=%d)\n", __func__, (int) ret);
00123     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00124 
00125     /* check node key_names are identical */
00126     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: KV value (%s) is not as expected (%s).\n", __func__, read_buf, cfstore_write_test_01_kv_data[0].value);
00127     TEST_ASSERT_MESSAGE(strncmp(read_buf, cfstore_write_test_01_kv_data[0].value, strlen(cfstore_write_test_01_kv_data[0].value)) == 0, cfstore_write_utest_msg_g);
00128 
00129     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Close() call failed.\n", __func__);
00130     TEST_ASSERT_MESSAGE(drv->Close(hkey) >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00131 
00132     cfstore_test_delete_all();
00133     ret = drv->Uninitialize();
00134     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__);
00135     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00136     return CaseNext;
00137 }
00138 
00139 /** @brief  test to write with a NULL buffer, which should fail gracefully
00140  *
00141  * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
00142  */
00143 control_t cfstore_write_test_02_end(const size_t call_count)
00144 {
00145     int32_t ret = ARM_DRIVER_ERROR;
00146     ARM_CFSTORE_SIZE len = 0;
00147     ARM_CFSTORE_KEYDESC kdesc;
00148     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00149 
00150     CFSTORE_DBGLOG("%s:entered\n", __func__);
00151     (void) call_count;
00152     memset(&kdesc, 0, sizeof(kdesc));
00153 
00154     /* create an empty KV of the required length */
00155     kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE;
00156     len = strlen(cfstore_write_test_01_kv_data[0].value);
00157     ret = cfstore_test_create(cfstore_write_test_01_kv_data[0].key_name, NULL, &len, &kdesc);
00158     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error KV creation should have failed due to null write buffer but something else happended (ret=%d).\n", __func__, (int) ret);
00159     TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_INVALID_WRITE_BUFFER, cfstore_write_utest_msg_g);
00160 
00161     ret = drv->Uninitialize();
00162     CFSTORE_TEST_UTEST_MESSAGE(cfstore_write_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__);
00163     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_write_utest_msg_g);
00164     return CaseNext;
00165 }
00166 
00167 
00168 /// @cond CFSTORE_DOXYGEN_DISABLE
00169 utest::v1::status_t  greentea_setup(const size_t number_of_cases)
00170 {
00171     GREENTEA_SETUP(200, "default_auto");
00172     return greentea_test_setup_handler(number_of_cases);
00173 }
00174 
00175 Case cases[] = {
00176            /*          1         2         3         4         5         6        7  */
00177            /* 1234567890123456789012345678901234567890123456789012345678901234567890 */
00178         Case("WRITE_test_00", cfstore_write_test_00),
00179         Case("WRITE_test_01_start", cfstore_utest_default_start),
00180         Case("WRITE_test_01_end", cfstore_write_test_01_end),
00181         Case("WRITE_test_02_start", cfstore_utest_default_start),
00182         Case("WRITE_test_02_end", cfstore_write_test_02_end),
00183 };
00184 
00185 
00186 /* Declare your test specification with a custom setup handler */
00187 Specification specification(greentea_setup, cases);
00188 
00189 int main()
00190 {
00191     return !Harness::run(specification);
00192 }
00193 /// @endcond