blob: 9f70538191c20e188d692fccd8db87ae24719f8e [file] [log] [blame]
satokc91437d2011-03-03 13:54:38 -08001/*
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
17package android.os;
18
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -070019import android.test.suitebuilder.annotation.SmallTest;
Andreas Gampe7074e6f2018-03-16 16:14:29 -070020
satokc91437d2011-03-03 13:54:38 -080021import junit.framework.TestCase;
22
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -070023import java.util.Objects;
24import java.util.concurrent.CountDownLatch;
25import java.util.concurrent.TimeUnit;
satokc91437d2011-03-03 13:54:38 -080026
27public class SystemPropertiesTest extends TestCase {
28 private static final String KEY = "sys.testkey";
Daniel Colascioneed4023a2019-11-22 14:23:11 -080029 private static final String UNSET_KEY = "Aiw7woh6ie4toh7W";
satok656f7a72011-03-03 16:52:43 -080030 private static final String PERSIST_KEY = "persist.sys.testkey";
31
satokc91437d2011-03-03 13:54:38 -080032 @SmallTest
satok656f7a72011-03-03 16:52:43 -080033 public void testStressPersistPropertyConsistency() throws Exception {
34 for (int i = 0; i < 100; ++i) {
35 SystemProperties.set(PERSIST_KEY, Long.toString(i));
36 long ret = SystemProperties.getLong(PERSIST_KEY, -1);
37 assertEquals(i, ret);
38 }
39 }
40
41 @SmallTest
42 public void testStressMemoryPropertyConsistency() throws Exception {
satokc91437d2011-03-03 13:54:38 -080043 for (int i = 0; i < 100; ++i) {
44 SystemProperties.set(KEY, Long.toString(i));
45 long ret = SystemProperties.getLong(KEY, -1);
46 assertEquals(i, ret);
47 }
48 }
49
50 @SmallTest
51 public void testProperties() throws Exception {
52 String value;
53
54 SystemProperties.set(KEY, "");
55 value = SystemProperties.get(KEY, "default");
56 assertEquals("default", value);
57
Andreas Gampea90534b2017-07-29 14:14:39 -070058 // null default value is the same as "".
59 SystemProperties.set(KEY, null);
60 value = SystemProperties.get(KEY, "default");
61 assertEquals("default", value);
62
satokc91437d2011-03-03 13:54:38 -080063 SystemProperties.set(KEY, "SA");
64 value = SystemProperties.get(KEY, "default");
65 assertEquals("SA", value);
66
67 value = SystemProperties.get(KEY);
68 assertEquals("SA", value);
69
70 SystemProperties.set(KEY, "");
71 value = SystemProperties.get(KEY, "default");
72 assertEquals("default", value);
73
Andreas Gampea90534b2017-07-29 14:14:39 -070074 // null value is the same as "".
75 SystemProperties.set(KEY, "SA");
76 SystemProperties.set(KEY, null);
77 value = SystemProperties.get(KEY, "default");
78 assertEquals("default", value);
79
satokc91437d2011-03-03 13:54:38 -080080 value = SystemProperties.get(KEY);
81 assertEquals("", value);
82 }
Andreas Gampea90534b2017-07-29 14:14:39 -070083
84 private static void testInt(String setVal, int defValue, int expected) {
85 SystemProperties.set(KEY, setVal);
86 int value = SystemProperties.getInt(KEY, defValue);
87 assertEquals(expected, value);
88 }
89
90 private static void testLong(String setVal, long defValue, long expected) {
91 SystemProperties.set(KEY, setVal);
92 long value = SystemProperties.getLong(KEY, defValue);
93 assertEquals(expected, value);
94 }
95
96 @SmallTest
Daniel Colascione6e2cff72019-11-14 13:22:31 -080097 private static void testHandle() throws Exception {
98 String value;
99 SystemProperties.Handle handle = SystemProperties.find("doesnotexist_2341431");
100 assertNull(handle);
101 SystemProperties.set(KEY, "abc");
102 handle = SystemProperties.find(KEY);
103 assertNotNull(handle);
104 value = handle.get();
105 assertEquals("abc", value);
106 SystemProperties.set(KEY, "blarg");
107 value = handle.get();
108 assertEquals("blarg", value);
109 SystemProperties.set(KEY, "1");
110 assertEquals(1, handle.getInt(-1));
111 assertEquals(1, handle.getLong(-1));
112 assertEquals(true, handle.getBoolean(false));
113 SystemProperties.set(KEY, "");
114 assertEquals(12345, handle.getInt(12345));
115 }
116
117 @SmallTest
Andreas Gampea90534b2017-07-29 14:14:39 -0700118 public void testIntegralProperties() throws Exception {
119 testInt("", 123, 123);
120 testInt("", 0, 0);
121 testInt("", -123, -123);
122
123 testInt("123", 124, 123);
124 testInt("0", 124, 0);
125 testInt("-123", 124, -123);
126
127 testLong("", 3147483647L, 3147483647L);
128 testLong("", 0, 0);
129 testLong("", -3147483647L, -3147483647L);
130
131 testLong("3147483647", 124, 3147483647L);
132 testLong("0", 124, 0);
133 testLong("-3147483647", 124, -3147483647L);
134 }
135
136 @SmallTest
Daniel Colascioneed4023a2019-11-22 14:23:11 -0800137 public void testUnset() throws Exception {
138 assertEquals("abc", SystemProperties.get(UNSET_KEY, "abc"));
139 assertEquals(true, SystemProperties.getBoolean(UNSET_KEY, true));
140 assertEquals(false, SystemProperties.getBoolean(UNSET_KEY, false));
141 assertEquals(5, SystemProperties.getInt(UNSET_KEY, 5));
142 assertEquals(-10, SystemProperties.getLong(UNSET_KEY, -10));
143 }
144
145 @SmallTest
Andreas Gampea90534b2017-07-29 14:14:39 -0700146 @SuppressWarnings("null")
147 public void testNullKey() throws Exception {
148 try {
149 SystemProperties.get(null);
150 fail("Expected NullPointerException");
151 } catch (NullPointerException npe) {
152 }
153
154 try {
155 SystemProperties.get(null, "default");
156 fail("Expected NullPointerException");
157 } catch (NullPointerException npe) {
158 }
159
160 try {
161 SystemProperties.set(null, "value");
162 fail("Expected NullPointerException");
163 } catch (NullPointerException npe) {
164 }
165
166 try {
167 SystemProperties.getInt(null, 0);
168 fail("Expected NullPointerException");
169 } catch (NullPointerException npe) {
170 }
171
172 try {
173 SystemProperties.getLong(null, 0);
174 fail("Expected NullPointerException");
175 } catch (NullPointerException npe) {
176 }
177 }
Andreas Gampe7074e6f2018-03-16 16:14:29 -0700178
179 @SmallTest
180 public void testCallbacks() {
181 // Latches are not really necessary, but are easy to use.
182 final CountDownLatch wait1 = new CountDownLatch(1);
183 final CountDownLatch wait2 = new CountDownLatch(1);
184
185 Runnable r1 = new Runnable() {
186 boolean done = false;
187 @Override
188 public void run() {
189 if (done) {
190 return;
191 }
192 done = true;
193
194 wait1.countDown();
195 throw new RuntimeException("test");
196 }
197 };
198
199 Runnable r2 = new Runnable() {
200 @Override
201 public void run() {
202 wait2.countDown();
203 }
204 };
205
206 SystemProperties.addChangeCallback(r1);
207 SystemProperties.addChangeCallback(r2);
208
209 SystemProperties.reportSyspropChanged();
210
211 try {
212 assertTrue(wait1.await(5, TimeUnit.SECONDS));
213 } catch (InterruptedException e) {
214 fail("InterruptedException");
215 }
216 try {
217 assertTrue(wait2.await(5, TimeUnit.SECONDS));
218 } catch (InterruptedException e) {
219 fail("InterruptedException");
220 }
221 }
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700222
223 @SmallTest
224 public void testDigestOf() {
225 final String empty = SystemProperties.digestOf();
226 final String finger = SystemProperties.digestOf("ro.build.fingerprint");
227 final String fingerBrand = SystemProperties.digestOf(
228 "ro.build.fingerprint", "ro.product.brand");
229 final String brandFinger = SystemProperties.digestOf(
230 "ro.product.brand", "ro.build.fingerprint");
231
232 // Shouldn't change over time
233 assertTrue(Objects.equals(finger, SystemProperties.digestOf("ro.build.fingerprint")));
234
235 // Different properties means different results
236 assertFalse(Objects.equals(empty, finger));
237 assertFalse(Objects.equals(empty, fingerBrand));
238 assertFalse(Objects.equals(finger, fingerBrand));
239
240 // Same properties means same result
241 assertTrue(Objects.equals(fingerBrand, brandFinger));
242 }
satokc91437d2011-03-03 13:54:38 -0800243}