Tiem Song | abdba82 | 2022-02-10 21:46:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | @file:Suppress("UnstableApiUsage") |
| 18 | |
| 19 | package androidx.build.lint |
| 20 | |
| 21 | import org.junit.Test |
| 22 | import org.junit.runner.RunWith |
| 23 | import org.junit.runners.JUnit4 |
| 24 | |
| 25 | @RunWith(JUnit4::class) |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 26 | class AndroidManifestServiceExportedDetectorTest : |
| 27 | AbstractLintDetectorTest( |
| 28 | useDetector = AndroidManifestServiceExportedDetector(), |
| 29 | useIssues = listOf(AndroidManifestServiceExportedDetector.ISSUE), |
| 30 | ) { |
Tiem Song | abdba82 | 2022-02-10 21:46:02 -0800 | [diff] [blame] | 31 | |
| 32 | @Test |
| 33 | fun `Detect missing exported=true declaration in service tag`() { |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 34 | val input = arrayOf(manifestSample()) |
Tiem Song | abdba82 | 2022-02-10 21:46:02 -0800 | [diff] [blame] | 35 | |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 36 | val expected = |
| 37 | """ |
Aurimas Liutikas | dcfa035 | 2022-03-14 16:05:33 -0700 | [diff] [blame] | 38 | AndroidManifest.xml:21: Error: Missing exported=true in <service> tag [MissingServiceExportedEqualsTrue] |
Tiem Song | abdba82 | 2022-02-10 21:46:02 -0800 | [diff] [blame] | 39 | <service android:name="androidx.core.app.JobIntentService"> |
| 40 | ^ |
| 41 | 1 errors, 0 warnings |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 42 | """ |
| 43 | .trimIndent() |
Tiem Song | abdba82 | 2022-02-10 21:46:02 -0800 | [diff] [blame] | 44 | |
| 45 | check(*input).expect(expected) |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | fun `Detect present exported=true declaration in service tag`() { |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 50 | val input = |
| 51 | xml( |
| 52 | "AndroidManifest.xml", |
| 53 | """ |
Tiem Song | abdba82 | 2022-02-10 21:46:02 -0800 | [diff] [blame] | 54 | <manifest xmlns:android="http://schemas.android.com/apk/res/android"> |
| 55 | <application> |
| 56 | <service |
| 57 | android:name="androidx.service" |
| 58 | android:exported="true" /> |
| 59 | </application> |
| 60 | </manifest> |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 61 | """ |
| 62 | .trimIndent() |
| 63 | ) |
Tiem Song | abdba82 | 2022-02-10 21:46:02 -0800 | [diff] [blame] | 64 | |
| 65 | check(input).expectClean() |
| 66 | } |
| 67 | } |