satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 19 | import android.test.suitebuilder.annotation.SmallTest; |
Andreas Gampe | 7074e6f | 2018-03-16 16:14:29 -0700 | [diff] [blame] | 20 | |
satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 21 | import junit.framework.TestCase; |
| 22 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | import java.util.concurrent.CountDownLatch; |
| 25 | import java.util.concurrent.TimeUnit; |
satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 26 | |
| 27 | public class SystemPropertiesTest extends TestCase { |
| 28 | private static final String KEY = "sys.testkey"; |
Daniel Colascione | ed4023a | 2019-11-22 14:23:11 -0800 | [diff] [blame] | 29 | private static final String UNSET_KEY = "Aiw7woh6ie4toh7W"; |
satok | 656f7a7 | 2011-03-03 16:52:43 -0800 | [diff] [blame] | 30 | private static final String PERSIST_KEY = "persist.sys.testkey"; |
| 31 | |
satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 32 | @SmallTest |
satok | 656f7a7 | 2011-03-03 16:52:43 -0800 | [diff] [blame] | 33 | 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 { |
satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 43 | 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 Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 58 | // null default value is the same as "". |
| 59 | SystemProperties.set(KEY, null); |
| 60 | value = SystemProperties.get(KEY, "default"); |
| 61 | assertEquals("default", value); |
| 62 | |
satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 63 | 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 Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 74 | // 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 | |
satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 80 | value = SystemProperties.get(KEY); |
| 81 | assertEquals("", value); |
| 82 | } |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 83 | |
| 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 Colascione | 6e2cff7 | 2019-11-14 13:22:31 -0800 | [diff] [blame] | 97 | 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 Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 118 | 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 Colascione | ed4023a | 2019-11-22 14:23:11 -0800 | [diff] [blame] | 137 | 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 Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 146 | @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 Gampe | 7074e6f | 2018-03-16 16:14:29 -0700 | [diff] [blame] | 178 | |
| 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 Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 222 | |
| 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 | } |
satok | c91437d | 2011-03-03 13:54:38 -0800 | [diff] [blame] | 243 | } |