• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertNotNull;
21 import static org.junit.Assert.assertThrows;
22 
23 import android.aconfig.DeviceProtosTestUtil;
24 import android.aconfig.nano.Aconfig;
25 import android.aconfig.nano.Aconfig.parsed_flag;
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.AconfigPackage;
31 import android.os.flagging.AconfigStorageReadException;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 import java.io.IOException;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 
42 @RunWith(JUnit4.class)
43 public class AconfigPackageTests {
44 
45     @Test
testAconfigPackage_StorageFilesCache()46     public void testAconfigPackage_StorageFilesCache() throws IOException {
47         List<parsed_flag> flags = DeviceProtosTestUtil.loadAndParseFlagProtos();
48         for (parsed_flag flag : flags) {
49             if (flag.permission == Aconfig.READ_ONLY && flag.state == Aconfig.DISABLED) {
50                 continue;
51             }
52             String container = flag.container;
53             String packageName = flag.package_;
54             assertNotNull(AconfigPackage.load(packageName));
55         }
56     }
57 
58     @Test
testExternalAconfigPackageInstance()59     public void testExternalAconfigPackageInstance() throws IOException {
60         List<parsed_flag> flags = DeviceProtosTestUtil.loadAndParseFlagProtos();
61         Map<String, AconfigPackage> readerMap = new HashMap<>();
62         StorageFileProvider fp = StorageFileProvider.getDefaultProvider();
63 
64         for (parsed_flag flag : flags) {
65             if (flag.permission == Aconfig.READ_ONLY && flag.state == Aconfig.DISABLED) {
66                 continue;
67             }
68             String container = flag.container;
69             String packageName = flag.package_;
70             String flagName = flag.name;
71 
72             PackageTable pTable = fp.getPackageTable(container);
73             PackageTable.Node pNode = pTable.get(packageName);
74             FlagTable fTable = fp.getFlagTable(container);
75             FlagTable.Node fNode = fTable.get(pNode.getPackageId(), flagName);
76             FlagValueList fList = fp.getFlagValueList(container);
77             boolean rVal = fList.getBoolean(pNode.getBooleanStartIndex() + fNode.getFlagIndex());
78 
79             AconfigPackage reader = readerMap.get(packageName);
80             if (reader == null) {
81                 reader = AconfigPackage.load(packageName);
82                 readerMap.put(packageName, reader);
83             }
84             boolean jVal = reader.getBooleanFlagValue(flagName, false);
85 
86             assertEquals(rVal, jVal);
87         }
88     }
89 
90     @Test
testAconfigPackage_load_withError()91     public void testAconfigPackage_load_withError() {
92         // load fake package
93         AconfigStorageReadException e =
94                 assertThrows(
95                         AconfigStorageReadException.class,
96                         () -> AconfigPackage.load("fake_package"));
97         assertEquals(AconfigStorageReadException.ERROR_PACKAGE_NOT_FOUND, e.getErrorCode());
98     }
99 }
100