Fix thread credential synchronization tests
[platform/core/test/security-tests.git] / src / security-manager-tests / common / sm_db.cpp
1 /*
2  * Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15 */
16
17 /*
18  * @file        sm_db.cpp
19  * @author      Marcin Lis (m.lis@samsung.com)
20  * @version     1.0
21  * @brief       security-manager tests database record check functions
22  */
23
24 #include <tests_common.h>
25 #include <tzplatform_config.h>
26 #include <sstream>
27 #include "sm_db.h"
28 #include "db_sqlite.h"
29
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");
32
33 /* Initialize static constants */
34 const bool TestSecurityManagerDatabase::NOT_REMOVED = false;
35 const bool TestSecurityManagerDatabase::REMOVED     = true;
36
37 TestSecurityManagerDatabase::TestSecurityManagerDatabase() : m_base(PRIVILEGE_DB_PATH, SQLITE_OPEN_READWRITE)
38 {
39 }
40
41 void TestSecurityManagerDatabase::test_db_after__app_install(const std::string &app_name,
42                                                              const std::string &pkg_name)
43 {
44     if (!m_base.is_open())
45         m_base.open();
46
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");
49
50     check_app_and_pkg(app_name, pkg_name, NOT_REMOVED);
51 }
52
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)
56 {
57     if (!m_base.is_open())
58         m_base.open();
59
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");
62
63     check_app_and_pkg(app_name, pkg_name, REMOVED);
64     check_pkg(pkg_name, is_pkg_removed);
65 }
66
67 void TestSecurityManagerDatabase::check_app_and_pkg(const std::string &app_name, const std::string &pkg_name,
68                                                     const bool is_app_removed)
69 {
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);
76
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]");
83 }
84
85 void TestSecurityManagerDatabase::check_pkg(const std::string &pkg_name,
86                                             const bool is_pkg_removed)
87 {
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);
94
95     RUNNER_ASSERT_MSG(result.rows.size() == expected_rows, "query : <" <<
96                          sql.str() << "> returned [" << result.rows.size() << "] rows, expected [" <<
97                          expected_rows << "] rows");
98 }
99
100 void TestSecurityManagerDatabase::setup_privilege_groups(const std::string &privilege,
101                                                          const std::vector<std::string> &groups)
102 {
103     Sqlite3DBaseSelectResult result;
104     std::ostringstream sql;
105
106     if (!m_base.is_open())
107         m_base.open();
108
109     for (const auto &group : groups) {
110         sql.clear();
111         sql.str("");
112         sql << "INSERT OR IGNORE INTO privilege_group (privilege_name, group_name) "
113                "VALUES ("
114                 << "'" << privilege << "'" << ","
115                 << "'" << group << "'" << ")";
116         m_base.execute(sql.str(), result);
117     }
118 }
119
120 int64_t TestSecurityManagerDatabase::get_author_id(const std::string &authorName)
121 {
122     Sqlite3DBaseSelectResult result;
123     std::ostringstream sql;
124
125     if (!m_base.is_open())
126         m_base.open();
127
128     sql.clear();
129     sql.str("SELECT author_id FROM author where name=\"" + authorName + "\"");
130     m_base.execute(sql.str(), result);
131
132     if(result.rows.empty())
133         return 0;
134
135     std::istringstream os(result.rows[0][0]);
136     int64_t id;
137     os >> id;
138     return id;
139 }
140
141 std::string TestSecurityManagerDatabase::get_path_label(const std::string &path)
142 {
143     Sqlite3DBaseSelectResult result;
144     std::ostringstream sql;
145     if (!m_base.is_open())
146         m_base.open();
147     sql.clear();
148     sql.str("SELECT path_label FROM shared_path WHERE path=\"" + path + "\"");
149     m_base.execute(sql.str(), result);
150
151     if(result.rows.empty())
152         return "";
153
154     return result.rows[0][0];
155 }