Add tests for perm_app_get_paths().
authorDamian Chromejko <d.chromejko@samsung.com>
Thu, 28 Nov 2013 14:20:51 +0000 (15:20 +0100)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 23 Jan 2014 14:21:36 +0000 (15:21 +0100)
[Issue#]       SSDWSSP-670
[Feature/Bug]  New test cases for perm_app_get_paths().
[Cause]        Extension of libprivilege-control API
[Solution]     Added tests for new methods.
[Verification] Build, install and run libprivilege-control-test.

Change-Id: Ifc280e76022f8c5341fd4dd26a33024e7ad336c8

tests/libprivilege-control-tests/common/libprivilege-control_test_common.h
tests/libprivilege-control-tests/test_cases.cpp
tests/libprivilege-control-tests/test_cases_incorrect_params.cpp

index 58bf240..7e6c5a5 100644 (file)
@@ -121,6 +121,76 @@ struct free_deleter {
 };
 typedef std::unique_ptr<char, free_deleter> CStringPtr;
 
+struct list_deleter {
+    void operator()(void* p) {
+        char** list = (char**) p;
+
+        for (int i = 0; list[i] != NULL; ++i) {
+            free(list[i]);
+        }
+
+        free(p);
+    }
+};
+typedef std::unique_ptr<char*, list_deleter> CStringListPtr;
+
+class Directory
+{
+public:
+    Directory(std::string path, mode_t mode) : m_errorCode(0), m_path(path)
+    {
+        if (mkdir(path.c_str(), mode) != 0) {
+            m_errorCode = errno;
+        }
+    }
+
+    Directory(const Directory& directory) = delete;
+
+    Directory(Directory&& directory)
+            : m_errorCode(std::move(directory.m_errorCode)), m_path(std::move(directory.m_path))
+    {
+        directory.m_path = "";
+    }
+
+    const Directory& operator=(const Directory& directory) = delete;
+
+    const Directory& operator=(Directory&& directory)
+    {
+        m_errorCode = directory.m_errorCode;
+        m_path = std::move(directory.m_path);
+        directory.m_path = "";
+
+        return *this;
+    }
+
+    ~Directory()
+    {
+        if (m_errorCode == 0 && !m_path.empty()) {
+            rmdir(m_path.c_str());
+        }
+    }
+
+    bool isCreated() const
+    {
+        return m_errorCode == 0;
+    }
+
+    int errorCode() const
+    {
+        return m_errorCode;
+    }
+
+    const std::string& path() const
+    {
+        return m_path;
+    }
+
+private:
+    int m_errorCode;
+
+    std::string m_path;
+};
+
 // Rules from test_privilege_control_rules.smack
 const rules_t rules = {
     { APP_ID, "test_book_1", "r" },
index 13cee41..e0a0712 100644 (file)
@@ -1868,3 +1868,85 @@ RUNNER_TEST(privilege_control25_test_libprivilege_strerror) {
     RUNNER_ASSERT_MSG(strcmp(result, "Unknown error") == 0,
             "Bad message returned for invalid error code: \"" << result << "\"");
 }
+
+RUNNER_TEST(privilege_control28_perm_app_get_paths_empty)
+{
+    char **pp_paths = NULL;
+    int result;
+    CStringListPtr paths;
+
+    DB_BEGIN
+
+    result = perm_app_uninstall(APP_ID);
+    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
+            "perm_app_uninstall failed: " << perm_strerror(result));
+
+    result = perm_app_install(APP_ID);
+    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_install failed: " <<
+            perm_strerror(result));
+
+    DB_END
+
+    result = perm_app_get_paths(APP_ID, PERM_APP_PATH_PUBLIC, &pp_paths);
+    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_get_paths failed: " <<
+            perm_strerror(result));
+    paths.reset(pp_paths);
+
+    RUNNER_ASSERT_MSG(pp_paths != NULL,
+            "perm_app_get_paths failed to set pointer to cstring array");
+    RUNNER_ASSERT_MSG(*pp_paths == NULL, "perm_app_get_paths found paths when not supposed to");
+}
+
+RUNNER_TEST(privilege_control28_perm_app_get_paths)
+{
+    char **pp_paths = NULL;
+    int result;
+    size_t i;
+    size_t DIR_NUM = 3;
+    CStringListPtr paths;
+    std::vector<Directory> test_paths;
+
+    for (i = 0; i < DIR_NUM; ++i) {
+        test_paths.push_back(Directory("/tmp/dir" + std::to_string(i), 0));
+        RUNNER_ASSERT_MSG(test_paths[i].isCreated(), "failed to create a directory " <<
+                test_paths[i].path() << ": " << strerror(test_paths[i].errorCode()));
+    }
+
+    DB_BEGIN
+
+    result = perm_app_uninstall(APP_ID);
+    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_uninstall failed: " <<
+            perm_strerror(result));
+
+    result = perm_app_install(APP_ID);
+    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_install failed: " <<
+            perm_strerror(result));
+
+    for (auto itr = test_paths.begin(); itr != test_paths.end(); ++itr) {
+        result = perm_app_setup_path(APP_ID, itr->path().c_str(), PERM_APP_PATH_PUBLIC);
+        RUNNER_ASSERT_MSG(result == 0, "perm_app_setup_path failed: " << perm_strerror(result));
+    }
+
+    DB_END
+
+    result = perm_app_get_paths(APP_ID, PERM_APP_PATH_PUBLIC, &pp_paths);
+    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_get_paths failed: " <<
+            perm_strerror(result));
+    paths.reset(pp_paths);
+
+    for(i = 0; pp_paths[i] != NULL; ++i) {
+        RUNNER_ASSERT_MSG(i < test_paths.size(), "perm_app_get_paths returned too many paths");
+        RUNNER_ASSERT_MSG(test_paths[i].path() == pp_paths[i],
+                "perm_app_get_paths returned unexpected path, " << pp_paths[i] << " != " <<
+                test_paths[i].path());
+    }
+    RUNNER_ASSERT_MSG(i == test_paths.size(), "perm_app_get_paths returned too few paths");
+
+    DB_BEGIN
+
+    result = perm_app_uninstall(APP_ID);
+    RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "perm_app_uninstall failed: " <<
+            perm_strerror(result));
+
+    DB_END
+}
index b886be2..42a0026 100644 (file)
@@ -199,3 +199,22 @@ RUNNER_TEST(privilege_control21n_incorrect_params_perm_app_setup_permissions)
             "perm_app_setup_permissions didn't check if pkg_id is valid");
 }
 
+RUNNER_TEST(privilege_control22n_incorrect_params_perm_app_get_paths)
+{
+    char **pp_paths;
+
+    RUNNER_ASSERT_MSG(perm_app_get_paths(NULL, PERM_APP_PATH_PUBLIC,
+                                         &pp_paths) == PC_ERR_INVALID_PARAM,
+            "perm_app_get_paths didn't check if pkg_id isn't NULL.");
+
+    RUNNER_ASSERT_MSG(perm_app_get_paths(APP_ID, PERM_APP_PATH_PUBLIC,
+                                         NULL) == PC_ERR_INVALID_PARAM,
+            "perm_app_get_paths didn't check if ppp_paths isn't NULL.");
+
+    RUNNER_ASSERT_MSG(perm_app_get_paths(APP_ID, PERM_APP_PATH_PRIVATE,
+                                         &pp_paths) == PC_ERR_INVALID_PARAM,
+            "perm_app_get_paths shouldn't accept paths of type PERM_APP_PATH_PRIVATE");
+    RUNNER_ASSERT_MSG(perm_app_get_paths(APP_ID, PERM_APP_PATH_ANY_LABEL,
+                                         &pp_paths) == PC_ERR_INVALID_PARAM,
+            "perm_app_get_paths should not accept paths of type PERM_APP_PATH_ANY_LABEL");
+}