Merge branch 'tizen' into security-manager
[platform/core/test/security-tests.git] / src / security-manager-tests / common / sm_user_request.cpp
1 /*
2  * Copyright (c) 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_user_request.h>
18
19 #include <dpl/test/test_runner.h>
20
21 namespace SecurityManagerTest {
22
23 UserRequest::UserRequest()
24     : m_req(nullptr)
25     , m_uid(false, 0)
26     , m_utype(false, static_cast<security_manager_user_type>(0))
27 {
28     int result = security_manager_user_req_new(&m_req);
29     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
30                       "creation of new request failed. Result: " << result);
31     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
32 }
33
34 UserRequest::~UserRequest()
35 {
36     security_manager_user_req_free(m_req);
37 }
38
39 void UserRequest::setUid(const uid_t uid, lib_retcode expectedResult)
40 {
41     int result = security_manager_user_req_set_uid(m_req, uid);
42     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
43                      "setting uid returned wrong value."
44                           << " Uid: " << uid << ";"
45                           << " Result: " << result << ";"
46                           << " Expected result: " << expectedResult);
47     m_uid.first = true;
48     m_uid.second = uid;
49 }
50
51 void UserRequest::setUserType(const security_manager_user_type utype, lib_retcode expectedResult)
52 {
53     int result = security_manager_user_req_set_user_type(m_req, utype);
54     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
55                      "setting user type returned wrong value."
56                           << " User type: " << utype << ";"
57                           << " Result: " << result << ";"
58                           << " Expected result: " << expectedResult);
59     m_utype.first = true;
60     m_utype.second = utype;
61 }
62
63 std::ostream& operator<<(std::ostream &os, const UserRequest &request)
64 {
65     if (request.m_uid.first)
66         os << "uid: " << request.m_uid.second << "; ";
67
68     if (request.m_utype.first)
69         os << "utype: " << request.m_utype.second << "; ";
70
71     return os;
72 }
73
74 } // namespace SecurityManagerTest