Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9252e5ce authored by Thiébaud Weksteen's avatar Thiébaud Weksteen
Browse files

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
parent e7ad23ad
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -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.
+21 −0
Original line number Diff line number Diff line
@@ -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(
            """