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.
open.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 open.cpp Test cases to open KVs in the CFSTORE using the drv->Open() 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 "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 <stdlib.h> /*rand()*/ 00038 #include <inttypes.h> 00039 00040 using namespace utest::v1; 00041 00042 static char cfstore_open_utest_msg_g[CFSTORE_UTEST_MSG_BUF_SIZE]; 00043 00044 00045 00046 /// @cond CFSTORE_DOXYGEN_DISABLE 00047 #ifdef CFSTORE_DEBUG 00048 #define CFSTORE_OPEN_GREENTEA_TIMEOUT_S 3000 00049 #else 00050 #define CFSTORE_OPEN_GREENTEA_TIMEOUT_S 1000 00051 #endif 00052 /// @endcond 00053 00054 00055 /* support functions */ 00056 00057 /* 00058 * open tests that focus on testing cfstore_open() 00059 * cfstore_handle_t cfstore_open(const char* key_name, char* data, ARM_CFSTORE_SIZE* len, cfstore_key_desc_t* kdesc) 00060 */ 00061 00062 /* KV data for test_01 */ 00063 static cfstore_kv_data_t cfstore_open_test_01_kv_data[] = { 00064 { "yotta.hello-world.animal{wobbly-dog}{foot}frontLeft", "missing"}, 00065 { NULL, NULL}, 00066 }; 00067 00068 00069 /* report whether built/configured for flash sync or async mode */ 00070 static control_t cfstore_open_test_00(const size_t call_count) 00071 { 00072 int32_t ret = ARM_DRIVER_ERROR; 00073 00074 (void) call_count; 00075 ret = cfstore_test_startup(); 00076 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret); 00077 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00078 return CaseNext; 00079 } 00080 00081 /** @brief 00082 * Basic open test which does the following: 00083 * - creates KV with default rw perms and writes some data to the value blob. 00084 * - closes the newly created KV. 00085 * - opens the KV with the default permissions (r-only) 00086 * - reads the KV data and checks its the same as the previously created data. 00087 * - closes the opened key 00088 * 00089 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00090 */ 00091 control_t cfstore_open_test_01_end(const size_t call_count) 00092 { 00093 char* read_buf; 00094 int32_t ret = ARM_DRIVER_ERROR; 00095 ARM_CFSTORE_SIZE len = 0; 00096 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00097 ARM_CFSTORE_KEYDESC kdesc; 00098 ARM_CFSTORE_HANDLE_INIT(hkey); 00099 cfstore_kv_data_t *node; 00100 ARM_CFSTORE_FMODE flags; 00101 00102 CFSTORE_DBGLOG("%s:entered\n", __func__); 00103 (void) call_count; 00104 node = cfstore_open_test_01_kv_data; 00105 memset(&kdesc, 0, sizeof(kdesc)); 00106 memset(&flags, 0, sizeof(flags)); 00107 00108 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00109 CFSTORE_DBGLOG("%s:About to create new node (key_name=\"%s\", value=\"%s\")\n", __func__, node->key_name, node->value); 00110 ret = drv->Create(node->key_name, strlen(node->value), &kdesc, hkey); 00111 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_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); 00112 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00113 00114 CFSTORE_DBGLOG("%s:length of KV=%d (key_name=\"%s\", value=\"%s\")\n", __func__, (int) len, node->key_name, node->value); 00115 len = strlen(node->value); 00116 ret = drv->Write(hkey, (char*) node->value, &len); 00117 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_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); 00118 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00119 00120 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_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); 00121 TEST_ASSERT_MESSAGE(len == strlen(node->value), cfstore_open_utest_msg_g); 00122 00123 CFSTORE_DBGLOG("Created KV successfully (key_name=\"%s\", value=\"%s\")\n", node->key_name, node->value); 00124 ret = drv->Close(hkey); 00125 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close handle (ret=%d)\n", __func__, (int) ret); 00126 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00127 00128 /* now open the newly created key */ 00129 ret = drv->Open(node->key_name, flags, hkey); 00130 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_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); 00131 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00132 00133 len = strlen(node->value) + 1; 00134 read_buf = (char*) malloc(len); 00135 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to allocated read buffer \n", __func__); 00136 TEST_ASSERT_MESSAGE(read_buf != NULL, cfstore_open_utest_msg_g); 00137 00138 CFSTORE_DBGLOG("Opened KV successfully (key_name=\"%s\", value=\"%s\")\n", node->key_name, node->value); 00139 memset(read_buf, 0, len); 00140 ret = drv->Read(hkey, read_buf, &len); 00141 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write key (key_name=\"%s\", value=\"%s\")\n", __func__, node->key_name, node->value); 00142 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00143 00144 /* check read data is as expected */ 00145 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_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); 00146 TEST_ASSERT_MESSAGE(strncmp(read_buf, node->value, strlen(node->value)) == 0, cfstore_open_utest_msg_g); 00147 00148 if(read_buf){ 00149 free(read_buf); 00150 } 00151 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Close() call failed.\n", __func__); 00152 TEST_ASSERT_MESSAGE(drv->Close(hkey) >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00153 00154 ret = drv->Uninitialize(); 00155 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00156 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00157 return CaseNext; 00158 } 00159 00160 static cfstore_kv_data_t cfstore_open_test_02_data[] = { 00161 CFSTORE_INIT_1_TABLE_MID_NODE, 00162 { NULL, NULL}, 00163 }; 00164 00165 /** 00166 * @brief test to open() a pre-existing key and try to write it, which should fail 00167 * as by default pre-existing keys are opened read-only 00168 * 00169 * Basic open test which does the following: 00170 * - creates KV with default rw perms and writes some data to the value blob. 00171 * - closes the newly created KV. 00172 * - opens the KV with the default permissions (read-only) 00173 * - tries to write the KV data which should fail because KV was not opened with write flag set. 00174 * - closes the opened key 00175 * 00176 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00177 */ 00178 control_t cfstore_open_test_02_end(const size_t call_count) 00179 { 00180 int32_t ret = ARM_DRIVER_ERROR; 00181 ARM_CFSTORE_SIZE len = 0; 00182 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00183 ARM_CFSTORE_KEYDESC kdesc; 00184 ARM_CFSTORE_HANDLE_INIT(hkey); 00185 ARM_CFSTORE_FMODE flags; 00186 00187 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00188 (void) call_count; 00189 memset(&kdesc, 0, sizeof(kdesc)); 00190 /* dont set any flags to get default settings */ 00191 memset(&flags, 0, sizeof(flags)); 00192 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00193 len = strlen(cfstore_open_test_02_data[0].value); 00194 ret = cfstore_test_create(cfstore_open_test_02_data[0].key_name, (char*) cfstore_open_test_02_data[0].value, &len, &kdesc); 00195 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create KV in store (ret=%d).\n", __func__, (int) ret); 00196 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00197 00198 /* by default, owner of key opens with read-only permissions*/ 00199 ret = drv->Open(cfstore_open_test_02_data[0].key_name, flags, hkey); 00200 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open node (key_name=\"%s\")(ret=%d)\n", __func__, cfstore_open_test_02_data[0].key_name, (int) ret); 00201 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00202 00203 len = strlen(cfstore_open_test_02_data[0].value); 00204 ret = drv->Write(hkey, cfstore_open_test_02_data[0].value, &len); 00205 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: call to Write() succeeded when should have failed (key_name=\"%s\")(ret=%d).\n", __func__, cfstore_open_test_02_data[0].key_name, (int) ret); 00206 TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_READ_ONLY, cfstore_open_utest_msg_g); 00207 00208 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Close() call failed.\n", __func__); 00209 TEST_ASSERT_MESSAGE(drv->Close(hkey) >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00210 00211 ret = drv->Uninitialize(); 00212 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00213 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00214 return CaseNext; 00215 } 00216 00217 00218 /** 00219 * @brief test to open() a pre-existing key and try to write it, which should succeed 00220 * because the key was opened read-write permissions explicitly 00221 * 00222 * Basic open test which does the following: 00223 * - creates KV with default rw perms and writes some data to the value blob. 00224 * - closes the newly created KV. 00225 * - opens the KV with the rw permissions (non default) 00226 * - tries to write the KV data which should succeeds because KV was opened with write flag set. 00227 * - closes the opened key 00228 * 00229 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00230 */ 00231 control_t cfstore_open_test_03_end(const size_t call_count) 00232 { 00233 int32_t ret = ARM_DRIVER_ERROR; 00234 ARM_CFSTORE_SIZE len = 0; 00235 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00236 ARM_CFSTORE_KEYDESC kdesc; 00237 ARM_CFSTORE_HANDLE_INIT(hkey); 00238 ARM_CFSTORE_FMODE flags; 00239 00240 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00241 (void) call_count; 00242 memset(&kdesc, 0, sizeof(kdesc)); 00243 /* dont set any flags to get default settings */ 00244 memset(&flags, 0, sizeof(flags)); 00245 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00246 len = strlen(cfstore_open_test_02_data[0].value); 00247 ret = cfstore_test_create(cfstore_open_test_02_data[0].key_name, (char*) cfstore_open_test_02_data[0].value, &len, &kdesc); 00248 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create KV in store (ret=%d).\n", __func__, (int) ret); 00249 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00250 00251 /* opens with read-write permissions*/ 00252 flags.read = true; 00253 flags.write = true; 00254 ret = drv->Open(cfstore_open_test_02_data[0].key_name, flags, hkey); 00255 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open node (key_name=\"%s\")(ret=%d)\n", __func__, cfstore_open_test_02_data[0].key_name, (int) ret); 00256 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00257 00258 len = strlen(cfstore_open_test_02_data[0].value); 00259 ret = drv->Write(hkey, cfstore_open_test_02_data[0].value, &len); 00260 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: call to Write() failed when should have succeeded (key_name=\"%s\")(ret=%d).\n", __func__, cfstore_open_test_02_data[0].key_name, (int) ret); 00261 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00262 00263 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Close() call failed.\n", __func__); 00264 TEST_ASSERT_MESSAGE(drv->Close(hkey) >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00265 00266 ret = drv->Uninitialize(); 00267 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00268 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00269 return CaseNext; 00270 } 00271 00272 00273 00274 /** @brief test to call cfstore_open() with a key_name string that exceeds 00275 * the maximum length 00276 * 00277 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00278 */ 00279 control_t cfstore_open_test_04_end(const size_t call_count) 00280 { 00281 char kv_name_good[CFSTORE_KEY_NAME_MAX_LENGTH+1]; /* extra char for terminating null */ 00282 char kv_name_bad[CFSTORE_KEY_NAME_MAX_LENGTH+2]; 00283 int32_t ret = ARM_DRIVER_ERROR; 00284 ARM_CFSTORE_SIZE len = 0; 00285 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00286 ARM_CFSTORE_KEYDESC kdesc; 00287 ARM_CFSTORE_FMODE flags; 00288 00289 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00290 (void) call_count; 00291 memset(kv_name_good, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00292 memset(kv_name_bad, 0, CFSTORE_KEY_NAME_MAX_LENGTH+2); 00293 memset(&kdesc, 0, sizeof(kdesc)); 00294 /* dont set any flags to get default settings */ 00295 memset(&flags, 0, sizeof(flags)); 00296 00297 len = CFSTORE_KEY_NAME_MAX_LENGTH; 00298 ret = cfstore_test_kv_name_gen(kv_name_good, len); 00299 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: unable to generate kv_name_good.\n", __func__); 00300 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK , cfstore_open_utest_msg_g); 00301 00302 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: kv_name_good is not the correct length (len=%d, expected=%d).\n", __func__, (int) strlen(kv_name_good), (int) len); 00303 TEST_ASSERT_MESSAGE(strlen(kv_name_good) == CFSTORE_KEY_NAME_MAX_LENGTH, cfstore_open_utest_msg_g); 00304 00305 ret = cfstore_test_create(kv_name_good, kv_name_good, &len, &kdesc); 00306 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create KV in store for kv_name_good(ret=%d).\n", __func__, (int) ret); 00307 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00308 00309 len = CFSTORE_KEY_NAME_MAX_LENGTH+1; 00310 ret = cfstore_test_kv_name_gen(kv_name_bad, len); 00311 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: unable to generate kv_name_bad.\n", __func__); 00312 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK , cfstore_open_utest_msg_g); 00313 00314 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: kv_name_bad is not the correct length (len=%d, expected=%d).\n", __func__, (int) strlen(kv_name_bad), (int) len); 00315 TEST_ASSERT_MESSAGE(strlen(kv_name_bad) == CFSTORE_KEY_NAME_MAX_LENGTH+1, cfstore_open_utest_msg_g); 00316 00317 memset(&kdesc, 0, sizeof(kdesc)); 00318 ret = cfstore_test_create(kv_name_bad, kv_name_bad, &len, &kdesc); 00319 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: created KV in store for kv_name_bad when should have failed(ret=%d).\n", __func__, (int) ret); 00320 TEST_ASSERT_MESSAGE(ret < ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00321 00322 ret = drv->Uninitialize(); 00323 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00324 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00325 return CaseNext; 00326 } 00327 00328 00329 /// @cond CFSTORE_DOXYGEN_DISABLE 00330 typedef struct cfstore_open_kv_name_ascii_node { 00331 uint32_t code; 00332 uint32_t f_allowed : 1; 00333 } cfstore_open_kv_name_ascii_node; 00334 /// @endcond 00335 00336 static const uint32_t cfstore_open_kv_name_ascii_table_code_sentinel_g = 256; 00337 00338 /*@brief table recording ascii character codes permitted in kv names */ 00339 static cfstore_open_kv_name_ascii_node cfstore_open_kv_name_ascii_table[] = 00340 { 00341 {0, false}, /* codes 0-44 not allowed */ 00342 {45, true}, /* codes 45-46 == [-.] allowed */ 00343 {47, false}, /* code 47 not allowed */ 00344 {48, true}, /* codes 48-57 not allowed */ 00345 {58, false}, /* codes 46-64 not allowed */ 00346 {64, true}, /* codes 64-91 allowed [@A-Z] */ 00347 {91, false}, /* code 91-96 not allowed */ 00348 {95, true}, /* code 95 allowed '_' */ 00349 {96, false}, /* codes 96 not allowed */ 00350 {97, true}, /* codes 65-90 allowed [A-Z] and {*/ 00351 {123, false}, /* codes 123 '}' not allowed on its own*/ 00352 {124, false}, /* codes 124 not allowed */ 00353 {125, false}, /* code 125 '}' not allowed on its own*/ 00354 {126, false}, /* codes 126-255 not allowed */ 00355 {cfstore_open_kv_name_ascii_table_code_sentinel_g, false}, /* sentinel */ 00356 }; 00357 00358 /// @cond CFSTORE_DOXYGEN_DISABLE 00359 enum cfstore_open_kv_name_pos { 00360 cfstore_open_kv_name_pos_start = 0x0, 00361 cfstore_open_kv_name_pos_mid, 00362 cfstore_open_kv_name_pos_end, 00363 cfstore_open_kv_name_pos_max 00364 }; 00365 /// @endcond 00366 00367 /** @brief test to call cfstore_open() with key_name that in includes 00368 * illegal characters 00369 * - the character(s) can be at the beginning of the key_name 00370 * - the character(s) can be at the end of the key_name 00371 * - the character(s) can be somewhere within the key_name string 00372 * - a max-length string of random characters (legal and illegal) 00373 * - a max-length string of random illegal characters only 00374 * 00375 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00376 */ 00377 control_t cfstore_open_test_05_end(const size_t call_count) 00378 { 00379 bool f_allowed = false; 00380 char kv_name[CFSTORE_KEY_NAME_MAX_LENGTH+1]; /* extra char for terminating null */ 00381 uint32_t j = 0; 00382 int32_t ret = ARM_DRIVER_OK; 00383 size_t name_len = CFSTORE_KEY_NAME_MAX_LENGTH; 00384 ARM_CFSTORE_KEYDESC kdesc; 00385 cfstore_open_kv_name_ascii_node* node = NULL; 00386 uint32_t pos; 00387 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00388 00389 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00390 (void) call_count; 00391 00392 #ifdef CFSTORE_DEBUG 00393 /* symbol only used why debug is enabled */ 00394 const char* pos_str = NULL; 00395 #endif 00396 00397 /* create bad keyname strings with invalid character code at start of keyname */ 00398 node = cfstore_open_kv_name_ascii_table; 00399 while(node->code != cfstore_open_kv_name_ascii_table_code_sentinel_g) 00400 { 00401 /* loop over range */ 00402 for(j = node->code; j < (node+1)->code; j++) 00403 { 00404 /* set the start, mid, last character of the name to the test char code */ 00405 for(pos = (uint32_t) cfstore_open_kv_name_pos_start; pos < (uint32_t) cfstore_open_kv_name_pos_max; pos++) 00406 { 00407 name_len = CFSTORE_KEY_NAME_MAX_LENGTH; 00408 memset(kv_name, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00409 memset(&kdesc, 0, sizeof(kdesc)); 00410 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00411 00412 ret = cfstore_test_kv_name_gen(kv_name, name_len); 00413 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: unable to generate kv_name.\n", __func__); 00414 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK , cfstore_open_utest_msg_g); 00415 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: kv_name incorrect length (len=%d, expected= %d).\n", __func__, (int) strlen(kv_name), (int) name_len); 00416 TEST_ASSERT_MESSAGE(strlen(kv_name) == name_len, cfstore_open_utest_msg_g); 00417 00418 /* overwrite a char at the pos start, mid, end of the kv_name with an ascii char code (both illegal and legal)*/ 00419 switch(pos) 00420 { 00421 case cfstore_open_kv_name_pos_start: 00422 kv_name[0] = (char) j; 00423 break; 00424 case cfstore_open_kv_name_pos_mid: 00425 /* create bad keyname strings with invalid character code in the middle of keyname */ 00426 kv_name[name_len/2] = (char) j; 00427 break; 00428 case cfstore_open_kv_name_pos_end: 00429 /* create bad keyname strings with invalid character code at end of keyname */ 00430 kv_name[name_len-1] = (char) j; 00431 break; 00432 default: 00433 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: unexpected value of pos (pos=%d).\n", __func__, (int) pos); 00434 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00435 break; 00436 } 00437 00438 #ifdef CFSTORE_DEBUG 00439 /* processing only required when debug trace enabled */ 00440 switch(pos) 00441 { 00442 case cfstore_open_kv_name_pos_start: 00443 pos_str = "start"; 00444 break; 00445 case cfstore_open_kv_name_pos_mid: 00446 pos_str = "middle"; 00447 break; 00448 case cfstore_open_kv_name_pos_end: 00449 pos_str = "end"; 00450 break; 00451 default: 00452 break; 00453 } 00454 #endif 00455 ret = cfstore_test_create(kv_name, kv_name, &name_len, &kdesc); 00456 00457 /* special cases */ 00458 switch(j) 00459 { 00460 case 0 : 00461 case 46 : 00462 switch(pos) 00463 { 00464 /* for code = 0 (null terminator). permitted at mid and end of string */ 00465 /* for code = 46 ('.'). permitted at mid and end of string but not at start */ 00466 case cfstore_open_kv_name_pos_start: 00467 f_allowed = false; 00468 break; 00469 case cfstore_open_kv_name_pos_mid: 00470 case cfstore_open_kv_name_pos_end: 00471 default: 00472 f_allowed = true; 00473 break; 00474 } 00475 break; 00476 default: 00477 f_allowed = node->f_allowed; 00478 break; 00479 } 00480 if(f_allowed == true) 00481 { 00482 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create KV in store when kv_name contains valid characters (code=%d, ret=%d).\n", __func__, (int) j, (int) ret); 00483 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00484 /* revert CFSTORE_LOG for more trace */ 00485 CFSTORE_DBGLOG("Successfully created a KV with valid keyname containing ascii character code %d (%c) at the %s of the keyname.\n", (int) j, (int) j, pos_str); 00486 CFSTORE_LOG("%c", '.'); 00487 00488 ret = cfstore_test_delete(kv_name); 00489 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to delete KV previously created (code=%d, ret=%d).\n", __func__, (int) j, (int) ret); 00490 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00491 } 00492 else 00493 { /*node->f_allowed == false => not allowed to create kv name with ascii code */ 00494 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: created KV in store when kv_name contains an invalid character (code=%d, ret=%d).\n", __func__, (int) j, (int) ret); 00495 TEST_ASSERT_MESSAGE(ret < ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00496 /* revert CFSTORE_LOG for more trace */ 00497 CFSTORE_DBGLOG("Successfully failed to create a KV with an invalid keyname containing ascii character code %d at the %s of the keyname.\n", (int) j, pos_str); 00498 CFSTORE_LOG("%c", '.'); 00499 } 00500 } 00501 } 00502 node++; 00503 } 00504 00505 CFSTORE_LOG("%c", '\n'); 00506 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00507 TEST_ASSERT_MESSAGE(drv->Uninitialize() >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00508 return CaseNext; 00509 } 00510 00511 00512 static const char cfstore_open_ascii_illegal_buf_g[] = "!\"�$%&'()*+,./:;<=>?@[\\]^_`{|}~"; /* 31 chars */ 00513 00514 /** @brief test to call cfstore_open() with key_name that in includes 00515 * illegal characters 00516 * - a max-length string of random illegal characters only 00517 * 00518 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00519 */ 00520 control_t cfstore_open_test_06_end(const size_t call_count) 00521 { 00522 char kv_name[CFSTORE_KEY_NAME_MAX_LENGTH+1]; /* extra char for terminating null */ 00523 size_t i = 0; 00524 uint32_t pos = 0; 00525 int32_t ret = ARM_DRIVER_OK; 00526 size_t name_len = CFSTORE_KEY_NAME_MAX_LENGTH; 00527 ARM_CFSTORE_KEYDESC kdesc; 00528 size_t buf_data_max = 0; 00529 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00530 00531 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00532 (void) call_count; 00533 00534 /* create bad keyname strings with invalid character code at start of keyname */ 00535 buf_data_max = strlen(cfstore_open_ascii_illegal_buf_g); 00536 name_len = CFSTORE_KEY_NAME_MAX_LENGTH; 00537 memset(kv_name, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00538 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00539 00540 /* generate a kv name of illegal chars*/ 00541 for(i = 0; i < name_len; i++) 00542 { 00543 pos = rand() % (buf_data_max+1); 00544 kv_name[i] = cfstore_open_ascii_illegal_buf_g[pos]; 00545 } 00546 00547 ret = cfstore_test_create(kv_name, kv_name, &name_len, &kdesc); 00548 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: created KV in store when kv_name contains invalid characters (ret=%d).\n", __func__, (int) ret); 00549 TEST_ASSERT_MESSAGE(ret < ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00550 00551 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00552 TEST_ASSERT_MESSAGE(drv->Uninitialize() >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00553 return CaseNext; 00554 } 00555 00556 /** @brief test to call cfstore_open() with key_name that in includes 00557 * illegal characters 00558 * - a max-length string of random characters (legal and illegal) 00559 * 00560 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. 00561 */ 00562 control_t cfstore_open_test_07_end(const size_t call_count) 00563 { 00564 char kv_name[CFSTORE_KEY_NAME_MAX_LENGTH+1]; /* extra char for terminating null */ 00565 size_t i = 0; 00566 int32_t ret = ARM_DRIVER_OK; 00567 size_t name_len = CFSTORE_KEY_NAME_MAX_LENGTH; 00568 ARM_CFSTORE_KEYDESC kdesc; 00569 size_t buf_data_max = 0; 00570 ARM_CFSTORE_DRIVER* drv = &cfstore_driver; 00571 00572 CFSTORE_FENTRYLOG("%s:entered\n", __func__); 00573 (void) call_count; 00574 00575 /* create bad keyname strings with invalid character code at start of keyname */ 00576 buf_data_max = strlen(cfstore_open_ascii_illegal_buf_g); 00577 name_len = CFSTORE_KEY_NAME_MAX_LENGTH; 00578 memset(kv_name, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00579 kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; 00580 00581 ret = cfstore_test_kv_name_gen(kv_name, name_len); 00582 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: unable to generate kv_name.\n", __func__); 00583 TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK , cfstore_open_utest_msg_g); 00584 00585 /* pepper the illegal chars across the string*/ 00586 for(i++; i < buf_data_max; i++){ 00587 kv_name[rand() % (name_len+1)] = cfstore_open_ascii_illegal_buf_g[i]; 00588 } 00589 ret = cfstore_test_create(kv_name, kv_name, &name_len, &kdesc); 00590 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: created KV in store when kv_name contains invalid characters (ret=%d).\n", __func__, (int) ret); 00591 TEST_ASSERT_MESSAGE(ret < ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00592 00593 CFSTORE_TEST_UTEST_MESSAGE(cfstore_open_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); 00594 TEST_ASSERT_MESSAGE(drv->Uninitialize() >= ARM_DRIVER_OK, cfstore_open_utest_msg_g); 00595 return CaseNext; 00596 } 00597 00598 /// @cond CFSTORE_DOXYGEN_DISABLE 00599 utest::v1::status_t greentea_setup(const size_t number_of_cases) 00600 { 00601 GREENTEA_SETUP(CFSTORE_OPEN_GREENTEA_TIMEOUT_S, "default_auto"); 00602 return greentea_test_setup_handler(number_of_cases); 00603 } 00604 00605 Case cases[] = { 00606 /* 1 2 3 4 5 6 7 */ 00607 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ 00608 Case("OPEN_test_00", cfstore_open_test_00), 00609 Case("OPEN_test_01_start", cfstore_utest_default_start), 00610 Case("OPEN_test_01_end", cfstore_open_test_01_end), 00611 Case("OPEN_test_02_start", cfstore_utest_default_start), 00612 Case("OPEN_test_02_end", cfstore_open_test_02_end), 00613 Case("OPEN_test_03_start", cfstore_utest_default_start), 00614 Case("OPEN_test_03_end", cfstore_open_test_03_end), 00615 Case("OPEN_test_04_start", cfstore_utest_default_start), 00616 Case("OPEN_test_04_end", cfstore_open_test_04_end), 00617 Case("OPEN_test_05_start", cfstore_utest_default_start), 00618 Case("OPEN_test_05_end", cfstore_open_test_05_end), 00619 Case("OPEN_test_06_start", cfstore_utest_default_start), 00620 Case("OPEN_test_06_end", cfstore_open_test_06_end), 00621 Case("OPEN_test_07_start", cfstore_utest_default_start), 00622 Case("OPEN_test_07_end", cfstore_open_test_07_end), 00623 }; 00624 00625 00626 /* Declare your test specification with a custom setup handler */ 00627 Specification specification(greentea_setup, cases); 00628 00629 int main() 00630 { 00631 return !Harness::run(specification); 00632 } 00633 /// @endcond
Generated on Tue Aug 9 2022 00:37:17 by
1.7.2