Replace magic policy level strings with constexpr
[platform/core/test/security-tests.git] / src / common / sm_policy_request.h
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 #ifndef SECURITY_MANAGER_TEST_POLICYREQUEST
18 #define SECURITY_MANAGER_TEST_POLICYREQUEST
19
20 #include <iostream>
21 #include <sys/types.h>
22 #include <utility>
23 #include <vector>
24
25 #include <security-manager.h>
26
27 namespace SecurityManagerTest {
28
29 class PolicyEntry
30 {
31 public:
32     static constexpr char LEVEL_ALLOW[] = "Allow";
33     static constexpr char LEVEL_DENY[] = "Deny";
34     static constexpr char LEVEL_ASK_USER[] = "Ask user";
35
36     PolicyEntry();
37
38     PolicyEntry(const std::string &appId,
39                 const std::string &user,
40                 const std::string &privilege
41                 );
42     ~PolicyEntry();
43
44     PolicyEntry(policy_entry &entry);
45
46     policy_entry *get() const { return m_entry; }
47     std::string getUser() const         { return m_user.second; }
48     std::string getAppId() const        { return m_appId.second; }
49     std::string getPrivilege() const    { return m_privilege.second; }
50     std::string getCurrentLevel() const { return m_currentLevel.second; }
51     std::string getMaxLevel() const     { return m_maxLevel.second; }
52     void setLevel(const std::string &level);
53     void setMaxLevel(const std::string &level);
54     void free(void);
55
56     friend std::ostream& operator<<(std::ostream &, const PolicyEntry&);
57     bool operator==(const PolicyEntry &) const;
58     std::string toString() const;
59
60 private:
61     policy_entry *m_entry;
62     std::pair<bool, std::string> m_appId;
63     std::pair<bool, std::string> m_user;
64     std::pair<bool, std::string> m_privilege;
65     std::pair<bool, std::string> m_currentLevel;
66     std::pair<bool, std::string> m_maxLevel;
67 };
68
69 std::ostream& operator<<(std::ostream &os, const SecurityManagerTest::PolicyEntry &request);
70
71 class PolicyRequest
72 {
73 public:
74     PolicyRequest();
75     PolicyRequest(const PolicyRequest&) = delete;
76     PolicyRequest& operator=(const PolicyRequest&) = delete;
77     ~PolicyRequest();
78
79     void addEntry(PolicyEntry &entry, lib_retcode expectedResult = SECURITY_MANAGER_SUCCESS);
80
81     policy_update_req *get() const { return m_req; }
82     friend std::ostream& operator<<(std::ostream &, const PolicyRequest&);
83
84 private:
85     policy_update_req *m_req;
86     std::vector<PolicyEntry> m_entries;
87 };
88
89 std::ostream& operator<<(std::ostream &os, const SecurityManagerTest::PolicyRequest &request);
90
91 } // namespace SecurityManagerTest
92
93 namespace std {
94
95 template<>
96 struct hash<SecurityManagerTest::PolicyEntry> {
97     size_t operator()(const SecurityManagerTest::PolicyEntry &x) const { return hash<string>()(x.toString()); }
98 };
99
100 } // namespace std
101
102 #endif // SECURITY_MANAGER_TEST_USERREQUEST