From: Aleksander Zdyb Date: Sat, 20 Dec 2014 11:49:27 +0000 (+0100) Subject: Introduce CommandsDispatcher X-Git-Tag: accepted/tizen/common/20150119.084431~28 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e3bd02b695ee50b6da7fc0a21c4fa4bca3aa5787;p=platform%2Fcore%2Fsecurity%2Fcynara.git Introduce CommandsDispatcher Change-Id: I220422f667c32c673e0b55b04d9faac39d840037 --- diff --git a/src/cyad/AdminLibraryInitializationFailedException.h b/src/cyad/AdminLibraryInitializationFailedException.h new file mode 100644 index 0000000..570660f --- /dev/null +++ b/src/cyad/AdminLibraryInitializationFailedException.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/AdminLibraryInitializationFailedException.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Exception thrown when cynara_admin_initialize() fails + */ + +#ifndef SRC_CYAD_ADMINLIBRARYINITIALIZATIONFAILEDEXCEPTION_H_ +#define SRC_CYAD_ADMINLIBRARYINITIALIZATIONFAILEDEXCEPTION_H_ + +#include + +#include + +namespace Cynara { + +class AdminLibraryInitializationFailedException: public Exception { +public: + AdminLibraryInitializationFailedException(int errorCode) : m_errorCode(errorCode) { + m_message = "Initialization of cynara-admin failed: [" + std::to_string(m_errorCode) + "]"; + } + virtual ~AdminLibraryInitializationFailedException() {}; + + virtual const std::string &message(void) const { + return m_message; + } + + int errorCode(void) const { + return m_errorCode; + } + +private: + int m_errorCode; + std::string m_message; +}; + +} /* namespace Cynara */ + +#endif /* SRC_CYAD_ADMINLIBRARYINITIALIZATIONFAILEDEXCEPTION_H_ */ diff --git a/src/cyad/CMakeLists.txt b/src/cyad/CMakeLists.txt index 788e8f2..1ce4d1e 100644 --- a/src/cyad/CMakeLists.txt +++ b/src/cyad/CMakeLists.txt @@ -22,6 +22,8 @@ SET(CYAD_SOURCES ${CYAD_PATH}/AdminApiWrapper.cpp ${CYAD_PATH}/AdminPolicyParser.cpp ${CYAD_PATH}/CynaraAdminPolicies.cpp + ${CYAD_PATH}/CommandlineParser/CyadCommand.cpp + ${CYAD_PATH}/CommandsDispatcher.cpp ${CYAD_PATH}/DispatcherIO.cpp ${CYAD_PATH}/main.cpp ) diff --git a/src/cyad/CommandlineParser/CyadCommand.cpp b/src/cyad/CommandlineParser/CyadCommand.cpp new file mode 100644 index 0000000..6723e7e --- /dev/null +++ b/src/cyad/CommandlineParser/CyadCommand.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/CommandlineParser/CyadCommand.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief A representation of Cyad command + */ + +#include + +#include "CyadCommand.h" + +namespace Cynara { + +int CyadCommand::run(CommandsDispatcher &dispatcher) { + return dispatcher.execute(*this); +} + +int ErrorCyadCommand::run(CommandsDispatcher &dispatcher) { + return dispatcher.execute(*this); +} + +int HelpCyadCommand::run(CommandsDispatcher &dispatcher) { + return dispatcher.execute(*this); +} + +} /* namespace Cynara */ diff --git a/src/cyad/CommandlineParser/CyadCommand.h b/src/cyad/CommandlineParser/CyadCommand.h new file mode 100644 index 0000000..2a233d7 --- /dev/null +++ b/src/cyad/CommandlineParser/CyadCommand.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/CommandlineParser/CyadCommand.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief A representation of Cyad command + */ + +#ifndef SRC_CYAD_COMMANDLINEPARSER_CYADCOMMAND_H_ +#define SRC_CYAD_COMMANDLINEPARSER_CYADCOMMAND_H_ + +#include + +namespace Cynara { + +class CommandsDispatcher; + +class CyadCommand { +public: + CyadCommand() = default; + virtual ~CyadCommand() {} + + virtual int run(CommandsDispatcher &dispatcher); + + virtual bool isError(void) const { + return false; + } +}; + +class ErrorCyadCommand : public CyadCommand { +public: + ErrorCyadCommand(const std::string &message) : m_message(message) {} + virtual ~ErrorCyadCommand() {} + + virtual int run(CommandsDispatcher &dispatcher); + + virtual bool isError(void) const { + return true; + } + + virtual const std::string &message(void) const { + return m_message; + } + +private: + std::string m_message; +}; + +class HelpCyadCommand : public CyadCommand { +public: + using CyadCommand::CyadCommand; + + virtual int run(CommandsDispatcher &dispatcher); +}; + +} /* namespace Cynara */ + +#endif /* SRC_CYAD_COMMANDLINEPARSER_CYADCOMMAND_H_ */ diff --git a/src/cyad/CommandsDispatcher.cpp b/src/cyad/CommandsDispatcher.cpp new file mode 100644 index 0000000..dee8ce3 --- /dev/null +++ b/src/cyad/CommandsDispatcher.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/CommandsDispatcher.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief CommandsDispatcher class (implementation) + */ + +#include + +#include + +#include "CommandsDispatcher.h" + +namespace Cynara { + +CommandsDispatcher::CommandsDispatcher(BaseDispatcherIO &io, BaseAdminApiWrapper &adminApiWrapper) + : m_io(io), m_adminApiWrapper(adminApiWrapper), m_cynaraAdmin(nullptr) +{ + auto ret = m_adminApiWrapper.cynara_admin_initialize(&m_cynaraAdmin); + if (ret != CYNARA_API_SUCCESS) + throw AdminLibraryInitializationFailedException(ret); +} + +CommandsDispatcher::~CommandsDispatcher() { + m_adminApiWrapper.cynara_admin_finish(m_cynaraAdmin); +} + +int CommandsDispatcher::execute(CyadCommand &) { + m_io.cout() << "Whatever you wanted, it's not implemented" << std::endl; + return CYNARA_API_UNKNOWN_ERROR; +} + +int CommandsDispatcher::execute(HelpCyadCommand &) { + m_io.cout() << helpMessage << std::endl; + return CYNARA_API_SUCCESS; +} + +int CommandsDispatcher::execute(ErrorCyadCommand &result) { + m_io.cout() << "There was an error in command-line options:" << std::endl; + m_io.cout() << result.message() << std::endl; + + m_io.cout() << std::endl << helpMessage << std::endl; + return CYNARA_API_INVALID_COMMANDLINE_PARAM; +} + +} /* namespace Cynara */ diff --git a/src/cyad/CommandsDispatcher.h b/src/cyad/CommandsDispatcher.h new file mode 100644 index 0000000..a776682 --- /dev/null +++ b/src/cyad/CommandsDispatcher.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/CommandsDispatcher.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief CommandsDispatcher class + */ + +#ifndef SRC_CYAD_COMMANDSDISPATCHER_H_ +#define SRC_CYAD_COMMANDSDISPATCHER_H_ + +#include +#include +#include + +struct cynara_admin; +struct cynara_admin_policy; + +namespace Cynara { + +class CommandsDispatcher { +public: + CommandsDispatcher(BaseDispatcherIO &io, BaseAdminApiWrapper &adminApiWrapper); + virtual ~CommandsDispatcher(); + + virtual int execute(CyadCommand &); + virtual int execute(HelpCyadCommand &); + virtual int execute(ErrorCyadCommand &); + +private: + // TODO: Get argv[0] instead of hardcoded name + const std::string helpMessage = "Usage: cyad [OPTIONS]\n\n" + "Help options:\n" + " -h, --help print help message"; + BaseDispatcherIO &m_io; + BaseAdminApiWrapper &m_adminApiWrapper; + struct cynara_admin *m_cynaraAdmin; +}; + +} /* namespace Cynara */ + +#endif /* SRC_CYAD_COMMANDSDISPATCHER_H_ */ diff --git a/src/include/cynara-error.h b/src/include/cynara-error.h index 8ac7de1..66d75f0 100644 --- a/src/include/cynara-error.h +++ b/src/include/cynara-error.h @@ -18,6 +18,7 @@ * @author Lukasz Wojciechowski * @author Zofia Abramowska * @author Radoslaw Bartosiak + * @author Aleksander Zdyb * @version 1.0 * @brief This file contains error codes returned by APIs of Cynara. */ @@ -33,46 +34,49 @@ */ /*! \brief indicating access that was checked is allowed */ -#define CYNARA_API_ACCESS_ALLOWED 2 +#define CYNARA_API_ACCESS_ALLOWED 2 /*! \brief indicating that access that was checked is denied */ -#define CYNARA_API_ACCESS_DENIED 1 +#define CYNARA_API_ACCESS_DENIED 1 /*! \brief indicating the result of the one specific API is successful */ -#define CYNARA_API_SUCCESS 0 +#define CYNARA_API_SUCCESS 0 /*! \brief indicating that value is not present in cache */ -#define CYNARA_API_CACHE_MISS -1 +#define CYNARA_API_CACHE_MISS -1 /*! \brief indicating that pending requests reached maximum */ -#define CYNARA_API_MAX_PENDING_REQUESTS -2 +#define CYNARA_API_MAX_PENDING_REQUESTS -2 /*! \brief indicating system is running out of memory state */ -#define CYNARA_API_OUT_OF_MEMORY -3 +#define CYNARA_API_OUT_OF_MEMORY -3 /*! \brief indicating the API's parameter is malformed */ -#define CYNARA_API_INVALID_PARAM -4 +#define CYNARA_API_INVALID_PARAM -4 /*! \brief indicating that service is not available */ -#define CYNARA_API_SERVICE_NOT_AVAILABLE -5 +#define CYNARA_API_SERVICE_NOT_AVAILABLE -5 /*! \brief indicating that provided method is not supported by library */ -#define CYNARA_API_METHOD_NOT_SUPPORTED -6 +#define CYNARA_API_METHOD_NOT_SUPPORTED -6 /*! \brief cynara service does not allow to perform requested operation */ -#define CYNARA_API_OPERATION_NOT_ALLOWED -7 +#define CYNARA_API_OPERATION_NOT_ALLOWED -7 /*! \brief cynara service failed to perform requested operation */ -#define CYNARA_API_OPERATION_FAILED -8 +#define CYNARA_API_OPERATION_FAILED -8 /*! \brief cynara service hasn't found requested bucket */ -#define CYNARA_API_BUCKET_NOT_FOUND -9 +#define CYNARA_API_BUCKET_NOT_FOUND -9 /*! \brief indicating an unknown error */ -#define CYNARA_API_UNKNOWN_ERROR -10 +#define CYNARA_API_UNKNOWN_ERROR -10 /*! \brief indicating configuration error */ -#define CYNARA_API_CONFIGURATION_ERROR -11 +#define CYNARA_API_CONFIGURATION_ERROR -11 + +/*! \brief indicating invalid parameter in command-line */ +#define CYNARA_API_INVALID_COMMANDLINE_PARAM -12 /** @}*/ #endif /* CYNARA_ERROR_H */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6e6d12c..f99d555 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -49,6 +49,8 @@ SET(CYNARA_SOURCES_FOR_TESTS ${CYNARA_SRC}/common/types/PolicyResult.cpp ${CYNARA_SRC}/common/types/PolicyType.cpp ${CYNARA_SRC}/cyad/AdminPolicyParser.cpp + ${CYNARA_SRC}/cyad/CommandlineParser/CyadCommand.cpp + ${CYNARA_SRC}/cyad/CommandsDispatcher.cpp ${CYNARA_SRC}/cyad/CynaraAdminPolicies.cpp ${CYNARA_SRC}/helpers/creds-commons/CredsCommonsInner.cpp ${CYNARA_SRC}/helpers/creds-commons/creds-commons.cpp @@ -72,6 +74,7 @@ SET(CYNARA_TESTS_SOURCES common/protocols/admin/listresponse.cpp common/types/policybucket.cpp credsCommons/parser/Parser.cpp + cyad/commands_dispatcher.cpp cyad/helpers.cpp cyad/policy_collection.cpp cyad/policy_parser.cpp diff --git a/test/cyad/CyadCommandlineDispatcherTest.h b/test/cyad/CyadCommandlineDispatcherTest.h new file mode 100644 index 0000000..504af80 --- /dev/null +++ b/test/cyad/CyadCommandlineDispatcherTest.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file test/cyad/CyadCommandlineDispatcherTest.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Test fixture for CyadCommandlineDispatcher + */ + +#ifndef TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_ +#define TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_ + +#include +#include + +#include "FakeDispatcherIO.h" + +class CyadCommandlineDispatcherTest : public ::testing::Test { +protected: + FakeDispatcherIO m_io; +}; + +#endif /* TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_ */ diff --git a/test/cyad/commands_dispatcher.cpp b/test/cyad/commands_dispatcher.cpp new file mode 100644 index 0000000..9a948e6 --- /dev/null +++ b/test/cyad/commands_dispatcher.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file test/cyad/commands_dispatcher.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Tests for CommandsDispatcher + */ + +#include +#include + +#include + +#include +#include + +#include "CyadCommandlineDispatcherTest.h" +#include "FakeAdminApiWrapper.h" + +/** + * @brief Dispatcher should not touch admin API on help or error + * @test Scenario: + * - Prepare some parsing results not requiring API calls + * - Check if no API calls were made + */ +TEST_F(CyadCommandlineDispatcherTest, noApi) { + using ::testing::_; + using ::testing::Return; + + FakeAdminApiWrapper adminApi; + + EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS)); + EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS)); + + Cynara::CommandsDispatcher dispatcher(m_io, adminApi); + + Cynara::CyadCommand result; + Cynara::HelpCyadCommand helpResult; + Cynara::ErrorCyadCommand errorResult("Fake error"); + + dispatcher.execute(result); + dispatcher.execute(helpResult); + dispatcher.execute(errorResult); +}