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 */ 15 package ohos; 16 17 import java.util.Collections; 18 import java.util.List; 19 20 /** 21 * Collections of method for HQF verify. 22 * 23 */ 24 class 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 */ checkHQFIsValid(List<HQFInfo> hqfVerifyInfos)33 public static boolean checkHQFIsValid(List<HQFInfo> hqfVerifyInfos) { 34 // check app fields 35 if (hqfVerifyInfos.isEmpty()) { 36 LOG.error(PackingToolErrMsg.CHECK_HQF_INVALID.toString("Input hqf file is empty.")); 37 return false; 38 } 39 if (!checkAppFields(hqfVerifyInfos)) { 40 LOG.error(PackingToolErrMsg.CHECK_HQF_INVALID.toString("Input hqf file has different fields in app.")); 41 return false; 42 } 43 if (!checkModuleIsValid(hqfVerifyInfos)) { 44 LOG.error(PackingToolErrMsg.CHECK_HQF_INVALID.toString("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 */ checkAppFields(List<HQFInfo> hqfVerifyInfos)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 String errMsg = "Input hqf file has different bundleName."; 65 LOG.error(PackingToolErrMsg.CHECK_APP_FIELDS_FAILED.toString(errMsg)); 66 return false; 67 } 68 if (versionCode != hqfVerifyInfo.getVersionCode()) { 69 String errMsg = "Input hqf file has different versionCode."; 70 LOG.error(PackingToolErrMsg.CHECK_APP_FIELDS_FAILED.toString(errMsg)); 71 return false; 72 } 73 if (versionName == null || !versionName.equals(hqfVerifyInfo.getVersionName())) { 74 String errMsg = "Input hqf file has different versionName."; 75 LOG.error(PackingToolErrMsg.CHECK_APP_FIELDS_FAILED.toString(errMsg)); 76 return false; 77 } 78 if (patchVersionCode != hqfVerifyInfo.getPatchVersionCode()) { 79 String errMsg = "Input hqf file has different patchVersionCode."; 80 LOG.error(PackingToolErrMsg.CHECK_APP_FIELDS_FAILED.toString(errMsg)); 81 return false; 82 } 83 if (patchVersionName == null || !patchVersionName.equals(hqfVerifyInfo.getPatchVersionName())) { 84 String errMsg = "Input hqf file has different patchVersionName."; 85 LOG.error(PackingToolErrMsg.CHECK_APP_FIELDS_FAILED.toString(errMsg)); 86 return false; 87 } 88 } 89 return true; 90 } 91 92 /** 93 * check hqf module is valid. 94 * 95 * @param hqfVerifyInfos is the collection of hqf infos 96 * @return the result 97 */ checkModuleIsValid(List<HQFInfo> hqfVerifyInfos)98 private static boolean checkModuleIsValid(List<HQFInfo> hqfVerifyInfos) { 99 for (int i = 0; i < hqfVerifyInfos.size(); ++i) { 100 for (int j = i + 1; j < hqfVerifyInfos.size(); ++j) { 101 if (checkModuleIsDuplicated(hqfVerifyInfos.get(i), hqfVerifyInfos.get(j))) { 102 String errMsg = "Input hqf file moduleName is duplicated."; 103 LOG.error(PackingToolErrMsg.CHECK_MODULE_INVALID.toString(errMsg)); 104 return false; 105 } 106 } 107 } 108 109 return true; 110 } 111 112 /** 113 * check module name duplicated. 114 * 115 * @param hqfVerifyInfoLeft is one HQFVerifyInfo 116 * @param hqfVerifyInfoRight is another HQFVerifyInfo 117 * @return the result 118 */ checkModuleIsDuplicated(HQFInfo hqfVerifyInfoLeft, HQFInfo hqfVerifyInfoRight)119 private static boolean checkModuleIsDuplicated(HQFInfo hqfVerifyInfoLeft, HQFInfo hqfVerifyInfoRight) { 120 if (!hqfVerifyInfoLeft.getModuleName().equals(hqfVerifyInfoRight.getModuleName())) { 121 return false; 122 } 123 if (Collections.disjoint(hqfVerifyInfoLeft.getDeviceTypes(), hqfVerifyInfoRight.getDeviceTypes())) { 124 return false; 125 } 126 return true; 127 } 128 } 129