Add objective wrappers for auparse functions 27/43727/1
authorAleksander Zdyb <a.zdyb@samsung.com>
Mon, 29 Jun 2015 16:08:43 +0000 (18:08 +0200)
committerAleksander Zdyb <a.zdyb@samsung.com>
Fri, 10 Jul 2015 12:50:28 +0000 (14:50 +0200)
The wrapper is needed to make unit testing possible.

Change-Id: I4c8a09ce722df525791c63e9a571abf527c3220b

src/Audit/AuparseSourceFeedWrapper.cpp [new file with mode: 0644]
src/Audit/AuparseSourceFeedWrapper.h [new file with mode: 0644]
src/Audit/AuparseWrapper.cpp [new file with mode: 0644]
src/Audit/AuparseWrapper.h [new file with mode: 0644]
src/Audit/BaseAuparseWrapper.h [new file with mode: 0644]
src/Audit/ErrorException.h [new file with mode: 0644]
src/Audit/EventHandler.h [new file with mode: 0644]

diff --git a/src/Audit/AuparseSourceFeedWrapper.cpp b/src/Audit/AuparseSourceFeedWrapper.cpp
new file mode 100644 (file)
index 0000000..d2352ae
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+
+#include <auparse.h>
+
+#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 (file)
index 0000000..a2a1a58
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @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 (file)
index 0000000..645e689
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+
+#include <auparse.h>
+
+#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<EventHandler *>(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 (file)
index 0000000..734da1a
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+
+#ifndef SRC_AUDIT_AUPARSEWRAPPER_H
+#define SRC_AUDIT_AUPARSEWRAPPER_H
+
+#include <functional>
+#include <memory>
+
+#include <auparse.h>
+
+#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<auparse_state_t, std::function<void(auparse_state_t *)>>
+            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 (file)
index 0000000..4353e2e
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+
+#ifndef SRC_AUDIT_BASEAUPARSEWRAPPER_H
+#define SRC_AUDIT_BASEAUPARSEWRAPPER_H
+
+#include <cstddef>
+
+#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 (file)
index 0000000..8d16772
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+
+#ifndef SRC_AUDIT_ERROREXCEPTION_H
+#define SRC_AUDIT_ERROREXCEPTION_H
+
+#include <Utils/WithMessageException.h>
+
+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 (file)
index 0000000..98668e6
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @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 */