/* @brief test utility function to delete all of the KVs in the cfstore * @note this function expects cfstore to have been initialised with * a call to ARM_CFSTORE_DRIVER::Initialize() */ int32_t cfstore_test_delete_all(void) { const char* key_name_query = "*"; char key_name[CFSTORE_KEY_NAME_MAX_LENGTH+1]; uint8_t len = CFSTORE_KEY_NAME_MAX_LENGTH+1; int32_t ret = ARM_DRIVER_ERROR; ARM_CFSTORE_DRIVER* drv = &cfstore_driver; ARM_CFSTORE_HANDLE_INIT(next); ARM_CFSTORE_HANDLE_INIT(prev); CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__); while((ret = drv->Find(key_name_query, prev, next)) == ARM_DRIVER_OK) { len = CFSTORE_KEY_NAME_MAX_LENGTH+1; drv->GetKeyName(next, key_name, &len); CFSTORE_TP(CFSTORE_TP_DELETE, "%s:deleting key_name=%s, len=%d\r\n", __func__, key_name, (int) len); ret = drv->Delete(next); if(ret < ARM_DRIVER_OK){ CFSTORE_ERRLOG("%s:Error: failed to delete key_name=%s, len=%d\r\n", __func__, key_name, (int) len); return ret; } CFSTORE_HANDLE_SWAP(prev, next); } if(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND) { /* as expected, no more keys have been found by the Find()*/ ret = ARM_DRIVER_OK; } CFSTORE_FENTRYLOG("%s:exiting (ret=%d).\r\n", __func__, (int) ret); return ret; }
int32_t cfstore_flush3_delete_file(const char *fileDir, size_t maxFilePathSize, const char *fileName) { int32_t cfsStatus; int32_t status = ARM_DRIVER_OK; ARM_CFSTORE_DRIVER *cfstoreDriver = &cfstore_driver; ARM_CFSTORE_FMODE flags; ARM_CFSTORE_HANDLE_INIT(hkey); CFSTORE_DBGLOG("%s:IN. File name %s\n", __func__, fileName); CFSTORE_UNUSED_PARAM(fileDir); CFSTORE_UNUSED_PARAM(maxFilePathSize); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Uninitialize Cfstore\n", __func__); TEST_ASSERT_MESSAGE(cfstoreDriver != NULL, cfstore_flush3_utest_msg_g); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Invalid file name\n", __func__); TEST_ASSERT_MESSAGE(fileName != NULL, cfstore_flush3_utest_msg_g); memset(&flags, 0, sizeof(flags)); flags.write = true; cfsStatus = cfstoreDriver->Open(fileName, flags, hkey); if (cfsStatus == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND) { /* printf added: modification to original code supplied by Jenia Kogan. */ CFSTORE_DBGLOG("%s: cfsStatus == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND. returning success\n", __func__); return ARM_DRIVER_OK; } if(cfsStatus < ARM_DRIVER_OK){ CFSTORE_DBGLOG("Open failed (err %ld)\n", cfsStatus); return ARM_DRIVER_ERROR; } cfsStatus = cfstoreDriver->Delete(hkey); if(cfsStatus < ARM_DRIVER_OK){ CFSTORE_DBGLOG("Failed deleting (%s) failed (err %ld)\n", fileName, cfsStatus); status = ARM_DRIVER_ERROR; goto out; } out: cfsStatus = cfstoreDriver->Close(hkey); if(cfsStatus < ARM_DRIVER_OK){ CFSTORE_DBGLOG("Close failed (err %ld)\n", cfsStatus); return ARM_DRIVER_ERROR; } /* Flash-to-flash only on success */ if (status == ARM_DRIVER_OK) { cfsStatus = cfstoreDriver->Flush(); if(cfsStatus < ARM_DRIVER_OK){ CFSTORE_DBGLOG("Flush to flash failed (err %ld)\n", cfsStatus); return ARM_DRIVER_ERROR; } } CFSTORE_DBGLOG("%s:OUT: status=%d\n", __func__, (int) status); return status; }
/** @brief * * This test case does the following: * - creates a KV. * - deletes the KV. * - checks that the deleted KV can no longer be found in the store. * * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. */ control_t cfstore_add_del_test_01_end(const size_t call_count) { bool bfound = false; int32_t ret = ARM_DRIVER_ERROR; ARM_CFSTORE_SIZE len = 0; ARM_CFSTORE_DRIVER* drv = &cfstore_driver; ARM_CFSTORE_KEYDESC kdesc; ARM_CFSTORE_HANDLE_INIT(hkey); ARM_CFSTORE_FMODE flags; CFSTORE_FENTRYLOG("%s:entered\n", __func__); (void) call_count; memset(&kdesc, 0, sizeof(kdesc)); memset(&flags, 0, sizeof(flags)); kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE; len = strlen(cfstore_add_del_test_07_data[0].value); ret = cfstore_test_create(cfstore_add_del_test_07_data[0].key_name, (char*) cfstore_add_del_test_07_data[0].value, &len, &kdesc); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create KV in store (ret=%d).\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); /* now delete KV*/ ret = drv->Open(cfstore_add_del_test_07_data[0].key_name, flags, hkey); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to Open() (ret=%d).\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); if(hkey != NULL){ ret = drv->Delete(hkey); drv->Close(hkey); hkey = NULL; } /* check that the KV has been deleted */ /* revert to CFSTORE_LOG if more trace required */ CFSTORE_DBGLOG("LOG: WARNING: About to look for non-existent key (key_name=%s) (which will generate internal trace reporting errors if debug trace enabled).\n", cfstore_add_del_test_07_data[0].key_name); ret = cfstore_test_kv_is_found(cfstore_add_del_test_07_data[0].key_name, &bfound); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error failed to delete a key (ret=%d).\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND, cfstore_add_del_utest_msg_g); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Test failed: found KV that was previously deleted (key_name=%s)\n", __func__, cfstore_add_del_test_07_data[0].key_name); TEST_ASSERT_MESSAGE(bfound == false, cfstore_add_del_utest_msg_g); ret = drv->Uninitialize(); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); return CaseNext; }
/* @brief test utility function to delete the cfstore key identified by key_name * @note this function expects cfstore to have been initialised with * a call to ARM_CFSTORE_DRIVER::Initialize() */ int32_t cfstore_test_delete(const char* key_name) { int32_t ret = ARM_DRIVER_ERROR; ARM_CFSTORE_DRIVER* drv = &cfstore_driver; ARM_CFSTORE_HANDLE_INIT(hkey); ARM_CFSTORE_FMODE flags; CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__); memset(&flags, 0, sizeof(flags)); ret = drv->Open(key_name, flags, hkey); if(ret < ARM_DRIVER_OK){ return ret; } if(hkey != NULL){ ret = drv->Delete(hkey); drv->Close(hkey); } return ret; }
/* @brief simple flush test */ static control_t cfstore_flush3_test_02(const size_t call_count) { int32_t cfsStatus = ARM_DRIVER_ERROR; ARM_CFSTORE_KEYDESC kdesc; ARM_CFSTORE_FMODE flags; ARM_CFSTORE_SIZE len = strlen("key0"); ARM_CFSTORE_HANDLE_INIT(hkey); ARM_CFSTORE_DRIVER* drv = &cfstore_driver; (void) call_count; memset(&kdesc, 0, sizeof(kdesc)); memset(&flags, 0, sizeof(flags)); CFSTORE_DBGLOG("%s:Initialize()\n", __func__); cfsStatus = drv->Initialize(NULL, NULL); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); CFSTORE_DBGLOG("%s:Create()\n", __func__); cfsStatus = drv->Create("key0", len, &kdesc, hkey); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); len = strlen("some-value"); CFSTORE_DBGLOG("%s:Write()\n", __func__); cfsStatus = drv->Write(hkey, "some-value", &len); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); CFSTORE_DBGLOG("%s:Close()\n", __func__); cfsStatus = drv->Close(hkey); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); CFSTORE_DBGLOG("%s:Flush()\n", __func__); cfsStatus = drv->Flush(); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); CFSTORE_DBGLOG("%s:Open()\n", __func__); cfsStatus = drv->Open("key0", flags, hkey); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); CFSTORE_DBGLOG("%s:Delete()\n", __func__); cfsStatus = drv->Delete(hkey); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); CFSTORE_DBGLOG("%s:Close()\n", __func__); cfsStatus = drv->Close(hkey); /////// <--- cfsStatus = ARM_CFSTORE_DRIVER_ERROR_PREEXISTING_KEY CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); CFSTORE_DBGLOG("%s:got status = %d\n", __func__, (int) cfsStatus); CFSTORE_DBGLOG("%s:Uninitialize()\n", __func__); cfsStatus = drv->Uninitialize(); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush3_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error:%d:cfsStatus=%d", __func__, __LINE__, (int) cfsStatus); TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_flush3_utest_msg_g); return CaseNext; }
/** @brief Delete an attribute after an internal realloc of the cfstore memory area * * This test case goes through the following steps: * 1. Creates attribute att_1 of size x, and write some data. This causes an internal * cfstore realloc to allocate heap memory for the attribute. * 2. Allocates some memory on the heap. Typically, this will be immediately after the * memory used by cfstore for the KV area. This means that if any cfstore reallocs are * made to increase size the memory area will have to move. * 3. Creates attribute att_2 of size y. This causes an internal cfstore realloc to move * the KV memory area to a new location. * 4. Delete att_1. This causes an internal realloc to shrink the area and tests that the * internal data structures that contain pointers to different parts of the KV area * are updated correctly. * 5. Allocates some memory on the heap. If the heap has been corrupted, this will likely trigger * a crash * * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. */ control_t cfstore_add_del_test_05_end(const size_t call_count) { char data[] = "some test data"; char filename[] = "file1"; char filename2[] = "file2"; int32_t ret = ARM_DRIVER_ERROR; void *test_buf1 = NULL; void *test_buf2 = NULL; ARM_CFSTORE_DRIVER *cfstoreDriver = &cfstore_driver; ARM_CFSTORE_KEYDESC keyDesc1; ARM_CFSTORE_HANDLE_INIT(hkey1); ARM_CFSTORE_KEYDESC keyDesc2; ARM_CFSTORE_HANDLE_INIT(hkey2); ARM_CFSTORE_SIZE count = sizeof(data); CFSTORE_FENTRYLOG("%s:entered\n", __func__); (void) call_count; /* step 1 */ memset(&keyDesc1, 0, sizeof(keyDesc1)); keyDesc1.drl = ARM_RETENTION_NVM; keyDesc1.flags.read = true; keyDesc1.flags.write = true; ret = cfstoreDriver->Create(filename, 1024, &keyDesc1, hkey1); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create attribute 1 (ret=%d)\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); /* Write data to file */ ret = cfstoreDriver->Write(hkey1, (const char *)data, &count); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write to attribute 1 (ret=%d)\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); /* step 2 */ test_buf1 = malloc(CFSTORE_ADD_DEL_MALLOC_SIZE); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to allocate memory (test_buf1=%p)\n", __func__, test_buf1); TEST_ASSERT_MESSAGE(test_buf1 != NULL, cfstore_add_del_utest_msg_g); /* step 3 */ memset(&keyDesc2, 0, sizeof(keyDesc2)); keyDesc2.drl = ARM_RETENTION_NVM; keyDesc2.flags.read = true; keyDesc2.flags.write = true; ret = cfstoreDriver->Create(filename2, 1024, &keyDesc2, hkey2); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create attribute 2 (ret=%d)\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); /* Write data to file */ count = sizeof(data); ret = cfstoreDriver->Write(hkey2, (const char *)data, &count); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write to attribute 2 (ret=%d)\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); /* step 4 */ ret = cfstoreDriver->Delete(hkey1); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to delete to attribute 1 (ret=%d)\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); ret = cfstoreDriver->Close(hkey1); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close to attribute 1 (ret=%d)\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); /* step 5 */ test_buf2 = malloc(CFSTORE_ADD_DEL_MALLOC_SIZE); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to allocate memory (test_buf2=%p)\n", __func__, test_buf2); TEST_ASSERT_MESSAGE(test_buf2 != NULL, cfstore_add_del_utest_msg_g); /* clean up */ ret = cfstoreDriver->Close(hkey2); CFSTORE_TEST_UTEST_MESSAGE(cfstore_add_del_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close to attribute 2 (ret=%d)\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_add_del_utest_msg_g); free(test_buf2); free(test_buf1); return CaseNext; }