1 /*
2 * Copyright (c) 2023 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 "utd_cfgs_checker.h"
17
18 #include <vector>
19 #include <regex>
20 #include "utd_graph.h"
21 #include "logger.h"
22
23 namespace OHOS {
24 namespace UDMF {
25 constexpr const char *TYPE_ID_REGEX = "[a-zA-Z0-9/.-]+$";
26 constexpr const char FILE_EXTENSION_PREFIX = '.';
27 constexpr const int32_t MAX_UTD_SIZE = 50;
28
UtdCfgsChecker()29 UtdCfgsChecker::UtdCfgsChecker()
30 {
31 }
32
~UtdCfgsChecker()33 UtdCfgsChecker::~UtdCfgsChecker()
34 {
35 }
36
GetInstance()37 UtdCfgsChecker &UtdCfgsChecker::GetInstance()
38 {
39 static auto instance = new UtdCfgsChecker();
40 return *instance;
41 }
42
CheckTypeDescriptors(CustomUtdCfgs & typeCfgs,const std::vector<TypeDescriptorCfg> & presetCfgs,const std::vector<TypeDescriptorCfg> & customCfgs,const std::string & bundleName)43 bool UtdCfgsChecker::CheckTypeDescriptors(CustomUtdCfgs &typeCfgs, const std::vector<TypeDescriptorCfg> &presetCfgs,
44 const std::vector<TypeDescriptorCfg> &customCfgs, const std::string &bundleName)
45 {
46 if (!CheckTypesFormat(typeCfgs, bundleName)) {
47 LOG_ERROR(UDMF_CLIENT, "CheckTypesFormat not pass, bundleName: %{public}s.", bundleName.c_str());
48 return false;
49 }
50 if (!CheckTypesRelation(typeCfgs, presetCfgs, customCfgs)) {
51 LOG_ERROR(UDMF_CLIENT, "CheckTypesRelation not pass, bundleName: %{public}s.", bundleName.c_str());
52 return false;
53 }
54 return true;
55 }
56
CheckTypesFormat(CustomUtdCfgs & typeCfgs,const std::string & bundleName)57 bool UtdCfgsChecker::CheckTypesFormat(CustomUtdCfgs &typeCfgs, const std::string &bundleName)
58 {
59 for (auto declarationType: typeCfgs.first) {
60 if (!std::regex_match(declarationType.typeId, std::regex(bundleName + TYPE_ID_REGEX))) {
61 LOG_ERROR(UDMF_CLIENT, "Declaration typeId check failed, id: %{public}s, bundleName: %{public}s.",
62 declarationType.typeId.c_str(), bundleName.c_str());
63 return false;
64 }
65 }
66 for (auto referenceTypes: typeCfgs.second) {
67 if (!std::regex_match(referenceTypes.typeId, std::regex(TYPE_ID_REGEX))) {
68 LOG_ERROR(UDMF_CLIENT, "Reference typeId check failed, id: %{public}s, bundleName: %{public}s.",
69 referenceTypes.typeId.c_str(), bundleName.c_str());
70 return false;
71 }
72 }
73 std::vector<TypeDescriptorCfg> inputTypeCfgs;
74 if (!typeCfgs.first.empty()) {
75 inputTypeCfgs.insert(inputTypeCfgs.end(), typeCfgs.first.begin(), typeCfgs.first.end());
76 }
77 if (!typeCfgs.second.empty()) {
78 inputTypeCfgs.insert(inputTypeCfgs.end(), typeCfgs.second.begin(), typeCfgs.second.end());
79 }
80 for (TypeDescriptorCfg &typeCfg : inputTypeCfgs) {
81 for (std::string filenames : typeCfg.filenameExtensions) {
82 if (!(filenames[0] == FILE_EXTENSION_PREFIX)) {
83 LOG_ERROR(UDMF_CLIENT, "Extension not valid, extension: %{public}s, bundleName: %{public}s.",
84 filenames.c_str(), bundleName.c_str());
85 return false;
86 }
87 }
88 if (typeCfg.belongingToTypes.empty()) {
89 LOG_ERROR(UDMF_CLIENT, "BelongingToTypes can not be empty, bundleName: %{public}s.", bundleName.c_str());
90 return false;
91 }
92 for (std::string mimeType : typeCfg.mimeTypes) {
93 if (mimeType.empty()) {
94 LOG_ERROR(UDMF_CLIENT, "mimeType can not be an empty string, typeId: %{public}s.",
95 typeCfg.typeId.c_str());
96 return false;
97 }
98 }
99 }
100 return true;
101 }
102
CheckTypesRelation(CustomUtdCfgs & typeCfgs,const std::vector<TypeDescriptorCfg> & presetCfgs,const std::vector<TypeDescriptorCfg> & customCfgs)103 bool UtdCfgsChecker::CheckTypesRelation(CustomUtdCfgs &typeCfgs, const std::vector<TypeDescriptorCfg> &presetCfgs,
104 const std::vector<TypeDescriptorCfg> &customCfgs)
105 {
106 std::vector<TypeDescriptorCfg> inputTypeCfgs;
107 if (!typeCfgs.first.empty()) {
108 inputTypeCfgs.insert(inputTypeCfgs.end(), typeCfgs.first.begin(), typeCfgs.first.end());
109 }
110 if (!typeCfgs.second.empty()) {
111 inputTypeCfgs.insert(inputTypeCfgs.end(), typeCfgs.second.begin(), typeCfgs.second.end());
112 }
113 std::vector<std::string> typeIds;
114 for (auto &inputTypeCfg: inputTypeCfgs) {
115 typeIds.push_back(inputTypeCfg.typeId);
116 }
117 if (typeIds.size() > MAX_UTD_SIZE) {
118 LOG_ERROR(UDMF_CLIENT, "Create more UTDs than limit.");
119 return false;
120 }
121 for (auto &presetCfg: presetCfgs) {
122 typeIds.push_back(presetCfg.typeId);
123 }
124 if (std::set<std::string>(typeIds.begin(), typeIds.end()).size() != typeIds.size()) {
125 LOG_ERROR(UDMF_CLIENT, "Find duplicated typeIds.");
126 return false;
127 }
128 if (!CheckBelongingToTypes(inputTypeCfgs, presetCfgs)) {
129 LOG_ERROR(UDMF_CLIENT, "BelongingToType check failed.");
130 return false;
131 }
132 if (!CanConstructDAG(typeCfgs, presetCfgs, customCfgs)) {
133 LOG_ERROR(UDMF_CLIENT, "Can not construct DAG.");
134 return false;
135 }
136 return true;
137 }
138
CheckBelongingToTypes(const std::vector<TypeDescriptorCfg> & typeCfgs,const std::vector<TypeDescriptorCfg> & presetCfgs)139 bool UtdCfgsChecker::CheckBelongingToTypes(const std::vector<TypeDescriptorCfg> &typeCfgs,
140 const std::vector<TypeDescriptorCfg> &presetCfgs)
141 {
142 std::vector<std::string> typeIds;
143 for (auto &typeCfg: typeCfgs) {
144 typeIds.push_back(typeCfg.typeId);
145 }
146 for (auto &presetCfg: presetCfgs) {
147 typeIds.push_back(presetCfg.typeId);
148 }
149 for (auto &inputCfg : typeCfgs) {
150 for (std::string belongingToType : inputCfg.belongingToTypes) {
151 if (belongingToType.empty()) {
152 LOG_ERROR(UDMF_CLIENT, "BelongingToType can not be an empty string, typeId: %{public}s.",
153 inputCfg.typeId.c_str());
154 return false;
155 }
156 if (inputCfg.typeId == belongingToType) {
157 LOG_ERROR(UDMF_CLIENT, "TypeId cannot equals belongingToType, typeId: %{public}s.",
158 inputCfg.typeId.c_str());
159 return false;
160 }
161 if (find(typeIds.begin(), typeIds.end(), belongingToType) == typeIds.end()) {
162 LOG_ERROR(UDMF_CLIENT, "BelongingToType can not find in typeids, belongingToType: %{public}s.",
163 belongingToType.c_str());
164 return false;
165 }
166 }
167 }
168 return true;
169 }
170
CanConstructDAG(CustomUtdCfgs & typeCfgs,const std::vector<TypeDescriptorCfg> & presetCfgs,const std::vector<TypeDescriptorCfg> & customCfgs)171 bool UtdCfgsChecker::CanConstructDAG(CustomUtdCfgs &typeCfgs, const std::vector<TypeDescriptorCfg> &presetCfgs,
172 const std::vector<TypeDescriptorCfg> &customCfgs)
173 {
174 std::vector<TypeDescriptorCfg> allTypeCfgs;
175 if (!customCfgs.empty()) {
176 allTypeCfgs.insert(allTypeCfgs.end(), customCfgs.begin(), customCfgs.end());
177 }
178 for (TypeDescriptorCfg &declarationType : typeCfgs.first) {
179 for (auto iter = allTypeCfgs.begin(); iter != allTypeCfgs.end();) {
180 if (iter->typeId == declarationType.typeId) {
181 iter = allTypeCfgs.erase(iter);
182 } else {
183 iter ++;
184 }
185 }
186 allTypeCfgs.push_back(declarationType);
187 }
188 for (TypeDescriptorCfg &referenceTypes : typeCfgs.second) {
189 bool found = false;
190 for (auto &typeCfg : allTypeCfgs) {
191 if (typeCfg.typeId == referenceTypes.typeId) {
192 found = true;
193 break;
194 }
195 }
196 if (!found) {
197 allTypeCfgs.push_back(referenceTypes);
198 }
199 }
200 if (!presetCfgs.empty()) {
201 allTypeCfgs.insert(allTypeCfgs.end(), presetCfgs.begin(), presetCfgs.end());
202 }
203 if (!allTypeCfgs.empty()) {
204 UtdGraph::GetInstance().InitUtdGraph(allTypeCfgs);
205 if (UtdGraph::GetInstance().IsDAG()) {
206 return true;
207 }
208 }
209 return false;
210 }
211 } // namespace UDMF
212 } // namespace OHOS
213