Fix behavior of getter 72/261872/2
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 28 Jul 2021 05:22:54 +0000 (14:22 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 28 Jul 2021 05:34:22 +0000 (14:34 +0900)
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 <h.jhun@samsung.com>
preference/preference-internal.cc
preference/stub.cc
unittests/preference_unittest.cc

index 23633317d2bf5c23d503abbb472a355816bde13d..88040230a87e6c5c51823d90fa3144f907188dbf 100644 (file)
@@ -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<preference_type_e>(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<int>(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<preference_type_e>(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<int>(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<preference_type_e>(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<int>(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<preference_type_e>(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<int>(type), key.c_str());
+    set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+  }
 
   return d->GetBoolean();
 }
index 39d71b72a4643425e12fb1bf6a21e397be2c332d..5dd96b96033f3b4e9f1273044ff5a6db3080bf4c 100644 (file)
@@ -22,6 +22,7 @@
 #include <thread>
 
 #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) {
index 5026c2565da9c2b07eb51f086f47e2d31a94fa62..d3188be5aa31571e0232902328eddf632edd747f 100644 (file)
@@ -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) {