summaryrefslogtreecommitdiff
path: root/errorprone/tests
diff options
context:
space:
mode:
authorAshwini Oruganti <ashfall@google.com>2020-10-06 15:31:21 -0700
committerAshwini Oruganti <ashfall@google.com>2020-10-07 23:06:07 -0700
commit1f0c08134a19557048765aaaff86c0bcc735fcfc (patch)
treeece5f2d33a410b2fc6cbefa8c351c49019d93737 /errorprone/tests
parent65d966e1d1f2040f3ed57cd7b76a9dccae342439 (diff)
Error prone checks for mutability flags on PI
Add a mutability flag check for all method calls that create a PendingIntent. Bug: 160794467 Test: atest error_prone_android_framework_test:com.google.errorprone.bugpatterns.android.PendingIntentMutabilityCheckerTest Change-Id: I26a51a6dddb2793e9a56e72876f3f9d2aea4e3fb
Diffstat (limited to 'errorprone/tests')
-rw-r--r--errorprone/tests/java/com/google/errorprone/bugpatterns/android/PendingIntentMutabilityCheckerTest.java292
-rw-r--r--errorprone/tests/res/android/app/PendingIntent.java69
-rw-r--r--errorprone/tests/res/android/content/Intent.java20
-rw-r--r--errorprone/tests/res/android/os/Bundle.java20
4 files changed, 401 insertions, 0 deletions
diff --git a/errorprone/tests/java/com/google/errorprone/bugpatterns/android/PendingIntentMutabilityCheckerTest.java b/errorprone/tests/java/com/google/errorprone/bugpatterns/android/PendingIntentMutabilityCheckerTest.java
new file mode 100644
index 000000000000..a8badf6bdb0b
--- /dev/null
+++ b/errorprone/tests/java/com/google/errorprone/bugpatterns/android/PendingIntentMutabilityCheckerTest.java
@@ -0,0 +1,292 @@
+/*
+ * 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 com.google.errorprone.bugpatterns.android;
+
+import com.google.errorprone.CompilationTestHelper;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class PendingIntentMutabilityCheckerTest {
+ private CompilationTestHelper mCompilationHelper;
+
+ @Before
+ public void setUp() {
+ mCompilationHelper = CompilationTestHelper.newInstance(
+ PendingIntentMutabilityChecker.class, getClass());
+ }
+
+ @Test
+ public void testGetActivity() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent intent;",
+ " void example() {",
+ " PendingIntent.getActivity(context, 42, intent, PendingIntent.FLAG_MUTABLE);",
+ " PendingIntent.getActivity(context, 42, intent, PendingIntent.FLAG_IMMUTABLE);",
+ " PendingIntent.getActivity(context, 42, intent, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getActivity(context, 42, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getActivity(context, 42, intent, 0 | PendingIntent.FLAG_MUTABLE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivity(context, 42, intent, PendingIntent.FLAG_ONE_SHOT);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivity(context, 42, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivity(context, 42, intent, 0);",
+ " }",
+ "}")
+ .doTest();
+ }
+
+ @Test
+ public void testGetActivityAsUser() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent intent;",
+ " void example() {",
+ " PendingIntent.getActivityAsUser(context, 42, intent, PendingIntent.FLAG_MUTABLE, null, null);",
+ " PendingIntent.getActivityAsUser(context, 42, intent, PendingIntent.FLAG_IMMUTABLE, null, null);",
+ " PendingIntent.getActivityAsUser(context, 42, intent, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null, null);",
+ " PendingIntent.getActivityAsUser(context, 42, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null, null);",
+ " PendingIntent.getActivityAsUser(context, 42, intent, 0 | PendingIntent.FLAG_MUTABLE, null, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivityAsUser(context, 42, intent, PendingIntent.FLAG_ONE_SHOT, null, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivityAsUser(context, 42, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE, null, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivityAsUser(context, 42, intent, 0, null, null);",
+ " }",
+ "}")
+ .doTest();
+ }
+
+ @Test
+ public void testGetActivities() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent[] intents;",
+ " void example() {",
+ " PendingIntent.getActivities(context, 42, intents, PendingIntent.FLAG_MUTABLE, null);",
+ " PendingIntent.getActivities(context, 42, intents, PendingIntent.FLAG_IMMUTABLE, null);",
+ " PendingIntent.getActivities(context, 42, intents, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null);",
+ " PendingIntent.getActivities(context, 42, intents, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null);",
+ " PendingIntent.getActivities(context, 42, intents, 0 | PendingIntent.FLAG_MUTABLE, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivities(context, 42, intents, PendingIntent.FLAG_ONE_SHOT, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivities(context, 42, intents, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivities(context, 42, intents, 0, null);",
+ " }",
+ "}")
+ .doTest();
+ }
+
+ @Test
+ public void testGetActivitiesAsUser() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent[] intents;",
+ " void example() {",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, PendingIntent.FLAG_MUTABLE, null, null);",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, PendingIntent.FLAG_IMMUTABLE, null, null);",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null, null);",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null, null);",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, 0 | PendingIntent.FLAG_MUTABLE, null, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, PendingIntent.FLAG_ONE_SHOT, null, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE, null, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getActivitiesAsUser(context, 42, intents, 0, null, null);",
+ " }",
+ "}")
+ .doTest();
+ }
+
+
+ @Test
+ public void testGetBroadcast() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent intent;",
+ " void example() {",
+ " PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_MUTABLE);",
+ " PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_IMMUTABLE);",
+ " PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getBroadcast(context, 42, intent, 0 | PendingIntent.FLAG_MUTABLE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_ONE_SHOT);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getBroadcast(context, 42, intent, 0);",
+ " }",
+ "}")
+ .doTest();
+ }
+
+ @Test
+ public void testGetBroadcastAsUser() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent intent;",
+ " void example() {",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, PendingIntent.FLAG_MUTABLE, null);",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, PendingIntent.FLAG_IMMUTABLE, null);",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null);",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT, null);",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, 0 | PendingIntent.FLAG_MUTABLE, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, PendingIntent.FLAG_ONE_SHOT, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE, null);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getBroadcastAsUser(context, 42, intent, 0, null);",
+ " }",
+ "}")
+ .doTest();
+ }
+
+ @Test
+ public void testGetService() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent intent;",
+ " void example() {",
+ " PendingIntent.getService(context, 42, intent, PendingIntent.FLAG_MUTABLE);",
+ " PendingIntent.getService(context, 42, intent, PendingIntent.FLAG_IMMUTABLE);",
+ " PendingIntent.getService(context, 42, intent, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getService(context, 42, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getService(context, 42, intent, 0 | PendingIntent.FLAG_MUTABLE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getService(context, 42, intent, PendingIntent.FLAG_ONE_SHOT);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getService(context, 42, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getService(context, 42, intent, 0);",
+ " }",
+ "}")
+ .doTest();
+ }
+
+ @Test
+ public void testGetForegroundService() {
+ mCompilationHelper
+ .addSourceFile("/android/app/PendingIntent.java")
+ .addSourceFile("/android/content/Context.java")
+ .addSourceFile("/android/content/Intent.java")
+ .addSourceFile("/android/os/UserHandle.java")
+ .addSourceFile("/android/os/Bundle.java")
+ .addSourceLines("Example.java",
+ "import android.app.PendingIntent;",
+ "import android.content.Context;",
+ "import android.content.Intent;",
+ "public class Example {",
+ " Context context;",
+ " Intent intent;",
+ " void example() {",
+ " PendingIntent.getForegroundService(context, 42, intent, PendingIntent.FLAG_MUTABLE);",
+ " PendingIntent.getForegroundService(context, 42, intent, PendingIntent.FLAG_IMMUTABLE);",
+ " PendingIntent.getForegroundService(context, 42, intent, PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getForegroundService(context, 42, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);",
+ " PendingIntent.getForegroundService(context, 42, intent, 0 | PendingIntent.FLAG_MUTABLE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getForegroundService(context, 42, intent, PendingIntent.FLAG_ONE_SHOT);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getForegroundService(context, 42, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);",
+ " // BUG: Diagnostic contains:",
+ " PendingIntent.getForegroundService(context, 42, intent, 0);",
+ " }",
+ "}")
+ .doTest();
+ }
+}
diff --git a/errorprone/tests/res/android/app/PendingIntent.java b/errorprone/tests/res/android/app/PendingIntent.java
new file mode 100644
index 000000000000..a0cdedfd1d7a
--- /dev/null
+++ b/errorprone/tests/res/android/app/PendingIntent.java
@@ -0,0 +1,69 @@
+/*
+ * 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.app;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.UserHandle;
+
+public class PendingIntent {
+ public static final int FLAG_ONE_SHOT = 1<<30;
+ public static final int FLAG_IMMUTABLE = 1<<26;
+ public static final int FLAG_MUTABLE = 1<<25;
+ public static final int FLAG_NO_CREATE = 1<<29;
+
+ public static PendingIntent getActivity(Context context, int requestCode,
+ Intent intent, int flags) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static PendingIntent getActivityAsUser(Context context, int requestCode,
+ Intent intent, int flags, Bundle options, UserHandle user) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static PendingIntent getActivities(Context context, int requestCode,
+ Intent[] intents, int flags, Bundle options) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static PendingIntent getActivitiesAsUser(Context context, int requestCode,
+ Intent[] intents, int flags, Bundle options, UserHandle user) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static PendingIntent getBroadcast(Context context, int requestCode,
+ Intent intent, int flags) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static PendingIntent getBroadcastAsUser(Context context, int requestCode,
+ Intent intent, int flags, UserHandle userHandle) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static PendingIntent getService(Context context, int requestCode,
+ Intent intent, int flags) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static PendingIntent getForegroundService(Context context, int requestCode,
+ Intent intent, int flags) {
+ throw new UnsupportedOperationException();
+ }
+}
diff --git a/errorprone/tests/res/android/content/Intent.java b/errorprone/tests/res/android/content/Intent.java
new file mode 100644
index 000000000000..9d22d04b8cb8
--- /dev/null
+++ b/errorprone/tests/res/android/content/Intent.java
@@ -0,0 +1,20 @@
+/*
+ * 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.content;
+
+public class Intent {
+}
diff --git a/errorprone/tests/res/android/os/Bundle.java b/errorprone/tests/res/android/os/Bundle.java
new file mode 100644
index 000000000000..6d2f7b899502
--- /dev/null
+++ b/errorprone/tests/res/android/os/Bundle.java
@@ -0,0 +1,20 @@
+/*
+ * 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.os;
+
+public class Bundle {
+}