Test parameter validity when adding a job 38/138838/5
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 14 Jul 2017 04:26:28 +0000 (13:26 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 14 Jul 2017 10:13:24 +0000 (10:13 +0000)
Change-Id: Id0fd45e091dc8342b8b6bbd6326d1f1dfee274c6
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/JobManager.cpp
src/shared/Conf.h [new file with mode: 0644]
src/shared/JobInfo.cpp
src/shared/JobInfo.h

index 416747949aeb9a487337e835833339189e643923..aca4e9eaa29c401de6380d30d4f6a5eb035be932 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <deque>
 #include <ServerUtil.h>
+#include <Conf.h>
 #include <JobAction.h>
 #include "SchedTimer.h"
 #include "JobRunner.h"
@@ -260,14 +261,27 @@ bool JobManager::__isPermitted(IClient* client, const std::string& uri)
 
 void JobManager::__verifyPeriodicJob(PeriodicJobInfo* jobInfo, IClient* owner)
 {
-       //TODO: parameter check
+       IF_FAIL_THROW_TAG(!(jobInfo->isOneTime() && jobInfo->isPersistent()), E_PARAM,
+                       _W, "A one-time job cannot be persistent.");
+
+       IF_FAIL_THROW_TAG(jobInfo->getInterval() >= CTX_MIN_JOB_PERIOD, E_PARAM,
+                       _W, "The interval is too short.");
+
+       IF_FAIL_THROW_TAG(jobInfo->getInterval() <= CTX_MAX_JOB_PERIOD, E_PARAM,
+                       _W, "The interval is too long.");
+
+       IF_FAIL_THROW_TAG(jobInfo->getAnchor() >= 0, E_PARAM,
+                       _W, "The anchor should be positive.");
+
        __verifyRequirements(jobInfo, owner);
        __verifyAction(jobInfo, owner);
 }
 
 void JobManager::__verifyOnDemandJob(OnDemandJobInfo* jobInfo, IClient* owner)
 {
-       //TODO: parameter check
+       IF_FAIL_THROW_TAG(!(jobInfo->isOneTime() && jobInfo->isPersistent()), E_PARAM,
+                       _W, "A one-time job cannot be persistent.");
+
        __verifyTriggers(jobInfo, owner);
        __verifyRequirements(jobInfo, owner);
        __verifyAction(jobInfo, owner);
@@ -275,29 +289,34 @@ void JobManager::__verifyOnDemandJob(OnDemandJobInfo* jobInfo, IClient* owner)
 
 void JobManager::__verifyTriggers(OnDemandJobInfo* jobInfo, IClient* owner)
 {
+       IF_FAIL_THROW_TAG(!jobInfo->getTriggers().empty(), E_PARAM, _W, "No trigger");
+
        for (auto& trig : jobInfo->getTriggers()) {
                const std::string& uri = trig->getUri();
                _D("Check %s", uri.c_str());
 
-               if (!isSupported(JobContext::Type::TRIGGER, uri))
-                       throw static_cast<int>(E_SUPPORT);
+               IF_FAIL_THROW_TAG(isSupported(JobContext::Type::TRIGGER, uri), E_SUPPORT,
+                               _W, "'%s' is not supported.", uri.c_str());
 
-               if (!__isPermitted(owner, uri))
-                       throw static_cast<int>(E_ACCESS);
+               IF_FAIL_THROW_TAG(__isPermitted(owner, uri), E_ACCESS,
+                               _W, "'%s' is not allowed to use '%s'.", owner->getName().c_str(), uri.c_str());
        }
 }
 
 void JobManager::__verifyRequirements(JobInfo* jobInfo, IClient* owner)
 {
+       IF_FAIL_THROW_TAG(jobInfo->getRequirementTimeout() <= CTX_MAX_REQ_TIMEOUT, E_PARAM,
+                       _W, "The requirement timeout is too short.");
+
        for (auto& req : jobInfo->getRequirements()) {
                const std::string& uri = req->getUri();
                _D("Check %s", uri.c_str());
 
-               if (!isSupported(JobContext::Type::REQUIREMENT, uri))
-                       throw static_cast<int>(E_SUPPORT);
+               IF_FAIL_THROW_TAG(isSupported(JobContext::Type::REQUIREMENT, uri), E_SUPPORT,
+                               _W, "'%s' is not supported.", uri.c_str());
 
-               if (!__isPermitted(owner, uri))
-                       throw static_cast<int>(E_ACCESS);
+               IF_FAIL_THROW_TAG(__isPermitted(owner, uri), E_ACCESS,
+                               _W, "'%s' is not allowed to use '%s'.", owner->getName().c_str(), uri.c_str());
        }
 }
 
@@ -338,7 +357,8 @@ void JobManager::__verifyAction(JobInfo* jobInfo, IClient* owner)
        }();
        if (verified) return;
 
-       throw static_cast<int>(E_PARAM);
+       _W("Invalid action");
+       throw E_PARAM;
 }
 
 unsigned int JobManager::__addRunner(JobRunner* runner)
diff --git a/src/shared/Conf.h b/src/shared/Conf.h
new file mode 100644 (file)
index 0000000..52d195f
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_CONF_H__
+#define __CONTEXT_JOB_SCHEDULER_CONF_H__
+
+#define CTX_MIN_JOB_PERIOD             1               // minutes
+#define CTX_MAX_JOB_PERIOD             1440    // minutes
+#define CTX_MAX_REQ_TIMEOUT            10000   // milli-seconds
+
+#endif /* __CONTEXT_JOB_SCHEDULER_CONF_H__ */
index 1896a0026e7245365981dc3739da2ee5da8975f9..5eaf8f33ff0ca766d5e6495f2210ee09655b41b9 100644 (file)
@@ -290,6 +290,16 @@ JobInfo::Type PeriodicJobInfo::getType() const
        return JobInfo::Type::PERIODIC;
 }
 
+unsigned int PeriodicJobInfo::getInterval() const
+{
+       return __intervalMin;
+}
+
+time_t PeriodicJobInfo::getAnchor() const
+{
+       return __anchor;
+}
+
 void PeriodicJobInfo::__toJson(Json::Value& jsonRoot) const
 {
        jsonRoot[KEY_INTERVAL] = __intervalMin;
index dbe661b7cc9fa26a480aa09189fe6cbe891a614b..812e8ea4c27bf65290da9a9bc80d372a0137f961 100644 (file)
@@ -111,6 +111,9 @@ namespace ctx {
 
                JobInfo::Type getType() const;
 
+               unsigned int getInterval() const;
+               time_t getAnchor() const;
+
        private:
                void __toJson(Json::Value& jsonRoot) const;