--- /dev/null
+/*
+ * 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 <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief Exception thrown when cynara_admin_initialize() fails
+ */
+
+#ifndef SRC_CYAD_ADMINLIBRARYINITIALIZATIONFAILEDEXCEPTION_H_
+#define SRC_CYAD_ADMINLIBRARYINITIALIZATIONFAILEDEXCEPTION_H_
+
+#include <string>
+
+#include <exceptions/Exception.h>
+
+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_ */
${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
)
--- /dev/null
+/*
+ * 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 <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief A representation of Cyad command
+ */
+
+#include <cyad/CommandsDispatcher.h>
+
+#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 */
--- /dev/null
+/*
+ * 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 <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief A representation of Cyad command
+ */
+
+#ifndef SRC_CYAD_COMMANDLINEPARSER_CYADCOMMAND_H_
+#define SRC_CYAD_COMMANDLINEPARSER_CYADCOMMAND_H_
+
+#include <string>
+
+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_ */
--- /dev/null
+/*
+ * 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 <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief CommandsDispatcher class (implementation)
+ */
+
+#include <cynara-error.h>
+
+#include <cyad/AdminLibraryInitializationFailedException.h>
+
+#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 */
--- /dev/null
+/*
+ * 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 <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief CommandsDispatcher class
+ */
+
+#ifndef SRC_CYAD_COMMANDSDISPATCHER_H_
+#define SRC_CYAD_COMMANDSDISPATCHER_H_
+
+#include <cyad/BaseAdminApiWrapper.h>
+#include <cyad/CommandlineParser/CyadCommand.h>
+#include <cyad/DispatcherIO.h>
+
+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_ */
* @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
* @author Zofia Abramowska <z.abramowska@samsung.com>
* @author Radoslaw Bartosiak <r.bartosiak@samsung.com>
+ * @author Aleksander Zdyb <a.zdyb@samsung.com>
* @version 1.0
* @brief This file contains error codes returned by APIs of Cynara.
*/
*/
/*! \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 */
${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
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
--- /dev/null
+/*
+ * 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 <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief Test fixture for CyadCommandlineDispatcher
+ */
+
+#ifndef TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_
+#define TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "FakeDispatcherIO.h"
+
+class CyadCommandlineDispatcherTest : public ::testing::Test {
+protected:
+ FakeDispatcherIO m_io;
+};
+
+#endif /* TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_ */
--- /dev/null
+/*
+ * 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 <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief Tests for CommandsDispatcher
+ */
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <cynara-error.h>
+
+#include <cyad/CommandlineParser/CyadCommand.h>
+#include <cyad/CommandsDispatcher.h>
+
+#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);
+}