From 28e282d06ad9c2aa3a6d6100d136e2f933949dc7 Mon Sep 17 00:00:00 2001 From: Jaeyun Jung Date: Tue, 2 May 2023 13:32:56 +0900 Subject: [PATCH] [Service] handle invalid version Raise exception when given version number is 0. Signed-off-by: Jaeyun Jung --- daemon/service-db.cc | 6 ++++++ tests/daemon/unittest_service_db.cc | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/daemon/service-db.cc b/daemon/service-db.cc index 72b4665..720b027 100644 --- a/daemon/service-db.cc +++ b/daemon/service-db.cc @@ -445,6 +445,9 @@ MLServiceDB::update_model_description ( if (name.empty () || description.empty ()) throw std::invalid_argument ("Invalid name or description parameter!"); + if (version == 0U) + throw std::invalid_argument ("Invalid version number!"); + std::string key_with_prefix = DB_KEY_PREFIX; key_with_prefix += name; @@ -491,6 +494,9 @@ MLServiceDB::activate_model (const std::string name, const guint version) if (name.empty ()) throw std::invalid_argument ("Invalid name parameter!"); + if (version == 0U) + throw std::invalid_argument ("Invalid version number!"); + std::string key_with_prefix = DB_KEY_PREFIX; key_with_prefix += name; diff --git a/tests/daemon/unittest_service_db.cc b/tests/daemon/unittest_service_db.cc index 9e6c11f..44772bc 100644 --- a/tests/daemon/unittest_service_db.cc +++ b/tests/daemon/unittest_service_db.cc @@ -148,7 +148,7 @@ TEST (serviceDB, get_model_n) } /** - * @brief Negative test for update_model_description. Empty name or description. + * @brief Negative test for update_model_description. Invalid param case (empty name or description, invalid version). */ TEST (serviceDB, update_model_description_n) { @@ -157,7 +157,7 @@ TEST (serviceDB, update_model_description_n) db.connectDB (); try { - db.update_model_description ("", 0, "description"); + db.update_model_description ("", 1, "description"); } catch (const std::exception &e) { g_critical ("Got Exception: %s", e.what ()); gotException = 1; @@ -166,7 +166,16 @@ TEST (serviceDB, update_model_description_n) gotException = 0; try { - db.update_model_description ("test", 0, ""); + db.update_model_description ("test", 1, ""); + } catch (const std::exception &e) { + g_critical ("Got Exception: %s", e.what ()); + gotException = 1; + } + EXPECT_EQ (gotException, 1); + + gotException = 0; + try { + db.update_model_description ("test", 0, "description"); } catch (const std::exception &e) { g_critical ("Got Exception: %s", e.what ()); gotException = 1; @@ -176,7 +185,7 @@ TEST (serviceDB, update_model_description_n) } /** - * @brief Negative test for activate_model. Empty name + * @brief Negative test for activate_model. Invalid param case (empty name or invalid version). */ TEST (serviceDB, activate_model_n) { @@ -185,7 +194,16 @@ TEST (serviceDB, activate_model_n) db.connectDB (); try { - db.activate_model ("", 0); + db.activate_model ("", 1); + } catch (const std::exception &e) { + g_critical ("Got Exception: %s", e.what ()); + gotException = 1; + } + EXPECT_EQ (gotException, 1); + + gotException = 0; + try { + db.activate_model ("test", 0); } catch (const std::exception &e) { g_critical ("Got Exception: %s", e.what ()); gotException = 1; -- 2.7.4