1 /* 2 * Copyright (C) 2024 The Android Open Source Project 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 17 package android.os.flagging.test; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertThrows; 21 22 import android.aconfig.DeviceProtosTestUtil; 23 import android.aconfig.nano.Aconfig; 24 import android.aconfig.nano.Aconfig.parsed_flag; 25 import android.aconfig.storage.AconfigStorageException; 26 import android.aconfig.storage.FlagTable; 27 import android.aconfig.storage.FlagValueList; 28 import android.aconfig.storage.PackageTable; 29 import android.aconfig.storage.StorageFileProvider; 30 import android.os.flagging.AconfigPackageInternal; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 import java.io.IOException; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40 41 @RunWith(JUnit4.class) 42 public class AconfigPackageInternalTests { 43 @Test testAconfigPackageInternal_load()44 public void testAconfigPackageInternal_load() throws IOException { 45 List<parsed_flag> flags = DeviceProtosTestUtil.loadAndParseFlagProtos(); 46 Map<String, AconfigPackageInternal> readerMap = new HashMap<>(); 47 StorageFileProvider fp = StorageFileProvider.getDefaultProvider(); 48 49 for (parsed_flag flag : flags) { 50 if (flag.permission == Aconfig.READ_ONLY && flag.state == Aconfig.DISABLED) { 51 continue; 52 } 53 String container = flag.container; 54 String packageName = flag.package_; 55 String flagName = flag.name; 56 57 PackageTable pTable = fp.getPackageTable(container); 58 PackageTable.Node pNode = pTable.get(packageName); 59 FlagTable fTable = fp.getFlagTable(container); 60 FlagTable.Node fNode = fTable.get(pNode.getPackageId(), flagName); 61 FlagValueList fList = fp.getFlagValueList(container); 62 63 int index = pNode.getBooleanStartIndex() + fNode.getFlagIndex(); 64 boolean rVal = fList.getBoolean(index); 65 66 long fingerprint = pNode.getPackageFingerprint(); 67 68 AconfigPackageInternal reader = readerMap.get(packageName); 69 if (reader == null) { 70 reader = AconfigPackageInternal.load(packageName, fingerprint); 71 readerMap.put(packageName, reader); 72 } 73 boolean jVal = reader.getBooleanFlagValue(fNode.getFlagIndex()); 74 75 assertEquals(rVal, jVal); 76 } 77 } 78 79 @Test testAconfigPackageInternal_load_withError()80 public void testAconfigPackageInternal_load_withError() throws IOException { 81 // package not found 82 AconfigStorageException e = 83 assertThrows( 84 AconfigStorageException.class, 85 () -> AconfigPackageInternal.load("fake_package", 0)); 86 assertEquals(AconfigStorageException.ERROR_PACKAGE_NOT_FOUND, e.getErrorCode()); 87 88 // fingerprint doesn't match 89 List<parsed_flag> flags = DeviceProtosTestUtil.loadAndParseFlagProtos(); 90 StorageFileProvider fp = StorageFileProvider.getDefaultProvider(); 91 92 parsed_flag flag = flags.get(0); 93 94 String container = flag.container; 95 String packageName = flag.package_; 96 97 PackageTable pTable = fp.getPackageTable(container); 98 PackageTable.Node pNode = pTable.get(packageName); 99 if (pNode.hasPackageFingerprint()) { 100 long fingerprint = pNode.getPackageFingerprint(); 101 e = 102 assertThrows( 103 AconfigStorageException.class, 104 () -> AconfigPackageInternal.load(packageName, fingerprint + 1)); 105 assertEquals(AconfigStorageException.ERROR_FILE_FINGERPRINT_MISMATCH, e.getErrorCode()); 106 } 107 } 108 } 109