From 0aca3eef9676c7fe81ad03de6e11296f9b56b004 Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Mon, 29 Jun 2015 18:08:43 +0200 Subject: [PATCH] Add objective wrappers for auparse functions The wrapper is needed to make unit testing possible. Change-Id: I4c8a09ce722df525791c63e9a571abf527c3220b --- src/Audit/AuparseSourceFeedWrapper.cpp | 30 +++++++++++ src/Audit/AuparseSourceFeedWrapper.h | 37 +++++++++++++ src/Audit/AuparseWrapper.cpp | 99 ++++++++++++++++++++++++++++++++++ src/Audit/AuparseWrapper.h | 67 +++++++++++++++++++++++ src/Audit/BaseAuparseWrapper.h | 57 ++++++++++++++++++++ src/Audit/ErrorException.h | 37 +++++++++++++ src/Audit/EventHandler.h | 37 +++++++++++++ 7 files changed, 364 insertions(+) create mode 100644 src/Audit/AuparseSourceFeedWrapper.cpp create mode 100644 src/Audit/AuparseSourceFeedWrapper.h create mode 100644 src/Audit/AuparseWrapper.cpp create mode 100644 src/Audit/AuparseWrapper.h create mode 100644 src/Audit/BaseAuparseWrapper.h create mode 100644 src/Audit/ErrorException.h create mode 100644 src/Audit/EventHandler.h diff --git a/src/Audit/AuparseSourceFeedWrapper.cpp b/src/Audit/AuparseSourceFeedWrapper.cpp new file mode 100644 index 0000000..d2352ae --- /dev/null +++ b/src/Audit/AuparseSourceFeedWrapper.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2015 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/Audit/AuparseSourceFeedWrapper.cpp + * @author Aleksander Zdyb + * @version 1.0 + */ + +#include + +#include "AuparseSourceFeedWrapper.h" + +namespace Audit { + +AuparseSourceFeedWrapper::AuparseSourceFeedWrapper() : AuparseWrapper(::AUSOURCE_FEED, nullptr) {} + +} /* namespace Audit */ diff --git a/src/Audit/AuparseSourceFeedWrapper.h b/src/Audit/AuparseSourceFeedWrapper.h new file mode 100644 index 0000000..a2a1a58 --- /dev/null +++ b/src/Audit/AuparseSourceFeedWrapper.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 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/Audit/AuparseSourceFeedWrapper.h + * @author Aleksander Zdyb + * @version 1.0 + */ + +#ifndef SRC_AUDIT_AUPARSESOURCEFEEDWRAPPER_H +#define SRC_AUDIT_AUPARSESOURCEFEEDWRAPPER_H + +#include "AuparseWrapper.h" + +namespace Audit { + +class AuparseSourceFeedWrapper: public Audit::AuparseWrapper { +public: + AuparseSourceFeedWrapper(); + virtual ~AuparseSourceFeedWrapper() = default; +}; + +} /* namespace Audit */ + +#endif /* SRC_AUDIT_AUPARSESOURCEFEEDWRAPPER_H */ diff --git a/src/Audit/AuparseWrapper.cpp b/src/Audit/AuparseWrapper.cpp new file mode 100644 index 0000000..645e689 --- /dev/null +++ b/src/Audit/AuparseWrapper.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2015 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/Audit/AuparseWrapper.cpp + * @author Aleksander Zdyb + * @version 1.0 + */ + +#include + +#include "AuparseWrapper.h" +#include "ErrorException.h" + +namespace Audit { + +static void handleEvent(auparse_state_t *au, auparse_cb_event_t cb_event_type, void *user_data) { + if (cb_event_type != AUPARSE_CB_EVENT_READY) + return; + + auto eventHandler = static_cast(user_data); + eventHandler->handleEvent(); +} + +AuparseWrapper::AuparseWrapper(ausource_t source, const void *b) { + auto auParseState = ::auparse_init(source, b); + + if (auParseState == nullptr) { + throw ErrorException("Could not initialize audit parser"); + } + + m_auParseState = decltype(m_auParseState)(auParseState, ::auparse_destroy); +} + +int AuparseWrapper::auparse_feed(const char *data, size_t data_len) { + return ::auparse_feed(m_auParseState.get(), data, data_len); +} + +int AuparseWrapper::auparse_flush_feed(void) { + return ::auparse_flush_feed(m_auParseState.get()); +} + +int AuparseWrapper::auparse_feed_has_data(void) { + return ::auparse_feed_has_data(m_auParseState.get()); +} + +void AuparseWrapper::auparse_add_callback(EventHandler *eventHandler) { + ::auparse_add_callback(m_auParseState.get(), handleEvent, eventHandler, nullptr); +} + +int AuparseWrapper::auparse_first_record(void) { + return ::auparse_first_record(m_auParseState.get()); +} + +int AuparseWrapper::auparse_next_record(void) { + return ::auparse_next_record(m_auParseState.get()); +} + +int AuparseWrapper::auparse_get_type(void) { + return ::auparse_get_type(m_auParseState.get()); +} + +const char *AuparseWrapper::auparse_get_type_name(void) { + return ::auparse_get_type_name(m_auParseState.get()); +} + +int AuparseWrapper::auparse_first_field(void) { + return ::auparse_first_field(m_auParseState.get()); +} + +int AuparseWrapper::auparse_next_field(void) { + return ::auparse_next_field(m_auParseState.get()); +} + +const char *AuparseWrapper::auparse_get_field_name(void) { + return ::auparse_get_field_name(m_auParseState.get()); +} + +int AuparseWrapper::auparse_get_field_type(void) { + return ::auparse_get_field_type(m_auParseState.get()); +} + +const char *AuparseWrapper::auparse_interpret_field(void) { + return ::auparse_interpret_field(m_auParseState.get()); +} + +} /* namespace Audit */ diff --git a/src/Audit/AuparseWrapper.h b/src/Audit/AuparseWrapper.h new file mode 100644 index 0000000..734da1a --- /dev/null +++ b/src/Audit/AuparseWrapper.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015 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/Audit/AuparseWrapper.h + * @author Aleksander Zdyb + * @version 1.0 + */ + +#ifndef SRC_AUDIT_AUPARSEWRAPPER_H +#define SRC_AUDIT_AUPARSEWRAPPER_H + +#include +#include + +#include + +#include "BaseAuparseWrapper.h" + +namespace Audit { + +class AuparseWrapper: public BaseAuparseWrapper { +public: + AuparseWrapper(ausource_t source, const void *b); + virtual ~AuparseWrapper() = default; + + virtual int auparse_feed(const char *data, size_t data_len); + virtual int auparse_flush_feed(void); + virtual int auparse_feed_has_data(void); + + virtual void auparse_add_callback(EventHandler *eventHandler); + + virtual int auparse_first_record(void); + virtual int auparse_next_record(void); + + virtual int auparse_get_type(void); + virtual const char *auparse_get_type_name(void); + + virtual int auparse_first_field(void); + virtual int auparse_next_field(void); + + virtual const char *auparse_get_field_name(void); + virtual int auparse_get_field_type(void); + virtual const char *auparse_interpret_field(void); + +protected: + typedef std::unique_ptr> + AuditParseStatePtr; + + AuditParseStatePtr m_auParseState = nullptr; +}; + +} /* namespace Audit */ + +#endif /* SRC_AUDIT_AUPARSEWRAPPER_H */ diff --git a/src/Audit/BaseAuparseWrapper.h b/src/Audit/BaseAuparseWrapper.h new file mode 100644 index 0000000..4353e2e --- /dev/null +++ b/src/Audit/BaseAuparseWrapper.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015 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/Audit/BaseAuparseWrapper.h + * @author Aleksander Zdyb + * @version 1.0 + */ + +#ifndef SRC_AUDIT_BASEAUPARSEWRAPPER_H +#define SRC_AUDIT_BASEAUPARSEWRAPPER_H + +#include + +#include "EventHandler.h" + +namespace Audit { + +class BaseAuparseWrapper { +public: + virtual ~BaseAuparseWrapper() = default; + + virtual int auparse_feed(const char *data, size_t data_len) = 0; + virtual int auparse_flush_feed(void) = 0; + virtual int auparse_feed_has_data(void) = 0; + + virtual void auparse_add_callback(EventHandler *eventHandler) = 0; + + virtual int auparse_first_record(void) = 0; + virtual int auparse_next_record(void) = 0; + + virtual int auparse_get_type(void) = 0; + virtual const char *auparse_get_type_name(void) = 0; + + virtual int auparse_first_field(void) = 0; + virtual int auparse_next_field(void) = 0; + + virtual const char *auparse_get_field_name(void) = 0; + virtual int auparse_get_field_type(void) = 0; + virtual const char *auparse_interpret_field(void) = 0; +}; + +} /* namespace Audit */ + +#endif /* SRC_AUDIT_BASEAUPARSEWRAPPER_H */ diff --git a/src/Audit/ErrorException.h b/src/Audit/ErrorException.h new file mode 100644 index 0000000..8d16772 --- /dev/null +++ b/src/Audit/ErrorException.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 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/Audit/ErrorException.h + * @author Aleksander Zdyb + * @version 1.0 + */ + +#ifndef SRC_AUDIT_ERROREXCEPTION_H +#define SRC_AUDIT_ERROREXCEPTION_H + +#include + +namespace Audit { + +class ErrorException : public Utils::WithMessageException { +public: + ErrorException(const std::string &message) : WithMessageException("Audit: " + message) {} +}; + +} // namespace Audit + + +#endif // SRC_AUDIT_ERROREXCEPTION_H diff --git a/src/Audit/EventHandler.h b/src/Audit/EventHandler.h new file mode 100644 index 0000000..98668e6 --- /dev/null +++ b/src/Audit/EventHandler.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 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/Audit/EventHandler.h + * @author Aleksander Zdyb + * @version 1.0 + */ + +#ifndef SRC_AUDIT_EVENTHANDLER_H +#define SRC_AUDIT_EVENTHANDLER_H + +namespace Audit { + +class EventHandler { +public: + virtual ~EventHandler() = default; + virtual void handleEvent() = 0; +}; + +} /* namespace Audit */ + + + +#endif /* SRC_AUDIT_EVENTHANDLER_H */ -- 2.7.4