1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package ohos;
18
19import java.math.BigDecimal;
20import java.util.regex.Pattern;
21
22/**
23 * scanVerify info
24 *
25 * @since 2023/11/23
26 */
27
28public class ScanVerify {
29
30    static final String HAP = ".hap";
31    static final String HSP = ".hsp";
32    static final String APP = ".app";
33    private static final String EMPTY_STRING = "";
34    private static final Pattern PATTERN = Pattern.compile("[0-9]*");
35    private static final Long MAX_VALUE = 4294967295L;
36
37    private static final Log LOG = new Log(ScanVerify.class.toString());
38
39    /**
40     * if args valid.
41     *
42     * @param utility common data
43     * @return commandVerify if command valid.
44     */
45    public static boolean commandVerify(Utility utility) {
46        if (utility == null) {
47            LOG.error(ScanErrorEnum.SCAN_VERIFY_UTILITY_EMPTY_ERROR.toString());
48            return false;
49        }
50
51        if (utility.getInput().isEmpty()) {
52            LOG.error(ScanErrorEnum.SCAN_VERIFY_INPUT_EMPTY_ERROR.toString());
53            return false;
54        }
55
56        if (!(utility.getInput().endsWith(HAP) || utility.getInput().endsWith(HSP)
57                || utility.getInput().endsWith(APP))) {
58            LOG.error(ScanErrorEnum.SCAN_VERIFY_INPUT_INVALID_ERROR.toString());
59            return false;
60        }
61
62        if (utility.getOutPath().isEmpty()) {
63            LOG.error(ScanErrorEnum.SCAN_VERIFY_OUT_PATH_EMPTY_ERROR.toString());
64            return false;
65        }
66
67        if (!utility.getStatFileSize().isEmpty()) {
68            if (!PATTERN.matcher(utility.getStatFileSize()).matches()) {
69                LOG.error(ScanErrorEnum.SCAN_VERIFY_STAT_FILE_SIZE_INVALID_ERROR.toString());
70                return false;
71            }
72            if (new BigDecimal(utility.getStatFileSize()).compareTo(BigDecimal.ZERO) < 0
73                    || new BigDecimal(utility.getStatFileSize()).compareTo(new BigDecimal(MAX_VALUE)) > 0) {
74                LOG.error(ScanErrorEnum.SCAN_VERIFY_STAT_FILE_SIZE_INVALID_ERROR.toString());
75                return false;
76            }
77        }
78
79        if (!utility.getStatSuffix() && !utility.getStatDuplicate()
80                && EMPTY_STRING.equals(utility.getStatFileSize())) {
81            LOG.error(ScanErrorEnum.SCAN_VERIFY_STAT_PARAMETER_EMPTY_ERROR.toString());
82        }
83
84        return true;
85    }
86}
87