From: Youngjae Shin Date: Thu, 26 Sep 2019 01:49:25 +0000 (+0900) Subject: add value validation checker X-Git-Tag: submit/tizen/20200319.043412~53 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dbc380b45d2d2c4d295cbbd6f2956f2bba5e2e66;p=platform%2Fcore%2Fsystem%2Fmodes.git add value validation checker --- diff --git a/example/mode/tizen_ex1_mode.xml b/example/mode/tizen_ex1_mode.xml index 43ee7c1..9887d3a 100644 --- a/example/mode/tizen_ex1_mode.xml +++ b/example/mode/tizen_ex1_mode.xml @@ -1,13 +1,12 @@ - on - on - com.vpn.usa123 - https://pbskids.org/ - Modes-JBL - beatles-yesterday.mp3 - SETTING_PSMODE_NORMAL + on + com.vpn.usa123 + https://pbskids.org/ + Modes-JBL + beatles-yesterday.mp3 + PRINT_TWO diff --git a/example/mode/tizen_ex2_mode.xml b/example/mode/tizen_ex2_mode.xml index 5f2277b..bf4d721 100644 --- a/example/mode/tizen_ex2_mode.xml +++ b/example/mode/tizen_ex2_mode.xml @@ -1,7 +1,7 @@ - SETTING_PSMODE_WEARABLE - off + PRINT_FOUR + off diff --git a/example/mode/tizen_invalid1_mode.xml b/example/mode/tizen_invalid1_mode.xml new file mode 100644 index 0000000..c34416b --- /dev/null +++ b/example/mode/tizen_invalid1_mode.xml @@ -0,0 +1,6 @@ + + + + 123 + + diff --git a/example/mode/tizen_invalid2_mode.xml b/example/mode/tizen_invalid2_mode.xml new file mode 100644 index 0000000..374eef1 --- /dev/null +++ b/example/mode/tizen_invalid2_mode.xml @@ -0,0 +1,6 @@ + + + + PRINT_TREE + + diff --git a/example/rule/tizen_ex_rule.xml b/example/rule/tizen_ex_rule.xml index dfebdb7..c99b940 100644 --- a/example/rule/tizen_ex_rule.xml +++ b/example/rule/tizen_ex_rule.xml @@ -1,30 +1,49 @@ - - sensord.exactness + + test.exactness Sensor Accuracy System - - sensord.changeAccuracy + + test.changeAccuracy Sensor Exactness System - + browser Web Framework - + display.autoLight It makes the screen color warmer. System + + Launch App + App Framework + + + bluetooth Audio Connect + Network & Connectivity + + + Audio/Video player + Multimedia + - It prints integerv value + 0 + 1 + 2 + 3 + 4 + It prints integer value System + 1 + 0 It prints boolean value System diff --git a/example/rule/tizen_ex_rule_fail.xml b/example/rule/tizen_ex_rule_fail.xml deleted file mode 100644 index 9d64a56..0000000 --- a/example/rule/tizen_ex_rule_fail.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - sensord.exactness - Sensor Accuracy - System - - - sensord.changeAccuracy - Sensor Exactness - System - - - browser - Web Framework - - - display.autoLight - It makes the screen color warmer. - System - - - It prints integerv value - System - - - It prints boolean value - System - - - It prints new boolean value - System - - - diff --git a/example/rule/tizen_fail1_rule.xml b/example/rule/tizen_fail1_rule.xml new file mode 100644 index 0000000..b3d3621 --- /dev/null +++ b/example/rule/tizen_fail1_rule.xml @@ -0,0 +1,36 @@ + + + + + sensord.exactness + Sensor Accuracy + System + + + sensord.changeAccuracy + Sensor Exactness + System + + + browser + Web Framework + + + display.autoLight + It makes the screen color warmer. + System + + + It prints integer value + System + + + It prints boolean value + System + + + It is a duplicated Rule, It Must be unique + System + + + diff --git a/example/rule/tizen_fail2_rule.xml b/example/rule/tizen_fail2_rule.xml new file mode 100644 index 0000000..3c5b401 --- /dev/null +++ b/example/rule/tizen_fail2_rule.xml @@ -0,0 +1,37 @@ + + + + + sensord.exactness + Sensor Accuracy + System + + + sensord.changeAccuracy + Sensor Exactness + System + + + browser + Web Framework + + + display.autoLight + It makes the screen color warmer. + System + + + 0 + 1 + 2 + 3 + 4th + It prints integer value + System + + + It prints boolean value + System + + + diff --git a/supervisor/Action.h b/supervisor/Action.h index 2817a62..ac8c184 100644 --- a/supervisor/Action.h +++ b/supervisor/Action.h @@ -45,7 +45,7 @@ public: void setIsChanged(); bool getIsChanged(); void setPlugin(Plugin *pi); - virtual void setValue(const std::string &val) = 0; + virtual int setValue(const std::string &val) = 0; virtual std::string getStringOfValue() = 0; virtual int apply() = 0; virtual int applyOneShot() = 0; diff --git a/supervisor/ActionRule.h b/supervisor/ActionRule.h index ee6762c..191315d 100644 --- a/supervisor/ActionRule.h +++ b/supervisor/ActionRule.h @@ -29,7 +29,7 @@ public: std::string getPlugin(); void setPlugin(const std::string &pi); virtual Action* makeAction() = 0; - virtual void addAlias(const std::string &alias, const std::string &value) = 0; + virtual int addAlias(const std::string &alias, const std::string &value) = 0; protected: std::string ruleName; private: diff --git a/supervisor/Mode.h b/supervisor/Mode.h index 104df15..7d90974 100644 --- a/supervisor/Mode.h +++ b/supervisor/Mode.h @@ -59,7 +59,7 @@ private: std::list> actionList; std::list> undoList; #ifdef MDS_TEST - friend class TestParser; + friend class ParserTest; #endif }; diff --git a/supervisor/ModeXMLParser.cpp b/supervisor/ModeXMLParser.cpp index 9890c32..f16966a 100644 --- a/supervisor/ModeXMLParser.cpp +++ b/supervisor/ModeXMLParser.cpp @@ -168,7 +168,7 @@ void ModeXMLParser::parseAction(xmlNodePtr node) mode.addAction(action); } -Action *ModeXMLParser::parseActionInfo(xmlNodePtr node) +Action* ModeXMLParser::parseActionInfo(xmlNodePtr node) { char *ruleProp = (char*)xmlGetProp(node, ModeTag::RULE); if (ruleProp == NULL) { @@ -192,13 +192,17 @@ Action *ModeXMLParser::parseActionInfo(xmlNodePtr node) parseActionAttr(node, action); char *nodeContent = (char*)xmlNodeGetContent(node); - if (nodeContent == NULL) { + if (nodeContent == NULL) ERR("Node Content is null!"); - } - action->setValue(nodeContent ? nodeContent : ""); + int ret = action->setValue(nodeContent ? nodeContent : ""); xmlFree(nodeContent); + if (MODES_ERROR_NONE != ret) { + ERR("Action(%s) setValue() Fail(%d)", action->getRuleName().c_str(), ret); + throw ModesEx(ModesEx::PARSER_ERROR, "Invalid Action Value"); + } + action->printInfo(); return action; } diff --git a/supervisor/RuleManager.cpp b/supervisor/RuleManager.cpp index a0dd886..397de55 100644 --- a/supervisor/RuleManager.cpp +++ b/supervisor/RuleManager.cpp @@ -59,9 +59,7 @@ void RuleManager::makeRuleMap() parseActionRule(fileFullPath); DBG("Action Rule(size=%d) Load", ruleMap.size()); } catch (ModesEx &e) { - ERR("parseActionRule(%s) Fail(%s)", fileFullPath.c_str(), e.what()); - closedir(dir); - throw; + ERR("parseActionRule(%s) Fail(%s) : It will be ignored", fileFullPath.c_str(), e.what()); } } closedir(dir); @@ -92,7 +90,14 @@ void RuleManager::parseElement(xmlNodePtr cur, ActionRule *actionRule) return; } - actionRule->addAlias(alias, value); + int ret = actionRule->addAlias(alias, value); + if (MODES_ERROR_NONE != ret) { + ERR("Rule(%s) %s(%s) Fail(%d)", actionRule->getName().c_str(), alias, value, ret); + xmlFree(value); + xmlFree(alias); + throw ModesEx(ModesEx::NO_DATA, "Invalid Alias Value"); + } + xmlFree(value); xmlFree(alias); } else if (MDS_EQUAL == xmlStrcmp(cur->name, RULE_TAGS[TagElemConflict])) { @@ -155,7 +160,7 @@ ActionRule* RuleManager::makeRule(xmlNodePtr node) plugin = getProp(node, TagAttPlugin); } catch (ModesEx &e) { ERR("getProp() Fail(%s)", e.what()); - return nullptr; + throw; } DBG("Action(%s, %s) Added", name.c_str(), type.c_str()); diff --git a/supervisor/RuleManager.h b/supervisor/RuleManager.h index 4b0ca21..7b82a7d 100644 --- a/supervisor/RuleManager.h +++ b/supervisor/RuleManager.h @@ -58,6 +58,7 @@ private: #ifdef MDS_TEST friend class RuleManagerTest; + friend class ParserTest; #endif }; diff --git a/supervisor/TAction.h b/supervisor/TAction.h index c0a57a1..bcb1d39 100644 --- a/supervisor/TAction.h +++ b/supervisor/TAction.h @@ -20,6 +20,7 @@ #include #include "mdss.h" #include "Action.h" +#include "ValueChecker.h" MODES_NAMESPACE_BEGIN @@ -52,15 +53,22 @@ public: return val; } - void setValue(const std::string &val) override + int setValue(const std::string &val) override { auto it = valueAliases.find(val); if (it != valueAliases.end()) { value = it->second; } else { - std::istringstream iss(val); - iss >> value; + ValueChecker checker; + if (checker.checkValue(T(), val)) { + std::istringstream iss(val); + iss >> value; + } else { + return MODES_ERROR_INVALID_TYPE; + } } + + return MODES_ERROR_NONE; } int apply() override @@ -89,7 +97,7 @@ public: int ret = plugin->set(ruleName.substr(pos + 1), value, &tmpAction); if (MODES_ERROR_NONE != ret) { ERR("plugin(%s) set Fail(%d)", plugin->getName().c_str(), ret); - return ret; + return ret; } piAction = tmpAction; diff --git a/supervisor/TActionRule.h b/supervisor/TActionRule.h index 96081ac..920ee35 100644 --- a/supervisor/TActionRule.h +++ b/supervisor/TActionRule.h @@ -41,12 +41,20 @@ public: return action; } - void addAlias(const std::string &alias, const std::string &value) override + int addAlias(const std::string &alias, const std::string &value) override { + ValueChecker checker; + if (false == checker.checkValue(T(), value)) { + ERR("checkValue() Fail"); + return MODES_ERROR_INVALID_TYPE; + } + T realVal; std::istringstream iss(value); iss >> realVal; valueAliasList.insert(std::pair(alias, realVal)); + + return MODES_ERROR_NONE; } private: std::map valueAliasList; diff --git a/supervisor/ValueChecker.cpp b/supervisor/ValueChecker.cpp new file mode 100644 index 0000000..c9d9042 --- /dev/null +++ b/supervisor/ValueChecker.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ValueChecker.h" + +#include "mdss.h" + +MODES_NAMESPACE_USE; + +bool ValueChecker::checkValue(int, const std::string &val) +{ + for (auto it = val.begin(); it != val.end(); ++it) { + if (*it < '0' || '9' < *it) + return false; + } + + return true; +} + +bool ValueChecker::checkValue(double, const std::string &val) +{ + int dotNum = 0; + for (auto it = val.begin(); it != val.end(); ++it) { + if (*it < '0' || '9' < *it) { + if ('.' != *it) + return false; + else if (dotNum) + return false; + else + dotNum++; + } + } + + return true; +} + +bool ValueChecker::checkValue(bool, const std::string &val) +{ + if (MDS_EQUAL == val.compare("1")) + return true; + if (MDS_EQUAL == val.compare("0")) + return true; + + return false; +} + +bool ValueChecker::checkValue(std::string, const std::string &val) +{ + return true; +} diff --git a/supervisor/ValueChecker.h b/supervisor/ValueChecker.h new file mode 100644 index 0000000..9e69906 --- /dev/null +++ b/supervisor/ValueChecker.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include "mdss.h" + +MODES_NAMESPACE_BEGIN + +class ValueChecker { +public: + ValueChecker() = default; + ~ValueChecker() = default; + + bool checkValue(int, const std::string &val); + bool checkValue(double, const std::string &val); + bool checkValue(bool, const std::string &val); + bool checkValue(std::string, const std::string &val); +}; + +MODES_NAMESPACE_END diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 438aa9d..43d8ee5 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -28,6 +28,7 @@ FILE(GLOB GTEST_MODEMGR_SRCS ${SUPERVISOR_DIR}/Mode.cpp ${SUPERVISOR_DIR}/ModesEx.cpp ${SUPERVISOR_DIR}/PluginManager.cpp + ${SUPERVISOR_DIR}/ValueChecker.cpp modes_test_modemgr.cpp ) ADD_EXECUTABLE(${GTEST_MODEMGR} ${SRC} ${GTEST_MODEMGR_SRCS}) @@ -57,6 +58,7 @@ FILE(GLOB GTEST_PARSER_SRCS ${SUPERVISOR_DIR}/ModesEx.cpp ${SUPERVISOR_DIR}/Mode.cpp ${SUPERVISOR_DIR}/ModeTag.cpp + ${SUPERVISOR_DIR}/ValueChecker.cpp modes_test_parser.cpp ) ADD_EXECUTABLE(${GTEST_PARSER} ${SRC} ${GTEST_PARSER_SRCS}) @@ -90,6 +92,7 @@ FILE(GLOB GTEST_GENERATOR_SRCS ${SUPERVISOR_DIR}/PluginManager.cpp ${SUPERVISOR_DIR}/RuleManager.cpp ${SUPERVISOR_DIR}/ActionRule.cpp + ${SUPERVISOR_DIR}/ValueChecker.cpp "modes_test_generator.cpp" ) ADD_EXECUTABLE(${GTEST_GENERATOR} ${SRC} ${GTEST_GENERATOR_SRCS}) @@ -110,6 +113,7 @@ FILE(GLOB GTEST_CONFLICT_SRCS ${SUPERVISOR_DIR}/PluginManager.cpp ${SUPERVISOR_DIR}/RuleManager.cpp ${SUPERVISOR_DIR}/ActionRule.cpp + ${SUPERVISOR_DIR}/ValueChecker.cpp modes_test_conflict.cpp ) ADD_EXECUTABLE(${GTEST_CONFLICT} ${SRC} ${GTEST_CONFLICT_SRCS}) @@ -124,6 +128,7 @@ FILE(GLOB GTEST_RULE_SRCS ${SUPERVISOR_DIR}/ModesEx.cpp ${SUPERVISOR_DIR}/Mode.cpp ${SUPERVISOR_DIR}/Action.cpp + ${SUPERVISOR_DIR}/ValueChecker.cpp modes_test_rulemgr.cpp ) ADD_EXECUTABLE(${GTEST_RULEMGR} ${SRC} ${GTEST_RULE_SRCS}) diff --git a/unittest/modes_test_parser.cpp b/unittest/modes_test_parser.cpp index 951a46f..04c2216 100644 --- a/unittest/modes_test_parser.cpp +++ b/unittest/modes_test_parser.cpp @@ -17,22 +17,32 @@ #include #include #include "supervisor/Mode.h" -#include "supervisor/ModeXMLParser.h" +#include "supervisor/ModesEx.h" #include "supervisor/RuleManager.h" +#include "supervisor/ModeXMLParser.h" MODES_NAMESPACE_USE; using namespace std; MODES_NAMESPACE_BEGIN -class TestParser { +class ParserTest { public: - void parserTest(const string &fileName); + void initRuleManger(); list> getActionList(Mode& m); + + RuleManager ruleMgr; private: }; MODES_NAMESPACE_END -list> TestParser::getActionList(Mode& m) + +void ParserTest::initRuleManger() +{ + ruleMgr.setOptions(std::string(), "./" MODES_ACTIONRULE_DEFAULT_XSD_FILE); + ruleMgr.parseActionRule("./tizen_ex_rule.xml"); +} + +list> ParserTest::getActionList(Mode& m) { return m.actionList; } @@ -71,7 +81,7 @@ TEST(Mode, printAction) RuleManager ruleMgr; ModeXMLParser modeparser("tizen_ex1_mode.xml", ruleMgr, pluginManager); Mode mode = modeparser.getMode(); - TestParser parserT; + ParserTest parserT; list> actionList = parserT.getActionList(mode); EXPECT_FALSE(actionList.empty()); @@ -82,3 +92,16 @@ TEST(Mode, printAction) //cout << "\t\t- Content : " << (*it)->getValue() << endl; } } + +TEST(Mode, invalidActionValue) +{ + ParserTest parserT; + parserT.initRuleManger(); + + PluginManager piMgr; + ModeXMLParser modeparser1("tizen_invalid1_mode.xml", parserT.ruleMgr, piMgr); + EXPECT_THROW(modeparser1.getMode(), ModesEx); + + ModeXMLParser modeparser2("tizen_invalid2_mode.xml", parserT.ruleMgr, piMgr); + EXPECT_THROW(modeparser2.getMode(), ModesEx); +} diff --git a/unittest/modes_test_rulemgr.cpp b/unittest/modes_test_rulemgr.cpp index 7e1967b..18230f9 100644 --- a/unittest/modes_test_rulemgr.cpp +++ b/unittest/modes_test_rulemgr.cpp @@ -44,7 +44,6 @@ RuleManagerTest::RuleManagerTest() rMgr.setOptions(config.actionRuleDir, config.actionRuleXsdFile); } - TEST(RuleManagerTest, makeRuleMapP) { RuleManagerTest testBroker; @@ -57,8 +56,14 @@ TEST(RuleManagerTest, parseActionRuleP) EXPECT_NO_THROW(testBroker.parseActionRule("./tizen_ex_rule.xml")); } -TEST(RuleManagerTest, parseActionRuleN) +TEST(RuleManagerTest, DuplicatedRule) +{ + RuleManagerTest testBroker; + EXPECT_THROW(testBroker.parseActionRule("./tizen_fail1_rule.xml"), ModesEx); +} + +TEST(RuleManagerTest, InvalidValue) { RuleManagerTest testBroker; - EXPECT_THROW(testBroker.parseActionRule("./tizen_ex_rule_fail.xml"), ModesEx); + EXPECT_THROW(testBroker.parseActionRule("./tizen_fail2_rule.xml"), ModesEx); }