xref: /aosp_15_r20/frameworks/base/core/tests/systemproperties/src/android/os/SystemPropertiesTest.java (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2011 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;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 
26 import android.platform.test.ravenwood.RavenwoodRule;
27 
28 import androidx.test.filters.SmallTest;
29 
30 import org.junit.Rule;
31 import org.junit.Test;
32 
33 import java.util.Objects;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 
37 public class SystemPropertiesTest {
38     private static final String KEY = "sys.testkey";
39     private static final String UNSET_KEY = "Aiw7woh6ie4toh7W";
40     private static final String PERSIST_KEY = "persist.sys.testkey";
41     private static final String NONEXIST_KEY = "doesnotexist_2341431";
42 
43     @Rule
44     public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder()
45             .setSystemPropertyMutable(KEY, null)
46             .setSystemPropertyMutable(UNSET_KEY, null)
47             .setSystemPropertyMutable(PERSIST_KEY, null)
48             .setSystemPropertyImmutable(NONEXIST_KEY, null)
49             .build();
50 
51     @Test
52     @SmallTest
testStressPersistPropertyConsistency()53     public void testStressPersistPropertyConsistency() throws Exception {
54         for (int i = 0; i < 100; ++i) {
55             SystemProperties.set(PERSIST_KEY, Long.toString(i));
56             long ret = SystemProperties.getLong(PERSIST_KEY, -1);
57             assertEquals(i, ret);
58         }
59     }
60 
61     @Test
62     @SmallTest
testStressMemoryPropertyConsistency()63     public void testStressMemoryPropertyConsistency() throws Exception {
64         for (int i = 0; i < 100; ++i) {
65             SystemProperties.set(KEY, Long.toString(i));
66             long ret = SystemProperties.getLong(KEY, -1);
67             assertEquals(i, ret);
68         }
69     }
70 
71     @Test
72     @SmallTest
testProperties()73     public void testProperties() throws Exception {
74         String value;
75 
76         SystemProperties.set(KEY, "");
77         value = SystemProperties.get(KEY, "default");
78         assertEquals("default", value);
79 
80         // null default value is the same as "".
81         SystemProperties.set(KEY, null);
82         value = SystemProperties.get(KEY, "default");
83         assertEquals("default", value);
84 
85         SystemProperties.set(KEY, "SA");
86         value = SystemProperties.get(KEY, "default");
87         assertEquals("SA", value);
88 
89         value = SystemProperties.get(KEY);
90         assertEquals("SA", value);
91 
92         SystemProperties.set(KEY, "");
93         value = SystemProperties.get(KEY, "default");
94         assertEquals("default", value);
95 
96         // null value is the same as "".
97         SystemProperties.set(KEY, "SA");
98         SystemProperties.set(KEY, null);
99         value = SystemProperties.get(KEY, "default");
100         assertEquals("default", value);
101 
102         value = SystemProperties.get(KEY);
103         assertEquals("", value);
104     }
105 
testInt(String setVal, int defValue, int expected)106     private static void testInt(String setVal, int defValue, int expected) {
107       SystemProperties.set(KEY, setVal);
108       int value = SystemProperties.getInt(KEY, defValue);
109       assertEquals(expected, value);
110     }
111 
testLong(String setVal, long defValue, long expected)112     private static void testLong(String setVal, long defValue, long expected) {
113       SystemProperties.set(KEY, setVal);
114       long value = SystemProperties.getLong(KEY, defValue);
115       assertEquals(expected, value);
116     }
117 
118     @Test
119     @SmallTest
testHandle()120     public void testHandle() throws Exception {
121         String value;
122         SystemProperties.Handle handle = SystemProperties.find(NONEXIST_KEY);
123         assertNull(handle);
124         SystemProperties.set(KEY, "abc");
125         handle = SystemProperties.find(KEY);
126         assertNotNull(handle);
127         value = handle.get();
128         assertEquals("abc", value);
129         SystemProperties.set(KEY, "blarg");
130         value = handle.get();
131         assertEquals("blarg", value);
132         SystemProperties.set(KEY, "1");
133         assertEquals(1, handle.getInt(-1));
134         assertEquals(1, handle.getLong(-1));
135         assertEquals(true, handle.getBoolean(false));
136         SystemProperties.set(KEY, "");
137         assertEquals(12345, handle.getInt(12345));
138     }
139 
140     @Test
141     @SmallTest
testIntegralProperties()142     public void testIntegralProperties() throws Exception {
143         testInt("", 123, 123);
144         testInt("", 0, 0);
145         testInt("", -123, -123);
146 
147         testInt("123", 124, 123);
148         testInt("0", 124, 0);
149         testInt("-123", 124, -123);
150 
151         testLong("", 3147483647L, 3147483647L);
152         testLong("", 0, 0);
153         testLong("", -3147483647L, -3147483647L);
154 
155         testLong("3147483647", 124, 3147483647L);
156         testLong("0", 124, 0);
157         testLong("-3147483647", 124, -3147483647L);
158     }
159 
160     @Test
161     @SmallTest
testUnset()162     public void testUnset() throws Exception {
163         assertEquals("abc", SystemProperties.get(UNSET_KEY, "abc"));
164         assertEquals(true, SystemProperties.getBoolean(UNSET_KEY, true));
165         assertEquals(false, SystemProperties.getBoolean(UNSET_KEY, false));
166         assertEquals(5, SystemProperties.getInt(UNSET_KEY, 5));
167         assertEquals(-10, SystemProperties.getLong(UNSET_KEY, -10));
168     }
169 
170     @Test
171     @SmallTest
172     @SuppressWarnings("null")
testNullKey()173     public void testNullKey() throws Exception {
174         try {
175             SystemProperties.get(null);
176             fail("Expected NullPointerException");
177         } catch (NullPointerException npe) {
178         }
179 
180         try {
181             SystemProperties.get(null, "default");
182             fail("Expected NullPointerException");
183         } catch (NullPointerException npe) {
184         }
185 
186         try {
187             SystemProperties.set(null, "value");
188             fail("Expected NullPointerException");
189         } catch (NullPointerException npe) {
190         }
191 
192         try {
193             SystemProperties.getInt(null, 0);
194             fail("Expected NullPointerException");
195         } catch (NullPointerException npe) {
196         }
197 
198         try {
199             SystemProperties.getLong(null, 0);
200             fail("Expected NullPointerException");
201         } catch (NullPointerException npe) {
202         }
203     }
204 
205     @Test
206     @SmallTest
testCallbacks()207     public void testCallbacks() {
208         // Latches are not really necessary, but are easy to use.
209         final CountDownLatch wait1 = new CountDownLatch(1);
210         final CountDownLatch wait2 = new CountDownLatch(1);
211 
212         Runnable r1 = new Runnable() {
213             boolean done = false;
214             @Override
215             public void run() {
216                 if (done) {
217                     return;
218                 }
219                 done = true;
220 
221                 wait1.countDown();
222                 throw new RuntimeException("test");
223             }
224         };
225 
226         Runnable r2 = new Runnable() {
227             @Override
228             public void run() {
229                 wait2.countDown();
230             }
231         };
232 
233         SystemProperties.addChangeCallback(r1);
234         SystemProperties.addChangeCallback(r2);
235 
236         SystemProperties.reportSyspropChanged();
237 
238         try {
239             assertTrue(wait1.await(5, TimeUnit.SECONDS));
240         } catch (InterruptedException e) {
241             fail("InterruptedException");
242         }
243         try {
244             assertTrue(wait2.await(5, TimeUnit.SECONDS));
245         } catch (InterruptedException e) {
246             fail("InterruptedException");
247         }
248     }
249 
250     @Test
251     @SmallTest
testDigestOf()252     public void testDigestOf() {
253         final String empty = SystemProperties.digestOf();
254         final String finger = SystemProperties.digestOf("ro.build.fingerprint");
255         final String fingerBrand = SystemProperties.digestOf(
256                 "ro.build.fingerprint", "ro.product.brand");
257         final String brandFinger = SystemProperties.digestOf(
258                 "ro.product.brand", "ro.build.fingerprint");
259 
260         // Shouldn't change over time
261         assertTrue(Objects.equals(finger, SystemProperties.digestOf("ro.build.fingerprint")));
262 
263         // Different properties means different results
264         assertFalse(Objects.equals(empty, finger));
265         assertFalse(Objects.equals(empty, fingerBrand));
266         assertFalse(Objects.equals(finger, fingerBrand));
267 
268         // Same properties means same result
269         assertTrue(Objects.equals(fingerBrand, brandFinger));
270     }
271 }
272