• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 package ohos;
17 
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Locale;
22 
23 /**
24  * compress comment,command parser.
25  *
26  */
27 public class CompressVerify {
28     private static final String COMMA_SPLIT = ",";
29     private static final String JSON_PROFILE = "config.json";
30     private static final String MODULE_PROFILE = "module.json";
31     private static final String PATCH_PROFILE = "patch.json";
32     private static final String PROFILE_NAME = "CAPABILITY.profile";
33     private static final String INDEX_PROFILE = "resources.index";
34     private static final String RPCID_PROFILE = "rpcid.sc";
35     private static final String PACK_INFO = "pack.info";
36     private static final String PACK_RES = "pack.res";
37     private static final String HAP_SUFFIX = ".hap";
38     private static final String HAR_SUFFIX = ".har";
39     private static final String APP_SUFFIX = ".app";
40     private static final String APK_SUFFIX = ".apk";
41     private static final String DEX_SUFFIX = ".dex";
42     private static final String ABC_SUFFIX = ".abc";
43     private static final String SO_SUFFIX = ".so";
44     private static final String JAR_SUFFIX = ".jar";
45     private static final String TXT_SUFFIX = ".txt";
46     private static final String PNG_SUFFIX = ".png";
47     private static final String RES_SUFFIX = ".res";
48     private static final String HQF_SUFFIX = ".hqf";
49     private static final String APPQF_SUFFIX = ".appqf";
50     private static final String HSP_SUFFIX = ".hsp";
51     private static final String FALSE = "false";
52     private static final String ENTRY_CARD_DIRECTORY_NAME = "EntryCard";
53 
54     private static final Log LOG = new Log(CompressVerify.class.toString());
55 
56     private static final boolean TYPE_FILE = true;
57     private static final boolean TYPE_DIR = false;
58 
59     /**
60      * if args valid.
61      *
62      * @param utility common data
63      * @return commandVerify if command valid.
64      */
commandVerify(Utility utility)65     public static boolean commandVerify(Utility utility) {
66         if (utility == null) {
67             LOG.error("CompressVerify::commandVerify utility is null.");
68             return false;
69         }
70 
71         if (!utility.getForceRewrite().isEmpty() && !"true".equals(utility.getForceRewrite())
72                 && !"false".equals(utility.getForceRewrite())) {
73             LOG.error("CompressVerify::commandVerify forceRewrite is invalid.");
74             return false;
75         }
76         return commandPathVerify(utility);
77     }
78 
79     /**
80      * verify path.
81      *
82      * @param utility common data
83      * @return commandPathVerify if command valid.
84      */
commandPathVerify(Utility utility)85     private static boolean commandPathVerify(Utility utility) {
86         switch (utility.getMode()) {
87             case Utility.MODE_HAP:
88                 if (!utility.getBinPath().isEmpty() && utility.getJsonPath().isEmpty()) {
89                     return isOutPathValid(utility, HAP_SUFFIX);
90                 } else {
91                     return isVerifyValidInHapCommonMode(utility) && isVerifyValidInHapMode(utility);
92                 }
93             case Utility.MODE_HAR:
94                 return isVerifyValidInHarMode(utility);
95             case Utility.MODE_APP:
96                 return isVerifyValidInAppMode(utility);
97             case Utility.MODE_RES:
98                 return isVerifyValidInResMode(utility);
99             case Utility.MODE_MULTI_APP:
100                 return isVerifyValidInMultiAppMode(utility);
101             case Utility.MODE_HQF:
102                 return isVerifyValidInHQFMode(utility);
103             case Utility.MODE_APPQF:
104                 return isVerifyValidInAPPQFMode(utility);
105             case Utility.MODE_HSP:
106                 return isVerifyValidInHspMode(utility);
107             default:
108                 LOG.error("CompressVerify::commandVerify mode is invalid.");
109                 return false;
110         }
111     }
112 
isValidRpcid(Utility utility)113     private static boolean isValidRpcid(Utility utility) {
114         if (!utility.getRpcidPath().isEmpty()) {
115             File file = new File(utility.getRpcidPath());
116             if (!file.isFile()) {
117                 LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path is not a file.");
118                 return false;
119             }
120             if (!RPCID_PROFILE.equals(file.getName())) {
121                 LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path must be rpcid.sc file.");
122                 return false;
123             }
124         }
125         return true;
126     }
127 
isValidPackInfo(Utility utility)128     private static boolean isValidPackInfo(Utility utility) {
129         if (!utility.getPackInfoPath().isEmpty()) {
130             File file = new File(utility.getPackInfoPath());
131             if (!file.isFile()) {
132                 LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path is not a file.");
133                 return false;
134             }
135             if (!PACK_INFO.equals(file.getName())) {
136                 LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path must be pack.info file.");
137                 return false;
138             }
139         }
140         return true;
141     }
142 
isVerifyValidInHapCommonMode(Utility utility)143     private static boolean isVerifyValidInHapCommonMode(Utility utility) {
144         if (utility.getJsonPath().isEmpty()) {
145             LOG.error("CompressVerify::commandPathVerify json-path is empty.");
146             return false;
147         }
148         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, JSON_PROFILE)
149                 && !isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) {
150             LOG.error("CompressVerify::isArgsValidInHarMode json-path must be config.json file.");
151             return false;
152         }
153 
154         if (!isValidRpcid(utility) || !isValidPackInfo(utility)) {
155             return false;
156         }
157 
158         if (!utility.getApkPath().isEmpty() && !compatibleProcess(utility, utility.getApkPath(),
159                 utility.getFormattedApkPathList(), APK_SUFFIX)) {
160             LOG.error("CompressVerify::isArgsValidInHapMode shell-apk-path is invalid.");
161             return false;
162         }
163 
164         if (!utility.getProfilePath().isEmpty()) {
165             File file = new File(utility.getProfilePath());
166             if (!file.isFile() || !PROFILE_NAME.equals(file.getName())) {
167                 LOG.error("CompressVerify::isArgsValidInHapMode profile-path must be CAPABILITY.profile file.");
168                 return false;
169             }
170         }
171 
172         if (!utility.getDexPath().isEmpty() && !compatibleProcess(utility, utility.getDexPath(),
173                 utility.getFormattedDexPathList(), DEX_SUFFIX)) {
174             LOG.error("CompressVerify::isArgsValidInHapMode dex-path is invalid.");
175             return false;
176         }
177 
178         if (!utility.getAbcPath().isEmpty() && !compatibleProcess(utility, utility.getAbcPath(),
179                 utility.getFormattedAbcPathList(), ABC_SUFFIX)) {
180             LOG.error("CompressVerify::isArgsValidInHapMode abc-path is invalid.");
181             return false;
182         }
183 
184         if (!utility.getDirList().isEmpty() &&
185             !splitDirList(utility, utility.getDirList(), utility.getFormatedDirList())) {
186             LOG.error("CompressVerify::isArgsValidInHapMode --dir-list is invalid.");
187             return false;
188         }
189         return true;
190     }
191 
192     /**
193      * parse and check args if valid in hap mode.
194      *
195      * @param utility common data
196      * @return isVerifyValidInHapMode if verify valid in hap mode.
197      */
isVerifyValidInHapMode(Utility utility)198     private static boolean isVerifyValidInHapMode(Utility utility) {
199         File file = new File(utility.getIndexPath());
200         if (!utility.getIndexPath().isEmpty() && !file.isFile() && INDEX_PROFILE.equals(file.getName())) {
201             LOG.error("CompressVerify::isArgsValidInHapMode index-path must be resources.index file.");
202             return false;
203         }
204 
205         if (!utility.getSoPath().isEmpty() &&
206                 !compatibleProcess(utility, utility.getSoPath(), utility.getFormattedSoPathList(), SO_SUFFIX)) {
207             LOG.error("CompressVerify::isArgsValidInHapMode maple-so-path is invalid.");
208             return false;
209         }
210 
211         if (!utility.getAbilitySoPath().isEmpty() && !compatibleProcess(utility, utility.getAbilitySoPath(),
212                 utility.getFormattedAbilitySoPathList(), SO_SUFFIX)) {
213             LOG.error("CompressVerify::isArgsValidInHapMode ability-so-path is invalid.");
214             return false;
215         }
216 
217         if (isHapPathValid(utility.getSoDir())) {
218             LOG.error("CompressVerify::isArgsValidInHapMode maple-so-dir is invalid.");
219             return false;
220         }
221 
222         if (isHapPathValid(utility.getLibPath())) {
223             LOG.error("CompressVerify::isArgsValidInHapMode lib-path is invalid.");
224             return false;
225         }
226 
227         if (isHapPathValid(utility.getResPath())) {
228             LOG.error("CompressVerify::isArgsValidInHapMode res-path is invalid.");
229             return false;
230         }
231 
232         if (isHapPathValid(utility.getResourcesPath())) {
233             LOG.error("CompressVerify::isArgsValidInHapMode resources-path is invalid.");
234             return false;
235         }
236 
237         if (isHapPathValid(utility.getAssetsPath())) {
238             LOG.error("CompressVerify::isArgsValidInHapMode assets-path is invalid.");
239             return false;
240         }
241 
242         if (isHapPathValid(utility.getSharedLibsPath())) {
243             LOG.error("CompressVerify::isArgsValidInHapMode shared-libs-path is invalid.");
244             return false;
245         }
246 
247         if (!utility.getJarPath().isEmpty()
248                 && !compatibleProcess(utility, utility.getJarPath(), utility.getFormattedJarPathList(), JAR_SUFFIX)) {
249             LOG.error("CompressVerify::isArgsValidInHapMode jar-path is invalid.");
250             return false;
251         }
252 
253         if (!utility.getTxtPath().isEmpty()
254                 && !compatibleProcess(utility, utility.getTxtPath(), utility.getFormattedTxtPathList(), TXT_SUFFIX)) {
255             LOG.error("CompressVerify::isArgsValidInHapMode txt-path is invalid.");
256             return false;
257         }
258 
259         if (isHapPathValid(utility.getANPath())) {
260             LOG.error("CompressVerify::isArgsValidInHapMode an-path is invalid.");
261             return false;
262         }
263 
264         return isOutPathValid(utility, HAP_SUFFIX);
265     }
266 
267     /**
268      * check hap path if valid
269      *
270      * @param path path input
271      * @return isPathValid if path verify
272      */
isHapPathValid(String path)273     private static boolean isHapPathValid(String path) {
274         return (!path.isEmpty() && !isPathValid(path, TYPE_DIR, null));
275     }
276 
277     /**
278      * parse and check args if valid in har mode.
279      *
280      * @param utility common data
281      * @return isVerifyValidInHarMode if verify valid in har mode.
282      */
isVerifyValidInHarMode(Utility utility)283     private static boolean isVerifyValidInHarMode(Utility utility) {
284         if (utility.getJsonPath().isEmpty()) {
285             LOG.error("CompressVerify::isArgsValidInHarMode json-path is empty.");
286             return false;
287         }
288 
289         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, JSON_PROFILE)
290             && !isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) {
291             LOG.error("CompressVerify::isArgsValidInHarMode json-path must be config.json file.");
292             return false;
293         }
294 
295         if (!utility.getJarPath().isEmpty()
296                 && !compatibleProcess(utility, utility.getJarPath(), utility.getFormattedJarPathList(), JAR_SUFFIX)) {
297             LOG.error("CompressVerify::isArgsValidInHarMode jar-path is invalid.");
298             return false;
299         }
300 
301         if (!utility.getTxtPath().isEmpty()
302                 && !compatibleProcess(utility, utility.getTxtPath(), utility.getFormattedTxtPathList(), TXT_SUFFIX)) {
303             LOG.error("CompressVerify::isArgsValidInHarMode txt-path is invalid.");
304             return false;
305         }
306 
307         if (!utility.getLibPath().isEmpty() && !isPathValid(utility.getLibPath(), TYPE_DIR, null)) {
308             LOG.error("CompressVerify::isArgsValidInHarMode lib-path is invalid.");
309             return false;
310         }
311 
312         if (!utility.getResPath().isEmpty() && !isPathValid(utility.getResPath(), TYPE_DIR, null)) {
313             LOG.error("CompressVerify::isArgsValidInHarMode res-path is invalid.");
314             return false;
315         }
316 
317         if (utility.getResourcesPath().isEmpty() || !isPathValid(utility.getResourcesPath(), TYPE_DIR, null)) {
318             LOG.error("CompressVerify::isArgsValidInHarMode resources-path is invalid.");
319             return false;
320         }
321 
322         if (!utility.getAssetsPath().isEmpty() && !isPathValid(utility.getAssetsPath(), TYPE_DIR, null)) {
323             LOG.error("CompressVerify::isArgsValidInHarMode assets-path is invalid.");
324             return false;
325         }
326 
327         return isOutPathValid(utility, HAR_SUFFIX);
328     }
329 
330     /**
331      * parse and check args if valid in app mode.
332      *
333      * @param utility common data
334      * @return isVerifyValidInAppMode if verify valid in app mode.
335      */
isVerifyValidInAppMode(Utility utility)336     private static boolean isVerifyValidInAppMode(Utility utility) {
337         if (utility.getHapPath().isEmpty() && utility.getHspPath().isEmpty()) {
338             LOG.error("CompressVerify::isArgsValidInAppMode hap-path is empty.");
339             return false;
340         }
341 
342         if (!compatibleProcess(utility, utility.getHapPath(), utility.getFormattedHapPathList(), HAP_SUFFIX)) {
343             LOG.error("CompressVerify::isArgsValidInAppMode hap-path is invalid.");
344             return false;
345         }
346 
347         if (!utility.getHspPath().isEmpty()
348                 && !compatibleProcess(utility, utility.getHspPath(), utility.getFormattedHspPathList(), HSP_SUFFIX)) {
349             LOG.error("CompressVerify::isArgsValidInAppMode hsp-path is invalid.");
350             return false;
351         }
352 
353         if (utility.getPackInfoPath().isEmpty()) {
354             LOG.error("CompressVerify::isArgsValidInAppMode pack-info-path is empty.");
355             return false;
356         }
357 
358         File file = new File(utility.getPackInfoPath());
359         if (!file.isFile() || !PACK_INFO.equals(file.getName())) {
360             LOG.error("CompressVerify::isArgsValidInAppMode pack-info-path is invalid.");
361             return false;
362         }
363 
364         if (!utility.getSignaturePath().isEmpty() && !(new File(utility.getSignaturePath())).isFile()) {
365             LOG.error("CompressVerify::isArgsValidInAppMode signature-path is invalid.");
366             return false;
367         }
368 
369         if (!utility.getCertificatePath().isEmpty() && !(new File(utility.getCertificatePath())).isFile()) {
370             LOG.error("CompressVerify::isArgsValidInAppMode certificate-path is invalid.");
371             return false;
372         }
373 
374         if (!utility.getEntryCardPath().isEmpty() &&
375                 !compatibleProcess(utility, utility.getEntryCardPath(),
376                         utility.getformattedEntryCardPathList(), PNG_SUFFIX)) {
377             LOG.error("CompressVerify::isArgsValidInAppMode entrycard-path is invalid.");
378             return false;
379         }
380         if (!utility.getPackResPath().isEmpty() && !isPathValid(utility.getPackResPath(), TYPE_FILE, PACK_RES)) {
381             LOG.error("CompressVerify::isArgsValidInAppMode pack-res-path is invalid.");
382             return false;
383         }
384 
385         return isOutPathValid(utility, APP_SUFFIX);
386     }
387 
388     /**
389      * parse and check args if valid in multiApp mode.
390      *
391      * @param utility common data
392      * @return isVerifyValidInMultiAppMode if verify valid in multiApp mode.
393      */
isVerifyValidInMultiAppMode(Utility utility)394     private static boolean isVerifyValidInMultiAppMode(Utility utility) {
395         if (utility.getAppList().isEmpty() && utility.getHapList().isEmpty()) {
396             LOG.error("CompressVerify::isVerifyValidInMultiAppMode input app-list and hap-list are null.");
397             return false;
398         }
399         if (!utility.getAppList().isEmpty()) {
400             if (!compatibleProcess(utility,utility.getAppList(), utility.getFormattedAppList(), APP_SUFFIX)) {
401                 LOG.error("CompressVerify::isVerifyValidInMultiAppMode app-list is invalid.");
402                 return false;
403             }
404         }
405         if (!utility.getHapList().isEmpty()) {
406             if (!compatibleProcess(utility, utility.getHapList(), utility.getFormattedHapList(), HAP_SUFFIX)) {
407                 LOG.error("CompressVerify::isVerifyValidInMultiAppMode hap-list is invalid.");
408                 return false;
409             }
410         }
411 
412         if (!utility.getHspList().isEmpty()) {
413             if (!compatibleProcess(utility, utility.getHspList(), utility.getFormattedHapList(), HSP_SUFFIX)) {
414                 LOG.error("CompressVerify::isVerifyValidInMultiAppMode hsp-list is invalid.");
415                 return false;
416             }
417         }
418 
419         File outFile = new File(utility.getOutPath());
420         if (("false".equals(utility.getForceRewrite())) && outFile.exists()) {
421             LOG.error("CompressVerify::isVerifyValidInMultiAppMode out file already existed.");
422             return false;
423         }
424         if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(APP_SUFFIX)) {
425             LOG.error("CompressVerify::isVerifyValidInMultiAppMode out-path must end with .app.");
426             return false;
427         }
428         return true;
429     }
430 
431 
432     /**
433      * parse and check args if valid in res mode.
434      *
435      * @param utility common data
436      * @return isVerifyValidInAppMode if verify valid in app mode.
437      */
isVerifyValidInResMode(Utility utility)438     private static boolean isVerifyValidInResMode(Utility utility) {
439         if (!isPathValid(utility.getPackInfoPath(), TYPE_FILE, PACK_INFO)) {
440             LOG.error("CompressVerify::isArgsValidInResMode pack-info-path is invalid.");
441             return false;
442         }
443 
444         if (!isDirectoryValidStrictCase(utility.getEntryCardPath(), ENTRY_CARD_DIRECTORY_NAME)) {
445             LOG.error("CompressVerify::isArgsValidInResMode the level-1 directory name must is EntryCard" +
446                 ", current is " + utility.getEntryCardPath());
447             return false;
448         }
449         if (!compatibleProcess(utility, utility.getEntryCardPath(),
450                 utility.getformattedEntryCardPathList(), PNG_SUFFIX)) {
451             LOG.error("CompressVerify::isArgsValidInResMode entrycard-path is invalid.");
452             return false;
453         }
454         return isOutPathValid(utility, RES_SUFFIX);
455     }
456 
isVerifyValidInHQFMode(Utility utility)457     private static boolean isVerifyValidInHQFMode(Utility utility) {
458         if (utility.getJsonPath().isEmpty()) {
459             LOG.error("Error: must input patch.json file when pack hqf file.");
460             return false;
461         }
462         if (utility.getEtsPath().isEmpty() || !isPathValid(utility.getEtsPath(), TYPE_DIR, null)) {
463             LOG.error("Error: must input valid ets path when pack hqf file.");
464             return false;
465         }
466         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, PATCH_PROFILE)) {
467             LOG.error("Error: input patch.json is invalid when pack hqf file.");
468             return false;
469         }
470         if (!utility.getLibPath().isEmpty()) {
471             if (!isPathValid(utility.getLibPath(), TYPE_DIR, null)) {
472                 LOG.error("Error: input lib path is invalid when pack hqf file.");
473                 return false;
474             }
475         }
476         File outFile = new File(utility.getOutPath());
477         if ((FALSE.equals(utility.getForceRewrite())) && (outFile.exists())) {
478             LOG.error("Error: " + outFile.getName() + " already exist.");
479             return false;
480         }
481         if (!utility.getOutPath().endsWith(HQF_SUFFIX)) {
482             LOG.error("Error: input out file must end with .hqf.");
483             return false;
484         }
485         return true;
486     }
487 
isVerifyValidInAPPQFMode(Utility utility)488     private static boolean isVerifyValidInAPPQFMode(Utility utility) {
489         if (utility.getHqfList().isEmpty()) {
490             LOG.error("Error: input hqf list is empty.");
491             return false;
492         }
493         if (!compatibleProcess(utility, utility.getHqfList(), utility.getFormatedHQFList(), HQF_SUFFIX)) {
494             LOG.error("Error: input hqf list is invalid.");
495             return false;
496         }
497         File outFile = new File(utility.getOutPath());
498         if ((FALSE.equals(utility.getForceRewrite())) && outFile.exists()) {
499             LOG.error("Error out file already existed.");
500             return false;
501         }
502         if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(APPQF_SUFFIX)) {
503             LOG.error("Error out-path must end with .app.");
504             return false;
505         }
506         return true;
507     }
508 
509     /**
510      * Compatible file input and directory input
511      *
512      * @param utility   common data
513      * @param inputPath input path
514      * @param fileList  save files' path with list
515      * @param suffix    process type
516      * @return Returns {@code true} if the compatible is successful; returns {@code false} otherwise.
517      */
compatibleProcess(Utility utility, String inputPath, List<String> fileList, String suffix)518     private static boolean compatibleProcess(Utility utility, String inputPath,
519         List<String> fileList, String suffix) {
520         if (isPathValid(inputPath, TYPE_DIR, null)) {
521             File inputFile = new File(inputPath);
522             File[] files = inputFile.listFiles();
523             if (files == null) {
524                 return true;
525             }
526             for (File fileItem : files) {
527                 if (fileItem.getName().toLowerCase(Locale.ENGLISH).endsWith(suffix)) {
528                     fileList.add(fileItem.toString());
529                 }
530             }
531             return true;
532         } else {
533             String formattedPathItem = "";
534             List<String> pathList = removeDuplicatePath(inputPath);
535             for (String pathItem : pathList) {
536                 formattedPathItem = utility.getFormattedPath(pathItem);
537                 if (!isPathValid(formattedPathItem, TYPE_FILE, suffix)) {
538                     return false;
539                 }
540                 fileList.add(formattedPathItem);
541             }
542             return true;
543         }
544     }
545 
splitDirList(Utility utility, String dirList, List<String> fileList)546     private static boolean splitDirList(Utility utility, String dirList, List<String> fileList) {
547         List<String> pathList = removeDuplicatePath(dirList);
548         for (String pathItem : pathList) {
549             String formattedPathItem = utility.getFormattedPath(pathItem);
550             if (!isPathValid(formattedPathItem, TYPE_DIR, null)) {
551                 return false;
552             }
553             fileList.add(formattedPathItem);
554         }
555         return true;
556     }
557 
558     /**
559      * turn input path block to formatted path list
560      *
561      * @param utility common data
562      * @param suffix  used to determine type
563      * @return isVerifyValidInAppMode if verify valid in app mode.
564      */
isOutPathValid(Utility utility, String suffix)565     private static boolean isOutPathValid(Utility utility, String suffix) {
566         File outFile = new File(utility.getOutPath());
567 
568         if (("false".equals(utility.getForceRewrite())) && (outFile.exists())) {
569             LOG.error("CompressVerify::isOutPathValid out file already existed.");
570             return false;
571         }
572 
573         if (HAP_SUFFIX.equals(suffix)) {
574             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(HAP_SUFFIX)) {
575                 LOG.error("CompressVerify::isOutPathValid out-path must end with .hap.");
576                 return false;
577             } else {
578                 return true;
579             }
580         }
581 
582         if (HAR_SUFFIX.equals(suffix)) {
583             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(HAR_SUFFIX)) {
584                 LOG.error("CompressVerify::isOutPathValid out-path must end with .har.");
585                 return false;
586             } else {
587                 return true;
588             }
589         }
590 
591         if (APP_SUFFIX.equals(suffix)) {
592             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(APP_SUFFIX)) {
593                 LOG.error("CompressVerify::isOutPathValid out-path must end with .app.");
594                 return false;
595             } else {
596                 return true;
597             }
598         }
599 
600         if (RES_SUFFIX.equals(suffix)) {
601             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(RES_SUFFIX)) {
602                 LOG.error("CompressVerify::isOutPathValid out-path must end with .res.");
603                 return false;
604             } else {
605                 return true;
606             }
607         }
608 
609         if (HSP_SUFFIX.equals(suffix)) {
610             if (!outFile.getName().toLowerCase(Locale.ENGLISH).endsWith(HSP_SUFFIX)) {
611                 LOG.error("CompressVerify::isOutPathValid out-path must end with .hsp.");
612                 return false;
613             } else {
614                 return true;
615             }
616         }
617         return false;
618     }
619 
620     /**
621      * check path if valid
622      *
623      * @param path   path input
624      * @param isFile type input
625      * @param flag   flag input
626      * @return isPathValid if path verify
627      */
isPathValid(String path, boolean isFile, String flag)628     private static boolean isPathValid(String path, boolean isFile, String flag) {
629         File file = new File(path);
630         if (isFile && (file.isFile()) && file.getName().toLowerCase(Locale.ENGLISH).endsWith(flag)) {
631             return true;
632         }
633         return (!isFile) && file.isDirectory();
634     }
635 
isDirectoryValidStrictCase(String path, String directoryName)636     private static boolean isDirectoryValidStrictCase(String path, String directoryName) {
637         File file = new File(path);
638         if (!file.exists()) {
639             LOG.error("CompressVerify::isDirectoryValidStrictCase directory is not exist, directoryPath: "
640                 + path + ".");
641             return false;
642         }
643         if (file.isDirectory()) {
644             return directoryName.equals(file.getName());
645         }
646         return false;
647     }
648 
649     /**
650      * remove duplicate in path.
651      *
652      * @param path input path, use comma separate.
653      * @return result list
654      */
removeDuplicatePath(String path)655     private static List<String> removeDuplicatePath(String path) {
656         String[] array = path.split(COMMA_SPLIT);
657         List<String> list = new ArrayList<>();
658 
659         for (String item : array) {
660             if (!list.contains(item)) {
661                 list.add(item);
662             }
663         }
664         return list;
665     }
666 
isVerifyValidInHspMode(Utility utility)667     private static boolean isVerifyValidInHspMode(Utility utility) {
668         if (utility.getJsonPath().isEmpty()) {
669             LOG.error("CompressVerify::isArgsValidInHspMode json-path is empty.");
670             return false;
671         }
672 
673         if (!isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) {
674             LOG.error("CompressVerify::isArgsValidInHspMode json-path must be module.json file.");
675             return false;
676         }
677 
678         if (!utility.getJarPath().isEmpty()
679                 && !compatibleProcess(utility, utility.getJarPath(), utility.getFormattedJarPathList(), JAR_SUFFIX)) {
680             LOG.error("CompressVerify::isArgsValidInHspMode jar-path is invalid.");
681             return false;
682         }
683 
684         if (!utility.getTxtPath().isEmpty()
685                 && !compatibleProcess(utility, utility.getTxtPath(), utility.getFormattedTxtPathList(), TXT_SUFFIX)) {
686             LOG.error("CompressVerify::isArgsValidInHspMode txt-path is invalid.");
687             return false;
688         }
689 
690         if (!utility.getLibPath().isEmpty() && !isPathValid(utility.getLibPath(), TYPE_DIR, null)) {
691             LOG.error("CompressVerify::isArgsValidInHspMode lib-path is invalid.");
692             return false;
693         }
694 
695         if (!utility.getResPath().isEmpty() && !isPathValid(utility.getResPath(), TYPE_DIR, null)) {
696             LOG.error("CompressVerify::isArgsValidInHspMode res-path is invalid.");
697             return false;
698         }
699 
700         if (!utility.getResourcesPath().isEmpty() && !isPathValid(utility.getResourcesPath(), TYPE_DIR, null)) {
701             LOG.error("CompressVerify::isArgsValidInHspMode resources-path is invalid.");
702             return false;
703         }
704 
705         if (!utility.getAssetsPath().isEmpty() && !isPathValid(utility.getAssetsPath(), TYPE_DIR, null)) {
706             LOG.error("CompressVerify::isArgsValidInHspMode assets-path is invalid.");
707             return false;
708         }
709 
710         return isOutPathValid(utility, HSP_SUFFIX);
711     }
712 }
713