Cynara: Add cynara-monitor tests 47/93147/6
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Tue, 15 Nov 2016 09:12:13 +0000 (18:12 +0900)
committerZofia Abramowska <z.abramowska@samsung.com>
Tue, 15 Nov 2016 15:20:22 +0000 (07:20 -0800)
Change-Id: Iaddb659c511737738c5a7663dc9150e8c940bb10

src/cynara-tests/CMakeLists.txt
src/cynara-tests/common/cynara_test_monitor.cpp [new file with mode: 0644]
src/cynara-tests/common/cynara_test_monitor.h [new file with mode: 0644]
src/cynara-tests/test_cases_monitor.cpp [new file with mode: 0644]

index 7a42d65..093027d 100644 (file)
@@ -28,6 +28,7 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP
     cynara-creds-gdbus
     cynara-creds-self
     cynara-creds-socket
+    cynara-monitor
     cynara-plugin
     dbus-1
     glib-2.0
@@ -47,6 +48,7 @@ SET(CYNARA_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_cynara_mask.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_env.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_file_operations.cpp
+    ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_monitor.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/plugins/plugins.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/cynara-test.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases.cpp
@@ -54,6 +56,7 @@ SET(CYNARA_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_async.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_db.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_helpers.cpp
+    ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_monitor.cpp
     )
 
 #header directories
diff --git a/src/cynara-tests/common/cynara_test_monitor.cpp b/src/cynara-tests/common/cynara_test_monitor.cpp
new file mode 100644 (file)
index 0000000..f01df58
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2016 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.
+ */
+
+#include <cynara_test_monitor.h>
+
+#include <tests_common.h>
+
+namespace CynaraTestMonitor {
+
+MonitorEntries::MonitorEntries(cynara_monitor_entry **entries)
+    : m_entries(entries)
+{
+}
+
+MonitorEntries::~MonitorEntries()
+{
+    cynara_monitor_entries_free(m_entries);
+}
+
+size_t MonitorEntries::count()
+{
+    size_t count = 0;
+
+    while (m_entries[count]) {
+        ++count;
+    }
+
+    return count;
+}
+
+const MonitorEntries& MonitorEntries::operator=(cynara_monitor_entry **other)
+{
+    m_entries = other;
+
+    return *this;
+}
+
+const cynara_monitor_entry* MonitorEntries::operator[](size_t i) const
+{
+    return m_entries[i];
+}
+
+Monitor::Monitor(size_t entries)
+    : m_monitor(nullptr)
+{
+    int ret;
+    struct cynara_monitor_configuration *cfg = nullptr;
+
+    if (entries > 0) {
+        ret = cynara_monitor_configuration_create(&cfg);
+        RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
+                          "cynara_monitor_configuration_create failed. "
+                          "ret: " << ret);
+        RUNNER_ASSERT_MSG(cfg != nullptr, "cynara_monitor_configuration struct "
+                          "was not initialized");
+
+        ret = cynara_monitor_configuration_set_buffer_size(cfg, entries);
+        RUNNER_ASSERT_MSG(ret ==  CYNARA_API_SUCCESS,
+                          "cynara_monitor_configuration_set_buffer_size failed. "
+                          "ret: " << ret);
+    }
+
+    ret = cynara_monitor_initialize(&m_monitor, cfg);
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
+                      "cynara_monitor_initialize failed. ret: " << ret);
+    RUNNER_ASSERT_MSG(m_monitor != nullptr, "cynara_monitor struct was not initialized");
+}
+
+Monitor::~Monitor()
+{
+    cynara_monitor_finish(m_monitor);
+}
+
+cynara_monitor_entry** Monitor::get()
+{
+    struct cynara_monitor_entry **entries;
+    int ret = cynara_monitor_entries_get(m_monitor, &entries);
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
+                      "cynara_monitor_entries_get failed. ret: " << ret);
+    RUNNER_ASSERT_MSG(entries != nullptr, "cynara_monitor_entry table was not initialized");
+
+    return entries;
+}
+
+void Monitor::flush()
+{
+    cynara_monitor_entries_flush(m_monitor);
+}
+
+} //namespace CynaraTestMonitor
diff --git a/src/cynara-tests/common/cynara_test_monitor.h b/src/cynara-tests/common/cynara_test_monitor.h
new file mode 100644 (file)
index 0000000..55cbeaf
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2016 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.
+ */
+
+#ifndef CYNARA_TEST_MONITOR_H
+#define CYNARA_TEST_MONITOR_H
+
+#include <cynara-monitor.h>
+
+namespace CynaraTestMonitor {
+
+class MonitorEntries
+{
+public:
+    MonitorEntries(cynara_monitor_entry **entries = NULL);
+    MonitorEntries(const MonitorEntries& that) = delete;
+    virtual ~MonitorEntries();
+
+    const MonitorEntries& operator=(const MonitorEntries &other) = delete;
+    const MonitorEntries& operator=(cynara_monitor_entry **other);
+    const cynara_monitor_entry* operator[](size_t i) const;
+
+    size_t count();
+
+private:
+    cynara_monitor_entry **m_entries;
+};
+
+class Monitor
+{
+public:
+    Monitor(size_t entries = 0);
+    Monitor(const Monitor &that) = delete;
+    virtual ~Monitor();
+
+    const Monitor& operator=(const Monitor &other) = delete;
+
+    cynara_monitor_entry** get();
+    void flush();
+
+private:
+    cynara_monitor *m_monitor;
+};
+
+} //namespace CynaraTestMonitor
+
+#endif // CYNARA_TEST_MONITOR_H
diff --git a/src/cynara-tests/test_cases_monitor.cpp b/src/cynara-tests/test_cases_monitor.cpp
new file mode 100644 (file)
index 0000000..bce58ed
--- /dev/null
@@ -0,0 +1,231 @@
+/*
+ * Copyright (c) 2016 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_cases_monitor.cpp
+ * @author      Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
+ * @version     1.0
+ * @brief       Tests for libcynara-monitor
+ */
+
+#include <cynara-error.h>
+#include <cynara-client.h>
+#include <cynara-monitor.h>
+
+#include <dpl/test/test_runner.h>
+#include <cynara_test_commons.h>
+#include <cynara_test_client.h>
+#include <cynara_test_monitor.h>
+#include <timeout.h>
+
+using namespace CynaraTestClient;
+using namespace CynaraTestMonitor;
+
+namespace {
+
+inline void assertResult(int ret, int expectedResult)
+{
+    RUNNER_ASSERT_MSG(ret == expectedResult,"Expected result: " << expectedResult
+                      << ", got: " << ret);
+}
+
+void clientChecks(size_t num, size_t start = 0)
+{
+    Client cynaraClient;
+
+    for (size_t i = 0; i < num; ++i) {
+        std::string index = std::to_string(i + start);
+        std::string client = "client" + index;
+        std::string session = "session" + index;
+        std::string user = "user" + index;
+        std::string privilege = "privilege" + index;
+
+        cynaraClient.check(client.c_str(), session.c_str(), user.c_str(),
+                           privilege.c_str(), CYNARA_API_ACCESS_DENIED);
+    }
+}
+
+bool validateEntries(const MonitorEntries &entries, size_t num, size_t start = 0)
+{
+    for (size_t i = 0; i < num; ++i) {
+        std::string index = std::to_string(i + start);
+        std::string client = "client" + index;
+        std::string user = "user" + index;
+        std::string privilege = "privilege" + index;
+
+        if (client != std::string(cynara_monitor_entry_get_client(entries[i])) ||
+            user != std::string(cynara_monitor_entry_get_user(entries[i])) ||
+            privilege != std::string(cynara_monitor_entry_get_privilege(entries[i]))) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+} // namespace
+
+void tcm01_config_invalid_param_func()
+{
+    int ret = cynara_monitor_configuration_create(nullptr);
+    assertResult(ret, CYNARA_API_INVALID_PARAM);
+}
+
+void tcm02_config_buffer_oversize_func()
+{
+    int ret;
+    struct cynara_monitor_configuration *cfg;
+
+    ret = cynara_monitor_configuration_create(&cfg);
+    assertResult(ret, CYNARA_API_SUCCESS);
+
+    ret = cynara_monitor_configuration_set_buffer_size(cfg, CYNARA_MAX_MONITOR_BUFFER_SIZE);
+    assertResult(ret, CYNARA_API_SUCCESS);
+
+    ret = cynara_monitor_configuration_set_buffer_size(cfg, CYNARA_MAX_MONITOR_BUFFER_SIZE + 1);
+    assertResult(ret, CYNARA_API_INVALID_PARAM);
+}
+
+void tcm03_init_invalid_param_func()
+{
+    int ret = cynara_monitor_initialize(nullptr, nullptr);
+    assertResult(ret, CYNARA_API_INVALID_PARAM);
+}
+
+void tcm04_entries_get_flush_no_entries_func()
+{
+    auto timeLimit = std::chrono::seconds(1);
+    Monitor monitor(100);
+    MonitorEntries entries;
+
+    auto getMonitorEntries = [&monitor, &entries]() {
+        entries = monitor.get();
+    };
+
+    auto flush = [&monitor]() {
+        monitor.flush();
+    };
+
+    Timeout::callAndWait(timeLimit, Timeout::ExpectMode::TIMEOUT,
+                         flush, getMonitorEntries);
+
+    // TODO: system's cynara checks might increase this number
+    if (entries.count() != 0)
+        RUNNER_IGNORED_MSG("no entries should've been returned");
+}
+
+void tcm05_entries_get_no_flush_func()
+{
+    auto timeLimit = std::chrono::seconds(1);
+    Monitor monitor(100);
+    MonitorEntries entries;
+
+    auto getMonitorEntries = [&monitor, &entries]() {
+        entries = monitor.get();
+    };
+
+    auto client = [&monitor]() {
+        clientChecks(100);
+    };
+
+    Timeout::callAndWait(timeLimit, Timeout::ExpectMode::TIMEOUT,
+                         client, getMonitorEntries);
+
+    RUNNER_ASSERT_MSG(entries.count() == 100, "100 entries should've been returned");
+
+    // TODO: system's cynara checks might cause the entries to differ
+    if (!validateEntries(entries, 100))
+        RUNNER_IGNORED_MSG("Entries received from monitor are not as expected");
+}
+
+void tcm06_entries_get_flush_func()
+{
+    auto getLimit = std::chrono::seconds(1);
+    Monitor monitor(200);
+    MonitorEntries entries;
+
+    auto getMonitorEntries = [&monitor, &entries]() {
+        entries = monitor.get();
+    };
+
+    auto clientAndFlush = [&monitor]() {
+        clientChecks(10);
+        monitor.flush();
+    };
+
+    Timeout::callAndWait(getLimit, Timeout::ExpectMode::TIMEOUT,
+                         clientAndFlush, getMonitorEntries);
+
+    // TODO: system's cynara checks might increase this number
+    if (entries.count() != 10)
+        RUNNER_IGNORED_MSG("10 entries should've been returned");
+
+    // TODO: system's cynara checks might cause the entries to differ
+    if (!validateEntries(entries, 10))
+        RUNNER_IGNORED_MSG("Entries received from monitor are not as expected");
+}
+
+void tcm07_entries_get_two_func()
+{
+    auto get1Limit = std::chrono::seconds(1);
+    Monitor monitor1(200);
+    Monitor monitor2(100);
+    MonitorEntries entries1;
+    MonitorEntries entries2;
+
+    auto getMonitor1Entries = [&monitor1, &entries1]() {
+        entries1 = monitor1.get();
+    };
+
+    auto clientAndGetMonitor2Entries = [&monitor2, &entries2]() {
+        auto get2Limit = std::chrono::seconds(1);
+
+        clientChecks(100);
+
+        auto getMonitor2Entries = [&monitor2, &entries2]() {
+            entries2 = monitor2.get();
+        };
+
+        auto client = []() {
+            clientChecks(100, 100);
+        };
+
+        Timeout::callAndWait(get2Limit, Timeout::ExpectMode::TIMEOUT,
+                             client, getMonitor2Entries);
+    };
+
+    Timeout::callAndWait(get1Limit, Timeout::ExpectMode::TIMEOUT,
+                         clientAndGetMonitor2Entries, getMonitor1Entries);
+
+    RUNNER_ASSERT_MSG(entries1.count() == 200, "200 entries should've been returned by the first monitor");
+    RUNNER_ASSERT_MSG(entries2.count() == 100, "100 entries should've been returned by the second monitor");
+
+    // TODO: system's cynara checks might cause the entries to differ
+    if (!validateEntries(entries1, 200))
+        RUNNER_IGNORED_MSG("Entries received from the first monitor are not as expected");
+    if (!validateEntries(entries2, 100, 100))
+        RUNNER_IGNORED_MSG("Entries received from the second monitor are not as expected");
+}
+
+RUNNER_TEST_GROUP_INIT(cynara_monitor_tests)
+
+RUN_CYNARA_TEST(tcm01_config_invalid_param)
+RUN_CYNARA_TEST(tcm02_config_buffer_oversize)
+RUN_CYNARA_TEST(tcm03_init_invalid_param)
+RUN_CYNARA_TEST(tcm04_entries_get_flush_no_entries)
+RUN_CYNARA_TEST(tcm05_entries_get_no_flush)
+RUN_CYNARA_TEST(tcm06_entries_get_flush)
+RUN_CYNARA_TEST(tcm07_entries_get_two)