| /* |
| * Copyright (C) 2022 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package android.security.cts.CVE_2021_39808; |
| |
| import static androidx.test.core.app.ApplicationProvider.getApplicationContext; |
| import static org.junit.Assert.assertNotEquals; |
| import static org.junit.Assume.assumeNoException; |
| import static org.junit.Assume.assumeTrue; |
| |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.SharedPreferences; |
| import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
| import android.content.res.Resources; |
| |
| import androidx.test.runner.AndroidJUnit4; |
| |
| import java.util.concurrent.Semaphore; |
| import java.util.concurrent.TimeUnit; |
| |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| |
| @RunWith(AndroidJUnit4.class) |
| public class DeviceTest { |
| |
| @Test |
| public void testService() { |
| try { |
| Context context = getApplicationContext(); |
| Intent intent = new Intent(context, PocService.class); |
| intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| context.startService(intent); |
| SharedPreferences sh = context.getSharedPreferences( |
| context.getString(R.string.sharedPreference), |
| Context.MODE_APPEND); |
| final Semaphore preferenceChanged = new Semaphore(0); |
| OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() { |
| @Override |
| public void onSharedPreferenceChanged( |
| SharedPreferences sharedPreferences, String key) { |
| if (key.equals(context.getString(R.string.resultKey))) { |
| if (sharedPreferences.getInt(key, 0) == context |
| .getResources().getInteger(R.integer.pass)) { |
| preferenceChanged.release(); |
| } |
| } |
| } |
| }; |
| sh.registerOnSharedPreferenceChangeListener(listener); |
| |
| preferenceChanged.tryAcquire( |
| context.getResources().getInteger(R.integer.timeoutMs), |
| TimeUnit.MILLISECONDS); |
| |
| int result = sh.getInt(context.getString(R.string.resultKey), |
| context.getResources().getInteger(R.integer.pass)); |
| String message = sh.getString(context.getString(R.string.messageKey), |
| context.getString(R.string.passMessage)); |
| assumeTrue(message, result != context.getResources() |
| .getInteger(R.integer.assumptionFailure)); |
| assertNotEquals(message, result, |
| context.getResources().getInteger(R.integer.fail)); |
| } catch (Exception e) { |
| assumeNoException(e); |
| } |
| } |
| } |