blob: b48ac3347093f91f2421da1ecc3ca6d38d5a9af7 [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";
satok656f7a72011-03-03 16:52:43 -080029 private static final String PERSIST_KEY = "persist.sys.testkey";
30
satokc91437d2011-03-03 13:54:38 -080031 @SmallTest
satok656f7a72011-03-03 16:52:43 -080032 public void testStressPersistPropertyConsistency() throws Exception {
33 for (int i = 0; i < 100; ++i) {
34 SystemProperties.set(PERSIST_KEY, Long.toString(i));
35 long ret = SystemProperties.getLong(PERSIST_KEY, -1);
36 assertEquals(i, ret);
37 }
38 }
39
40 @SmallTest
41 public void testStressMemoryPropertyConsistency() throws Exception {
satokc91437d2011-03-03 13:54:38 -080042 for (int i = 0; i < 100; ++i) {
43 SystemProperties.set(KEY, Long.toString(i));
44 long ret = SystemProperties.getLong(KEY, -1);
45 assertEquals(i, ret);
46 }
47 }
48
49 @SmallTest
50 public void testProperties() throws Exception {
51 String value;
52
53 SystemProperties.set(KEY, "");
54 value = SystemProperties.get(KEY, "default");
55 assertEquals("default", value);
56
Andreas Gampea90534b2017-07-29 14:14:39 -070057 // null default value is the same as "".
58 SystemProperties.set(KEY, null);
59 value = SystemProperties.get(KEY, "default");
60 assertEquals("default", value);
61
satokc91437d2011-03-03 13:54:38 -080062 SystemProperties.set(KEY, "SA");
63 value = SystemProperties.get(KEY, "default");
64 assertEquals("SA", value);
65
66 value = SystemProperties.get(KEY);
67 assertEquals("SA", value);
68
69 SystemProperties.set(KEY, "");
70 value = SystemProperties.get(KEY, "default");
71 assertEquals("default", value);
72
Andreas Gampea90534b2017-07-29 14:14:39 -070073 // null value is the same as "".
74 SystemProperties.set(KEY, "SA");
75 SystemProperties.set(KEY, null);
76 value = SystemProperties.get(KEY, "default");
77 assertEquals("default", value);
78
satokc91437d2011-03-03 13:54:38 -080079 value = SystemProperties.get(KEY);
80 assertEquals("", value);
81 }
Andreas Gampea90534b2017-07-29 14:14:39 -070082
83 private static void testInt(String setVal, int defValue, int expected) {
84 SystemProperties.set(KEY, setVal);
85 int value = SystemProperties.getInt(KEY, defValue);
86 assertEquals(expected, value);
87 }
88
89 private static void testLong(String setVal, long defValue, long expected) {
90 SystemProperties.set(KEY, setVal);
91 long value = SystemProperties.getLong(KEY, defValue);
92 assertEquals(expected, value);
93 }
94
95 @SmallTest
Daniel Colascione6e2cff72019-11-14 13:22:31 -080096 private static void testHandle() throws Exception {
97 String value;
98 SystemProperties.Handle handle = SystemProperties.find("doesnotexist_2341431");
99 assertNull(handle);
100 SystemProperties.set(KEY, "abc");
101 handle = SystemProperties.find(KEY);
102 assertNotNull(handle);
103 value = handle.get();
104 assertEquals("abc", value);
105 SystemProperties.set(KEY, "blarg");
106 value = handle.get();
107 assertEquals("blarg", value);
108 SystemProperties.set(KEY, "1");
109 assertEquals(1, handle.getInt(-1));
110 assertEquals(1, handle.getLong(-1));
111 assertEquals(true, handle.getBoolean(false));
112 SystemProperties.set(KEY, "");
113 assertEquals(12345, handle.getInt(12345));
114 }
115
116 @SmallTest
Andreas Gampea90534b2017-07-29 14:14:39 -0700117 public void testIntegralProperties() throws Exception {
118 testInt("", 123, 123);
119 testInt("", 0, 0);
120 testInt("", -123, -123);
121
122 testInt("123", 124, 123);
123 testInt("0", 124, 0);
124 testInt("-123", 124, -123);
125
126 testLong("", 3147483647L, 3147483647L);
127 testLong("", 0, 0);
128 testLong("", -3147483647L, -3147483647L);
129
130 testLong("3147483647", 124, 3147483647L);
131 testLong("0", 124, 0);
132 testLong("-3147483647", 124, -3147483647L);
133 }
134
135 @SmallTest
136 @SuppressWarnings("null")
137 public void testNullKey() throws Exception {
138 try {
139 SystemProperties.get(null);
140 fail("Expected NullPointerException");
141 } catch (NullPointerException npe) {
142 }
143
144 try {
145 SystemProperties.get(null, "default");
146 fail("Expected NullPointerException");
147 } catch (NullPointerException npe) {
148 }
149
150 try {
151 SystemProperties.set(null, "value");
152 fail("Expected NullPointerException");
153 } catch (NullPointerException npe) {
154 }
155
156 try {
157 SystemProperties.getInt(null, 0);
158 fail("Expected NullPointerException");
159 } catch (NullPointerException npe) {
160 }
161
162 try {
163 SystemProperties.getLong(null, 0);
164 fail("Expected NullPointerException");
165 } catch (NullPointerException npe) {
166 }
167 }
Andreas Gampe7074e6f2018-03-16 16:14:29 -0700168
169 @SmallTest
170 public void testCallbacks() {
171 // Latches are not really necessary, but are easy to use.
172 final CountDownLatch wait1 = new CountDownLatch(1);
173 final CountDownLatch wait2 = new CountDownLatch(1);
174
175 Runnable r1 = new Runnable() {
176 boolean done = false;
177 @Override
178 public void run() {
179 if (done) {
180 return;
181 }
182 done = true;
183
184 wait1.countDown();
185 throw new RuntimeException("test");
186 }
187 };
188
189 Runnable r2 = new Runnable() {
190 @Override
191 public void run() {
192 wait2.countDown();
193 }
194 };
195
196 SystemProperties.addChangeCallback(r1);
197 SystemProperties.addChangeCallback(r2);
198
199 SystemProperties.reportSyspropChanged();
200
201 try {
202 assertTrue(wait1.await(5, TimeUnit.SECONDS));
203 } catch (InterruptedException e) {
204 fail("InterruptedException");
205 }
206 try {
207 assertTrue(wait2.await(5, TimeUnit.SECONDS));
208 } catch (InterruptedException e) {
209 fail("InterruptedException");
210 }
211 }
Jeff Sharkeyb0c363b22018-12-15 11:53:03 -0700212
213 @SmallTest
214 public void testDigestOf() {
215 final String empty = SystemProperties.digestOf();
216 final String finger = SystemProperties.digestOf("ro.build.fingerprint");
217 final String fingerBrand = SystemProperties.digestOf(
218 "ro.build.fingerprint", "ro.product.brand");
219 final String brandFinger = SystemProperties.digestOf(
220 "ro.product.brand", "ro.build.fingerprint");
221
222 // Shouldn't change over time
223 assertTrue(Objects.equals(finger, SystemProperties.digestOf("ro.build.fingerprint")));
224
225 // Different properties means different results
226 assertFalse(Objects.equals(empty, finger));
227 assertFalse(Objects.equals(empty, fingerBrand));
228 assertFalse(Objects.equals(finger, fingerBrand));
229
230 // Same properties means same result
231 assertTrue(Objects.equals(fingerBrand, brandFinger));
232 }
satokc91437d2011-03-03 13:54:38 -0800233}