1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package ohos;
16
17import java.util.Collections;
18import java.util.List;
19
20/**
21 * Collections of method for HQF verify.
22 *
23 */
24class HQFVerify {
25    private static final Log LOG = new Log(HQFVerify.class.toString());
26
27    /**
28     * check hqf is valid.
29     *
30     * @param hqfVerifyInfos is the collection of hqf infos
31     * @return the result
32     */
33    public static boolean checkHQFIsValid(List<HQFInfo> hqfVerifyInfos) {
34        // check app fields
35        if (hqfVerifyInfos.isEmpty()) {
36            LOG.error("input hqf file is empty.");
37            return false;
38        }
39        if (!checkAppFields(hqfVerifyInfos)) {
40            LOG.error("input hqf file has different fields in app.");
41            return false;
42        }
43        if (!checkModuleIsValid(hqfVerifyInfos)) {
44            LOG.error("input hqf file moduleName is invalid.");
45            return false;
46        }
47        return true;
48    }
49
50    /**
51     * check hqf has same app fields.
52     *
53     * @param hqfVerifyInfos is the collection of hqf infos
54     * @return the result
55     */
56    private static boolean checkAppFields(List<HQFInfo> hqfVerifyInfos) {
57        String bundleName = hqfVerifyInfos.get(0).getBundleName();
58        int versionCode = hqfVerifyInfos.get(0).getVersionCode();
59        String versionName = hqfVerifyInfos.get(0).getVersionName();
60        int patchVersionCode = hqfVerifyInfos.get(0).getPatchVersionCode();
61        String patchVersionName = hqfVerifyInfos.get(0).getPatchVersionName();
62        for (HQFInfo hqfVerifyInfo : hqfVerifyInfos) {
63            if (bundleName == null || !bundleName.equals(hqfVerifyInfo.getBundleName())) {
64                LOG.error("input hqf file has different bundleName.");
65                return false;
66            }
67            if (versionCode != hqfVerifyInfo.getVersionCode()) {
68                LOG.error("input hqf file has different versionCode.");
69                return false;
70            }
71            if (versionName == null || !versionName.equals(hqfVerifyInfo.getVersionName())) {
72                LOG.error("input hqf file has different versionName.");
73                return false;
74            }
75            if (patchVersionCode != hqfVerifyInfo.getPatchVersionCode()) {
76                LOG.error("input hqf file has different patchVersionCode.");
77                return false;
78            }
79            if (patchVersionName == null || !patchVersionName.equals(hqfVerifyInfo.getPatchVersionName())) {
80                LOG.error("input hqf file has different patchVersionName.");
81                return false;
82            }
83        }
84        return true;
85    }
86
87    /**
88     * check hqf module is valid.
89     *
90     * @param hqfVerifyInfos is the collection of hqf infos
91     * @return the result
92     */
93    private static boolean checkModuleIsValid(List<HQFInfo> hqfVerifyInfos) {
94        for (int i = 0; i < hqfVerifyInfos.size(); ++i) {
95            for (int j = i + 1; j < hqfVerifyInfos.size(); ++j) {
96                if (checkModuleIsDuplicated(hqfVerifyInfos.get(i), hqfVerifyInfos.get(j))) {
97                    LOG.error("input hqf file moduleName duplicated.");
98                    return false;
99                }
100            }
101        }
102
103        return true;
104    }
105
106    /**
107     * check module name duplicated.
108     *
109     * @param hqfVerifyInfoLeft is one HQFVerifyInfo
110     * @param hqfVerifyInfoRight is another HQFVerifyInfo
111     * @return the result
112     */
113    private static boolean checkModuleIsDuplicated(HQFInfo hqfVerifyInfoLeft, HQFInfo hqfVerifyInfoRight) {
114        if (!hqfVerifyInfoLeft.getModuleName().equals(hqfVerifyInfoRight.getModuleName())) {
115            return false;
116        }
117        if (Collections.disjoint(hqfVerifyInfoLeft.getDeviceTypes(), hqfVerifyInfoRight.getDeviceTypes())) {
118            return false;
119        }
120        return true;
121    }
122}
123