Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 1 | /* |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 2 | * Copyright (C) 2019 The Android Open Source Project |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 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 | |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 17 | package android.provider.settings.validators; |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 18 | |
Amin Shaikh | bb58879 | 2019-04-02 18:07:40 -0400 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertTrue; |
Annie Meng | 5d26b8c | 2018-05-17 16:00:16 +0100 | [diff] [blame] | 23 | import static org.junit.Assert.fail; |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 24 | |
| 25 | import android.platform.test.annotations.Presubmit; |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 26 | import android.provider.Settings; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 27 | |
| 28 | import androidx.test.filters.SmallTest; |
| 29 | import androidx.test.runner.AndroidJUnit4; |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 30 | |
Amin Shaikh | bb58879 | 2019-04-02 18:07:40 -0400 | [diff] [blame] | 31 | import org.json.JSONException; |
| 32 | import org.json.JSONObject; |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 36 | import java.util.Map; |
| 37 | |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Tests that ensure all backed up settings have non-null validators. Also, common validators |
| 40 | * are tested. |
| 41 | */ |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 42 | @Presubmit |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | @SmallTest |
| 45 | public class SettingsValidatorsTest { |
| 46 | |
| 47 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 48 | public void testNonNegativeIntegerValidator() { |
| 49 | assertTrue(SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR.validate("1")); |
| 50 | assertTrue(SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR.validate("0")); |
| 51 | assertFalse(SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR.validate("-1")); |
| 52 | assertFalse(SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR.validate("rectangle")); |
| 53 | } |
| 54 | |
| 55 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 56 | public void testNonNegativeIntegerValidator_onNullValue_returnsFalse() { |
| 57 | assertFalse(SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR.validate(null)); |
| 58 | } |
| 59 | |
| 60 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 61 | public void testAnyIntegerValidator() { |
| 62 | assertTrue(SettingsValidators.ANY_INTEGER_VALIDATOR.validate("1")); |
| 63 | assertTrue(SettingsValidators.ANY_INTEGER_VALIDATOR.validate("0")); |
| 64 | assertTrue(SettingsValidators.ANY_INTEGER_VALIDATOR.validate("-1")); |
| 65 | assertFalse(SettingsValidators.ANY_INTEGER_VALIDATOR.validate("rectangle")); |
| 66 | } |
| 67 | |
| 68 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 69 | public void testAnyIntegerValidator_onNullValue_returnsFalse() { |
| 70 | assertFalse(SettingsValidators.ANY_INTEGER_VALIDATOR.validate(null)); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testUriValidator_onNullValue_returnsTrue() { |
| 75 | assertTrue(SettingsValidators.URI_VALIDATOR.validate(null)); |
| 76 | } |
| 77 | |
| 78 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 79 | public void testComponentNameValidator() { |
| 80 | assertTrue(SettingsValidators.COMPONENT_NAME_VALIDATOR.validate( |
Stefanot | e66c1c3 | 2018-08-28 16:51:08 +0100 | [diff] [blame] | 81 | "com.android.localtransport/.LocalTransport")); |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 82 | assertFalse(SettingsValidators.COMPONENT_NAME_VALIDATOR.validate("rectangle")); |
| 83 | } |
| 84 | |
| 85 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 86 | public void testComponentNameValidator_onNullValue_returnsFalse() { |
Annie Meng | 5d26b8c | 2018-05-17 16:00:16 +0100 | [diff] [blame] | 87 | assertFalse(SettingsValidators.COMPONENT_NAME_VALIDATOR.validate(null)); |
| 88 | } |
| 89 | |
| 90 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 91 | public void testLenientIpAddressValidator_onNullValue_returnsFalse() { |
| 92 | assertFalse(SettingsValidators.LENIENT_IP_ADDRESS_VALIDATOR.validate(null)); |
| 93 | } |
| 94 | |
| 95 | @Test |
Annie Meng | 4451179 | 2018-05-17 18:49:24 +0100 | [diff] [blame] | 96 | public void testNullableComponentNameValidator_onValidComponentName_returnsTrue() { |
| 97 | assertTrue(SettingsValidators.NULLABLE_COMPONENT_NAME_VALIDATOR.validate( |
Stefanot | e66c1c3 | 2018-08-28 16:51:08 +0100 | [diff] [blame] | 98 | "com.android.localtransport/.LocalTransport")); |
Annie Meng | 4451179 | 2018-05-17 18:49:24 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testNullableComponentNameValidator_onInvalidComponentName_returnsFalse() { |
| 103 | assertFalse(SettingsValidators.NULLABLE_COMPONENT_NAME_VALIDATOR.validate( |
| 104 | "rectangle")); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testNullableComponentNameValidator_onNullValue_returnsTrue() { |
| 109 | assertTrue(SettingsValidators.NULLABLE_COMPONENT_NAME_VALIDATOR.validate(null)); |
| 110 | } |
| 111 | |
| 112 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 113 | public void testLocaleValidator() { |
| 114 | assertTrue(SettingsValidators.LOCALE_VALIDATOR.validate("en_US")); |
| 115 | assertTrue(SettingsValidators.LOCALE_VALIDATOR.validate("es")); |
| 116 | assertFalse(SettingsValidators.LOCALE_VALIDATOR.validate("rectangle")); |
| 117 | } |
| 118 | |
| 119 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 120 | public void testLocaleValidator_onNullValue_returnsFalse() { |
| 121 | assertFalse(SettingsValidators.LOCALE_VALIDATOR.validate(null)); |
| 122 | } |
| 123 | |
| 124 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 125 | public void testPackageNameValidator() { |
| 126 | assertTrue(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate( |
| 127 | "com.google.android")); |
| 128 | assertFalse(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate("com.google.@android")); |
| 129 | assertFalse(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(".com.google.android")); |
| 130 | assertFalse(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(".com.google.5android")); |
| 131 | } |
| 132 | |
| 133 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 134 | public void testPackageNameValidator_onNullValue_returnsFalse() { |
| 135 | assertFalse(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(null)); |
| 136 | } |
| 137 | |
| 138 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 139 | public void testDiscreteValueValidator() { |
| 140 | String[] beerTypes = new String[]{"Ale", "American IPA", "Stout"}; |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 141 | Validator v = new DiscreteValueValidator(beerTypes); |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 142 | assertTrue(v.validate("Ale")); |
| 143 | assertTrue(v.validate("American IPA")); |
| 144 | assertTrue(v.validate("Stout")); |
| 145 | assertFalse(v.validate("Cider")); // just juice pretending to be beer |
| 146 | } |
| 147 | |
| 148 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 149 | public void testDiscreteValueValidator_onNullValue_returnsFalse() { |
| 150 | String[] discreteTypes = new String[]{"Type1", "Type2"}; |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 151 | Validator v = new DiscreteValueValidator(discreteTypes); |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 152 | |
| 153 | assertFalse(v.validate(null)); |
| 154 | } |
| 155 | |
| 156 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 157 | public void testInclusiveIntegerRangeValidator() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 158 | Validator v = new InclusiveIntegerRangeValidator(0, 5); |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 159 | assertTrue(v.validate("0")); |
| 160 | assertTrue(v.validate("2")); |
| 161 | assertTrue(v.validate("5")); |
| 162 | assertFalse(v.validate("-1")); |
| 163 | assertFalse(v.validate("6")); |
| 164 | } |
| 165 | |
| 166 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 167 | public void testInclusiveIntegerRangeValidator_onNullValue_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 168 | Validator v = new InclusiveIntegerRangeValidator(0, 5); |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 169 | |
| 170 | assertFalse(v.validate(null)); |
| 171 | } |
| 172 | |
| 173 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 174 | public void testInclusiveFloatRangeValidator() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 175 | Validator v = new InclusiveFloatRangeValidator(0.0f, 5.0f); |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 176 | assertTrue(v.validate("0.0")); |
| 177 | assertTrue(v.validate("2.0")); |
| 178 | assertTrue(v.validate("5.0")); |
| 179 | assertFalse(v.validate("-1.0")); |
| 180 | assertFalse(v.validate("6.0")); |
| 181 | } |
| 182 | |
| 183 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 184 | public void testInclusiveFloatRangeValidator_onNullValue_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 185 | Validator v = new InclusiveFloatRangeValidator(0.0f, 5.0f); |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 186 | |
| 187 | assertFalse(v.validate(null)); |
| 188 | } |
| 189 | |
| 190 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 191 | public void testComponentNameListValidator() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 192 | Validator v = new ComponentNameListValidator(","); |
Stefanot | e66c1c3 | 2018-08-28 16:51:08 +0100 | [diff] [blame] | 193 | assertTrue(v.validate("com.android.localtransport/.LocalTransport," |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 194 | + "com.google.android.gms/.backup.migrate.service.D2dTransport")); |
| 195 | assertFalse(v.validate("com.google.5android,android")); |
| 196 | } |
| 197 | |
| 198 | @Test |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 199 | public void testComponentNameListValidator_onNullValue_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 200 | Validator v = new ComponentNameListValidator(","); |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 201 | |
| 202 | assertFalse(v.validate(null)); |
| 203 | } |
| 204 | |
| 205 | @Test |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 206 | public void testPackageNameListValidator() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 207 | Validator v = new PackageNameListValidator(","); |
Stefanot | e66c1c3 | 2018-08-28 16:51:08 +0100 | [diff] [blame] | 208 | assertTrue(v.validate("com.android.localtransport.LocalTransport,com.google.android.gms")); |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 209 | assertFalse(v.validate("5com.android.internal.backup.LocalTransport,android")); |
| 210 | } |
| 211 | |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 212 | @Test |
| 213 | public void testPackageNameListValidator_onNullValue_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 214 | Validator v = new PackageNameListValidator(","); |
Annie Meng | eed1b81 | 2018-05-21 16:34:52 +0100 | [diff] [blame] | 215 | |
| 216 | assertFalse(v.validate(null)); |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void dateFormatValidator_onNullValue_returnsFalse() { |
| 221 | assertFalse(Settings.System.DATE_FORMAT_VALIDATOR.validate(null)); |
| 222 | } |
Michal Karpinski | 6fe6c8e | 2018-01-24 15:40:09 +0000 | [diff] [blame] | 223 | |
| 224 | @Test |
Amin Shaikh | bb58879 | 2019-04-02 18:07:40 -0400 | [diff] [blame] | 225 | public void testJSONObjectValidator() throws JSONException { |
| 226 | Validator v = SettingsValidators.JSON_OBJECT_VALIDATOR; |
| 227 | |
| 228 | assertThat(v.validate(new JSONObject().toString())).isTrue(); |
| 229 | assertThat(v.validate("{}")).isTrue(); |
| 230 | assertThat(v.validate(new JSONObject().put("foo", "bar").toString())) |
| 231 | .isTrue(); |
| 232 | assertThat(v.validate("{\"foo\": \"bar\"}")).isTrue(); |
| 233 | |
| 234 | assertThat(v.validate("random string")).isFalse(); |
| 235 | assertThat(v.validate("random: string")).isFalse(); |
| 236 | assertThat(v.validate("{random: }")).isFalse(); |
| 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void testJSONObjectValidator_onNullValue_returnsFalse() { |
| 241 | assertThat(SettingsValidators.JSON_OBJECT_VALIDATOR.validate(null)).isFalse(); |
| 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void testJSONObjectValidator_onEmptyString_returnsFalse() { |
| 246 | assertThat(SettingsValidators.JSON_OBJECT_VALIDATOR.validate("")).isFalse(); |
| 247 | } |
| 248 | |
| 249 | @Test |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 250 | public void ensureAllBackedUpSystemSettingsHaveValidators() { |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 251 | String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP, |
| 252 | Settings.System.LEGACY_RESTORE_SETTINGS), Settings.System.VALIDATORS); |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 253 | |
| 254 | failIfOffendersPresent(offenders, "Settings.System"); |
| 255 | } |
| 256 | |
| 257 | @Test |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 258 | public void testTTSListValidator_withValidInput_returnsTrue() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 259 | assertTrue( |
| 260 | SettingsValidators.TTS_LIST_VALIDATOR.validate( |
| 261 | "com.foo.ttsengine:en-US,com.bar.ttsengine:es_ES")); |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Test |
| 265 | public void testTTSListValidator_withInvalidInput_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 266 | assertFalse( |
| 267 | SettingsValidators.TTS_LIST_VALIDATOR.validate( |
| 268 | "com.foo.ttsengine:eng-USA,INVALID")); |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Test |
| 272 | public void testTTSListValidator_withEmptyInput_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 273 | assertFalse(SettingsValidators.TTS_LIST_VALIDATOR.validate("")); |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Test |
| 277 | public void testTTSListValidator_withNullInput_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 278 | assertFalse(SettingsValidators.TTS_LIST_VALIDATOR.validate(null)); |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Test |
| 282 | public void testTileListValidator_withValidInput_returnsTrue() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 283 | assertTrue(SettingsValidators.TILE_LIST_VALIDATOR.validate("1,2,3,4")); |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Test |
| 287 | public void testTileListValidator_withMissingValue_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 288 | assertFalse(SettingsValidators.TILE_LIST_VALIDATOR.validate("1,,3")); |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void testTileListValidator_withNullInput_returnsFalse() { |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame^] | 293 | assertFalse(SettingsValidators.TILE_LIST_VALIDATOR.validate(null)); |
Al Sutton | 867ded2 | 2019-08-08 12:14:23 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Test |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 297 | public void ensureAllBackedUpGlobalSettingsHaveValidators() { |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 298 | String offenders = getOffenders(concat(Settings.Global.SETTINGS_TO_BACKUP, |
| 299 | Settings.Global.LEGACY_RESTORE_SETTINGS), Settings.Global.VALIDATORS); |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 300 | |
| 301 | failIfOffendersPresent(offenders, "Settings.Global"); |
| 302 | } |
| 303 | |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 304 | @Test |
| 305 | public void ensureAllBackedUpSecureSettingsHaveValidators() { |
| 306 | String offenders = getOffenders(concat(Settings.Secure.SETTINGS_TO_BACKUP, |
| 307 | Settings.Secure.LEGACY_RESTORE_SETTINGS), Settings.Secure.VALIDATORS); |
| 308 | |
| 309 | failIfOffendersPresent(offenders, "Settings.Secure"); |
| 310 | } |
| 311 | |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 312 | private void failIfOffendersPresent(String offenders, String settingsType) { |
| 313 | if (offenders.length() > 0) { |
| 314 | fail("All " + settingsType + " settings that are backed up have to have a non-null" |
| 315 | + " validator, but those don't: " + offenders); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | private String getOffenders(String[] settingsToBackup, Map<String, Validator> validators) { |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 320 | StringBuilder offenders = new StringBuilder(); |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 321 | for (String setting : settingsToBackup) { |
| 322 | if (validators.get(setting) == null) { |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 323 | offenders.append(setting).append(" "); |
| 324 | } |
| 325 | } |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 326 | return offenders.toString(); |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 327 | } |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 328 | |
| 329 | private String[] concat(String[] first, String[] second) { |
| 330 | if (second == null || second.length == 0) { |
| 331 | return first; |
| 332 | } |
| 333 | final int firstLen = first.length; |
| 334 | final int secondLen = second.length; |
| 335 | String[] both = new String[firstLen + secondLen]; |
| 336 | System.arraycopy(first, 0, both, 0, firstLen); |
| 337 | System.arraycopy(second, 0, both, firstLen, secondLen); |
| 338 | return both; |
| 339 | } |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 340 | } |