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.
flush2.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 flush2.cpp Test cases to flush KVs in the CFSTORE using the drv->Flush() interface. 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 "utest/utest.h" 00026 #include "unity/unity.h" 00027 #include "cfstore_config.h" 00028 #include "configuration_store.h" 00029 #include "greentea-client/test_env.h" 00030 #include "cfstore_test.h" 00031 #include "cfstore_debug.h" 00032 00033 #include <stdio.h> 00034 #include <stdlib.h> 00035 #include <string.h> 00036 #include <inttypes.h> 00037 00038 using namespace utest::v1; 00039 00040 /// @cond CFSTORE_DOXYGEN_DISABLE 00041 /* 00042 * Defines 00043 */ 00044 #define CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE 256 00045 #define cfstore_flush_fsm_null NULL 00046 #define CFSTORE_FLUSH_CASE_TIMEOUT_MS 10000 00047 00048 00049 00050 /* 00051 * Globals 00052 * 00053 * cfstore_flush_utest_msg_g 00054 * buffer for storing TEST_ASSERT_xxx_MESSAGE messages 00055 */ 00056 static char cfstore_flush_utest_msg_g[CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE]; 00057 00058 /* KV data for test_03 */ 00059 static cfstore_kv_data_t cfstore_flush_test_02_kv_data[] = { 00060 { "com.arm.mbed.configurationstore.test.flush.cfstoreflushtest02", "1"}, 00061 /* 1 2 3 4 5 6 7 */ 00062 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ 00063 { NULL, NULL}, 00064 }; 00065 00066 /* async test version */ 00067 00068 typedef struct cfstore_flush_ctx_t 00069 { 00070 int32_t status; 00071 ARM_CFSTORE_OPCODE cmd_code; 00072 } cfstore_flush_ctx_t; 00073 /// @endcond 00074 00075 /* 00076 * Globals 00077 */ 00078 static cfstore_flush_ctx_t cfstore_flush_ctx_g; 00079 00080 /* 00081 * context related methods 00082 */ 00083 00084 /* @brief get a pointer to the global context data structure */ 00085 static cfstore_flush_ctx_t* cfstore_flush_ctx_get(void) 00086 { 00087 return &cfstore_flush_ctx_g; 00088 } 00089 00090 /* @brief initialize global context data structure */ 00091 static void cfstore_flush_ctx_init(cfstore_flush_ctx_t* ctx) 00092 { 00093 TEST_ASSERT_MESSAGE(ctx != NULL, "ctx is NULL"); 00094 00095 CFSTORE_FENTRYLOG("%s:entered\r\n", __func__); 00096 memset(&cfstore_flush_ctx_g, 0, sizeof(cfstore_flush_ctx_g)); 00097 } 00098 00099 00100 /* report whether built/configured for flash sync or async mode */ 00101 static control_t cfstore_flush2_test_00(const size_t call_count) 00102 { 00103 int32_t ret = ARM_DRIVER_ERROR; 00104 00105 (void) call_count; 00106 ret = cfstore_test_startup(); 00107 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret); 00108 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00109 return CaseNext; 00110 } 00111 00112 /** 00113 * @brief 00114 * 00115 * 00116 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00117 */ 00118 control_t cfstore_flush2_test_01_start (const size_t call_count) 00119 { 00120 int32_t ret = ARM_DRIVER_ERROR; 00121 cfstore_flush_ctx_t* ctx = cfstore_flush_ctx_get(); 00122 const ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00123 00124 /* check that the mtd is in synchronous mode */ 00125 CFSTORE_FENTRYLOG("%s:entered:\r\n", __func__); 00126 (void) call_count; 00127 cfstore_flush_ctx_init(ctx); 00128 ret = drv->Initialize(cfstore_utest_default_callback, ctx); 00129 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to initialize CFSTORE (ret=%d)\r\n", __func__, (int) ret); 00130 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00131 return CaseTimeout(100000); 00132 } 00133 00134 00135 /** 00136 * @brief 00137 * 00138 * 00139 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00140 */ 00141 control_t cfstore_flush2_test_01_mid (const size_t call_count) 00142 { 00143 00144 bool bfound = false; 00145 int32_t ivalue = 0; 00146 int32_t ret = ARM_DRIVER_ERROR; 00147 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00148 const char* key_name_query = "*"; 00149 char value[CFSTORE_KEY_NAME_MAX_LENGTH+1]; 00150 ARM_CFSTORE_SIZE len = CFSTORE_KEY_NAME_MAX_LENGTH+1; 00151 ARM_CFSTORE_HANDLE_INIT(next); 00152 ARM_CFSTORE_HANDLE_INIT(prev); 00153 ARM_CFSTORE_KEYDESC kdesc; 00154 00155 /* check that the mtd is in synchronous mode */ 00156 CFSTORE_FENTRYLOG("%s:entered: \r\n", __func__); 00157 (void) call_count; 00158 memset(&kdesc, 0, sizeof(kdesc)); 00159 00160 /* try to read key; should not be found */ 00161 ret = cfstore_test_kv_is_found(cfstore_flush_test_02_kv_data->key_name, &bfound); 00162 if(ret != ARM_DRIVER_OK && ret != ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND){ 00163 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: cfstore_test_kv_is_found() call failed (ret=%d).\r\n", __func__, (int) ret); 00164 TEST_ASSERT_MESSAGE(false, cfstore_flush_utest_msg_g); 00165 } 00166 00167 if(!bfound) 00168 { 00169 /* first time start up. nothing is stored in cfstore flash. check this is the case */ 00170 while(drv->Find(key_name_query, prev, next) == ARM_DRIVER_OK) 00171 { 00172 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: have found an entry in cfstore when none should be present", __func__); 00173 TEST_ASSERT_MESSAGE(false, cfstore_flush_utest_msg_g); 00174 } 00175 /* no entries found, which is correct. 00176 * store a value */ 00177 len = strlen(cfstore_flush_test_02_kv_data->value); 00178 ret = cfstore_test_create(cfstore_flush_test_02_kv_data->key_name, cfstore_flush_test_02_kv_data->value, &len, &kdesc); 00179 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error:1: failed to write kv data (ret=%d).\r\n", __func__, (int) ret); 00180 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00181 /* flush to flash */ 00182 ret = drv->Flush(); 00183 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to flush data to cfstore flash (ret=%d).\r\n", __func__, (int) ret); 00184 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00185 00186 } else { 00187 /*read the value, increment by 1 and write value back */ 00188 len = CFSTORE_KEY_NAME_MAX_LENGTH+1; 00189 ret = cfstore_test_read(cfstore_flush_test_02_kv_data->key_name, value, &len); 00190 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to read kv data (ret=%d).\r\n", __func__, (int) ret); 00191 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00192 00193 /* increment value */ 00194 ivalue = atoi(value); 00195 ++ivalue; 00196 snprintf(value, CFSTORE_KEY_NAME_MAX_LENGTH+1, "%d", (int) ivalue); 00197 len = strlen(value); 00198 ret = cfstore_test_write(cfstore_flush_test_02_kv_data->key_name, value, &len); 00199 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write kv data (ret=%d).\r\n", __func__, (int) ret); 00200 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00201 00202 /* flush to flash */ 00203 ret = drv->Flush(); 00204 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to flush data to cfstore flash (ret=%d).\r\n", __func__, (int) ret); 00205 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00206 } 00207 return CaseTimeout(100000); 00208 } 00209 00210 /** 00211 * @brief 00212 * 00213 * 00214 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00215 */ 00216 control_t cfstore_flush2_test_01_end (const size_t call_count) 00217 { 00218 const ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00219 00220 CFSTORE_FENTRYLOG("%s:entered:\r\n", __func__); 00221 (void) call_count; 00222 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00223 TEST_ASSERT_MESSAGE(drv->Uninitialize() >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); 00224 return CaseNext; 00225 } 00226 00227 00228 /** 00229 * @brief test to open(create) a very large value, write the data, close, then flush. 00230 * for a N keys simultaneously. 00231 * 00232 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00233 */ 00234 control_t cfstore_flush2_test_02(const size_t call_count) 00235 { 00236 (void) call_count; 00237 //todo: implement test 00238 CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Warn: Not implemented\n", __func__); 00239 CFSTORE_DBGLOG("%s: WARN: requires implementation\n", __func__); 00240 TEST_ASSERT_MESSAGE(true, cfstore_flush_utest_msg_g); 00241 return CaseNext; 00242 } 00243 00244 00245 /// @cond CFSTORE_DOXYGEN_DISABLE 00246 utest::v1::status_t greentea_setup(const size_t number_of_cases) 00247 { 00248 // Call the default reporting function- 00249 GREENTEA_SETUP(100, "default_auto"); 00250 return greentea_test_setup_handler(number_of_cases); 00251 } 00252 00253 Case cases[] = { 00254 Case("CFSTORE_FLUSH2_test_00", cfstore_flush2_test_00), 00255 Case("CFSTORE_FLUSH2_test_01_start", cfstore_flush2_test_01_start ), 00256 Case("CFSTORE_FLUSH2_test_01_mid", cfstore_flush2_test_01_mid ), 00257 Case("CFSTORE_FLUSH2_test_01_end", cfstore_flush2_test_01_end ), 00258 Case("CFSTORE_FLUSH2_test_02_start", cfstore_utest_default_start), 00259 Case("CFSTORE_FLUSH2_test_02_end", cfstore_flush2_test_02), 00260 }; 00261 00262 00263 // Declare your test specification with a custom setup handler 00264 Specification specification(greentea_setup, cases); 00265 00266 int main() 00267 { 00268 return !Harness::run(specification); 00269 } 00270 /// @endcond
Generated on Tue Aug 9 2022 00:37:07 by
1.7.2