+/*
+ * Copyright (c) 2012 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 libprivilege-control_test_db.cpp
+ * @author Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
+ * @version 1.0
+ * @brief libprivilege-control tests database record check functions
+ */
+
+#include <dpl/test/test_runner.h>
+#include <privilege-control.h>
+#include <sstream>
+#include <string.h>
+#include "db.h"
+#include "db_sqlite.h"
+#include "duplicates.h"
+
+const std::string DBASE_PATH = "/opt/dbspace/.rules-db.db3";
+const std::string ALL_APPS ="ALL_APPS";
+
+const int PERMISSION_VOLATILE = 1;
+const int PERMISSION_PERSISTENT = 0;
+
+const int PERMISSION_ENABLED = 1;
+const int PERMISSION_DISABLED = 0;
+
+using std::ostringstream;
+using std::string;
+
+TestLibPrivilegeControlDatabase::TestLibPrivilegeControlDatabase() : m_base(DBASE_PATH)
+{
+}
+
+void TestLibPrivilegeControlDatabase::test_db_after__perm_app_install(const Tracker& tracker,
+ const char* name)
+{
+ if(!m_base.is_open())
+ m_base.open(tracker);
+
+ app_label(tracker, name);
+ app_permission(tracker, name, ALL_APPS, ALL_APPS,
+ PERMISSION_PERSISTENT, PERMISSION_ENABLED);
+}
+
+void TestLibPrivilegeControlDatabase::test_db_after__perm_app_uninstall(const Tracker& tracker,
+ const char* name)
+{
+ if(!m_base.is_open())
+ m_base.open(tracker);
+
+ app_not_label(tracker, name);
+}
+
+void TestLibPrivilegeControlDatabase::test_db_after__perm_app_enable_permissions(
+ const Tracker& tracker, const char* name, app_type_t app_type, const char** perm_list,
+ bool persistent)
+{
+ if(!m_base.is_open())
+ m_base.open(tracker);
+
+ string permission_type_name = app_type_name(app_type);
+ string permission_group_type_name = app_type_group_name(app_type);
+ const int is_volatile = persistent ? PERMISSION_PERSISTENT : PERMISSION_VOLATILE;
+ string permission_name;
+ int ret;
+
+ app_permission(tracker, name, permission_type_name, permission_type_name, is_volatile,
+ PERMISSION_ENABLED);
+
+ int i;
+ for(i = 0; perm_list[i] != NULL; ++i) {
+ // Ignore empty lines
+ if(strspn(perm_list[i], " \t\n") == strlen(perm_list[i]))
+ continue;
+
+ ret = base_name_from_perm(perm_list[i], permission_name);
+ RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,
+ "DB: Error testing \"" << __FUNCTION__ <<
+ "\": permission : <" << perm_list[i] <<
+ "> cannot be converted to basename (iri parse error)");
+ app_permission(tracker, name, permission_name, permission_group_type_name,
+ is_volatile, PERMISSION_ENABLED);
+ }
+}
+
+void TestLibPrivilegeControlDatabase::app_label(const Tracker& tracker, const std::string& app_name)
+{
+ Sqlite3DBaseSelectResult result;
+ ostringstream sql;
+ sql << "SELECT app_id FROM app "
+ "NATURAL JOIN label "
+ "WHERE name == '" << app_name << "' ;";
+ m_base.execute(tracker, sql.str(), result);
+
+ RUNNER_ASSERT_MSG(result.rows.size() == 1 && result.rows[0].size() == 1,
+ "DB: Error testing \"" << tracker.str() << "\": querry : <" << sql.str() <<
+ "> returned [" << result.rows.size() << "] rows");
+}
+
+void TestLibPrivilegeControlDatabase::app_not_label(const Tracker& tracker,
+ const std::string& app_name)
+{
+ Sqlite3DBaseSelectResult result;
+ ostringstream sql;
+ sql << "SELECT label_id FROM label "
+ "WHERE name == '" << app_name << "' ;";
+ m_base.execute(tracker, sql.str(), result);
+
+ RUNNER_ASSERT_MSG(result.rows.size() == 0,
+ "DB: Error testing \"" << tracker.str() << "\": querry : <" << sql.str() <<
+ "> returned [" << result.rows.size() << "] rows");
+}
+
+void TestLibPrivilegeControlDatabase::app_permission(const Tracker& tracker,
+ const std::string& app_name, const std::string& permission_name,
+ const std::string& permission_type_name, int is_volatile, int is_enabled)
+{
+ Sqlite3DBaseSelectResult result;
+ ostringstream sql;
+ sql << "SELECT * FROM app_permission "
+ "INNER JOIN app USING(app_id) "
+ "INNER JOIN permission USING(permission_id) "
+ "INNER JOIN permission_type USING(permission_type_id)"
+ "INNER JOIN label USING(label_id)"
+ "WHERE "
+ "label.name == '" << app_name << "' "
+ "AND app_permission.is_enabled == " << is_enabled << " "
+ "AND app_permission.is_volatile == " << is_volatile << " "
+ "AND permission.name == '" << permission_name << "' "
+ "AND permission_type.type_name == '" << permission_type_name << "' "
+ ";";
+ m_base.execute(tracker, sql.str(), result);
+
+ RUNNER_ASSERT_MSG(result.rows.size() == 1,
+ "DB: Error testing \"" << tracker.str() << "\": querry : <" << sql.str() <<
+ "> returned [" << result.rows.size() << "] rows");
+}