CTS test for GWP-ASan.
Does not test the actual runtime behavior of GWP-ASan - the sampling
rate is too low to detect reliably. Instead, the test checks the
presence of GWP-ASan guarded memory mappings.
Bug: 149991821
Test: atest CtsGwpAsanTestCases
Change-Id: I4bb018390cb902d09d4d7fb111c59c76ffb6758e
diff --git a/tests/tests/gwp-asan/OWNERS b/tests/tests/gwp-asan/OWNERS
new file mode 100644
index 0000000..9686261
--- /dev/null
+++ b/tests/tests/gwp-asan/OWNERS
@@ -0,0 +1,5 @@
+# Bug component: 14890
+eugenis@google.com
+pcc@google.com
+mitchp@google.com
+
diff --git a/tests/tests/gwp-asan/TEST_MAPPING b/tests/tests/gwp-asan/TEST_MAPPING
new file mode 100644
index 0000000..1976d66
--- /dev/null
+++ b/tests/tests/gwp-asan/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+ "presubmit": [
+ {
+ "name": "CtsGwpAsanTestCases"
+ }
+ ]
+}
diff --git a/tests/tests/gwp-asan/enabled/Android.bp b/tests/tests/gwp-asan/enabled/Android.bp
new file mode 100644
index 0000000..533330a
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/Android.bp
@@ -0,0 +1,39 @@
+// Copyright (C) 2020 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.
+
+android_test {
+ name: "CtsGwpAsanTestCases",
+ defaults: ["cts_defaults"],
+ compile_multilib: "both",
+ // When built, explicitly put it in the data partition.
+ // Tag this module as a cts test artifact
+ test_suites: [
+ "cts",
+ "vts",
+ "general-tests",
+ ],
+ libs: [
+ "android.test.runner.stubs",
+ "android.test.base.stubs",
+ ],
+ static_libs: [
+ "ctstestrunner-axt",
+ "androidx.test.rules",
+ "androidx.test.core",
+ "androidx.test.ext.junit",
+ ],
+ srcs: ["src/**/*.java"],
+ sdk_version: "current",
+ use_embedded_native_libs: false,
+}
diff --git a/tests/tests/gwp-asan/enabled/AndroidManifest.xml b/tests/tests/gwp-asan/enabled/AndroidManifest.xml
new file mode 100644
index 0000000..afd8c9a
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/AndroidManifest.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.gwpasan.cts"
+ android:targetSandboxVersion="2">
+
+ <application android:extractNativeLibs="true"
+ android:enableGwpAsan="true">
+ <processes>
+ <process />
+ <process android:process=":gwp_asan_enabled"
+ android:enableGwpAsan="true" />
+ <process android:process=":gwp_asan_disabled"
+ android:enableGwpAsan="false" />
+ <process android:process=":gwp_asan_default" />
+ </processes>
+ <uses-library android:name="android.test.runner" />
+ <activity android:name="android.gwpasan.GwpAsanTestActivity" />
+ <activity android:name="android.gwpasan.GwpAsanEnabledActivity"
+ android:process=":gwp_asan_enabled" />
+ <activity android:name="android.gwpasan.GwpAsanDisabledActivity"
+ android:process=":gwp_asan_disabled" />
+ <activity android:name="android.gwpasan.GwpAsanDefaultActivity"
+ android:process=":gwp_asan_default" />
+ <service android:name="android.gwpasan.GwpAsanEnabledService"
+ android:process=":gwp_asan_enabled" />
+ <service android:name="android.gwpasan.GwpAsanDisabledService"
+ android:process=":gwp_asan_disabled" />
+ <service android:name="android.gwpasan.GwpAsanDefaultService"
+ android:process=":gwp_asan_default" />
+ </application>
+
+ <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.gwpasan.cts"
+ android:label="CTS tests of GWP-ASan">
+ <meta-data android:name="listener"
+ android:value="com.android.cts.runner.CtsTestRunListener" />
+ </instrumentation>
+</manifest>
diff --git a/tests/tests/gwp-asan/enabled/AndroidTest.xml b/tests/tests/gwp-asan/enabled/AndroidTest.xml
new file mode 100644
index 0000000..61580ca
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/AndroidTest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+<configuration description="Config for CTS GWP-ASan test cases">
+ <option name="test-suite-tag" value="cts" />
+ <option name="config-descriptor:metadata" key="component" value="art" />
+ <option name="config-descriptor:metadata" key="parameter" value="instant_app" />
+ <option name="config-descriptor:metadata" key="parameter" value="multi_abi" />
+ <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
+ <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+ <option name="cleanup-apks" value="true" />
+ <option name="test-file-name" value="CtsGwpAsanTestCases.apk" />
+ </target_preparer>
+ <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+ <option name="package" value="android.gwpasan.cts" />
+ </test>
+</configuration>
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDefaultActivity.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDefaultActivity.java
new file mode 100644
index 0000000..9643fa2
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDefaultActivity.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2020 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.gwpasan;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+
+import java.lang.Override;
+
+import android.gwpasan.Utils;
+
+public class GwpAsanDefaultActivity extends Activity {
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ try {
+ boolean enabled = Utils.isGwpAsanEnabled();
+ setResult(RESULT_FIRST_USER + (enabled ? 1 : 0));
+ } catch (Exception e) {
+ setResult(RESULT_OK);
+ }
+ finish();
+ }
+
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDefaultService.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDefaultService.java
new file mode 100644
index 0000000..d7e9ccf
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDefaultService.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2020 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.gwpasan;
+
+import android.app.Service;
+import android.content.Intent;
+import android.os.Binder;
+import android.os.IBinder;
+import android.os.Parcel;
+import java.lang.Override;
+
+import android.gwpasan.Utils;
+
+public class GwpAsanDefaultService extends Service {
+
+ private final IBinder mBinder = new LocalBinder();
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return mBinder;
+ }
+
+ public class LocalBinder extends Binder {
+
+ @Override
+ public boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
+ if (code == 42) {
+ try {
+ reply.writeInt(Utils.isGwpAsanEnabled() ? 1 : 0);
+ } catch (Exception e) {
+ reply.writeInt(-1);
+ }
+ return true;
+ }
+ return false;
+ }
+ }
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDisabledActivity.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDisabledActivity.java
new file mode 100644
index 0000000..4245820
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDisabledActivity.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2020 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.gwpasan;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+
+
+import java.lang.Override;
+
+import android.gwpasan.Utils;
+
+public class GwpAsanDisabledActivity extends Activity {
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ try {
+ boolean enabled = Utils.isGwpAsanEnabled();
+ setResult(RESULT_FIRST_USER + (enabled ? 1 : 0));
+ } catch (Exception e) {
+ setResult(RESULT_OK);
+ }
+ finish();
+ }
+
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDisabledService.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDisabledService.java
new file mode 100644
index 0000000..acc3c47
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanDisabledService.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2020 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.gwpasan;
+
+import android.app.Service;
+import android.content.Intent;
+import android.os.Binder;
+import android.os.IBinder;
+import android.os.Parcel;
+import java.lang.Override;
+
+import android.gwpasan.Utils;
+
+public class GwpAsanDisabledService extends Service {
+
+ private final IBinder mBinder = new LocalBinder();
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return mBinder;
+ }
+
+ public class LocalBinder extends Binder {
+
+ @Override
+ public boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
+ if (code == 42) {
+ try {
+ reply.writeInt(Utils.isGwpAsanEnabled() ? 1 : 0);
+ } catch (Exception e) {
+ reply.writeInt(-1);
+ }
+ return true;
+ }
+ return false;
+ }
+ }
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanEnabledActivity.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanEnabledActivity.java
new file mode 100644
index 0000000..93d49ad
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanEnabledActivity.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2020 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.gwpasan;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+
+
+import java.lang.Override;
+
+import android.gwpasan.Utils;
+
+public class GwpAsanEnabledActivity extends Activity {
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ try {
+ boolean enabled = Utils.isGwpAsanEnabled();
+ setResult(RESULT_FIRST_USER + (enabled ? 1 : 0));
+ } catch (Exception e) {
+ setResult(RESULT_OK);
+ }
+ finish();
+ }
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanEnabledService.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanEnabledService.java
new file mode 100644
index 0000000..489081f
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanEnabledService.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2020 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.gwpasan;
+
+import android.app.Service;
+import android.content.Intent;
+import android.os.Binder;
+import android.os.IBinder;
+import android.os.Parcel;
+import java.lang.Override;
+
+import android.gwpasan.Utils;
+
+public class GwpAsanEnabledService extends Service {
+
+ private final IBinder mBinder = new LocalBinder();
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return mBinder;
+ }
+
+ public class LocalBinder extends Binder {
+
+ @Override
+ public boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
+ if (code == 42) {
+ try {
+ reply.writeInt(Utils.isGwpAsanEnabled() ? 1 : 0);
+ } catch (Exception e) {
+ reply.writeInt(-1);
+ }
+ return true;
+ }
+ return false;
+ }
+ }
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanTestActivity.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanTestActivity.java
new file mode 100644
index 0000000..6cc1d43
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/GwpAsanTestActivity.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2020 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.gwpasan;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.content.Context;
+import android.os.Bundle;
+import android.util.Log;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+import java.lang.Override;
+
+import android.gwpasan.Utils;
+
+public class GwpAsanTestActivity extends Activity {
+ static final String TAG = "GwpAsanTestActivity";
+
+ private int mResult;
+ private final Object mFinishEvent = new Object();
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ mResult = resultCode;
+ synchronized (mFinishEvent) {
+ mFinishEvent.notify();
+ }
+ }
+
+ protected boolean callActivity(Class<?> cls) throws Exception {
+ Thread thread = new Thread() {
+ @Override
+ public void run() {
+ try {
+ Context context = getApplicationContext();
+ Intent intent = new Intent(context, cls);
+ startActivityForResult(intent, 0);
+
+ synchronized (mFinishEvent) {
+ mFinishEvent.wait();
+ }
+ } catch(Exception e) {
+ Log.d(TAG, "callActivity got an exception " + e.toString());
+ }
+ }
+ };
+ thread.start();
+ thread.join(20000 /* millis */);
+
+ if (mResult == RESULT_OK) {
+ throw new Exception();
+ }
+ return (mResult - RESULT_FIRST_USER) == 1;
+ }
+
+ public void testGwpAsanEnabledActivity() throws Exception {
+ assertTrue(callActivity(GwpAsanEnabledActivity.class));
+ }
+
+ public void testGwpAsanDisabledActivity() throws Exception {
+ assertFalse(callActivity(GwpAsanDisabledActivity.class));
+ }
+
+ public void testGwpAsanDefaultActivity() throws Exception {
+ // GwpAsanDefaultActivity inherits the application attribute.
+ assertTrue(callActivity(GwpAsanDefaultActivity.class));
+ }
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/Utils.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/Utils.java
new file mode 100644
index 0000000..395eea4
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/Utils.java
@@ -0,0 +1,19 @@
+package android.gwpasan;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+
+public class Utils {
+
+ public static boolean isGwpAsanEnabled() throws IOException {
+ BufferedReader reader = new BufferedReader(new FileReader("proc/self/maps"));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ if (line.contains("GWP-ASan Guard Page")) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/cts/GwpAsanActivityTest.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/cts/GwpAsanActivityTest.java
new file mode 100644
index 0000000..3d6d834
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/cts/GwpAsanActivityTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2020 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.gwpasan.cts;
+
+import android.test.ActivityInstrumentationTestCase2;
+import static org.junit.Assert.assertTrue;
+import android.util.Log;
+import android.gwpasan.GwpAsanTestActivity;
+import android.gwpasan.Utils;
+
+public class GwpAsanActivityTest extends ActivityInstrumentationTestCase2<GwpAsanTestActivity> {
+
+ private GwpAsanTestActivity mActivity;
+
+ public GwpAsanActivityTest() {
+ super(GwpAsanTestActivity.class);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mActivity = getActivity();
+ }
+
+ public void testGwpAsanEnabledApplication() throws Exception {
+ assertTrue(Utils.isGwpAsanEnabled());
+ }
+
+ public void testGwpAsanEnabledActivity() throws Exception {
+ mActivity.testGwpAsanEnabledActivity();
+ }
+
+ public void testGwpAsanDisabledActivity() throws Exception {
+ mActivity.testGwpAsanDisabledActivity();
+ }
+
+ public void testGwpAsanDefaultActivity() throws Exception {
+ mActivity.testGwpAsanDefaultActivity();
+ }
+}
diff --git a/tests/tests/gwp-asan/enabled/src/android/gwpasan/cts/GwpAsanServiceTest.java b/tests/tests/gwp-asan/enabled/src/android/gwpasan/cts/GwpAsanServiceTest.java
new file mode 100644
index 0000000..a7beaa4
--- /dev/null
+++ b/tests/tests/gwp-asan/enabled/src/android/gwpasan/cts/GwpAsanServiceTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2020 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.gwpasan.cts;
+
+import android.content.Intent;
+import android.os.IBinder;
+import android.os.Parcel;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.rule.ServiceTestRule;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.concurrent.TimeoutException;
+
+import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+import android.gwpasan.GwpAsanEnabledService;
+import android.gwpasan.GwpAsanDisabledService;
+import android.gwpasan.GwpAsanDefaultService;
+
+@RunWith(AndroidJUnit4.class)
+public class GwpAsanServiceTest {
+ @Rule
+ public final ServiceTestRule mServiceRule = new ServiceTestRule();
+
+ private boolean isGwpAsanEnabledInService(Class<?> cls) throws Exception {
+ Intent serviceIntent = new Intent(getApplicationContext(), cls);
+ IBinder binder = mServiceRule.bindService(serviceIntent);
+ final Parcel request = Parcel.obtain();
+ final Parcel reply = Parcel.obtain();
+ if (!binder.transact(42, request, reply, 0)) {
+ throw new Exception();
+ }
+ int res = reply.readInt();
+ if (res < 0) {
+ throw new Exception();
+ }
+ return res != 0;
+ }
+
+ @Test
+ public void testEnabledService() throws Exception {
+ assertTrue(isGwpAsanEnabledInService(GwpAsanEnabledService.class));
+ }
+
+ @Test
+ public void testDisabledService() throws Exception {
+ assertFalse(isGwpAsanEnabledInService(GwpAsanDisabledService.class));
+ }
+
+ @Test
+ public void testDefaultService() throws Exception {
+ // GwpAsanDefaultService inherits the application attribute.
+ assertTrue(isGwpAsanEnabledInService(GwpAsanDefaultService.class));
+ }
+}