Add install request wrapper class to security-manager tests 59/31859/4
authorMarcin Niesluchowski <m.niesluchow@samsung.com>
Wed, 10 Dec 2014 13:09:33 +0000 (14:09 +0100)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Mon, 15 Dec 2014 09:16:34 +0000 (10:16 +0100)
Change-Id: Iebf1008b4ba24fd02fd371253b7d1063780625a5

tests/security-manager-tests/CMakeLists.txt
tests/security-manager-tests/common/sm_request.cpp [new file with mode: 0644]
tests/security-manager-tests/common/sm_request.h [new file with mode: 0644]

index 9392c3e..dd8f049 100644 (file)
@@ -37,6 +37,7 @@ SET(TARGET_SEC_MGR_TESTS "security-manager-tests")
 SET(SEC_MGR_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/security-manager-tests/security_manager_tests.cpp
     ${PROJECT_SOURCE_DIR}/tests/security-manager-tests/common/sm_db.cpp
+    ${PROJECT_SOURCE_DIR}/tests/security-manager-tests/common/sm_request.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client.cpp
     ${PROJECT_SOURCE_DIR}/tests/libprivilege-control-tests/libprivilege-control_test_common.cpp
    )
diff --git a/tests/security-manager-tests/common/sm_request.cpp b/tests/security-manager-tests/common/sm_request.cpp
new file mode 100644 (file)
index 0000000..910bbfd
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * 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.
+ */
+
+#include <sm_request.h>
+
+#include <dpl/test/test_runner.h>
+
+namespace SecurityManagerTest {
+
+InstallRequest::InstallRequest()
+    : m_req(nullptr)
+    , m_appId(nullptr)
+    , m_pkgId(nullptr)
+    , m_uid(false, 0)
+{
+    int result = security_manager_app_inst_req_new(&m_req);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+                      "creation of new request failed. Result: " << result);
+    RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
+}
+
+InstallRequest::~InstallRequest()
+{
+    security_manager_app_inst_req_free(m_req);
+}
+
+void InstallRequest::setAppId(const char *appId, lib_retcode expectedResult)
+{
+    int result = security_manager_app_inst_req_set_app_id(m_req, appId);
+    RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
+                      "setting app id returned wrong value."
+                          << " App id: " << appId << ";"
+                          << " Result: " << result << ";"
+                          << " Expected result: " << expectedResult);
+    m_appId = appId;
+}
+
+void InstallRequest::setPkgId(const char *pkgId, lib_retcode expectedResult)
+{
+    int result = security_manager_app_inst_req_set_pkg_id(m_req, pkgId);
+    RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
+                      "setting pkg id returned wrong value."
+                          << " Pkg id: " << pkgId << ";"
+                          << " Result: " << result << ";"
+                          << " Expected result: " << expectedResult);
+    m_pkgId = pkgId;
+}
+
+void InstallRequest::addPrivilege(const char *privilege, lib_retcode expectedResult)
+{
+    int result = security_manager_app_inst_req_add_privilege(m_req, privilege);
+    RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
+                      "adding privilege returned wrong value."
+                          << " Privilege: " << privilege << ";"
+                          << " Result: " << result << ";"
+                          << " Expected result: " << expectedResult);
+    m_privileges.push_back(privilege);
+}
+
+void InstallRequest::addPath(const char *path, app_install_path_type pathType, lib_retcode expectedResult)
+{
+    int result = security_manager_app_inst_req_add_path(m_req, path, pathType);
+    RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
+                      "adding path returned wrong value."
+                          << " Path: " << path << ";"
+                          << " Path type: " << pathType << ";"
+                          << " Result: " << result << ";"
+                          << " Expected result: " << expectedResult);
+    m_paths.push_back(std::pair<std::string, app_install_path_type>(path, pathType));
+}
+
+void InstallRequest::setUid(const uid_t uid, lib_retcode expectedResult)
+{
+    int result = security_manager_app_inst_req_set_uid(m_req, uid);
+    RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
+                      "setting uid returned wrong value."
+                          << " Uid: " << uid << ";"
+                          << " Result: " << result << ";"
+                          << " Expected result: " << expectedResult);
+    m_uid.first = true;
+    m_uid.second = uid;
+}
+
+std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
+{
+    if (request.m_appId != nullptr)
+        os << "app id: " << request.m_appId << "; ";
+    if (request.m_pkgId != nullptr)
+        os << "pkg id: " << request.m_pkgId << "; ";
+    if (!request.m_privileges.empty()) {
+        os << "privileges: [ " << request.m_privileges[0];
+        for (size_t i=1; i < request.m_privileges.size(); ++i) {
+            os << "; " << request.m_privileges[i];
+        }
+        os << " ]";
+    }
+    if (!request.m_paths.empty()) {
+        os << "paths: [ " << "< " << request.m_paths[0].first << "; "
+                                  << request.m_paths[0].second << " >";
+        for (size_t i=1; i < request.m_paths.size(); ++i) {
+            os << "; < " << request.m_paths[i].first << "; "
+                         << request.m_paths[i].second << " >";
+        }
+        os << " ]";
+    }
+    if (request.m_uid.first)
+        os << "uid: " << request.m_uid.second << "; ";
+    return os;
+}
+
+} // namespace SecurityManagerTest
diff --git a/tests/security-manager-tests/common/sm_request.h b/tests/security-manager-tests/common/sm_request.h
new file mode 100644 (file)
index 0000000..0bd0878
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+
+#ifndef SECURITY_MANAGER_TEST_INSTALLREQUEST
+#define SECURITY_MANAGER_TEST_INSTALLREQUEST
+
+#include <iostream>
+#include <string>
+#include <sys/types.h>
+#include <utility>
+#include <vector>
+
+#include <security-manager.h>
+
+namespace SecurityManagerTest {
+
+class InstallRequest
+{
+public:
+    InstallRequest();
+    InstallRequest(const InstallRequest&) = delete;
+    InstallRequest& operator=(const InstallRequest&) = delete;
+    ~InstallRequest();
+
+    void setAppId(const char *appId, lib_retcode expectedresult = SECURITY_MANAGER_SUCCESS);
+    void setPkgId(const char *pkgId, lib_retcode expectedresult = SECURITY_MANAGER_SUCCESS);
+    void addPrivilege(const char *privilege, lib_retcode expectedresult = SECURITY_MANAGER_SUCCESS);
+    void addPath(const char *path, app_install_path_type pathType,
+                 lib_retcode expectedResult = SECURITY_MANAGER_SUCCESS);
+    void setUid(const uid_t uid, lib_retcode expectedresult = SECURITY_MANAGER_SUCCESS);
+
+    const app_inst_req *get() const { return m_req; }
+    friend std::ostream& operator<<(std::ostream &, const InstallRequest&);
+
+private:
+    app_inst_req *m_req;
+
+    const char *m_appId;
+    const char *m_pkgId;
+    std::vector<std::string> m_privileges;
+    std::vector<std::pair<std::string, app_install_path_type> > m_paths;
+    std::pair<bool, uid_t> m_uid;
+};
+
+std::ostream& operator<<(std::ostream &os, const SecurityManagerTest::InstallRequest &request);
+
+} // namespace SecurityManagerTest
+
+#endif // SECURITY_MANAGER_TEST_INSTALLREQUEST