blob: 89a6776c6c4aefa5fe480759cafe0811ef451370 [file] [log] [blame]
Tiem Songabdba822022-02-10 21:46:02 -08001/*
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
19package androidx.build.lint
20
21import org.junit.Test
22import org.junit.runner.RunWith
23import org.junit.runners.JUnit4
24
25@RunWith(JUnit4::class)
Omar Ismailedf734f2024-05-28 12:34:57 +010026class AndroidManifestServiceExportedDetectorTest :
27 AbstractLintDetectorTest(
28 useDetector = AndroidManifestServiceExportedDetector(),
29 useIssues = listOf(AndroidManifestServiceExportedDetector.ISSUE),
30 ) {
Tiem Songabdba822022-02-10 21:46:02 -080031
32 @Test
33 fun `Detect missing exported=true declaration in service tag`() {
Omar Ismailedf734f2024-05-28 12:34:57 +010034 val input = arrayOf(manifestSample())
Tiem Songabdba822022-02-10 21:46:02 -080035
Omar Ismailedf734f2024-05-28 12:34:57 +010036 val expected =
37 """
Aurimas Liutikasdcfa0352022-03-14 16:05:33 -070038AndroidManifest.xml:21: Error: Missing exported=true in <service> tag [MissingServiceExportedEqualsTrue]
Tiem Songabdba822022-02-10 21:46:02 -080039 <service android:name="androidx.core.app.JobIntentService">
40 ^
411 errors, 0 warnings
Omar Ismailedf734f2024-05-28 12:34:57 +010042 """
43 .trimIndent()
Tiem Songabdba822022-02-10 21:46:02 -080044
45 check(*input).expect(expected)
46 }
47
48 @Test
49 fun `Detect present exported=true declaration in service tag`() {
Omar Ismailedf734f2024-05-28 12:34:57 +010050 val input =
51 xml(
52 "AndroidManifest.xml",
53 """
Tiem Songabdba822022-02-10 21:46:02 -080054<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 Ismailedf734f2024-05-28 12:34:57 +010061 """
62 .trimIndent()
63 )
Tiem Songabdba822022-02-10 21:46:02 -080064
65 check(input).expectClean()
66 }
67}