6fc59ee016b96b6f30f4bed948df309b8fe8438e
[platform/core/test/security-tests.git] / tests / security-manager-tests / common / sm_api.cpp
1 /*
2  * Copyright (c) 2014-2015 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 #include <sm_api.h>
18
19 #include <dpl/test/test_runner.h>
20
21 namespace SecurityManagerTest {
22
23 namespace Api {
24
25 void install(const InstallRequest &request, lib_retcode expectedResult)
26 {
27     int result = security_manager_app_install(request.get());
28     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
29                       "installing app returned wrong value."
30                           << " InstallRequest: [ " << request << "];"
31                           << " Result: " << result << ";"
32                           << " Expected result: " << expectedResult);
33 }
34
35 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
36 {
37     int result = security_manager_app_uninstall(request.get());
38     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
39                       "uninstalling app returned wrong value."
40                           << " InstallRequest: [ " << request << "];"
41                           << " Result: " << result << ";"
42                           << " Expected result: " << expectedResult);
43 }
44
45 std::string getPkgId(const char *appId, lib_retcode expectedResult)
46 {
47     char *pkgId = nullptr;
48     int result = security_manager_get_app_pkgid(&pkgId, appId);
49     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
50                       "getting pkg id from app id returned wrong value."
51                           << " App id: " << appId << ";"
52                           << " Result: " << result << ";"
53                           << " Expected result: " << expectedResult);
54     if (expectedResult != SECURITY_MANAGER_SUCCESS)
55         return std::string();
56
57     RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
58     std::string str(pkgId);
59     free(pkgId);
60     return str;
61 }
62
63 void setProcessLabel(const char *appId, lib_retcode expectedResult)
64 {
65     int result = security_manager_set_process_label_from_appid(appId);
66     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
67                       "setting process label from app id returned wrong value."
68                           << " App id: " << appId << ";"
69                           << " Result: " << result << ";"
70                           << " Expected result: " << expectedResult);
71 }
72
73 void setProcessGroups(const char *appId, lib_retcode expectedResult)
74 {
75     int result = security_manager_set_process_groups_from_appid(appId);
76     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
77                       "setting process groups from app id returned wrong value."
78                           << " App id: " << appId << ";"
79                           << " Result: " << result << ";"
80                           << " Expected result: " << expectedResult);
81 }
82
83 void dropProcessPrivileges(lib_retcode expectedResult)
84 {
85     int result = security_manager_drop_process_privileges();
86     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
87                       "dropping process privileges returned wrong value."
88                           << " Result: " << result << ";"
89                           << " Expected result: " << expectedResult);
90 }
91
92 void prepareApp(const char *appId, lib_retcode expectedResult)
93 {
94     int result = security_manager_prepare_app(appId);
95     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
96                       "preparing app returned wrong value."
97                           << " App id: " << appId << ";"
98                           << " Result: " << result << ";"
99                           << " Expected result: " << expectedResult);
100 }
101
102 void addUser(const UserRequest &request, lib_retcode expectedResult)
103 {
104     int result = security_manager_user_add(request.get());
105     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
106                       "adding user returned wrong value."
107                           << " UserRequest: [ " << request << "];"
108                           << " Result: " << result << ";"
109                           << " Expected result: " << expectedResult);
110 }
111
112 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
113 {
114     int result = security_manager_user_delete(request.get());
115     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
116                       "deleting user returned wrong value."
117                           << " UserRequest: [ " << request << "];"
118                           << " Result: " << result << ";"
119                           << " Expected result: " << expectedResult);
120 }
121
122 } // namespace Api
123
124 } // namespace SecurityManagerTest