blob: 633622957d0bc5a0c785d7d8aa8f50ad80760819 [file] [log] [blame]
/*
* 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_39704;
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.pm.PackageManager;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Semaphore;
@RunWith(AndroidJUnit4.class)
public class DeviceTest {
@Test
public void testdeleteNotificationChannelGroup() {
try {
Context context = getApplicationContext();
PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager
.getLaunchIntentForPackage(context.getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(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);
}
}
}