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.
test_cs_nvm.c
00001 /* 00002 * Copyright (c) 2016 ARM Limited. All rights reserved. 00003 */ 00004 00005 #include <inttypes.h> 00006 #include <configuration-store/configuration_store.h> 00007 #include "platform/arm_hal_nvm.h" 00008 00009 #include "test_cs_nvm.h" 00010 #include "nsdynmemLIB_stub.h" 00011 #include "configuration_store_stub.h" 00012 00013 #define TEST_NS_NVM_HELPER_CONTEXT_CLEAR 0xffff 00014 #define TEST_PLATFORM_NVM_STATUS_CLEAR 0xffff 00015 00016 #define TEST_NS_NVM_HELPER_CONTEXT1 0x01 00017 #define TEST_NS_NVM_HELPER_CONTEXT2 0x02 00018 #define TEST_NS_NVM_HELPER_CONTEXT3 0x03 00019 00020 const char *key_name = "testkey"; 00021 uint8_t buf[100]; 00022 uint16_t buf_len; 00023 uint16_t data_len; 00024 00025 typedef struct 00026 { 00027 platform_nvm_status status; 00028 void *ctx; 00029 } test_platform_nvm_api_callback_t; 00030 00031 static test_platform_nvm_api_callback_t *active_cb_data_ptr; 00032 static test_platform_nvm_api_callback_t cb_data = {TEST_PLATFORM_NVM_STATUS_CLEAR, TEST_NS_NVM_HELPER_CONTEXT_CLEAR}; 00033 00034 void test_nvm_callback(platform_nvm_status status, void *context) 00035 { 00036 active_cb_data_ptr->status = status; 00037 active_cb_data_ptr->ctx = context; 00038 } 00039 00040 static void test_callback_data_clear(test_platform_nvm_api_callback_t *callback_data_ptr) 00041 { 00042 callback_data_ptr->status = TEST_PLATFORM_NVM_STATUS_CLEAR; 00043 callback_data_ptr->ctx = TEST_NS_NVM_HELPER_CONTEXT_CLEAR; 00044 } 00045 00046 static bool test_callback_executed(test_platform_nvm_api_callback_t *callback_data_ptr) 00047 { 00048 if (callback_data_ptr->status != TEST_PLATFORM_NVM_STATUS_CLEAR) { 00049 return true; 00050 } 00051 if (callback_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT_CLEAR) { 00052 return true; 00053 } 00054 return false; 00055 } 00056 00057 static bool test_nvm_initialize() 00058 { 00059 platform_nvm_status ret; 00060 active_cb_data_ptr = &cb_data; 00061 test_callback_data_clear(active_cb_data_ptr); 00062 00063 // OK initialization 00064 cfstore_stub.ret_val = ARM_DRIVER_OK; 00065 cfstore_stub.cmd_code = CFSTORE_OPCODE_INITIALIZE; 00066 nsdynmemlib_stub.returnCounter = 1; 00067 ret = platform_nvm_init(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT1); 00068 if (ret != PLATFORM_NVM_OK) { 00069 return false; 00070 } 00071 00072 // simulate configuration_store callback and cs_nvm timer callback 00073 cfstore_stub.cmd_code = CFSTORE_OPCODE_POWER_CONTROL; 00074 test_cfstore_callback_trigger(); 00075 00076 // simulate configuration_store callback and cs_nvm timer callback 00077 cfstore_stub.cmd_code = CFSTORE_OPCODE_INITIALIZE; 00078 test_cfstore_callback_trigger(); 00079 test_eventOS_timeout_trigger(); 00080 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00081 return false; 00082 } 00083 00084 return true; 00085 } 00086 00087 static bool test_nvm_finalize() 00088 { 00089 platform_nvm_status ret; 00090 active_cb_data_ptr = &cb_data; 00091 test_callback_data_clear(active_cb_data_ptr); 00092 00093 // finalize NVM - OK 00094 cfstore_stub.ret_val = ARM_DRIVER_OK; 00095 ret = platform_nvm_finalize(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT2); 00096 if (ret != PLATFORM_NVM_OK) { 00097 return false; 00098 } 00099 00100 // simulate configuration_store callback and cs_nvm timer callback 00101 cfstore_stub.cmd_code = CFSTORE_OPCODE_POWER_CONTROL; 00102 test_cfstore_callback_trigger(); 00103 00104 // simulate configuration_store callback and cs_nvm timer callback 00105 cfstore_stub.cmd_code = CFSTORE_OPCODE_UNINITIALIZE; 00106 test_callback_data_clear(active_cb_data_ptr); 00107 test_cfstore_callback_trigger(); 00108 test_eventOS_timeout_trigger(); 00109 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00110 return false; 00111 } 00112 return true; 00113 } 00114 00115 bool test_cs_nvm_init_finalize() 00116 { 00117 platform_nvm_status ret; 00118 active_cb_data_ptr = &cb_data; 00119 test_callback_data_clear(active_cb_data_ptr); 00120 00121 // No callback - fail 00122 cfstore_stub.ret_val = ARM_DRIVER_OK; 00123 cfstore_stub.cmd_code = CFSTORE_OPCODE_INITIALIZE; 00124 ret = platform_nvm_init(NULL, TEST_NS_NVM_HELPER_CONTEXT1); 00125 if (ret != PLATFORM_NVM_ERROR) { 00126 return false; 00127 } 00128 00129 // memory allocation fails 00130 nsdynmemlib_stub.returnCounter = 0; 00131 ret = platform_nvm_init(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT1); 00132 if (ret != PLATFORM_NVM_ERROR) { 00133 return false; 00134 } 00135 00136 // configuration-store returns failure 00137 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00138 nsdynmemlib_stub.returnCounter = 1; 00139 ret = platform_nvm_init(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT1); 00140 if (ret != PLATFORM_NVM_ERROR) { 00141 return false; 00142 } 00143 00144 // OK initialization 00145 cfstore_stub.ret_val = ARM_DRIVER_OK; 00146 nsdynmemlib_stub.returnCounter = 1; 00147 ret = platform_nvm_init(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT1); 00148 if (ret != PLATFORM_NVM_OK) { 00149 return false; 00150 } 00151 00152 // simulate configuration_store callback and cs_nvm timer callback 00153 cfstore_stub.cmd_code = CFSTORE_OPCODE_POWER_CONTROL; 00154 test_cfstore_callback_trigger(); 00155 00156 // simulate configuration_store callback and cs_nvm timer callback 00157 cfstore_stub.cmd_code = CFSTORE_OPCODE_INITIALIZE; 00158 test_cfstore_callback_trigger(); 00159 test_eventOS_timeout_trigger(); 00160 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00161 return false; 00162 } 00163 00164 // Already initialized 00165 ret = platform_nvm_init(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT1); 00166 if (ret != PLATFORM_NVM_ERROR) { 00167 return false; 00168 } 00169 00170 // finalize NVM - no callback 00171 ret = platform_nvm_finalize(NULL, TEST_NS_NVM_HELPER_CONTEXT2); 00172 if (ret != PLATFORM_NVM_ERROR) { 00173 return false; 00174 } 00175 00176 // finalize NVM - configuration-store error 00177 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00178 ret = platform_nvm_finalize(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT2); 00179 if (ret != PLATFORM_NVM_OK) { 00180 return false; 00181 } 00182 cfstore_stub.cmd_code = CFSTORE_OPCODE_UNINITIALIZE; 00183 test_cfstore_callback_trigger(); 00184 test_eventOS_timeout_trigger(); 00185 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00186 return false; 00187 } 00188 00189 // finalize NVM - OK 00190 if (!test_nvm_finalize()){ 00191 return false; 00192 } 00193 00194 // finalize - already finalized 00195 ret = platform_nvm_finalize(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT2); 00196 if (ret != PLATFORM_NVM_ERROR) { 00197 return false; 00198 } 00199 00200 return true; 00201 } 00202 00203 bool test_cs_nvm_key_create() 00204 { 00205 platform_nvm_status ret; 00206 active_cb_data_ptr = &cb_data; 00207 test_callback_data_clear(active_cb_data_ptr); 00208 00209 // OK initialization 00210 if (!test_nvm_initialize()) { 00211 return false; 00212 } 00213 00214 // Test - no callback 00215 ret = platform_nvm_key_create(NULL, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT2); 00216 if (ret != PLATFORM_NVM_ERROR) { 00217 return false; 00218 } 00219 00220 // No key_name 00221 ret = platform_nvm_key_create(test_nvm_callback, NULL, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT2); 00222 if (ret != PLATFORM_NVM_ERROR) { 00223 return false; 00224 } 00225 00226 // configuration-store returns error 00227 test_callback_data_clear(active_cb_data_ptr); 00228 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00229 ret = platform_nvm_key_create(test_nvm_callback, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT2); 00230 if (ret != PLATFORM_NVM_OK) { 00231 return false; 00232 } 00233 // simulate configuration_store callback and cs_nvm timer callback 00234 cfstore_stub.cmd_code = CFSTORE_OPCODE_CREATE; 00235 test_cfstore_callback_trigger(); 00236 test_eventOS_timeout_trigger(); 00237 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00238 return false; 00239 } 00240 00241 //OK key creation 00242 test_callback_data_clear(active_cb_data_ptr); 00243 cfstore_stub.ret_val = ARM_DRIVER_OK; 00244 ret = platform_nvm_key_create(test_nvm_callback, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT3); 00245 if (ret != PLATFORM_NVM_OK) { 00246 return false; 00247 } 00248 00249 // create callback 00250 cfstore_stub.cmd_code = CFSTORE_OPCODE_CREATE; 00251 test_callback_data_clear(active_cb_data_ptr); 00252 test_cfstore_callback_trigger(); 00253 test_eventOS_timeout_trigger(); 00254 00255 // close callback 00256 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00257 test_cfstore_callback_trigger(); 00258 test_eventOS_timeout_trigger(); 00259 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT3) { 00260 return false; 00261 } 00262 00263 //OK key creation - close failing 00264 test_callback_data_clear(active_cb_data_ptr); 00265 cfstore_stub.ret_val = ARM_DRIVER_OK; 00266 cfstore_stub.close_ret_val = ARM_DRIVER_ERROR; 00267 ret = platform_nvm_key_create(test_nvm_callback, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT3); 00268 if (ret != PLATFORM_NVM_OK) { 00269 return false; 00270 } 00271 00272 // create callback 00273 cfstore_stub.cmd_code = CFSTORE_OPCODE_CREATE; 00274 test_callback_data_clear(active_cb_data_ptr); 00275 test_cfstore_callback_trigger(); 00276 00277 // close callback 00278 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00279 test_cfstore_callback_trigger(); 00280 test_eventOS_timeout_trigger(); 00281 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT3) { 00282 return false; 00283 } 00284 00285 // OK key re-creation failure 00286 test_callback_data_clear(active_cb_data_ptr); 00287 cfstore_stub.ret_val = ARM_CFSTORE_DRIVER_ERROR_PREEXISTING_KEY; 00288 ret = platform_nvm_key_create(test_nvm_callback, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT3); 00289 if (ret != PLATFORM_NVM_OK) { 00290 return false; 00291 } 00292 00293 cfstore_stub.cmd_code = CFSTORE_OPCODE_CREATE; 00294 test_callback_data_clear(active_cb_data_ptr); 00295 test_cfstore_callback_trigger(); 00296 test_eventOS_timeout_trigger(); 00297 00298 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT3) { 00299 return false; 00300 } 00301 00302 // finalize NVM - OK 00303 if (!test_nvm_finalize()){ 00304 return false; 00305 } 00306 00307 // Create - not initialized 00308 ret = platform_nvm_key_create(test_nvm_callback, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT3); 00309 if (ret != PLATFORM_NVM_ERROR) { 00310 return false; 00311 } 00312 00313 return true; 00314 } 00315 00316 bool test_cs_nvm_key_delete() 00317 { 00318 platform_nvm_status ret; 00319 active_cb_data_ptr = &cb_data; 00320 test_callback_data_clear(active_cb_data_ptr); 00321 00322 // NVM not initialized 00323 ret = platform_nvm_key_delete(test_nvm_callback, key_name, TEST_NS_NVM_HELPER_CONTEXT1); 00324 if (ret != PLATFORM_NVM_ERROR) { 00325 return false; 00326 } 00327 00328 if (!test_nvm_initialize()) { 00329 return false; 00330 } 00331 00332 test_callback_data_clear(active_cb_data_ptr); 00333 00334 // No callback 00335 ret = platform_nvm_key_delete(NULL, key_name, TEST_NS_NVM_HELPER_CONTEXT1); 00336 if (ret != PLATFORM_NVM_ERROR) { 00337 return false; 00338 } 00339 00340 // No key_name 00341 ret = platform_nvm_key_delete(test_nvm_callback, NULL, TEST_NS_NVM_HELPER_CONTEXT1); 00342 if (ret != PLATFORM_NVM_ERROR) { 00343 return false; 00344 } 00345 00346 // Configuration-store returns error in OPEN directly 00347 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00348 ret = platform_nvm_key_delete(test_nvm_callback, key_name, TEST_NS_NVM_HELPER_CONTEXT1); 00349 if (ret != PLATFORM_NVM_OK) { 00350 return false; 00351 } 00352 // simulate failing configuration store 00353 test_callback_data_clear(active_cb_data_ptr); 00354 test_eventOS_timeout_trigger(); 00355 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00356 return false; 00357 } 00358 00359 // Configuration-store returns error in DELETE 00360 cfstore_stub.ret_val = ARM_DRIVER_OK; 00361 cfstore_stub.delete_ret_val = ARM_DRIVER_ERROR; 00362 test_callback_data_clear(active_cb_data_ptr); 00363 ret = platform_nvm_key_delete(test_nvm_callback, key_name, TEST_NS_NVM_HELPER_CONTEXT1); 00364 if (ret != PLATFORM_NVM_OK) { 00365 return false; 00366 } 00367 00368 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00369 cfstore_stub.close_ret_val = ARM_DRIVER_OK; 00370 test_callback_data_clear(active_cb_data_ptr); 00371 test_cfstore_callback_trigger(); 00372 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00373 if (test_callback_executed(active_cb_data_ptr)) { 00374 return false; 00375 } 00376 00377 cfstore_stub.ret_val = ARM_DRIVER_OK; 00378 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00379 test_cfstore_callback_trigger(); 00380 test_eventOS_timeout_trigger(); 00381 00382 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00383 return false; 00384 } 00385 00386 // OK delete 00387 cfstore_stub.ret_val = ARM_DRIVER_OK; 00388 cfstore_stub.delete_ret_val = ARM_DRIVER_OK; 00389 ret = platform_nvm_key_delete(test_nvm_callback, key_name, TEST_NS_NVM_HELPER_CONTEXT1); 00390 if (ret != PLATFORM_NVM_OK) { 00391 return false; 00392 } 00393 00394 // simulate open 00395 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00396 test_callback_data_clear(active_cb_data_ptr); 00397 test_cfstore_callback_trigger(); 00398 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00399 if (test_callback_executed(active_cb_data_ptr)) { 00400 return false; 00401 } 00402 00403 // simulate delete 00404 cfstore_stub.cmd_code = CFSTORE_OPCODE_DELETE; 00405 test_callback_data_clear(active_cb_data_ptr); 00406 test_cfstore_callback_trigger(); 00407 test_eventOS_timeout_trigger(); // should not have any effect since state is deleting 00408 if (test_callback_executed(active_cb_data_ptr)) { 00409 return false; 00410 } 00411 00412 // simulate close 00413 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00414 test_callback_data_clear(active_cb_data_ptr); 00415 test_cfstore_callback_trigger(); 00416 test_eventOS_timeout_trigger(); //trigger callback 00417 00418 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00419 return false; 00420 } 00421 00422 if (!test_nvm_finalize()) { 00423 return false; 00424 } 00425 00426 return true; 00427 } 00428 00429 bool test_cs_nvm_key_delete_failure_in_close() 00430 { 00431 platform_nvm_status ret; 00432 active_cb_data_ptr = &cb_data; 00433 00434 if (!test_nvm_initialize()) { 00435 return false; 00436 } 00437 test_callback_data_clear(active_cb_data_ptr); 00438 00439 // Delete is OK but closing fails 00440 cfstore_stub.ret_val = ARM_DRIVER_OK; 00441 cfstore_stub.read_ret_val = ARM_DRIVER_OK; 00442 cfstore_stub.close_ret_val = ARM_DRIVER_ERROR; 00443 test_callback_data_clear(active_cb_data_ptr); 00444 ret = platform_nvm_key_delete(test_nvm_callback, key_name, TEST_NS_NVM_HELPER_CONTEXT2); 00445 if (ret != PLATFORM_NVM_OK) { 00446 return false; 00447 } 00448 00449 // simulate open 00450 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00451 test_callback_data_clear(active_cb_data_ptr); 00452 test_cfstore_callback_trigger(); 00453 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00454 if (test_callback_executed(active_cb_data_ptr)) { 00455 return false; 00456 } 00457 00458 // simulate delete 00459 cfstore_stub.cmd_code = CFSTORE_OPCODE_DELETE; 00460 test_callback_data_clear(active_cb_data_ptr); 00461 test_cfstore_callback_trigger(); 00462 00463 // simulate close 00464 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00465 test_callback_data_clear(active_cb_data_ptr); 00466 test_cfstore_callback_trigger(); 00467 test_eventOS_timeout_trigger(); //trigger callback 00468 00469 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00470 return false; 00471 } 00472 00473 if (!test_nvm_finalize()) { 00474 return false; 00475 } 00476 00477 return true; 00478 } 00479 00480 bool test_cs_nvm_read() 00481 { 00482 platform_nvm_status ret; 00483 active_cb_data_ptr = &cb_data; 00484 test_callback_data_clear(active_cb_data_ptr); 00485 cfstore_stub.ret_val = ARM_DRIVER_OK; 00486 cfstore_stub.read_ret_val = ARM_DRIVER_OK; 00487 cfstore_stub.close_ret_val = ARM_DRIVER_OK; 00488 00489 // read uninitialized 00490 ret = platform_nvm_read(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00491 if (ret != PLATFORM_NVM_ERROR) { 00492 return false; 00493 } 00494 00495 if (!test_nvm_initialize()) { 00496 return false; 00497 } 00498 test_callback_data_clear(active_cb_data_ptr); 00499 00500 // no callback 00501 ret = platform_nvm_read(NULL, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00502 if (ret != PLATFORM_NVM_ERROR) { 00503 return false; 00504 } 00505 00506 // no key_name 00507 ret = platform_nvm_read(test_nvm_callback, NULL, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00508 if (ret != PLATFORM_NVM_ERROR) { 00509 return false; 00510 } 00511 00512 // no buf 00513 ret = platform_nvm_read(test_nvm_callback, key_name, NULL, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00514 if (ret != PLATFORM_NVM_ERROR) { 00515 return false; 00516 } 00517 00518 // no buf_len 00519 ret = platform_nvm_read(test_nvm_callback, key_name, buf, NULL, TEST_NS_NVM_HELPER_CONTEXT1); 00520 if (ret != PLATFORM_NVM_ERROR) { 00521 return false; 00522 } 00523 00524 // OK 00525 ret = platform_nvm_read(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00526 if (ret != PLATFORM_NVM_OK) { 00527 return false; 00528 } 00529 00530 // simulate open 00531 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00532 test_callback_data_clear(active_cb_data_ptr); 00533 test_cfstore_callback_trigger(); 00534 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00535 if (test_callback_executed(active_cb_data_ptr)) { 00536 return false; 00537 } 00538 00539 // simulate read 00540 cfstore_stub.cmd_code = CFSTORE_OPCODE_READ; 00541 test_callback_data_clear(active_cb_data_ptr); 00542 test_cfstore_callback_trigger(); 00543 test_eventOS_timeout_trigger(); // should not have any effect since state is deleting 00544 if (test_callback_executed(active_cb_data_ptr)) { 00545 return false; 00546 } 00547 00548 // simulate close 00549 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00550 test_callback_data_clear(active_cb_data_ptr); 00551 test_cfstore_callback_trigger(); 00552 test_eventOS_timeout_trigger(); //trigger callback 00553 00554 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00555 return false; 00556 } 00557 00558 test_callback_data_clear(active_cb_data_ptr); 00559 if (!test_nvm_finalize()) { 00560 return false; 00561 } 00562 00563 return true; 00564 } 00565 00566 bool test_cs_nvm_read_failure() 00567 { 00568 platform_nvm_status ret; 00569 active_cb_data_ptr = &cb_data; 00570 00571 if (!test_nvm_initialize()) { 00572 return false; 00573 } 00574 test_callback_data_clear(active_cb_data_ptr); 00575 00576 // Open fails 00577 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00578 ret = platform_nvm_read(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT2); 00579 if (ret != PLATFORM_NVM_OK) { 00580 return false; 00581 } 00582 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00583 test_callback_data_clear(active_cb_data_ptr); 00584 test_cfstore_callback_trigger(); 00585 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00586 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00587 return false; 00588 } 00589 00590 // Read fails 00591 cfstore_stub.ret_val = ARM_DRIVER_OK; 00592 cfstore_stub.read_ret_val = ARM_DRIVER_OK; 00593 cfstore_stub.close_ret_val = ARM_DRIVER_OK; 00594 test_callback_data_clear(active_cb_data_ptr); 00595 ret = platform_nvm_read(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT2); 00596 if (ret != PLATFORM_NVM_OK) { 00597 return false; 00598 } 00599 00600 // simulate open 00601 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00602 cfstore_stub.read_ret_val = ARM_DRIVER_ERROR; 00603 test_callback_data_clear(active_cb_data_ptr); 00604 test_cfstore_callback_trigger(); 00605 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00606 if (test_callback_executed(active_cb_data_ptr)) { 00607 return false; 00608 } 00609 00610 // simulate read 00611 cfstore_stub.cmd_code = CFSTORE_OPCODE_READ; 00612 test_callback_data_clear(active_cb_data_ptr); 00613 test_cfstore_callback_trigger(); 00614 test_eventOS_timeout_trigger(); // should not have any effect since state is deleting 00615 if (test_callback_executed(active_cb_data_ptr)) { 00616 return false; 00617 } 00618 00619 // simulate close 00620 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00621 test_callback_data_clear(active_cb_data_ptr); 00622 test_cfstore_callback_trigger(); 00623 test_eventOS_timeout_trigger(); //trigger callback 00624 00625 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00626 return false; 00627 } 00628 00629 if (!test_nvm_finalize()) { 00630 return false; 00631 } 00632 00633 return true; 00634 } 00635 00636 bool test_cs_nvm_read_failure_in_close() 00637 { 00638 platform_nvm_status ret; 00639 active_cb_data_ptr = &cb_data; 00640 00641 if (!test_nvm_initialize()) { 00642 return false; 00643 } 00644 test_callback_data_clear(active_cb_data_ptr); 00645 00646 // Open fails 00647 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00648 ret = platform_nvm_read(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT2); 00649 if (ret != PLATFORM_NVM_OK) { 00650 return false; 00651 } 00652 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00653 test_callback_data_clear(active_cb_data_ptr); 00654 test_cfstore_callback_trigger(); 00655 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00656 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00657 return false; 00658 } 00659 00660 // Read is OK but closing fails 00661 cfstore_stub.ret_val = ARM_DRIVER_OK; 00662 cfstore_stub.read_ret_val = ARM_DRIVER_OK; 00663 cfstore_stub.close_ret_val = ARM_DRIVER_ERROR; 00664 test_callback_data_clear(active_cb_data_ptr); 00665 ret = platform_nvm_read(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT2); 00666 if (ret != PLATFORM_NVM_OK) { 00667 return false; 00668 } 00669 00670 // simulate open 00671 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00672 test_callback_data_clear(active_cb_data_ptr); 00673 test_cfstore_callback_trigger(); 00674 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00675 if (test_callback_executed(active_cb_data_ptr)) { 00676 return false; 00677 } 00678 00679 // simulate read 00680 cfstore_stub.cmd_code = CFSTORE_OPCODE_READ; 00681 test_callback_data_clear(active_cb_data_ptr); 00682 test_cfstore_callback_trigger(); 00683 00684 // simulate close 00685 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00686 test_callback_data_clear(active_cb_data_ptr); 00687 test_cfstore_callback_trigger(); 00688 test_eventOS_timeout_trigger(); //trigger callback 00689 00690 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00691 return false; 00692 } 00693 00694 if (!test_nvm_finalize()) { 00695 return false; 00696 } 00697 00698 return true; 00699 } 00700 00701 bool test_cs_nvm_write() 00702 { 00703 platform_nvm_status ret; 00704 active_cb_data_ptr = &cb_data; 00705 test_callback_data_clear(active_cb_data_ptr); 00706 cfstore_stub.ret_val = ARM_DRIVER_OK; 00707 cfstore_stub.read_ret_val = ARM_DRIVER_OK; 00708 cfstore_stub.close_ret_val = ARM_DRIVER_OK; 00709 00710 // write uninitialized 00711 ret = platform_nvm_write(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00712 if (ret != PLATFORM_NVM_ERROR) { 00713 return false; 00714 } 00715 00716 if (!test_nvm_initialize()) { 00717 return false; 00718 } 00719 test_callback_data_clear(active_cb_data_ptr); 00720 00721 // no callback 00722 ret = platform_nvm_write(NULL, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00723 if (ret != PLATFORM_NVM_ERROR) { 00724 return false; 00725 } 00726 00727 // no key_name 00728 ret = platform_nvm_write(test_nvm_callback, NULL, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00729 if (ret != PLATFORM_NVM_ERROR) { 00730 return false; 00731 } 00732 00733 // no buf 00734 ret = platform_nvm_write(test_nvm_callback, key_name, NULL, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00735 if (ret != PLATFORM_NVM_ERROR) { 00736 return false; 00737 } 00738 00739 // no buf_len 00740 ret = platform_nvm_write(test_nvm_callback, key_name, buf, NULL, TEST_NS_NVM_HELPER_CONTEXT1); 00741 if (ret != PLATFORM_NVM_ERROR) { 00742 return false; 00743 } 00744 00745 // OK 00746 ret = platform_nvm_write(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00747 if (ret != PLATFORM_NVM_OK) { 00748 return false; 00749 } 00750 00751 // simulate open 00752 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00753 test_callback_data_clear(active_cb_data_ptr); 00754 test_cfstore_callback_trigger(); 00755 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00756 if (test_callback_executed(active_cb_data_ptr)) { 00757 return false; 00758 } 00759 00760 // simulate write 00761 cfstore_stub.cmd_code = CFSTORE_OPCODE_WRITE; 00762 test_callback_data_clear(active_cb_data_ptr); 00763 test_cfstore_callback_trigger(); 00764 test_eventOS_timeout_trigger(); // should not have any effect since state is deleting 00765 if (test_callback_executed(active_cb_data_ptr)) { 00766 return false; 00767 } 00768 00769 // simulate close 00770 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00771 test_callback_data_clear(active_cb_data_ptr); 00772 test_cfstore_callback_trigger(); 00773 test_eventOS_timeout_trigger(); //trigger callback 00774 00775 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00776 return false; 00777 } 00778 00779 test_callback_data_clear(active_cb_data_ptr); 00780 if (!test_nvm_finalize()) { 00781 return false; 00782 } 00783 00784 return true; 00785 } 00786 00787 bool test_cs_nvm_write_failure() 00788 { 00789 platform_nvm_status ret; 00790 active_cb_data_ptr = &cb_data; 00791 test_callback_data_clear(active_cb_data_ptr); 00792 00793 if (!test_nvm_initialize()) { 00794 return false; 00795 } 00796 test_callback_data_clear(active_cb_data_ptr); 00797 00798 // write fails on open 00799 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00800 cfstore_stub.write_ret_val = ARM_DRIVER_OK; 00801 cfstore_stub.close_ret_val = ARM_DRIVER_OK; 00802 ret = platform_nvm_write(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00803 if (ret != PLATFORM_NVM_OK) { 00804 return false; 00805 } 00806 // simulate open 00807 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00808 test_callback_data_clear(active_cb_data_ptr); 00809 test_cfstore_callback_trigger(); 00810 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00811 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00812 return false; 00813 } 00814 00815 // write fails on write and also close fails 00816 cfstore_stub.ret_val = ARM_DRIVER_OK; 00817 cfstore_stub.write_ret_val = ARM_DRIVER_ERROR; 00818 cfstore_stub.close_ret_val = ARM_DRIVER_ERROR; 00819 ret = platform_nvm_write(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00820 if (ret != PLATFORM_NVM_OK) { 00821 return false; 00822 } 00823 // simulate open, write - fails and close fails 00824 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00825 test_callback_data_clear(active_cb_data_ptr); 00826 test_cfstore_callback_trigger(); 00827 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00828 00829 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT1) { 00830 return false; 00831 } 00832 00833 test_callback_data_clear(active_cb_data_ptr); 00834 if (!test_nvm_finalize()) { 00835 return false; 00836 } 00837 00838 return true; 00839 } 00840 00841 bool test_cs_nvm_write_failure_in_close() 00842 { 00843 platform_nvm_status ret; 00844 active_cb_data_ptr = &cb_data; 00845 00846 if (!test_nvm_initialize()) { 00847 return false; 00848 } 00849 test_callback_data_clear(active_cb_data_ptr); 00850 00851 // write is OK but closing fails 00852 cfstore_stub.ret_val = ARM_DRIVER_OK; 00853 cfstore_stub.write_ret_val = ARM_DRIVER_OK; 00854 cfstore_stub.close_ret_val = ARM_DRIVER_ERROR; 00855 test_callback_data_clear(active_cb_data_ptr); 00856 ret = platform_nvm_write(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT2); 00857 if (ret != PLATFORM_NVM_OK) { 00858 return false; 00859 } 00860 00861 // simulate open 00862 cfstore_stub.cmd_code = CFSTORE_OPCODE_OPEN; 00863 test_callback_data_clear(active_cb_data_ptr); 00864 test_cfstore_callback_trigger(); 00865 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00866 if (test_callback_executed(active_cb_data_ptr)) { 00867 return false; 00868 } 00869 00870 // simulate write 00871 cfstore_stub.cmd_code = CFSTORE_OPCODE_WRITE; 00872 test_callback_data_clear(active_cb_data_ptr); 00873 test_cfstore_callback_trigger(); 00874 00875 // simulate close 00876 cfstore_stub.cmd_code = CFSTORE_OPCODE_CLOSE; 00877 test_callback_data_clear(active_cb_data_ptr); 00878 test_cfstore_callback_trigger(); 00879 test_eventOS_timeout_trigger(); //trigger callback 00880 00881 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT2) { 00882 return false; 00883 } 00884 00885 if (!test_nvm_finalize()) { 00886 return false; 00887 } 00888 00889 return true; 00890 } 00891 00892 bool test_cs_nvm_flush() 00893 { 00894 platform_nvm_status ret; 00895 active_cb_data_ptr = &cb_data; 00896 test_callback_data_clear(active_cb_data_ptr); 00897 cfstore_stub.ret_val = ARM_DRIVER_OK; 00898 cfstore_stub.write_ret_val = ARM_DRIVER_OK; 00899 cfstore_stub.close_ret_val = ARM_DRIVER_OK; 00900 00901 // flush with NVM uninitialized 00902 ret = platform_nvm_flush(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT3); 00903 if (ret != PLATFORM_NVM_ERROR) { 00904 return false; 00905 } 00906 00907 if (!test_nvm_initialize()) { 00908 return false; 00909 } 00910 test_callback_data_clear(active_cb_data_ptr); 00911 00912 // no callback 00913 ret = platform_nvm_flush(NULL, TEST_NS_NVM_HELPER_CONTEXT1); 00914 if (ret != PLATFORM_NVM_ERROR) { 00915 return false; 00916 } 00917 00918 // flush failure 00919 cfstore_stub.ret_val = ARM_DRIVER_ERROR; 00920 ret = platform_nvm_flush(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT3); 00921 if (ret != PLATFORM_NVM_OK) { 00922 return false; 00923 } 00924 // simulate flush 00925 cfstore_stub.cmd_code = CFSTORE_OPCODE_FLUSH; 00926 test_callback_data_clear(active_cb_data_ptr); 00927 test_cfstore_callback_trigger(); 00928 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00929 if (active_cb_data_ptr->status != PLATFORM_NVM_ERROR || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT3) { 00930 return false; 00931 } 00932 00933 // OK flush 00934 cfstore_stub.ret_val = ARM_DRIVER_OK; 00935 ret = platform_nvm_flush(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT3); 00936 if (ret != PLATFORM_NVM_OK) { 00937 return false; 00938 } 00939 // simulate flush 00940 cfstore_stub.cmd_code = CFSTORE_OPCODE_FLUSH; 00941 test_callback_data_clear(active_cb_data_ptr); 00942 test_cfstore_callback_trigger(); 00943 test_eventOS_timeout_trigger(); // should not have any effect as state is opening 00944 if (active_cb_data_ptr->status != PLATFORM_NVM_OK || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT3) { 00945 return false; 00946 } 00947 00948 test_callback_data_clear(active_cb_data_ptr); 00949 if (!test_nvm_finalize()) { 00950 return false; 00951 } 00952 00953 return true; 00954 } 00955 00956 bool test_cs_nvm_operation_in_progress() 00957 { 00958 platform_nvm_status ret; 00959 active_cb_data_ptr = &cb_data; 00960 test_callback_data_clear(active_cb_data_ptr); 00961 cfstore_stub.ret_val = ARM_DRIVER_OK; 00962 cfstore_stub.write_ret_val = ARM_DRIVER_OK; 00963 cfstore_stub.close_ret_val = ARM_DRIVER_OK; 00964 00965 00966 if (!test_nvm_initialize()) { 00967 return false; 00968 } 00969 00970 // Make key 00971 test_callback_data_clear(active_cb_data_ptr); 00972 cfstore_stub.ret_val = ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND; 00973 ret = platform_nvm_key_create(test_nvm_callback, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT3); 00974 if (ret != PLATFORM_NVM_OK) { 00975 return false; 00976 } 00977 00978 // make new requests while key creation is in progress 00979 ret = platform_nvm_init(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT1); 00980 if (ret != PLATFORM_NVM_ERROR) { 00981 return false; 00982 } 00983 ret = platform_nvm_key_create(test_nvm_callback, key_name, data_len, 0, TEST_NS_NVM_HELPER_CONTEXT3); 00984 if (ret != PLATFORM_NVM_ERROR) { 00985 return false; 00986 } 00987 ret = platform_nvm_key_delete(test_nvm_callback, key_name, TEST_NS_NVM_HELPER_CONTEXT1); 00988 if (ret != PLATFORM_NVM_ERROR) { 00989 return false; 00990 } 00991 ret = platform_nvm_read(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00992 if (ret != PLATFORM_NVM_ERROR) { 00993 return false; 00994 } 00995 ret = platform_nvm_write(test_nvm_callback, key_name, buf, &buf_len, TEST_NS_NVM_HELPER_CONTEXT1); 00996 if (ret != PLATFORM_NVM_ERROR) { 00997 return false; 00998 } 00999 ret = platform_nvm_flush(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT3); 01000 if (ret != PLATFORM_NVM_ERROR) { 01001 return false; 01002 } 01003 ret = platform_nvm_finalize(test_nvm_callback, TEST_NS_NVM_HELPER_CONTEXT2); 01004 if (ret != PLATFORM_NVM_ERROR) { 01005 return false; 01006 } 01007 01008 // continue with key creation 01009 cfstore_stub.cmd_code = CFSTORE_OPCODE_CREATE; 01010 test_callback_data_clear(active_cb_data_ptr); 01011 test_cfstore_callback_trigger(); 01012 test_eventOS_timeout_trigger(); 01013 if (active_cb_data_ptr->status != PLATFORM_NVM_KEY_NOT_FOUND || active_cb_data_ptr->ctx != TEST_NS_NVM_HELPER_CONTEXT3) { 01014 return false; 01015 } 01016 01017 // test2, check that other cmd_codes are not handled/affecting 01018 cfstore_stub.cmd_code = CFSTORE_OPCODE_MAX; 01019 test_callback_data_clear(active_cb_data_ptr); 01020 test_cfstore_callback_trigger(); 01021 test_eventOS_timeout_trigger(); 01022 if (test_callback_executed(active_cb_data_ptr)) { 01023 return false; 01024 } 01025 01026 if (!test_nvm_finalize()) { 01027 return false; 01028 } 01029 01030 return true; 01031 } 01032
Generated on Tue Jul 12 2022 14:24:41 by
