2 * Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @author Marcin Lis (m.lis@samsung.com)
21 * @brief security-manager tests database record check functions
24 #include <tests_common.h>
25 #include <tzplatform_config.h>
28 #include "db_sqlite.h"
30 /* Keep this consistent with the database file path used in the security-manager */
31 const char *const PRIVILEGE_DB_PATH = tzplatform_mkpath(TZ_SYS_DB, ".security-manager.db");
33 /* Initialize static constants */
34 const bool TestSecurityManagerDatabase::NOT_REMOVED = false;
35 const bool TestSecurityManagerDatabase::REMOVED = true;
37 TestSecurityManagerDatabase::TestSecurityManagerDatabase() : m_base(PRIVILEGE_DB_PATH, SQLITE_OPEN_READWRITE)
41 void TestSecurityManagerDatabase::test_db_after__app_install(const std::string &app_name,
42 const std::string &pkg_name)
44 if (!m_base.is_open())
47 RUNNER_ASSERT_MSG(!app_name.empty(), "Request is corrupted, appId is empty");
48 RUNNER_ASSERT_MSG(!pkg_name.empty(), "Request is corrupted, pkgId is empty");
50 check_app_and_pkg(app_name, pkg_name, NOT_REMOVED);
53 void TestSecurityManagerDatabase::test_db_after__app_uninstall(const std::string &app_name,
54 const std::string &pkg_name,
55 const bool is_pkg_removed)
57 if (!m_base.is_open())
60 RUNNER_ASSERT_MSG(!app_name.empty(), "Request is corrupted, appId is empty");
61 RUNNER_ASSERT_MSG(!pkg_name.empty(), "Request is corrupted, pkgId is empty");
63 check_app_and_pkg(app_name, pkg_name, REMOVED);
64 check_pkg(pkg_name, is_pkg_removed);
67 void TestSecurityManagerDatabase::check_app_and_pkg(const std::string &app_name, const std::string &pkg_name,
68 const bool is_app_removed)
70 Sqlite3DBaseSelectResult result;
71 std::ostringstream sql;
72 sql << "SELECT app_name, pkg_name FROM user_app_pkg_view"
73 " WHERE app_name == '" << app_name << "' "
74 " AND pkg_name == '" << pkg_name << "' ;";
75 m_base.execute(sql.str(), result);
77 if (is_app_removed) /* expect 0 results */
78 RUNNER_ASSERT_MSG(result.rows.size() == 0, "query : <" << sql.str() <<
79 "> returned [" << result.rows.size() << "] rows, expected [0]");
80 else /* expect exactly 1 result with 2 columns */
81 RUNNER_ASSERT_MSG(result.rows.size() == 1 && result.rows[0].size() == 2, "query : <" <<
82 sql.str() << "> returned [" << result.rows.size() << "] rows, expected [1]");
85 void TestSecurityManagerDatabase::check_pkg(const std::string &pkg_name,
86 const bool is_pkg_removed)
88 const unsigned expected_rows = is_pkg_removed ? 0 : 1;
89 Sqlite3DBaseSelectResult result;
90 std::ostringstream sql;
91 sql << "SELECT pkg_id FROM pkg"
92 " WHERE name == '" << pkg_name << "' ;";
93 m_base.execute(sql.str(), result);
95 RUNNER_ASSERT_MSG(result.rows.size() == expected_rows, "query : <" <<
96 sql.str() << "> returned [" << result.rows.size() << "] rows, expected [" <<
97 expected_rows << "] rows");
100 void TestSecurityManagerDatabase::setup_privilege_groups(const std::string &privilege,
101 const std::vector<std::string> &groups)
103 Sqlite3DBaseSelectResult result;
104 std::ostringstream sql;
106 if (!m_base.is_open())
109 for (const auto &group : groups) {
112 sql << "INSERT OR IGNORE INTO privilege_group (privilege_name, group_name) "
114 << "'" << privilege << "'" << ","
115 << "'" << group << "'" << ")";
116 m_base.execute(sql.str(), result);
120 int64_t TestSecurityManagerDatabase::get_author_id(const std::string &authorName)
122 Sqlite3DBaseSelectResult result;
123 std::ostringstream sql;
125 if (!m_base.is_open())
129 sql.str("SELECT author_id FROM author where name=\"" + authorName + "\"");
130 m_base.execute(sql.str(), result);
132 if(result.rows.empty())
135 std::istringstream os(result.rows[0][0]);
141 std::string TestSecurityManagerDatabase::get_path_label(const std::string &path)
143 Sqlite3DBaseSelectResult result;
144 std::ostringstream sql;
145 if (!m_base.is_open())
148 sql.str("SELECT path_label FROM shared_path WHERE path=\"" + path + "\"");
149 m_base.execute(sql.str(), result);
151 if(result.rows.empty())
154 return result.rows[0][0];