• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 package ohos;
17 
18 import com.alibaba.fastjson.JSONValidator;
19 
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileReader;
23 import java.io.InputStreamReader;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.nio.charset.StandardCharsets;
27 import java.util.ArrayList;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 import java.util.List;
33 import java.util.Optional;
34 
35 /**
36  * compress comment,command parser.
37  *
38  */
39 public class CompressVerify {
40     private static final String COMMA_SPLIT = ",";
41     private static final String JSON_PROFILE = "config.json";
42     private static final String MODULE_PROFILE = "module.json";
43     private static final String PATCH_PROFILE = "patch.json";
44     private static final String PKG_CONTEXT_INFO = "pkgContextInfo.json";
45     private static final String PROFILE_NAME = "CAPABILITY.profile";
46     private static final String INDEX_PROFILE = "resources.index";
47     private static final String RPCID_PROFILE = "rpcid.sc";
48     private static final String PACK_INFO = "pack.info";
49     private static final String PACK_RES = "pack.res";
50     private static final String HAP_SUFFIX = ".hap";
51     private static final String HAR_SUFFIX = ".har";
52     private static final String APP_SUFFIX = ".app";
53     private static final String APK_SUFFIX = ".apk";
54     private static final String DEX_SUFFIX = ".dex";
55     private static final String ABC_SUFFIX = ".abc";
56     private static final String SO_SUFFIX = ".so";
57     private static final String JAR_SUFFIX = ".jar";
58     private static final String TXT_SUFFIX = ".txt";
59     private static final String PNG_SUFFIX = ".png";
60     private static final String RES_SUFFIX = ".res";
61     private static final String HQF_SUFFIX = ".hqf";
62     private static final String APPQF_SUFFIX = ".appqf";
63     private static final String HSP_SUFFIX = ".hsp";
64     private static final String JSON_SUFFIX = ".json";
65     private static final String FALSE = "false";
66     private static final String ENTRY_CARD_DIRECTORY_NAME = "EntryCard";
67     private static final String VERSION_NAME_PATTERN = "^[0-9.]+|(?=.*[{])(?=.*[}])[0-9a-zA-Z_.{}]+$";
68     private static final String LINUX_FILE_SEPARATOR = "/";
69     private static final String BUNDLE_TYPE_SHARE = "shared";
70     private static final String BUNDLE_TYPE_APP = "app";
71     private static final String BUNDLE_TYPE_APP_SERVICE = "appService";
72     private static final String SKILLS_ENTITIES = "entities";
73     private static final String SKILLS_ACTIONS = "actions";
74     private static final String ACTION_SYSTEM_HOME = "action.system.home";
75     private static final String ENTITY_SYSTEM_HOME = "entity.system.home";
76     private static final String BUNDLE_NAME_PATTERN =
77             "([a-zA-Z]|[a-zA-Z]+(_*[0-9a-zA-Z])+)(\\.[0-9a-zA-Z]|\\.[0-9a-zA-Z]+(_*[0-9a-zA-Z])+){2,}";
78     private static final int BUNDLE_NAME_LEN_MIN = 7;
79     private static final int BUNDLE_NAME_LEN_MAX = 128;
80 
81     private static final Log LOG = new Log(CompressVerify.class.toString());
82 
83     private static final boolean TYPE_FILE = true;
84     private static final boolean TYPE_DIR = false;
85     private static final Integer ONE = 1;
86 
87     /**
88      * if args valid.
89      *
90      * @param utility common data
91      * @return commandVerify if command valid.
92      */
commandVerify(Utility utility)93     public static boolean commandVerify(Utility utility) {
94         if (utility == null) {
95             LOG.error("CompressVerify::commandVerify utility is null.");
96             return false;
97         }
98 
99         if (!utility.getForceRewrite().isEmpty() && !"true".equals(utility.getForceRewrite())
100                 && !"false".equals(utility.getForceRewrite())) {
101             LOG.error("CompressVerify::commandVerify forceRewrite is invalid.");
102             return false;
103         }
104         return commandPathVerify(utility);
105     }
106 
107     /**
108      * verify path.
109      *
110      * @param utility common data
111      * @return commandPathVerify if command valid.
112      */
commandPathVerify(Utility utility)113     private static boolean commandPathVerify(Utility utility) {
114         switch (utility.getMode()) {
115             case Utility.MODE_HAP:
116                 if (!utility.getBinPath().isEmpty() && utility.getJsonPath().isEmpty()) {
117                     return isOutPathValid(utility, HAP_SUFFIX);
118                 } else {
119                     return isVerifyValidInHapCommonMode(utility) && isVerifyValidInHapMode(utility);
120                 }
121             case Utility.MODE_HAR:
122                 return isVerifyValidInHarMode(utility);
123             case Utility.MODE_APP:
124                 return isVerifyValidInAppMode(utility);
125             case Utility.MODE_FAST_APP:
126                 return PackageUtil.isVerifyValidInFastAppMode(utility);
127             case Utility.MODE_RES:
128                 return isVerifyValidInResMode(utility);
129             case Utility.MODE_MULTI_APP:
130                 return isVerifyValidInMultiAppMode(utility);
131             case Utility.MODE_HQF:
132                 return isVerifyValidInHQFMode(utility);
133             case Utility.MODE_APPQF:
134                 return isVerifyValidInAPPQFMode(utility);
135             case Utility.MODE_HSP:
136                 return isVerifyValidInHspMode(utility);
137             case Utility.MODE_HAPADDITION:
138                 return isVerifyValidInHapAdditionMode(utility);
139             case Utility.VERSION_NORMALIZE:
140                 return validateVersionNormalizeMode(utility);
141             case Utility.PACKAGE_NORMALIZE:
142                 return validatePackageNormalizeMode(utility);
143             default:
144                 LOG.error("CompressVerify::commandVerify mode is invalid.");
145                 return false;
146         }
147     }
148 
isBundleNameValid(String bundleName)149     private static boolean isBundleNameValid(String bundleName) {
150         if (bundleName != null &&
151                 bundleName.length() >= BUNDLE_NAME_LEN_MIN &&
152                 bundleName.length() <= BUNDLE_NAME_LEN_MAX) {
153             Pattern pattern = Pattern.compile(BUNDLE_NAME_PATTERN);
154             return pattern.matcher(bundleName).matches();
155         }
156         return false;
157     }
158 
validatePackageNormalizeMode(Utility utility)159     private static boolean validatePackageNormalizeMode(Utility utility) {
160         if (utility.getHspList().isEmpty()) {
161             LOG.error("CompressVerify::validatePackageNormalizeMode hsp-list is empty.");
162             return false;
163         } else {
164             if (!compatibleProcess(utility, utility.getHspList(), utility.getFormattedHspPathList(), HSP_SUFFIX)) {
165                 LOG.error("CompressVerify::validatePackageNormalizeMode hsp-list is invalid.");
166                 return false;
167             }
168         }
169         if (!isBundleNameValid(utility.getBundleName())) {
170             LOG.error("CompressVerify::validatePackageNormalizeMode bundle-name is invalid.");
171             return false;
172         }
173         if (utility.getVersionCode() <= 0) {
174             LOG.error("CompressVerify::validatePackageNormalizeMode version-code is invalid.");
175             return false;
176         }
177         if (utility.getOutPath().isEmpty()) {
178             LOG.error("CompressVerify::validatePackageNormalizeMode out-path is empty.");
179             return false;
180         }
181         File outDir = new File(utility.getOutPath());
182         if (!outDir.isDirectory()) {
183             LOG.error("CompressVerify::validatePackageNormalizeMode out-path is not a directory.");
184             return false;
185         }
186         return true;
187     }
188 
validateVersionNormalizeMode(Utility utility)189     private static boolean validateVersionNormalizeMode(Utility utility) {
190         if (utility.getInputList().isEmpty()) {
191             LOG.error("CompressVerify::validateVersionNormalizeMode input-list is empty.");
192             return false;
193         }
194 
195         if (!handleHapAndHspInput(utility, utility.getInputList(), utility.getFormattedHapList())) {
196             LOG.error("CompressVerify::validateVersionNormalizeMode input-list is invalid.");
197             return false;
198         }
199 
200         if (utility.getFormattedHapList().isEmpty()) {
201             LOG.error("CompressVerify::validateVersionNormalizeMode input-list is empty.");
202             return false;
203         }
204 
205         if (utility.getVersionCode() <= 0) {
206             LOG.error("CompressVerify::validateVersionNormalizeMode version-code is invalid.");
207             return false;
208         }
209 
210         if (utility.getVersionName().isEmpty()) {
211             LOG.error("CompressVerify::validateVersionNormalizeMode version-name is empty.");
212             return false;
213         }
214 
215         Pattern versionNamePattern = Pattern.compile(VERSION_NAME_PATTERN);
216         Matcher versionNameMatcher = versionNamePattern.matcher(utility.getVersionName());
217         if (!versionNameMatcher.matches()) {
218             LOG.error("CompressVerify::validateVersionNormalizeMode version-name is not valid.");
219             return false;
220         }
221 
222         if (utility.getOutPath().isEmpty()) {
223             LOG.error("CompressVerify::validateVersionNormalizeMode out-path is empty.");
224             return false;
225         }
226 
227         File outDir = new File(utility.getOutPath());
228         if (!outDir.isDirectory()) {
229             LOG.error("CompressVerify::validateVersionNormalizeMode out-path is not a directory.");
230             return false;
231         }
232         return true;
233     }
234 
isValidRpcid(Utility utility)235     private static boolean isValidRpcid(Utility utility) {
236         if (!utility.getRpcidPath().isEmpty()) {
237             File file = new File(utility.getRpcidPath());
238             if (!file.isFile()) {
239                 LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path is not a file.");
240                 return false;
241             }
242             if (!RPCID_PROFILE.equals(file.getName())) {
243                 LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path must be rpcid.sc file.");
244                 return false;
245             }
246         }
247         return true;
248     }
249 
isValidPackInfo(Utility utility)250     private static boolean isValidPackInfo(Utility utility) {
251         if (!utility.getPackInfoPath().isEmpty()) {
252             File file = new File(utility.getPackInfoPath());
253             if (!file.isFile()) {
254                 LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path is not a file.");
255                 return false;
256             }
257             if (!PACK_INFO.equals(file.getName())) {
258                 LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path must be pack.info file.");
259                 return false;
260             }
261         }
262         return true;
263     }
264 
isVerifyValidInHapCommonMode(Utility utility)265     private static boolean isVerifyValidInHapCommonMode(Utility utility) {
266         if (utility.getJsonPath().isEmpty()) {
267             LOG.error("CompressVerify::commandPathVerify json-path is empty.");
268             return false;
269         }
270         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, JSON_PROFILE)
271                 && !isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) {
272             LOG.error("CompressVerify::isArgsValidInHarMode json-path must be config.json file.");
273             return false;
274         }
275 
276         if (!isValidRpcid(utility) || !isValidPackInfo(utility)) {
277             return false;
278         }
279 
280         if (!utility.getApkPath().isEmpty() && !compatibleProcess(utility, utility.getApkPath(),
281                 utility.getFormattedApkPathList(), APK_SUFFIX)) {
282             LOG.error("CompressVerify::isArgsValidInHapMode shell-apk-path is invalid.");
283             return false;
284         }
285 
286         if (!utility.getProfilePath().isEmpty()) {
287             File file = new File(utility.getProfilePath());
288             if (!file.isFile() || !PROFILE_NAME.equals(file.getName())) {
289                 LOG.error("CompressVerify::isArgsValidInHapMode profile-path must be CAPABILITY.profile file.");
290                 return false;
291             }
292         }
293 
294         if (!utility.getDexPath().isEmpty() && !compatibleProcess(utility, utility.getDexPath(),
295                 utility.getFormattedDexPathList(), DEX_SUFFIX)) {
296             LOG.error("CompressVerify::isArgsValidInHapMode dex-path is invalid.");
297             return false;
298         }
299 
300         if (!utility.getAbcPath().isEmpty() && !compatibleProcess(utility, utility.getAbcPath(),
301                 utility.getFormattedAbcPathList(), ABC_SUFFIX)) {
302             LOG.error("CompressVerify::isArgsValidInHapMode abc-path is invalid.");
303             return false;
304         }
305 
306         if (!utility.getDirList().isEmpty() && !splitDirList(utility, utility.getDirList(),
307                 utility.getFormatedDirList())) {
308             LOG.error("CompressVerify::isArgsValidInHapMode --dir-list is invalid.");
309             return false;
310         }
311         if (!utility.getPkgContextPath().isEmpty()) {
312             File file = new File(utility.getPkgContextPath());
313             if (!file.isFile() || !PKG_CONTEXT_INFO.equals(file.getName())) {
314                 LOG.error("CompressVerify::isArgsValidInHapMode --pkg-context-path file must be " + PKG_CONTEXT_INFO);
315                 return false;
316             }
317         }
318         return true;
319     }
320 
321     /**
322      * parse and check args if valid in hap mode.
323      *
324      * @param utility common data
325      * @return isVerifyValidInHapMode if verify valid in hap mode.
326      */
isVerifyValidInHapMode(Utility utility)327     private static boolean isVerifyValidInHapMode(Utility utility) {
328         File file = new File(utility.getIndexPath());
329         if (!utility.getIndexPath().isEmpty() && !file.isFile() && INDEX_PROFILE.equals(file.getName())) {
330             LOG.error("CompressVerify::isArgsValidInHapMode index-path must be resources.index file.");
331             return false;
332         }
333 
334         if (!utility.getSoPath().isEmpty() &&
335                 !compatibleProcess(utility, utility.getSoPath(), utility.getFormattedSoPathList(), SO_SUFFIX)) {
336             LOG.error("CompressVerify::isArgsValidInHapMode maple-so-path is invalid.");
337             return false;
338         }
339 
340         if (!utility.getAbilitySoPath().isEmpty() && !compatibleProcess(utility, utility.getAbilitySoPath(),
341                 utility.getFormattedAbilitySoPathList(), SO_SUFFIX)) {
342             LOG.error("CompressVerify::isArgsValidInHapMode ability-so-path is invalid.");
343             return false;
344         }
345 
346         if (isHapPathValid(utility.getSoDir())) {
347             LOG.error("CompressVerify::isArgsValidInHapMode maple-so-dir is invalid.");
348             return false;
349         }
350 
351         if (isHapPathValid(utility.getLibPath())) {
352             LOG.error("CompressVerify::isArgsValidInHapMode lib-path is invalid.");
353             return false;
354         }
355 
356         if (isHapPathValid(utility.getHnpPath())) {
357             LOG.error("CompressVerify::isArgsValidInHapMode hnp-path is invalid.");
358             return false;
359         }
360 
361         if (isHapPathValid(utility.getResPath())) {
362             LOG.error("CompressVerify::isArgsValidInHapMode res-path is invalid.");
363             return false;
364         }
365 
366         if (isHapPathValid(utility.getResourcesPath())) {
367             LOG.error("CompressVerify::isArgsValidInHapMode resources-path is invalid.");
368             return false;
369         }
370 
371         if (isHapPathValid(utility.getAssetsPath())) {
372             LOG.error("CompressVerify::isArgsValidInHapMode assets-path is invalid.");
373             return false;
374         }
375 
376         if (isHapPathValid(utility.getSharedLibsPath())) {
377             LOG.error("CompressVerify::isArgsValidInHapMode shared-libs-path is invalid.");
378             return false;
379         }
380 
381         if (!utility.getJarPath().isEmpty()
382                 && !compatibleProcess(utility, utility.getJarPath(), utility.getFormattedJarPathList(), JAR_SUFFIX)) {
383             LOG.error("CompressVerify::isArgsValidInHapMode jar-path is invalid.");
384             return false;
385         }
386 
387         if (!utility.getTxtPath().isEmpty()
388                 && !compatibleProcess(utility, utility.getTxtPath(), utility.getFormattedTxtPathList(), TXT_SUFFIX)) {
389             LOG.error("CompressVerify::isArgsValidInHapMode txt-path is invalid.");
390             return false;
391         }
392 
393         if (isHapPathValid(utility.getANPath())) {
394             LOG.error("CompressVerify::isArgsValidInHapMode an-path is invalid.");
395             return false;
396         }
397 
398         if (!utility.getEtsPath().isEmpty() && !isPathExists(utility.getEtsPath())) {
399             LOG.error("CompressVerify::isArgsValidInHapMode ets-path is invalid.");
400             return false;
401         }
402 
403         return isOutPathValid(utility, HAP_SUFFIX);
404     }
405 
406     /**
407      * check hap path if valid
408      *
409      * @param path path input
410      * @return isPathValid if path verify
411      */
isHapPathValid(String path)412     private static boolean isHapPathValid(String path) {
413         return (!path.isEmpty() && !isPathValid(path, TYPE_DIR, null));
414     }
415 
416     /**
417      * parse and check args if valid in har mode.
418      *
419      * @param utility common data
420      * @return isVerifyValidInHarMode if verify valid in har mode.
421      */
isVerifyValidInHarMode(Utility utility)422     private static boolean isVerifyValidInHarMode(Utility utility) {
423         if (utility.getJsonPath().isEmpty()) {
424             LOG.error("CompressVerify::isArgsValidInHarMode json-path is empty.");
425             return false;
426         }
427 
428         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, JSON_PROFILE)
429                 && !isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) {
430             LOG.error("CompressVerify::isArgsValidInHarMode json-path must be config.json file.");
431             return false;
432         }
433 
434         if (!utility.getJarPath().isEmpty()
435                 && !compatibleProcess(utility, utility.getJarPath(), utility.getFormattedJarPathList(), JAR_SUFFIX)) {
436             LOG.error("CompressVerify::isArgsValidInHarMode jar-path is invalid.");
437             return false;
438         }
439 
440         if (!utility.getTxtPath().isEmpty()
441                 && !compatibleProcess(utility, utility.getTxtPath(), utility.getFormattedTxtPathList(), TXT_SUFFIX)) {
442             LOG.error("CompressVerify::isArgsValidInHarMode txt-path is invalid.");
443             return false;
444         }
445 
446         if (!utility.getLibPath().isEmpty() && !isPathValid(utility.getLibPath(), TYPE_DIR, null)) {
447             LOG.error("CompressVerify::isArgsValidInHarMode lib-path is invalid.");
448             return false;
449         }
450 
451         if (!utility.getResPath().isEmpty() && !isPathValid(utility.getResPath(), TYPE_DIR, null)) {
452             LOG.error("CompressVerify::isArgsValidInHarMode res-path is invalid.");
453             return false;
454         }
455 
456         if (utility.getResourcesPath().isEmpty() || !isPathValid(utility.getResourcesPath(), TYPE_DIR, null)) {
457             LOG.error("CompressVerify::isArgsValidInHarMode resources-path is invalid.");
458             return false;
459         }
460 
461         if (!utility.getAssetsPath().isEmpty() && !isPathValid(utility.getAssetsPath(), TYPE_DIR, null)) {
462             LOG.error("CompressVerify::isArgsValidInHarMode assets-path is invalid.");
463             return false;
464         }
465 
466         return isOutPathValid(utility, HAR_SUFFIX);
467     }
468 
469     /**
470      * parse and check args if valid in app mode.
471      *
472      * @param utility common data
473      * @return isVerifyValidInAppMode if verify valid in app mode.
474      */
isVerifyValidInAppMode(Utility utility)475     private static boolean isVerifyValidInAppMode(Utility utility) {
476         if (!checkBundleTypeConsistency(utility)) {
477             LOG.error("CompressVerify::isArgsValidInAppMode bundleType is inconsistent.");
478             return false;
479         }
480 
481         if (!checkInputModulePath(utility)) {
482             LOG.warning("CompressVerify::isArgsValidInAppMode input hap-path or hspPath is invalid.");
483         }
484 
485         if (!utility.getHapPath().isEmpty()
486                 && !compatibleProcess(utility, utility.getHapPath(), utility.getFormattedHapPathList(), HAP_SUFFIX)) {
487             LOG.error("CompressVerify::isArgsValidInAppMode hap-path is invalid.");
488             return false;
489         }
490 
491         if (!utility.getHspPath().isEmpty()
492                 && !compatibleProcess(utility, utility.getHspPath(), utility.getFormattedHspPathList(), HSP_SUFFIX)) {
493             LOG.error("CompressVerify::isArgsValidInAppMode hsp-path is invalid.");
494             return false;
495         }
496 
497         if (utility.getPackInfoPath().isEmpty()) {
498             LOG.error("CompressVerify::isArgsValidInAppMode pack-info-path is empty.");
499             return false;
500         }
501 
502         File file = new File(utility.getPackInfoPath());
503         if (!file.isFile() || !PACK_INFO.equals(file.getName())) {
504             LOG.error("CompressVerify::isArgsValidInAppMode pack-info-path is invalid.");
505             return false;
506         }
507 
508         if (!isValidEncryptJsonFile(utility)) {
509             LOG.error("CompressVerify::isVerifyValidInAppMode encrypt-path is invalid.");
510             return false;
511         }
512 
513         if (!utility.getSignaturePath().isEmpty() && !(new File(utility.getSignaturePath())).isFile()) {
514             LOG.error("CompressVerify::isArgsValidInAppMode signature-path is invalid.");
515             return false;
516         }
517 
518         if (!utility.getCertificatePath().isEmpty() && !(new File(utility.getCertificatePath())).isFile()) {
519             LOG.error("CompressVerify::isArgsValidInAppMode certificate-path is invalid.");
520             return false;
521         }
522 
523         if (!utility.getEntryCardPath().isEmpty() &&
524                 !compatibleProcess(utility, utility.getEntryCardPath(),
525                         utility.getformattedEntryCardPathList(), PNG_SUFFIX)) {
526             LOG.error("CompressVerify::isArgsValidInAppMode entrycard-path is invalid.");
527             return false;
528         }
529         if (!utility.getPackResPath().isEmpty() && !isPathValid(utility.getPackResPath(), TYPE_FILE, PACK_RES)) {
530             LOG.error("CompressVerify::isArgsValidInAppMode pack-res-path is invalid.");
531             return false;
532         }
533 
534         return isOutPathValid(utility, APP_SUFFIX);
535     }
536 
checkBundleTypeConsistency(Utility utility)537     private static boolean checkBundleTypeConsistency(Utility utility) {
538         String bundleType = new String();
539         List<String> tmpHapPathList = new ArrayList<>();
540         List<String> tmpHspPathList = new ArrayList<>();
541         compatibleProcess(utility, utility.getHapPath(), tmpHapPathList, HAP_SUFFIX);
542         compatibleProcess(utility, utility.getHspPath(), tmpHspPathList, HSP_SUFFIX);
543         try {
544             if (!tmpHapPathList.isEmpty()) {
545                 HapVerifyInfo hapVerifyInfo = Compressor.parseStageHapVerifyInfo(tmpHapPathList.get(0));
546                 bundleType = hapVerifyInfo.getBundleType();
547             } else {
548                 HapVerifyInfo hapVerifyInfo = Compressor.parseStageHapVerifyInfo(tmpHspPathList.get(0));
549                 bundleType = hapVerifyInfo.getBundleType();
550             }
551             for (String hapPath : tmpHapPathList) {
552                 HapVerifyInfo hapVerifyInfo = Compressor.parseStageHapVerifyInfo(hapPath);
553                 if (!bundleType.equals(hapVerifyInfo.getBundleType())) {
554                     LOG.error("bundleType is not same");
555                     return false;
556                 }
557             }
558             for (String hspPath : tmpHspPathList) {
559                 HapVerifyInfo hapVerifyInfo = Compressor.parseStageHapVerifyInfo(hspPath);
560                 if (!bundleType.equals(hapVerifyInfo.getBundleType())) {
561                     LOG.error("bundleType is not same");
562                     return false;
563                 }
564             }
565         } catch (BundleException e) {
566             return true;
567         }
568         return true;
569     }
570 
checkInputModulePath(Utility utility)571     private static boolean checkInputModulePath(Utility utility) {
572         boolean isSharedApp = isSharedApp(utility);
573         boolean isAppService = isAppService(utility);
574         if (utility.getHapPath().isEmpty() && !isSharedApp && !isAppService) {
575             LOG.warning("CompressVerify::CheckInputModulePath hap-path is empty.");
576             return false;
577         }
578 
579         if (utility.getHspPath().isEmpty() && isAppService) {
580             LOG.warning("CompressVerify::CheckInputModulePath hsp-path is empty.");
581             return false;
582         }
583         return true;
584     }
585 
586     /**
587      * parse and check args if valid in multiApp mode.
588      *
589      * @param utility common data
590      * @return isVerifyValidInMultiAppMode if verify valid in multiApp mode.
591      */
isVerifyValidInMultiAppMode(Utility utility)592     private static boolean isVerifyValidInMultiAppMode(Utility utility) {
593         if (utility.getAppList().isEmpty() && utility.getHapList().isEmpty()) {
594             LOG.error("CompressVerify::isVerifyValidInMultiAppMode input app-list and hap-list are null.");
595             return false;
596         }
597         if (!utility.getAppList().isEmpty()) {
598             if (!compatibleProcess(utility, utility.getAppList(), utility.getFormattedAppList(), APP_SUFFIX)) {
599                 LOG.error("CompressVerify::isVerifyValidInMultiAppMode app-list is invalid.");
600                 return false;
601             }
602         }
603         if (!utility.getHapList().isEmpty()) {
604             if (!compatibleProcess(utility, utility.getHapList(), utility.getFormattedHapList(), HAP_SUFFIX)) {
605                 LOG.error("CompressVerify::isVerifyValidInMultiAppMode hap-list is invalid.");
606                 return false;
607             }
608         }
609 
610         if (!utility.getHspList().isEmpty()) {
611             if (!compatibleProcess(utility, utility.getHspList(), utility.getFormattedHapList(), HSP_SUFFIX)) {
612                 LOG.error("CompressVerify::isVerifyValidInMultiAppMode hsp-list is invalid.");
613                 return false;
614             }
615         }
616 
617         if (!isValidEncryptJsonFile(utility)) {
618             LOG.error("CompressVerify::isVerifyValidInMultiAppMode encrypt-path is invalid.");
619             return false;
620         }
621 
622         File outFile = new File(utility.getOutPath());
623         if (("false".equals(utility.getForceRewrite())) && outFile.exists()) {
624             LOG.error("CompressVerify::isVerifyValidInMultiAppMode out file already existed.");
625             return false;
626         }
627         if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(APP_SUFFIX)) {
628             LOG.error("CompressVerify::isVerifyValidInMultiAppMode out-path must end with .app.");
629             return false;
630         }
631         return true;
632     }
633 
634 
635     /**
636      * parse and check args if valid in res mode.
637      *
638      * @param utility common data
639      * @return isVerifyValidInAppMode if verify valid in app mode.
640      */
isVerifyValidInResMode(Utility utility)641     private static boolean isVerifyValidInResMode(Utility utility) {
642         if (!isPathValid(utility.getPackInfoPath(), TYPE_FILE, PACK_INFO)) {
643             LOG.error("CompressVerify::isArgsValidInResMode pack-info-path is invalid.");
644             return false;
645         }
646 
647         if (!isDirectoryValidStrictCase(utility.getEntryCardPath(), ENTRY_CARD_DIRECTORY_NAME)) {
648             LOG.error("CompressVerify::isArgsValidInResMode the level-1 directory name must is EntryCard" +
649                 ", current is " + utility.getEntryCardPath());
650             return false;
651         }
652         if (!compatibleProcess(utility, utility.getEntryCardPath(),
653                 utility.getformattedEntryCardPathList(), PNG_SUFFIX)) {
654             LOG.error("CompressVerify::isArgsValidInResMode entrycard-path is invalid.");
655             return false;
656         }
657         return isOutPathValid(utility, RES_SUFFIX);
658     }
659 
isVerifyValidInHQFMode(Utility utility)660     private static boolean isVerifyValidInHQFMode(Utility utility) {
661         if (utility.getJsonPath().isEmpty()) {
662             LOG.error("must input patch.json file when pack hqf file.");
663             return false;
664         }
665         if (!utility.getEtsPath().isEmpty()) {
666             if (!isPathValid(utility.getEtsPath(), TYPE_DIR, null)) {
667                 LOG.error("must input valid ets path when pack hqf file.");
668                 return false;
669             }
670         }
671         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, PATCH_PROFILE)) {
672             LOG.error("input patch.json is invalid when pack hqf file.");
673             return false;
674         }
675         if (!utility.getLibPath().isEmpty()) {
676             if (!isPathValid(utility.getLibPath(), TYPE_DIR, null)) {
677                 LOG.error("input lib path is invalid when pack hqf file.");
678                 return false;
679             }
680         }
681         if (!utility.getResourcesPath().isEmpty()) {
682             if (!isPathValid(utility.getResourcesPath(), TYPE_DIR, null)) {
683                 LOG.error("input resources path is invalid when pack hqf file.");
684                 return false;
685             }
686         }
687         File outFile = new File(utility.getOutPath());
688         if ((FALSE.equals(utility.getForceRewrite())) && (outFile.exists())) {
689             LOG.error(outFile.getName() + " already exist.");
690             return false;
691         }
692         if (!utility.getOutPath().endsWith(HQF_SUFFIX)) {
693             LOG.error("input out file must end with .hqf.");
694             return false;
695         }
696         return true;
697     }
698 
isVerifyValidInAPPQFMode(Utility utility)699     private static boolean isVerifyValidInAPPQFMode(Utility utility) {
700         if (utility.getHqfList().isEmpty()) {
701             LOG.error("input hqf list is empty.");
702             return false;
703         }
704         if (!compatibleProcess(utility, utility.getHqfList(), utility.getFormatedHQFList(), HQF_SUFFIX)) {
705             LOG.error("input hqf list is invalid.");
706             return false;
707         }
708         File outFile = new File(utility.getOutPath());
709         if ((FALSE.equals(utility.getForceRewrite())) && outFile.exists()) {
710             LOG.error("Error out file already existed.");
711             return false;
712         }
713         if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(APPQF_SUFFIX)) {
714             LOG.error("Error out-path must end with .app.");
715             return false;
716         }
717         return true;
718     }
719 
720     /**
721      * Compatible file input and directory input
722      *
723      * @param utility   common data
724      * @param inputPath input path
725      * @param fileList  save files' path with list
726      * @param suffix    process type
727      * @return Returns {@code true} if the compatible is successful; returns {@code false} otherwise.
728      */
compatibleProcess(Utility utility, String inputPath, List<String> fileList, String suffix)729     public static boolean compatibleProcess(Utility utility, String inputPath,
730         List<String> fileList, String suffix) {
731         if (isPathValid(inputPath, TYPE_DIR, null)) {
732             File inputFile = new File(inputPath);
733             File[] files = inputFile.listFiles();
734             if (files == null) {
735                 return true;
736             }
737             for (File fileItem : files) {
738                 if (fileItem.getName().toLowerCase(Locale.ENGLISH).endsWith(suffix)) {
739                     fileList.add(fileItem.toString());
740                 }
741             }
742             return true;
743         } else {
744             String formattedPathItem = "";
745             List<String> pathList = removeDuplicatePath(inputPath);
746             for (String pathItem : pathList) {
747                 formattedPathItem = utility.getFormattedPath(pathItem);
748                 if (!isPathValid(formattedPathItem, TYPE_FILE, suffix)) {
749                     return false;
750                 }
751                 fileList.add(formattedPathItem);
752             }
753             return true;
754         }
755     }
756 
handleHapAndHspInput(Utility utility, String inputPath, List<String> fileList)757     private static boolean handleHapAndHspInput(Utility utility, String inputPath, List<String> fileList) {
758         if (isPathValid(inputPath, TYPE_DIR, null)) {
759             File inputFile = new File(inputPath);
760             File[] files = inputFile.listFiles();
761             if (files == null) {
762                 return true;
763             }
764             for (File fileItem : files) {
765                 if (fileItem.getName().toLowerCase(Locale.ENGLISH).endsWith(HSP_SUFFIX) ||
766                     fileItem.getName().toLowerCase(Locale.ENGLISH).endsWith(HAP_SUFFIX)) {
767                     fileList.add(fileItem.toString());
768                 }
769             }
770             return true;
771         } else {
772             String formattedPathItem = "";
773             List<String> pathList = removeDuplicatePath(inputPath);
774             for (String pathItem : pathList) {
775                 formattedPathItem = utility.getFormattedPath(pathItem);
776                 if (!isPathValid(formattedPathItem, TYPE_FILE, HSP_SUFFIX) &&
777                     !isPathValid(formattedPathItem, TYPE_FILE, HAP_SUFFIX)) {
778                     LOG.error("input file " + formattedPathItem + " not valid");
779                     return false;
780                 }
781                 fileList.add(formattedPathItem);
782             }
783             return true;
784         }
785     }
786 
splitDirList(Utility utility, String dirList, List<String> fileList)787     private static boolean splitDirList(Utility utility, String dirList, List<String> fileList) {
788         List<String> pathList = removeDuplicatePath(dirList);
789         for (String pathItem : pathList) {
790             String formattedPathItem = utility.getFormattedPath(pathItem);
791             if (!isPathValid(formattedPathItem, TYPE_DIR, null)) {
792                 return false;
793             }
794             fileList.add(formattedPathItem);
795         }
796         return true;
797     }
798 
799     /**
800      * turn input path block to formatted path list
801      *
802      * @param utility common data
803      * @param suffix  used to determine type
804      * @return isVerifyValidInAppMode if verify valid in app mode.
805      */
isOutPathValid(Utility utility, String suffix)806     private static boolean isOutPathValid(Utility utility, String suffix) {
807         File outFile = new File(utility.getOutPath());
808 
809         if (("false".equals(utility.getForceRewrite())) && (outFile.exists())) {
810             LOG.error("CompressVerify::isOutPathValid out file already existed.");
811             return false;
812         }
813 
814         if (HAP_SUFFIX.equals(suffix)) {
815             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(HAP_SUFFIX)) {
816                 LOG.error("CompressVerify::isOutPathValid out-path must end with .hap.");
817                 return false;
818             } else {
819                 return true;
820             }
821         }
822 
823         if (HAR_SUFFIX.equals(suffix)) {
824             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(HAR_SUFFIX)) {
825                 LOG.error("CompressVerify::isOutPathValid out-path must end with .har.");
826                 return false;
827             } else {
828                 return true;
829             }
830         }
831 
832         if (APP_SUFFIX.equals(suffix)) {
833             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(APP_SUFFIX)) {
834                 LOG.error("CompressVerify::isOutPathValid out-path must end with .app.");
835                 return false;
836             } else {
837                 return true;
838             }
839         }
840 
841         if (RES_SUFFIX.equals(suffix)) {
842             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(RES_SUFFIX)) {
843                 LOG.error("CompressVerify::isOutPathValid out-path must end with .res.");
844                 return false;
845             } else {
846                 return true;
847             }
848         }
849 
850         if (HSP_SUFFIX.equals(suffix)) {
851             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(HSP_SUFFIX)) {
852                 LOG.error("CompressVerify::isOutPathValid out-path must end with .hsp.");
853                 return false;
854             } else {
855                 return true;
856             }
857         }
858         return false;
859     }
860 
861     /**
862      * check path if valid
863      *
864      * @param path   path input
865      * @param isFile type input
866      * @param flag   flag input
867      * @return isPathValid if path verify
868      */
isPathValid(String path, boolean isFile, String flag)869     private static boolean isPathValid(String path, boolean isFile, String flag) {
870         File file = new File(path);
871         if (isFile && (file.isFile()) && file.getName().toLowerCase(Locale.ENGLISH).endsWith(flag)) {
872             return true;
873         }
874         return (!isFile) && file.isDirectory();
875     }
876 
isPathExists(String path)877     private static boolean isPathExists(String path) {
878         if (path != null && !path.isEmpty()) {
879             File filePath = new File(path);
880             return filePath.exists();
881         }
882         return false;
883     }
884 
isDirectoryValidStrictCase(String path, String directoryName)885     private static boolean isDirectoryValidStrictCase(String path, String directoryName) {
886         File file = new File(path);
887         if (!file.exists()) {
888             LOG.error("CompressVerify::isDirectoryValidStrictCase directory is not exist, directoryPath: "
889                 + path + ".");
890             return false;
891         }
892         if (file.isDirectory()) {
893             return directoryName.equals(file.getName());
894         }
895         return false;
896     }
897 
898     /**
899      * remove duplicate in path.
900      *
901      * @param path input path, use comma separate.
902      * @return result list
903      */
removeDuplicatePath(String path)904     private static List<String> removeDuplicatePath(String path) {
905         String[] array = path.split(COMMA_SPLIT);
906         List<String> list = new ArrayList<>();
907 
908         for (String item : array) {
909             if (!list.contains(item)) {
910                 list.add(item);
911             }
912         }
913         return list;
914     }
915 
isVerifyValidInHspMode(Utility utility)916     private static boolean isVerifyValidInHspMode(Utility utility) {
917         if (utility.getJsonPath().isEmpty()) {
918             LOG.error("CompressVerify::isArgsValidInHspMode json-path is empty.");
919             return false;
920         }
921 
922         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) {
923             LOG.error("CompressVerify::isArgsValidInHspMode json-path must be module.json file.");
924             return false;
925         }
926 
927         if((isBundleTypeShared(utility) || isBundleTypeAppService(utility)) && hspHasAbilities(utility)) {
928             LOG.error("shared/appService hsp has abilities");
929             return false;
930         }
931 
932         if(hasHomeAbility(utility)) {
933             LOG.error("hsp has entry ability");
934             return false;
935         }
936 
937         if (!utility.getJarPath().isEmpty()
938                 && !compatibleProcess(utility, utility.getJarPath(), utility.getFormattedJarPathList(), JAR_SUFFIX)) {
939             LOG.error("CompressVerify::isArgsValidInHspMode jar-path is invalid.");
940             return false;
941         }
942 
943         if (!utility.getTxtPath().isEmpty()
944                 && !compatibleProcess(utility, utility.getTxtPath(), utility.getFormattedTxtPathList(), TXT_SUFFIX)) {
945             LOG.error("CompressVerify::isArgsValidInHspMode txt-path is invalid.");
946             return false;
947         }
948 
949         if (!utility.getLibPath().isEmpty() && !isPathValid(utility.getLibPath(), TYPE_DIR, null)) {
950             LOG.error("CompressVerify::isArgsValidInHspMode lib-path is invalid.");
951             return false;
952         }
953 
954         if (!utility.getResPath().isEmpty() && !isPathValid(utility.getResPath(), TYPE_DIR, null)) {
955             LOG.error("CompressVerify::isArgsValidInHspMode res-path is invalid.");
956             return false;
957         }
958 
959         if (!utility.getResourcesPath().isEmpty() && !isPathValid(utility.getResourcesPath(), TYPE_DIR, null)) {
960             LOG.error("CompressVerify::isArgsValidInHspMode resources-path is invalid.");
961             return false;
962         }
963 
964         if (!utility.getAssetsPath().isEmpty() && !isPathValid(utility.getAssetsPath(), TYPE_DIR, null)) {
965             LOG.error("CompressVerify::isArgsValidInHspMode assets-path is invalid.");
966             return false;
967         }
968 
969         if (!utility.getDirList().isEmpty() && !splitDirList(utility, utility.getDirList(),
970                 utility.getFormatedDirList())) {
971             LOG.error("CompressVerify::isArgsValidInHapMode --dir-list is invalid.");
972             return false;
973         }
974 
975         if (isHapPathValid(utility.getAPPath())) {
976             LOG.error("CompressVerify::isArgsValidInHapMode ap-path is invalid.");
977             return false;
978         }
979 
980         if (isHapPathValid(utility.getANPath())) {
981             LOG.error("CompressVerify::isArgsValidInHapMode an-path is invalid.");
982             return false;
983         }
984 
985         if (!utility.getPkgContextPath().isEmpty()) {
986             File file = new File(utility.getPkgContextPath());
987             if (!file.isFile() || !PKG_CONTEXT_INFO.equals(file.getName())) {
988                 LOG.error("CompressVerify::isArgsValidInHspMode --pkg-context-path file must be " + PKG_CONTEXT_INFO);
989                 return false;
990             }
991         }
992 
993         if (!utility.getEtsPath().isEmpty() && !isPathExists(utility.getEtsPath())) {
994             LOG.error("CompressVerify::isArgsValidInHspMode ets-path is invalid.");
995             return false;
996         }
997 
998         return isOutPathValid(utility, HSP_SUFFIX);
999     }
1000 
isVerifyValidInHapAdditionMode(Utility utility)1001     private static boolean isVerifyValidInHapAdditionMode(Utility utility) {
1002         if (utility.getHapPath().isEmpty()) {
1003             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode hapPath is empty.");
1004             return false;
1005         }
1006         String hapPath = utility.getAbsoluteHapPath();
1007         File hapFile = new File(hapPath);
1008         if (hapFile.isDirectory()) {
1009             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode hapPath cannot be a folder.");
1010             return false;
1011         }
1012         if (!(hapPath.endsWith(HAP_SUFFIX) || hapPath.endsWith(HSP_SUFFIX))) {
1013             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode hapPath is invalid.");
1014             return false;
1015         }
1016         if (!hapFile.exists()) {
1017             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode hap file does not exist.");
1018             return false;
1019         }
1020         if (utility.getJsonPath().isEmpty()) {
1021             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode jsonPath is empty.");
1022             return false;
1023         }
1024         if (!utility.getJsonPath().endsWith(JSON_SUFFIX)) {
1025             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode jsonPath is invalid.");
1026             return false;
1027         }
1028         File jsonFile = new File(utility.getJsonPath());
1029         if (!jsonFile.exists()) {
1030             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode json file does not exist.");
1031             return false;
1032         }
1033         if (!checkJsonIsValid(jsonFile)) {
1034             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode json format is incorrect.");
1035             return false;
1036         }
1037 
1038         if (utility.getOutPath().isEmpty()) {
1039             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode outPath is empty.");
1040             return false;
1041         }
1042         File dir = new File(utility.getOutPath());
1043         if (dir.exists() && dir.isFile()) {
1044             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode outPath is file.");
1045             return false;
1046         }
1047         File absoluteHapFile = new File(utility.getAbsoluteHapPath());
1048         String hapFileName = absoluteHapFile.getName();
1049         String destPath = utility.getOutPath() + LINUX_FILE_SEPARATOR + hapFileName;
1050         File destFile = new File(destPath);
1051         if ("false".equals(utility.getForceRewrite()) && destFile.exists()) {
1052             LOG.error("CompressVerify::isVerifyValidInHapAdditionMode target file already exists.");
1053             return false;
1054         }
1055         return true;
1056     }
1057 
checkJsonIsValid(File jsonFile)1058     private static boolean checkJsonIsValid(File jsonFile) {
1059         StringBuffer jsonData = new StringBuffer();
1060         try (FileReader fileReader = new FileReader(jsonFile);
1061              Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8)) {
1062             int ch = 0;
1063             while ((ch = reader.read()) != -1) {
1064                 jsonData.append((char) ch);
1065             }
1066         } catch (IOException e) {
1067             LOG.error("CompressVerify::CheckJsonIsValid failed: " + e.getMessage());
1068             return false;
1069         }
1070         JSONValidator validator = JSONValidator.from(jsonData.toString());
1071         return validator.validate();
1072     }
1073 
isSharedApp(Utility utility)1074     private static boolean isSharedApp(Utility utility) {
1075         if (!utility.getHapPath().isEmpty()) {
1076             return false;
1077         }
1078         if (utility.getHspPath().isEmpty()) {
1079             return false;
1080         }
1081         List<String> tmpHspPathList = new ArrayList<>();
1082         if (compatibleProcess(utility, utility.getHspPath(), tmpHspPathList, HSP_SUFFIX)
1083             && verifyIsSharedApp(tmpHspPathList)) {
1084             utility.setIsSharedApp(true);
1085             return true;
1086         }
1087         return false;
1088     }
1089 
isAppService(Utility utility)1090     private static boolean isAppService(Utility utility) {
1091         if (!utility.getHapPath().isEmpty()) {
1092             List<String> tmpHapPathList = new ArrayList<>();
1093             if (compatibleProcess(utility, utility.getHapPath(), tmpHapPathList, HSP_SUFFIX)
1094                     && verifyIsAppService(tmpHapPathList)) {
1095                 utility.setIsAppService(true);
1096                 return true;
1097             }
1098         }
1099         if (utility.getHspPath().isEmpty()) {
1100             return false;
1101         }
1102         List<String> tmpHspPathList = new ArrayList<>();
1103         if (compatibleProcess(utility, utility.getHspPath(), tmpHspPathList, HSP_SUFFIX)
1104                 && verifyIsAppService(tmpHspPathList)) {
1105             utility.setIsAppService(true);
1106             return true;
1107         }
1108         return false;
1109     }
verifyIsAppService(List<String> modulePathList)1110     private static boolean verifyIsAppService(List<String> modulePathList) {
1111         if (modulePathList.isEmpty()) {
1112             return false;
1113         }
1114         try {
1115             for (String modulePath : modulePathList) {
1116                 HapVerifyInfo hapVerifyInfo = Compressor.parseStageHapVerifyInfo(modulePath);
1117                 if (!hapVerifyInfo.getBundleType().equals(BUNDLE_TYPE_APP_SERVICE)) {
1118                     return false;
1119                 }
1120             }
1121         } catch (BundleException e) {
1122             return false;
1123         }
1124         return true;
1125     }
verifyIsSharedApp(List<String> hspPath)1126     private static boolean verifyIsSharedApp(List<String> hspPath) {
1127         try {
1128             HapVerifyInfo hapVerifyInfo = Compressor.parseStageHapVerifyInfo(hspPath.get(0));
1129             return hapVerifyInfo.getBundleType().equals(BUNDLE_TYPE_SHARE);
1130         } catch (BundleException e) {
1131             return false;
1132         }
1133     }
1134 
isBundleTypeShared(Utility utility)1135     private static boolean isBundleTypeShared(Utility utility) {
1136         try {
1137             Optional<String> optional = FileUtils.getFileContent(utility.getJsonPath());
1138             if(optional.isPresent()) {
1139                 return ModuleJsonUtil.parseStageBundleType(optional.get()).equals(BUNDLE_TYPE_SHARE);
1140             } else {
1141                 LOG.error("CompressVerify::isBundleTypeShared jsonPath content invalid");
1142                 return false;
1143             }
1144         } catch (BundleException e) {
1145             LOG.error("CompressVerify::isBundleTypeShared exception: " + e.getMessage());
1146             return false;
1147         }
1148     }
1149 
isBundleTypeAppService(Utility utility)1150     private static boolean isBundleTypeAppService(Utility utility) {
1151         try {
1152             Optional<String> optional = FileUtils.getFileContent(utility.getJsonPath());
1153             if(optional.isPresent()) {
1154                 return ModuleJsonUtil.parseStageBundleType(optional.get()).equals(BUNDLE_TYPE_APP_SERVICE);
1155             } else {
1156                 LOG.error("CompressVerify::isBundleTypeAppService jsonPath content invalid");
1157                 return false;
1158             }
1159         } catch (BundleException e) {
1160             LOG.error("CompressVerify::isBundleTypeAppService exception: " + e.getMessage());
1161             return false;
1162         }
1163     }
1164 
hspHasAbilities(Utility utility)1165     private static boolean hspHasAbilities(Utility utility) {
1166         try {
1167             Optional<String> optional = FileUtils.getFileContent(utility.getJsonPath());
1168             if(optional.isPresent()) {
1169                 return ModuleJsonUtil.parseModuleType(optional.get()).equals(BUNDLE_TYPE_SHARE) && !ModuleJsonUtil.parseAbilityNames(optional.get()).isEmpty();
1170             } else {
1171                 LOG.error("CompressVerify::hspHasAbilities jsonPath content invalid");
1172                 return false;
1173             }
1174         } catch (BundleException e) {
1175             LOG.error("CompressVerify::hspHasAbilities exception: " + e.getMessage());
1176             return false;
1177         }
1178     }
1179 
hasHomeAbility(Utility utility)1180     private static boolean hasHomeAbility(Utility utility) {
1181         try {
1182             boolean result = false;
1183             Optional<String> optional = FileUtils.getFileContent(utility.getJsonPath());
1184             if(!optional.isPresent()) {
1185                 LOG.error("CompressVerify::hasHomeAbility jsonPath content invalid");
1186                 return false;
1187             }
1188             Map<String, Boolean> abilitiesMap = ModuleJsonUtil.parseAbilitySkillsMap(optional.get());
1189             if (abilitiesMap.containsValue(true)) {
1190                 result = true;
1191             }
1192             LOG.info("CompressVerify::hasHomeAbilities result = " + result);
1193             return result;
1194         } catch (BundleException e) {
1195             LOG.error("CompressVerify::hasHomeAbilities exception: " + e.getMessage());
1196             return false;
1197         }
1198     }
1199 
1200     /**
1201      * Indicates whether the "--encrypt-path" parameter is valid.
1202      *
1203      * @param utility - compress parameters
1204      * @return true if "--encrypt-path" param exists and the file name is encrypt.json, or the "--encrypt-path"
1205      *         param is empty, or has no "--encrypt-path" param
1206      *         false other situations
1207      */
isValidEncryptJsonFile(Utility utility)1208     private static boolean isValidEncryptJsonFile(Utility utility) {
1209         if (!utility.getEncryptPath().isEmpty()) {
1210             File fileEncryptJson = new File(utility.getEncryptPath());
1211             return fileEncryptJson.isFile() && Constants.FILE_ENCRYPT_JSON.equals(fileEncryptJson.getName());
1212         }
1213         return true;
1214     }
1215 }
1216