#include "include/param_checker.hh"
-#include <iostream>
+#include <boost/exception/diagnostic_information.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/program_options.hpp>
+#include <boost/system/error_code.hpp>
+#include "include/logging.hh"
#include "include/request_type.hh"
#include "include/rsc_path_info.hh"
+#include <pkgmgr-info.h>
+
+#include <string>
+#include <iostream>
+
+namespace bs = boost::system;
+namespace bpo = boost::program_options;
+
namespace rsc_handler {
ParamChecker::ParamChecker(int argc, char* argv[]) {
- std::cout << "ParamChecker::ParamChecker" << std::endl;
+ bpo::options_description options;
+
+ options.add_options()
+ ("uid,u", bpo::value<int>()->default_value(0), "user id")
+ ("path,p", bpo::value<std::vector<std::string>>()->multitoken(),
+ "source-destination path")
+ ("remove,r", bpo::value<std::string>(), "remove shared resource")
+ ("delete,d", bpo::value<std::string>(),
+ "delete shared resource for package")
+ ("copy,c", bpo::value<std::string>(), "copy resource")
+ ("help,h", "Show this message");
+
+ bpo::parsed_options parsed_options = bpo::command_line_parser(argc, argv)
+ .options(options)
+ .run();
+
+ for (const bpo::option& o : parsed_options.options) {
+ if (o.string_key == "uid") {
+ uid_ = static_cast<uid_t>(std::stoi(o.value.front()));
+ } else if (o.string_key == "path") {
+ path_info_list_.emplace_back(o.value.front(), o.value.back());
+ } else if (o.string_key == "copy" ||
+ o.string_key == "delete" ||
+ o.string_key == "remove") {
+ pkgid_ = o.value.front();
+ SetRequestType(o.string_key);
+ } else if (o.string_key == "help") {
+ std::cout << options;
+ } else {
+ std::cout << "Invalid option : " << o.string_key << std::endl;
+ }
+ }
}
std::string ParamChecker::GetPkgID() const {
- std::cout << "ParamChecker::GetPkgID" << std::endl;
-
- return "test_text";
+ return pkgid_;
}
ReqType ParamChecker::GetRequestType() {
- std::cout << "ParamChecker::GetRequestType" << std::endl;
-
- return ReqType::REQ_TYPE_UNKNOWN;
+ return req_type_;
}
const std::list<RscPathInfo>& ParamChecker::GetPathList() const {
- std::cout << "ParamChecker::GetPathList()" << std::endl;
-
return path_info_list_;
}
bool ParamChecker::Validate() {
- std::cout << "ParamChecker::Validate" << std::endl;
+ if (uid_ == 0) {
+ LOG(ERROR) << "Invalid uid: " << uid_;
+ return false;
+ }
+
+ if (req_type_ == ReqType::REQ_TYPE_UNKNOWN) {
+ LOG(ERROR) << "Invalid request type";
+ return false;
+ }
+
+ if (!ValidatePkgID())
+ return false;
+
+ if (req_type_ != ReqType::REQ_TYPE_UNINSTALL) {
+ if (path_info_list_.size() == 0) {
+ LOG(ERROR) << "Path is not given";
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void ParamChecker::SetRequestType(std::string key) {
+ if (key == "copy")
+ req_type_ = ReqType::REQ_TYPE_NEW;
+ else if (key == "remove")
+ req_type_ = ReqType::REQ_TYPE_REMOVE;
+ else if (key == "delete")
+ req_type_ = ReqType::REQ_TYPE_UNINSTALL;
+}
+
+bool ParamChecker::ValidatePkgID() {
+ if (pkgid_.size() == 0) {
+ LOG(ERROR) << "pkgid is empty";
+ return false;
+ }
return true;
}
--- /dev/null
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#include <stdlib.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+
+#include <memory>
+
+#include "mock/os_mock.h"
+#include "mock/pkgmgr_info_mock.h"
+#include "mock/test_fixture.h"
+#include "include/param_checker.hh"
+#include "include/request_type.hh"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::InvokeArgument;
+using ::testing::SaveArg;
+
+using rsc_handler::ParamChecker;
+
+class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
+ public ::testing::NiceMock<OsMock> {};
+
+class ParamCheckerTest : public TestFixture {
+ public:
+ ParamCheckerTest() : TestFixture(std::make_unique<Mocks>()) {}
+ virtual ~ParamCheckerTest() {}
+
+ virtual void SetUp() {
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
+ .WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_destroy_pkginfo(_))
+ .WillRepeatedly(Return(0)) ;
+ }
+ virtual void TearDown() {}
+};
+
+TEST_F(ParamCheckerTest, InvalidUIDTest) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "0", "--copy",
+ "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, PkgIDNotGiven) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "5001",
+ "-p", "srcpath", "dstpath", "--copy", "", nullptr};
+ ParamChecker checker(8, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, CopyRsc) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "-p", "srcpath", "dstpath",
+ "--copy", "org.test.targetpkgid", nullptr};
+
+ ParamChecker checker(8, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), true);
+ EXPECT_EQ(checker.GetRequestType(), rsc_handler::ReqType::REQ_TYPE_NEW);
+ EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
+ EXPECT_EQ(checker.GetPathList().size(), 1);
+}
+
+TEST_F(ParamCheckerTest, RemoveRsc) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--remove",
+ "org.test.targetpkgid", "-p", "dstpath", nullptr};
+
+ ParamChecker checker(7, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), true);
+ EXPECT_EQ(checker.GetRequestType(), rsc_handler::ReqType::REQ_TYPE_REMOVE);
+ EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
+ EXPECT_EQ(checker.GetPathList().size(), 1);
+}
+
+TEST_F(ParamCheckerTest, DeleteRsc) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--delete",
+ "org.test.targetpkgid", nullptr};
+
+ ParamChecker checker(5, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), true);
+ EXPECT_EQ(checker.GetRequestType(), rsc_handler::ReqType::REQ_TYPE_UNINSTALL);
+ EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
+ EXPECT_EQ(checker.GetPathList().size(), 0);
+}
+
+TEST_F(ParamCheckerTest, EmptyPkgID) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--copy",
+ "", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8,(char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, UnknownReqType) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "-p",
+ "srcpath", "dstpath", nullptr};
+ ParamChecker checker(6, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, PathNotGiven) {
+ const char *argv[] = { "/bin/rsc-copy", "--uid", "5001",
+ "-c", "org.tizen.pathnotgiven", nullptr};
+ ParamChecker checker(5, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}