From e7ad23ad01c8bfdcabf85b2a80a66bcb95ab59dc Mon Sep 17 00:00:00 2001 From: ThiƩbaud Weksteen Date: Mon, 30 Oct 2023 12:55:15 +1100 Subject: Enable related issues in EnforcePermissionDetectorTest Enable ISSUE_ENFORCE_PERMISSION_HELPER and ISSUE_MISSING_ENFORCE_PERMISSION on EnforcePermissionDetectorTest. Update the Stub to avoid false negatives (and better reflect AIDL generated code). This is a no-op to confirm that issues do not negatively impact each other. Bug: 307433823 Test: atest --host AndroidGlobalLintCheckerTest Change-Id: I9ab2b654932a6d228a2a646e5b1fa9c5fc16db02 --- .../lint/aidl/EnforcePermissionDetectorTest.kt | 170 ++++++++++++--------- .../EnforcePermissionHelperDetectorCodegenTest.kt | 3 +- 2 files changed, 99 insertions(+), 74 deletions(-) (limited to 'tools') diff --git a/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt b/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt index b3dacbdf57a6..95ca8872c770 100644 --- a/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt +++ b/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt @@ -28,7 +28,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { override fun getIssues(): List = listOf( EnforcePermissionDetector.ISSUE_MISSING_ENFORCE_PERMISSION, - EnforcePermissionDetector.ISSUE_MISMATCHING_ENFORCE_PERMISSION + EnforcePermissionDetector.ISSUE_MISMATCHING_ENFORCE_PERMISSION, + EnforcePermissionDetector.ISSUE_ENFORCE_PERMISSION_HELPER, + EnforcePermissionDetector.ISSUE_MISUSING_ENFORCE_PERMISSION ) override fun lint(): TestLintTask = super.lint().allowMissingSdk(true) @@ -41,7 +43,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { public class TestClass2 extends IFooMethod.Stub { @Override @EnforcePermission(android.Manifest.permission.READ_PHONE_STATE) - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } } """).indented(), *stubs @@ -58,7 +62,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { public class TestClass11 extends IFooMethod.Stub { @Override @EnforcePermission(allOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE}) - public void testMethodAll() {} + public void testMethodAll() { + testMethodAll_enforcePermission(); + } } """).indented(), *stubs @@ -75,7 +81,10 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { public class TestClass111 extends IFooMethod.Stub { @Override @EnforcePermission(allOf={"android.permission.INTERNET", android.Manifest.permission.READ_PHONE_STATE}) - public void testMethodAllLiteral() {} + public void testMethodAllLiteral() { + testMethodAllLiteral_enforcePermission(); + + } } """).indented(), *stubs @@ -92,7 +101,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { public class TestClass12 extends IFooMethod.Stub { @Override @EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE}) - public void testMethodAny() {} + public void testMethodAny() { + testMethodAny_enforcePermission(); + } } """).indented(), *stubs @@ -109,7 +120,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { public class TestClass121 extends IFooMethod.Stub { @Override @EnforcePermission(anyOf={"android.permission.INTERNET", android.Manifest.permission.READ_PHONE_STATE}) - public void testMethodAnyLiteral() {} + public void testMethodAnyLiteral() { + testMethodAnyLiteral_enforcePermission(); + } } """).indented(), *stubs @@ -124,7 +137,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { package test.pkg; public class TestClass4 extends IFooMethod.Stub { @android.annotation.EnforcePermission(android.Manifest.permission.INTERNET) - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } } """).indented(), *stubs @@ -132,9 +147,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .run() .expect(""" src/test/pkg/TestClass4.java:4: Error: The method TestClass4.testMethod is annotated with @android.annotation.EnforcePermission(android.Manifest.permission.INTERNET) \ - which differs from the overridden method Stub.testMethod: @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE). \ + which differs from the overridden method IFooMethod.testMethod: @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE). \ The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethod() {} + public void testMethod() { ~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -146,7 +161,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { package test.pkg; public class TestClass41 extends IFooMethod.Stub { @android.annotation.EnforcePermission - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } } """).indented(), *stubs @@ -154,9 +171,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .run() .expect(""" src/test/pkg/TestClass41.java:4: Error: The method TestClass41.testMethod is annotated with @android.annotation.EnforcePermission \ - which differs from the overridden method Stub.testMethod: @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE). \ + which differs from the overridden method IFooMethod.testMethod: @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE). \ The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethod() {} + public void testMethod() { ~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -168,7 +185,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { package test.pkg; public class TestClass9 extends IFooMethod.Stub { @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.NFC}) - public void testMethodAny() {} + public void testMethodAny() { + testMethodAny_enforcePermission(); + } } """).indented(), *stubs @@ -177,10 +196,10 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .expect(""" src/test/pkg/TestClass9.java:4: Error: The method TestClass9.testMethodAny is annotated with \ @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.NFC}) \ - which differs from the overridden method Stub.testMethodAny: \ + which differs from the overridden method IFooMethod.testMethodAny: \ @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE}). \ The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethodAny() {} + public void testMethodAny() { ~~~~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -192,7 +211,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { package test.pkg; public class TestClass91 extends IFooMethod.Stub { @android.annotation.EnforcePermission(anyOf={"android.permission.INTERNET", "android.permissionoopsthisisatypo.READ_PHONE_STATE"}) - public void testMethodAnyLiteral() {} + public void testMethodAnyLiteral() { + testMethodAnyLiteral_enforcePermission(); + } } """).indented(), *stubs @@ -201,10 +222,10 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .expect(""" src/test/pkg/TestClass91.java:4: Error: The method TestClass91.testMethodAnyLiteral is annotated with \ @android.annotation.EnforcePermission(anyOf={"android.permission.INTERNET", "android.permissionoopsthisisatypo.READ_PHONE_STATE"}) \ - which differs from the overridden method Stub.testMethodAnyLiteral: \ + which differs from the overridden method IFooMethod.testMethodAnyLiteral: \ @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, "android.permission.READ_PHONE_STATE"}). \ The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethodAnyLiteral() {} + public void testMethodAnyLiteral() { ~~~~~~~~~~~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -216,7 +237,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { package test.pkg; public class TestClass10 extends IFooMethod.Stub { @android.annotation.EnforcePermission(allOf={android.Manifest.permission.INTERNET, android.Manifest.permission.NFC}) - public void testMethodAll() {} + public void testMethodAll() { + testMethodAll_enforcePermission(); + } } """).indented(), *stubs @@ -225,10 +248,10 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .expect(""" src/test/pkg/TestClass10.java:4: Error: The method TestClass10.testMethodAll is annotated with \ @android.annotation.EnforcePermission(allOf={android.Manifest.permission.INTERNET, android.Manifest.permission.NFC}) \ - which differs from the overridden method Stub.testMethodAll: \ + which differs from the overridden method IFooMethod.testMethodAll: \ @android.annotation.EnforcePermission(allOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE}). \ The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethodAll() {} + public void testMethodAll() { ~~~~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -240,7 +263,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { package test.pkg; public class TestClass101 extends IFooMethod.Stub { @android.annotation.EnforcePermission(allOf={"android.permission.INTERNET", "android.permissionoopsthisisatypo.READ_PHONE_STATE"}) - public void testMethodAllLiteral() {} + public void testMethodAllLiteral() { + testMethodAllLiteral_enforcePermission(); + } } """).indented(), *stubs @@ -249,10 +274,10 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .expect(""" src/test/pkg/TestClass101.java:4: Error: The method TestClass101.testMethodAllLiteral is annotated with \ @android.annotation.EnforcePermission(allOf={"android.permission.INTERNET", "android.permissionoopsthisisatypo.READ_PHONE_STATE"}) \ - which differs from the overridden method Stub.testMethodAllLiteral: \ + which differs from the overridden method IFooMethod.testMethodAllLiteral: \ @android.annotation.EnforcePermission(allOf={android.Manifest.permission.INTERNET, "android.permission.READ_PHONE_STATE"}). \ The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethodAllLiteral() {} + public void testMethodAllLiteral() { ~~~~~~~~~~~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -263,16 +288,18 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { """ package test.pkg; public class TestClass6 extends IFooMethod.Stub { - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } } """).indented(), *stubs ) .run() .expect(""" - src/test/pkg/TestClass6.java:3: Error: The method TestClass6.testMethod overrides the method Stub.testMethod which is annotated with @EnforcePermission. \ + src/test/pkg/TestClass6.java:3: Error: The method TestClass6.testMethod overrides the method IFooMethod.testMethod which is annotated with @EnforcePermission. \ The same annotation must be used on TestClass6.testMethod [MissingEnforcePermissionAnnotation] - public void testMethod() {} + public void testMethod() { ~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -284,16 +311,18 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { package test.pkg; public class TestClass7 extends IBar.Stub { @android.annotation.EnforcePermission(android.Manifest.permission.INTERNET) - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } } """).indented(), *stubs ) .run() .expect(""" - src/test/pkg/TestClass7.java:4: Error: The method TestClass7.testMethod overrides the method Stub.testMethod which is not annotated with @EnforcePermission. \ - The same annotation must be used on Stub.testMethod. Did you forget to annotate the AIDL definition? [MissingEnforcePermissionAnnotation] - public void testMethod() {} + src/test/pkg/TestClass7.java:4: Error: The method TestClass7.testMethod overrides the method IBar.testMethod which is not annotated with @EnforcePermission. \ + The same annotation must be used on IBar.testMethod. Did you forget to annotate the AIDL definition? [MissingEnforcePermissionAnnotation] + public void testMethod() { ~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation()) @@ -304,7 +333,9 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { """ package test.pkg; public class Default extends IFooMethod.Stub { - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } } """).indented(), *stubs @@ -313,8 +344,8 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .expect( """ src/test/pkg/Default.java:3: Error: The method Default.testMethod \ - overrides the method Stub.testMethod which is annotated with @EnforcePermission. The same annotation must be used on Default.testMethod [MissingEnforcePermissionAnnotation] - public void testMethod() {} + overrides the method IFooMethod.testMethod which is annotated with @EnforcePermission. The same annotation must be used on Default.testMethod [MissingEnforcePermissionAnnotation] + public void testMethod() { ~~~~~~~~~~ 1 errors, 0 warnings """.addLineContinuation() @@ -329,16 +360,24 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { public class TestClass121 extends IFooMethod.Stub { @Override @EnforcePermission("READ_PHONE_STATE") - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } @Override @EnforcePermission(android.Manifest.permission.READ_PHONE_STATE) - public void testMethodParentShortPermission() {} + public void testMethodParentShortPermission() { + testMethodParentShortPermission_enforcePermission(); + } @Override @EnforcePermission(anyOf={"INTERNET", "READ_PHONE_STATE"}) - public void testMethodAnyLiteral() {} + public void testMethodAnyLiteral() { + testMethodAnyLiteral_enforcePermission(); + } @Override @EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE}) - public void testMethodAnyLiteralParentsShortPermission() {} + public void testMethodAnyLiteralParentsShortPermission() { + testMethodAnyLiteralParentsShortPermission_enforcePermission(); + } } """).indented(), *stubs @@ -355,16 +394,24 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { public class TestClass121 extends IFooMethod.Stub { @Override @EnforcePermission("READ_WRONG_PHONE_STATE") - public void testMethod() {} + public void testMethod() { + testMethod_enforcePermission(); + } @Override @EnforcePermission(android.Manifest.permission.READ_WRONG_PHONE_STATE) - public void testMethodParentShortPermission() {} + public void testMethodParentShortPermission() { + testMethodParentShortPermission_enforcePermission(); + } @Override @EnforcePermission(anyOf={"WRONG_INTERNET", "READ_PHONE_STATE"}) - public void testMethodAnyLiteral() {} + public void testMethodAnyLiteral() { + testMethodAnyLiteral_enforcePermission(); + } @Override @EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_WRONG_PHONE_STATE}) - public void testMethodAnyLiteralParentsShortPermission() {} + public void testMethodAnyLiteralParentsShortPermission() { + testMethodAnyLiteralParentsShortPermission_enforcePermission(); + } } """).indented(), *stubs @@ -372,17 +419,17 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { .run() .expect( """ - src/test/pkg/TestClass121.java:6: Error: The method TestClass121.testMethod is annotated with @EnforcePermission("READ_WRONG_PHONE_STATE") which differs from the overridden method Stub.testMethod: @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethod() {} + src/test/pkg/TestClass121.java:6: Error: The method TestClass121.testMethod is annotated with @EnforcePermission("READ_WRONG_PHONE_STATE") which differs from the overridden method IFooMethod.testMethod: @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] + public void testMethod() { ~~~~~~~~~~ - src/test/pkg/TestClass121.java:9: Error: The method TestClass121.testMethodParentShortPermission is annotated with @EnforcePermission(android.Manifest.permission.READ_WRONG_PHONE_STATE) which differs from the overridden method Stub.testMethodParentShortPermission: @android.annotation.EnforcePermission("READ_PHONE_STATE"). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethodParentShortPermission() {} + src/test/pkg/TestClass121.java:11: Error: The method TestClass121.testMethodParentShortPermission is annotated with @EnforcePermission(android.Manifest.permission.READ_WRONG_PHONE_STATE) which differs from the overridden method IFooMethod.testMethodParentShortPermission: @android.annotation.EnforcePermission("READ_PHONE_STATE"). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] + public void testMethodParentShortPermission() { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - src/test/pkg/TestClass121.java:12: Error: The method TestClass121.testMethodAnyLiteral is annotated with @EnforcePermission(anyOf={"WRONG_INTERNET", "READ_PHONE_STATE"}) which differs from the overridden method Stub.testMethodAnyLiteral: @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, "android.permission.READ_PHONE_STATE"}). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethodAnyLiteral() {} + src/test/pkg/TestClass121.java:16: Error: The method TestClass121.testMethodAnyLiteral is annotated with @EnforcePermission(anyOf={"WRONG_INTERNET", "READ_PHONE_STATE"}) which differs from the overridden method IFooMethod.testMethodAnyLiteral: @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, "android.permission.READ_PHONE_STATE"}). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] + public void testMethodAnyLiteral() { ~~~~~~~~~~~~~~~~~~~~ - src/test/pkg/TestClass121.java:15: Error: The method TestClass121.testMethodAnyLiteralParentsShortPermission is annotated with @EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_WRONG_PHONE_STATE}) which differs from the overridden method Stub.testMethodAnyLiteralParentsShortPermission: @android.annotation.EnforcePermission(anyOf={"INTERNET", "READ_PHONE_STATE"}). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] - public void testMethodAnyLiteralParentsShortPermission() {} + src/test/pkg/TestClass121.java:21: Error: The method TestClass121.testMethodAnyLiteralParentsShortPermission is annotated with @EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_WRONG_PHONE_STATE}) which differs from the overridden method IFooMethod.testMethodAnyLiteralParentsShortPermission: @android.annotation.EnforcePermission(anyOf={"INTERNET", "READ_PHONE_STATE"}). The same annotation must be used for both methods. [MismatchingEnforcePermissionAnnotation] + public void testMethodAnyLiteralParentsShortPermission() { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 errors, 0 warnings """.addLineContinuation() @@ -396,27 +443,6 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { """ public interface IFooMethod extends android.os.IInterface { public static abstract class Stub extends android.os.Binder implements IFooMethod { - @Override - @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE) - public void testMethod() {} - @Override - @android.annotation.EnforcePermission("READ_PHONE_STATE") - public void testMethodParentShortPermission() {} - @Override - @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE}) - public void testMethodAny() {} - @Override - @android.annotation.EnforcePermission(anyOf={android.Manifest.permission.INTERNET, "android.permission.READ_PHONE_STATE"}) - public void testMethodAnyLiteral() {} - @Override - @android.annotation.EnforcePermission(anyOf={"INTERNET", "READ_PHONE_STATE"}) - public void testMethodAnyLiteralParentsShortPermission() {} - @Override - @android.annotation.EnforcePermission(allOf={android.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE}) - public void testMethodAll() {} - @Override - @android.annotation.EnforcePermission(allOf={android.Manifest.permission.INTERNET, "android.permission.READ_PHONE_STATE"}) - public void testMethodAllLiteral() {} } @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE) public void testMethod(); @@ -441,8 +467,6 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { """ public interface IBar extends android.os.IInterface { public static abstract class Stub extends android.os.Binder implements IBar { - @Override - public void testMethod() {} } public void testMethod(); } diff --git a/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionHelperDetectorCodegenTest.kt b/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionHelperDetectorCodegenTest.kt index 3ef02f865355..a4b0bc3a7c76 100644 --- a/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionHelperDetectorCodegenTest.kt +++ b/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionHelperDetectorCodegenTest.kt @@ -28,7 +28,8 @@ class EnforcePermissionHelperDetectorCodegenTest : LintDetectorTest() { override fun getDetector(): Detector = EnforcePermissionDetector() override fun getIssues(): List = listOf( - EnforcePermissionDetector.ISSUE_ENFORCE_PERMISSION_HELPER + EnforcePermissionDetector.ISSUE_ENFORCE_PERMISSION_HELPER, + EnforcePermissionDetector.ISSUE_MISUSING_ENFORCE_PERMISSION ) override fun lint(): TestLintTask = super.lint().allowMissingSdk(true) -- cgit v1.2.3-59-g8ed1b From 9252e5ceaf53349cdf6b344aa54a983cd1426341 Mon Sep 17 00:00:00 2001 From: ThiƩbaud Weksteen Date: Mon, 30 Oct 2023 14:17:43 +1100 Subject: Add test for mis-annotated method Previously, if a method: 1. was annotated with @EnforcePermission, 2. belonged to a class which implements an AIDL interface, 3. but did not override a method from that interface; an error was returned as the call to the helper was missing. The correct error for this case is that the method should not use the annotation. Update the detector and add a test for this case. Bug: 307433823 Test: atest --host AndroidGlobalLintCheckerTest Change-Id: I04c68e95bc4932459b46982a7dd1814424ff6b46 --- .../android/lint/aidl/EnforcePermissionDetector.kt | 9 +++++++++ .../lint/aidl/EnforcePermissionDetectorTest.kt | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) (limited to 'tools') diff --git a/tools/lint/global/checks/src/main/java/com/google/android/lint/aidl/EnforcePermissionDetector.kt b/tools/lint/global/checks/src/main/java/com/google/android/lint/aidl/EnforcePermissionDetector.kt index dcd94f1bcba4..83b8f163abee 100644 --- a/tools/lint/global/checks/src/main/java/com/google/android/lint/aidl/EnforcePermissionDetector.kt +++ b/tools/lint/global/checks/src/main/java/com/google/android/lint/aidl/EnforcePermissionDetector.kt @@ -197,6 +197,15 @@ class EnforcePermissionDetector : Detector(), SourceCodeScanner { /* Check that we are connected to the super class */ val overridingMethod = node as PsiMethod val parents = overridingMethod.findSuperMethods() + if (parents.isEmpty()) { + context.report( + ISSUE_MISUSING_ENFORCE_PERMISSION, + node, + context.getLocation(node), + "The method ${node.name} does not override an AIDL generated method" + ) + return + } for (overriddenMethod in parents) { // The equivalence check can be skipped, if both methods are // annotated, it will be verified by visitAnnotationUsage. diff --git a/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt b/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt index 95ca8872c770..d8afcb977594 100644 --- a/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt +++ b/tools/lint/global/checks/src/test/java/com/google/android/lint/aidl/EnforcePermissionDetectorTest.kt @@ -155,6 +155,27 @@ class EnforcePermissionDetectorTest : LintDetectorTest() { """.addLineContinuation()) } + fun testDetectIssuesAnnotationOnNonStubMethod() { + lint().files(java( + """ + package test.pkg; + public class TestClass42 extends IFooMethod.Stub { + @android.annotation.EnforcePermission(android.Manifest.permission.INTERNET) + public void aRegularMethodNotPartOfStub() { + } + } + """).indented(), + *stubs + ) + .run() + .expect(""" + src/test/pkg/TestClass42.java:3: Error: The method aRegularMethodNotPartOfStub does not override an AIDL generated method [MisusingEnforcePermissionAnnotation] + @android.annotation.EnforcePermission(android.Manifest.permission.INTERNET) + ^ + 1 errors, 0 warnings + """.addLineContinuation()) + } + fun testDetectIssuesEmptyAnnotationOnMethod() { lint().files(java( """ -- cgit v1.2.3-59-g8ed1b