• 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.aconfig.storage.test;
18 
19 import java.io.IOException;
20 import java.nio.MappedByteBuffer;
21 import java.nio.ByteBuffer;
22 import java.nio.ByteOrder;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Random;
26 
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 import android.aconfig.storage.AconfigStorageReadAPI;
35 import android.aconfig.storage.PackageReadContext;
36 import android.aconfig.storage.FlagReadContext;
37 import android.aconfig.storage.FlagReadContext.StoredFlagType;
38 
39 @RunWith(JUnit4.class)
40 public class AconfigStorageReadAPITest{
41 
42     private String mStorageDir = "/data/local/tmp/aconfig_java_api_test";
43 
44     @Test
testPackageContextQuery()45     public void testPackageContextQuery() {
46         MappedByteBuffer packageMap = null;
47         try {
48             packageMap = AconfigStorageReadAPI.mapStorageFile(
49                 mStorageDir + "/maps/mockup.package.map");
50         } catch(IOException ex){
51             assertTrue(ex.toString(), false);
52         }
53         assertTrue(packageMap != null);
54 
55         try {
56             PackageReadContext context = AconfigStorageReadAPI.getPackageReadContext(
57                 packageMap, "com.android.aconfig.storage.test_1");
58             assertEquals(context.mPackageId, 0);
59             assertEquals(context.mBooleanStartIndex, 0);
60 
61             context = AconfigStorageReadAPI.getPackageReadContext(
62                 packageMap, "com.android.aconfig.storage.test_2");
63             assertEquals(context.mPackageId, 1);
64             assertEquals(context.mBooleanStartIndex, 3);
65 
66             context = AconfigStorageReadAPI.getPackageReadContext(
67                 packageMap, "com.android.aconfig.storage.test_4");
68             assertEquals(context.mPackageId, 2);
69             assertEquals(context.mBooleanStartIndex, 6);
70         } catch (IOException ex) {
71             assertTrue(ex.toString(), false);
72         }
73     }
74 
75     @Test
testNonExistPackageContextQuery()76     public void testNonExistPackageContextQuery() {
77         MappedByteBuffer packageMap = null;
78         try {
79             packageMap = AconfigStorageReadAPI.mapStorageFile(
80                 mStorageDir + "/maps/mockup.package.map");
81         } catch(IOException ex){
82             assertTrue(ex.toString(), false);
83         }
84         assertTrue(packageMap != null);
85 
86         try {
87             PackageReadContext context = AconfigStorageReadAPI.getPackageReadContext(
88                 packageMap, "unknown");
89             assertEquals(context.mPackageId, -1);
90             assertEquals(context.mBooleanStartIndex, -1);
91         } catch(IOException ex){
92             assertTrue(ex.toString(), false);
93         }
94     }
95 
96     @Test
testFlagContextQuery()97     public void testFlagContextQuery() {
98         MappedByteBuffer flagMap = null;
99         try {
100             flagMap = AconfigStorageReadAPI.mapStorageFile(
101                 mStorageDir + "/maps/mockup.flag.map");
102         } catch(IOException ex){
103             assertTrue(ex.toString(), false);
104         }
105         assertTrue(flagMap!= null);
106 
107         class Baseline {
108             public int mPackageId;
109             public String mFlagName;
110             public StoredFlagType mFlagType;
111             public int mFlagIndex;
112 
113             public Baseline(int packageId,
114                     String flagName,
115                     StoredFlagType flagType,
116                     int flagIndex) {
117                 mPackageId = packageId;
118                 mFlagName = flagName;
119                 mFlagType = flagType;
120                 mFlagIndex = flagIndex;
121             }
122         }
123 
124         List<Baseline> baselines = new ArrayList();
125         baselines.add(new Baseline(0, "enabled_ro", StoredFlagType.ReadOnlyBoolean, 1));
126         baselines.add(new Baseline(0, "enabled_rw", StoredFlagType.ReadWriteBoolean, 2));
127         baselines.add(new Baseline(2, "enabled_rw", StoredFlagType.ReadWriteBoolean, 1));
128         baselines.add(new Baseline(1, "disabled_rw", StoredFlagType.ReadWriteBoolean, 0));
129         baselines.add(new Baseline(1, "enabled_fixed_ro", StoredFlagType.FixedReadOnlyBoolean, 1));
130         baselines.add(new Baseline(1, "enabled_ro", StoredFlagType.ReadOnlyBoolean, 2));
131         baselines.add(new Baseline(2, "enabled_fixed_ro", StoredFlagType.FixedReadOnlyBoolean, 0));
132         baselines.add(new Baseline(0, "disabled_rw", StoredFlagType.ReadWriteBoolean, 0));
133 
134         try {
135             for (Baseline baseline : baselines) {
136                 FlagReadContext context = AconfigStorageReadAPI.getFlagReadContext(
137                     flagMap, baseline.mPackageId,  baseline.mFlagName);
138                 assertEquals(context.mFlagType, baseline.mFlagType);
139                 assertEquals(context.mFlagIndex, baseline.mFlagIndex);
140             }
141         } catch (IOException ex) {
142             assertTrue(ex.toString(), false);
143         }
144     }
145 
146     @Test
testNonExistFlagContextQuery()147     public void testNonExistFlagContextQuery() {
148         MappedByteBuffer flagMap = null;
149         try {
150             flagMap = AconfigStorageReadAPI.mapStorageFile(
151                 mStorageDir + "/maps/mockup.flag.map");
152         } catch(IOException ex){
153             assertTrue(ex.toString(), false);
154         }
155         assertTrue(flagMap!= null);
156 
157         try {
158             FlagReadContext context = AconfigStorageReadAPI.getFlagReadContext(
159                 flagMap, 0,  "unknown");
160             assertEquals(context.mFlagType, null);
161             assertEquals(context.mFlagIndex, -1);
162 
163             context = AconfigStorageReadAPI.getFlagReadContext(
164                 flagMap, 3,  "enabled_ro");
165             assertEquals(context.mFlagType, null);
166             assertEquals(context.mFlagIndex, -1);
167         } catch (IOException ex) {
168             assertTrue(ex.toString(), false);
169         }
170     }
171 
172     @Test
testBooleanFlagValueQuery()173     public void testBooleanFlagValueQuery() {
174         MappedByteBuffer flagVal = null;
175         try {
176             flagVal = AconfigStorageReadAPI.mapStorageFile(
177                 mStorageDir + "/boot/mockup.val");
178         } catch (IOException ex) {
179             assertTrue(ex.toString(), false);
180         }
181         assertTrue(flagVal!= null);
182 
183         boolean[] baselines = {false, true, true, false, true, true, true, true};
184         for (int i = 0; i < 8; ++i) {
185             try {
186                 Boolean value = AconfigStorageReadAPI.getBooleanFlagValue(flagVal, i);
187                 assertEquals(value, baselines[i]);
188             } catch (IOException ex) {
189                 assertTrue(ex.toString(), false);
190             }
191         }
192     }
193 
194     @Test
testInvalidBooleanFlagValueQuery()195     public void testInvalidBooleanFlagValueQuery() {
196         MappedByteBuffer flagVal = null;
197         try {
198             flagVal = AconfigStorageReadAPI.mapStorageFile(
199                 mStorageDir + "/boot/mockup.val");
200         } catch (IOException ex) {
201             assertTrue(ex.toString(), false);
202         }
203         assertTrue(flagVal!= null);
204 
205         try {
206             Boolean value = AconfigStorageReadAPI.getBooleanFlagValue(flagVal, 9);
207             assertTrue("should throw", false);
208         } catch (IOException ex) {
209             String expectedErrmsg = "invalid storage file byte offset";
210             assertTrue(ex.toString(), ex.toString().contains(expectedErrmsg));
211         }
212     }
213  }
214