takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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