Unify privilege representation
[platform/core/test/security-tests.git] / src / common / app_def_privilege.h
1 /*
2  * Copyright (c) 2014-2020 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 #pragma once
17
18 #include <string>
19 #include <utility>
20 #include <vector>
21 #include <tuple>
22
23 #include <security-manager-types.h>
24 #include <dpl/test/test_runner.h>
25
26 class Privilege {
27 public:
28     enum Type {
29         UNSET,
30         UNTRUSTED,
31         LICENSED,
32         PRIVACY,
33     };
34
35     Privilege(const char* systemPrivilege, Type type = UNSET, std::string license = std::string())
36       : m_name(systemPrivilege)
37       , m_type(type)
38       , m_license(std::move(license))
39     {}
40
41     Privilege(std::string systemPrivilege, Type type = UNSET, std::string license = std::string())
42       : m_name(std::move(systemPrivilege))
43       , m_type(type)
44       , m_license(std::move(license))
45     {}
46
47     Privilege(std::string licensedPrivilege, std::string license)
48       : m_name(std::move(licensedPrivilege))
49       , m_type(LICENSED)
50       , m_license(std::move(license))
51     {}
52
53     bool isUnset() const { return m_type == UNSET; }
54     bool isUntrusted() const { return m_type == UNTRUSTED; }
55     bool isLicensed() const { return m_type == LICENSED; }
56     bool isPrivacy() const { return m_type == PRIVACY; }
57
58     int getType() const { return m_type; }
59     app_defined_privilege_type getSMType () const {
60         RUNNER_ASSERT(m_type != UNSET);
61         if (isLicensed()) return SM_APP_DEFINED_PRIVILEGE_TYPE_LICENSED;
62         return SM_APP_DEFINED_PRIVILEGE_TYPE_UNTRUSTED;
63     }
64
65     const std::string& getName() const { return m_name; }
66     const std::string& getLicense() const { return m_license; }
67
68     const char *getNameC() const { return m_name.c_str(); }
69     const char *getLicenseC() const { return m_license.c_str(); }
70
71     operator const char *() const { return m_name.c_str(); }
72
73     operator std::string() const { return m_name; }
74
75     bool operator==(const std::string& other) const { return m_name == other; }
76
77 private:
78     std::string m_name;
79     Type m_type;
80     std::string m_license;
81 };
82
83 typedef std::vector<Privilege> PrivilegeVector;
84