• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "data_validator.h"
17 #include "access_token.h"
18 
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
IsBundleNameValid(const std::string & bundleName)22 bool DataValidator::IsBundleNameValid(const std::string& bundleName)
23 {
24     return !bundleName.empty() && (bundleName.length() <= MAX_LENGTH);
25 }
26 
IsLabelValid(const std::string & label)27 bool DataValidator::IsLabelValid(const std::string& label)
28 {
29     return label.length() <= MAX_LENGTH;
30 }
31 
IsDescValid(const std::string & desc)32 bool DataValidator::IsDescValid(const std::string& desc)
33 {
34     return desc.length() <= MAX_LENGTH;
35 }
36 
IsPermissionNameValid(const std::string & permissionName)37 bool DataValidator::IsPermissionNameValid(const std::string& permissionName)
38 {
39     return !permissionName.empty() && (permissionName.length() <= MAX_LENGTH);
40 }
41 
IsUserIdValid(const int userId)42 bool DataValidator::IsUserIdValid(const int userId)
43 {
44     return userId >= 0;
45 }
46 
IsAppIDDescValid(const std::string & appIDDesc)47 bool DataValidator::IsAppIDDescValid(const std::string& appIDDesc)
48 {
49     return !appIDDesc.empty() && (appIDDesc.length() <= MAX_APPIDDESC_LENGTH);
50 }
51 
IsDomainValid(const std::string & domain)52 bool DataValidator::IsDomainValid(const std::string& domain)
53 {
54     return !domain.empty() && (domain.length() <= MAX_LENGTH);
55 }
56 
IsAplNumValid(const int apl)57 bool DataValidator::IsAplNumValid(const int apl)
58 {
59     return (apl == APL_NORMAL || apl == APL_SYSTEM_BASIC || apl == APL_SYSTEM_CORE);
60 }
61 
IsProcessNameValid(const std::string & processName)62 bool DataValidator::IsProcessNameValid(const std::string& processName)
63 {
64     return !processName.empty() && (processName.length() <= MAX_LENGTH);
65 }
66 
IsDeviceIdValid(const std::string & deviceId)67 bool DataValidator::IsDeviceIdValid(const std::string& deviceId)
68 {
69     return !deviceId.empty() && (deviceId.length() <= MAX_LENGTH);
70 }
71 
IsDcapValid(const std::string & dcap)72 bool DataValidator::IsDcapValid(const std::string& dcap)
73 {
74     return !dcap.empty() && (dcap.length() <= MAX_DCAP_LENGTH);
75 }
76 
IsPermissionFlagValid(int flag)77 bool DataValidator::IsPermissionFlagValid(int flag)
78 {
79     uint32_t unmaskedFlag =
80         static_cast<uint32_t>(flag) & (~PermissionFlag::PERMISSION_GRANTED_BY_POLICY);
81     return unmaskedFlag == PermissionFlag::PERMISSION_DEFAULT_FLAG ||
82         unmaskedFlag == PermissionFlag::PERMISSION_USER_SET ||
83         unmaskedFlag == PermissionFlag::PERMISSION_USER_FIXED ||
84         unmaskedFlag == PermissionFlag::PERMISSION_SYSTEM_FIXED ||
85         unmaskedFlag == PermissionFlag::PERMISSION_COMPONENT_SET ||
86         unmaskedFlag == PermissionFlag::PERMISSION_POLICY_FIXED;
87 }
88 
IsTokenIDValid(AccessTokenID id)89 bool DataValidator::IsTokenIDValid(AccessTokenID id)
90 {
91     return id != 0;
92 }
93 
IsDlpTypeValid(int dlpType)94 bool DataValidator::IsDlpTypeValid(int dlpType)
95 {
96     return ((dlpType == DLP_COMMON) || (dlpType == DLP_READ) || (dlpType == DLP_FULL_CONTROL));
97 }
98 } // namespace AccessToken
99 } // namespace Security
100 } // namespace OHOS
101