From 07c41923ef9b72f16d5950fbff8cc3983a10eb61 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 28 Jul 2021 14:22:54 +0900 Subject: [PATCH] Fix behavior of getter This patch is for backward compatibility. If the type of the keynode is not matched, the function returns PREFERENCE_ERROR_INVALID_PARAMETER error with the result. Change-Id: I1964c02198baccf97de3738bff5ece72d6a76f53 Signed-off-by: Hwankyu Jhun --- preference/preference-internal.cc | 47 ++++++++++++++++++++++++++++--- preference/stub.cc | 37 ++++++++++++++++++------ unittests/preference_unittest.cc | 26 +++++++++++++++++ 3 files changed, 98 insertions(+), 12 deletions(-) diff --git a/preference/preference-internal.cc b/preference/preference-internal.cc index 2363331..8804023 100644 --- a/preference/preference-internal.cc +++ b/preference/preference-internal.cc @@ -14,6 +14,9 @@ * limitations under the License. */ +#include "include/app_preference.h" +#include "preference/exception-internal.hh" +#include "preference/log-internal.hh" #include "preference/preference-internal.hh" namespace preference { @@ -51,7 +54,16 @@ int Preference::SetBoolean(const std::string& key, bool value) { int Preference::GetInt(const std::string& key) { auto d = backend_->Get(key); if (d == nullptr) - return -1; + THROW(PREFERENCE_ERROR_IO_ERROR); + + auto type = static_cast(d->GetType()); + if (type == PREFERENCE_TYPE_INT) { + set_last_result(PREFERENCE_ERROR_NONE); + } else { + _E("The type(%d) of keynode(%s) is not integer", + static_cast(type), key.c_str()); + set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER); + } return d->GetInt(); } @@ -59,7 +71,16 @@ int Preference::GetInt(const std::string& key) { double Preference::GetDouble(const std::string& key) { auto d = backend_->Get(key); if (d == nullptr) - return -1; + THROW(PREFERENCE_ERROR_IO_ERROR); + + auto type = static_cast(d->GetType()); + if (type == PREFERENCE_TYPE_DOUBLE) { + set_last_result(PREFERENCE_ERROR_NONE); + } else { + _E("The type(%d) of keynode(%s) is not double", + static_cast(type), key.c_str()); + set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER); + } return d->GetDouble(); } @@ -67,7 +88,16 @@ double Preference::GetDouble(const std::string& key) { std::string Preference::GetString(const std::string& key) { auto d = backend_->Get(key); if (d == nullptr) - return std::string(""); + THROW(PREFERENCE_ERROR_IO_ERROR); + + auto type = static_cast(d->GetType()); + if (type == PREFERENCE_TYPE_STRING) { + set_last_result(PREFERENCE_ERROR_NONE); + } else { + _E("The type(%d) of keynode(%s) is not string", + static_cast(type), key.c_str()); + set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER); + } return d->GetString(); } @@ -75,7 +105,16 @@ std::string Preference::GetString(const std::string& key) { bool Preference::GetBoolean(const std::string& key) { auto d = backend_->Get(key); if (d == nullptr) - return false; + THROW(PREFERENCE_ERROR_IO_ERROR); + + auto type = static_cast(d->GetType()); + if (type == PREFERENCE_TYPE_BOOLEAN) { + set_last_result(PREFERENCE_ERROR_NONE); + } else { + _E("The type(%d) of keynode(%s) is not boolean", + static_cast(type), key.c_str()); + set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER); + } return d->GetBoolean(); } diff --git a/preference/stub.cc b/preference/stub.cc index 39d71b7..5dd96b9 100644 --- a/preference/stub.cc +++ b/preference/stub.cc @@ -22,6 +22,7 @@ #include #include "include/app_preference.h" +#include "preference/exception-internal.hh" #include "preference/log-internal.hh" #include "preference/preference-internal.hh" @@ -180,9 +181,14 @@ extern "C" API int preference_get_int(const char* key, int* intval) { if (!pref.IsExisting(key)) return PREFERENCE_ERROR_NO_KEY; - *intval = pref.GetInt(key); + try { + *intval = pref.GetInt(key); + } catch (const preference::internal::Exception& e) { + return e.GetErrorCode(); + } + SECURE_LOGD("key(%s), value(%d)", key, *intval); - return PREFERENCE_ERROR_NONE; + return get_last_result(); } extern "C" API int preference_get_boolean(const char* key, bool* boolval) { @@ -194,9 +200,14 @@ extern "C" API int preference_get_boolean(const char* key, bool* boolval) { if (!pref.IsExisting(key)) return PREFERENCE_ERROR_NO_KEY; - *boolval = pref.GetBoolean(key); + try { + *boolval = pref.GetBoolean(key); + } catch (const preference::internal::Exception& e) { + return e.GetErrorCode(); + } + SECURE_LOGD("key(%s), value(%d)", key, *boolval); - return PREFERENCE_ERROR_NONE; + return get_last_result(); } extern "C" API int preference_get_double(const char* key, double* dblval) { @@ -208,9 +219,14 @@ extern "C" API int preference_get_double(const char* key, double* dblval) { if (!pref.IsExisting(key)) return PREFERENCE_ERROR_NO_KEY; - *dblval = pref.GetDouble(key); + try { + *dblval = pref.GetDouble(key); + } catch (const preference::internal::Exception& e) { + return e.GetErrorCode(); + } + SECURE_LOGD("key(%s), value(%lf)", key, *dblval); - return PREFERENCE_ERROR_NONE; + return get_last_result(); } extern "C" API int preference_get_string(const char* key, char** value) { @@ -222,9 +238,14 @@ extern "C" API int preference_get_string(const char* key, char** value) { if (!pref.IsExisting(key)) return PREFERENCE_ERROR_NO_KEY; - *value = strdup(pref.GetString(key).c_str()); + try { + *value = strdup(pref.GetString(key).c_str()); + } catch (const preference::internal::Exception& e) { + return e.GetErrorCode(); + } + SECURE_LOGD("key(%s), value(%s)", key, *value); - return PREFERENCE_ERROR_NONE; + return get_last_result(); } extern "C" API int preference_remove(const char* key) { diff --git a/unittests/preference_unittest.cc b/unittests/preference_unittest.cc index 5026c25..d3188be 100644 --- a/unittests/preference_unittest.cc +++ b/unittests/preference_unittest.cc @@ -73,6 +73,12 @@ TEST_F(PreferenceUnitTest, preference_get_int_P) { TEST_F(PreferenceUnitTest, preference_get_int_N) { int ret = preference_get_int(nullptr, nullptr); ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); + + ret = preference_set_string("key:string", "value"); + ASSERT_EQ(ret, PREFERENCE_ERROR_NONE); + int value; + ret = preference_get_int("key:string", &value); + ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); } TEST_F(PreferenceUnitTest, preference_set_double_P) { @@ -98,6 +104,12 @@ TEST_F(PreferenceUnitTest, preference_get_double_P) { TEST_F(PreferenceUnitTest, preference_get_double_N) { int ret = preference_get_double(nullptr, nullptr); ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); + + ret = preference_set_int("key:int", 100); + ASSERT_EQ(ret, PREFERENCE_ERROR_NONE); + double value; + ret = preference_get_double("key:int", &value); + ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); } TEST_F(PreferenceUnitTest, preference_set_string_P) { @@ -125,6 +137,13 @@ TEST_F(PreferenceUnitTest, preference_get_string_P) { TEST_F(PreferenceUnitTest, preference_get_string_N) { int ret = preference_get_string(nullptr, nullptr); ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); + + ret = preference_set_int("key:int", 101); + ASSERT_EQ(ret, PREFERENCE_ERROR_NONE); + char* value = nullptr; + ret = preference_get_string("key:int", &value); + free(value); + ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); } TEST_F(PreferenceUnitTest, preference_set_boolean_P) { @@ -150,6 +169,13 @@ TEST_F(PreferenceUnitTest, preference_get_boolean_P) { TEST_F(PreferenceUnitTest, preference_get_boolean_N) { int ret = preference_get_boolean(nullptr, nullptr); ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); + + ret = preference_set_int("key:int", 102); + ASSERT_EQ(ret, PREFERENCE_ERROR_NONE); + bool value; + ret = preference_get_boolean("key:int", &value); + ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER); + } TEST_F(PreferenceUnitTest, preference_remove_P) { -- 2.34.1