summaryrefslogtreecommitdiff
path: root/SafetyCenter/ConfigLintChecker/tests/java/android/safetycenter/lint/test/ParserExceptionDetectorTest.kt
blob: 869780eea41846df0d99b05c3577290f0f1f9e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2022 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.safetycenter.lint.test

import android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE
import android.safetycenter.lint.FileSdk
import android.safetycenter.lint.ParserExceptionDetector
import com.android.tools.lint.checks.infrastructure.LintDetectorTest
import com.android.tools.lint.checks.infrastructure.TestFile
import com.android.tools.lint.checks.infrastructure.TestLintTask
import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.Issue
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@Suppress("UnstableApiUsage")
@RunWith(JUnit4::class)
class ParserExceptionDetectorTest : LintDetectorTest() {
    override fun getDetector(): Detector = ParserExceptionDetector()

    override fun getIssues(): List<Issue> = listOf(ParserExceptionDetector.ISSUE)

    override fun lint(): TestLintTask = super.lint().allowMissingSdk(true)

    @After
    fun resetOverrides() {
        FileSdk.maxVersionOverride = null
    }

    @Test
    fun validMinimumConfig_doesNotThrow() {
        lint()
            .files(
                xml("res/raw/safety_center_config.xml", VALID_TIRAMISU_CONFIG),
                STRINGS_WITH_REFERENCE_XML
            )
            .run()
            .expectClean()
    }

    @Test
    fun validFutureConfig_doesNotThrow() {
        lint()
            .files(
                xml("res/raw-99/safety_center_config.xml", VALID_TIRAMISU_CONFIG),
                STRINGS_WITH_REFERENCE_XML
            )
            .run()
            .expectClean()
    }

    @Test
    fun invalidConfigWithSingleSchema_throwsOneError() {
        FileSdk.maxVersionOverride = UPSIDE_DOWN_CAKE
        lint()
            .files(xml("res/raw-v34/safety_center_config.xml", "<invalid-root/>"))
            .run()
            .expect(
                "res/raw-v34/safety_center_config.xml: Error: Parser exception at sdk=34: " +
                    "\"Element safety-center-config missing\", cause: \"null\" " +
                    "[InvalidSafetyCenterConfig]\n1 errors, 0 warnings"
            )
    }

    @Test
    fun invalidConfigWithMultipleSchemas_throwsMultipleErrors() {
        FileSdk.maxVersionOverride = UPSIDE_DOWN_CAKE
        lint()
            .files(xml("res/raw/safety_center_config.xml", "<invalid-root/>"))
            .run()
            .expect(
                "res/raw/safety_center_config.xml: Error: Parser exception at sdk=33: " +
                    "\"Element safety-center-config missing\", cause: \"null\" " +
                    "[InvalidSafetyCenterConfig]\nres/raw/safety_center_config.xml: " +
                    "Error: Parser exception at sdk=34: " +
                    "\"Element safety-center-config missing\", cause: \"null\" " +
                    "[InvalidSafetyCenterConfig]\n2 errors, 0 warnings"
            )
    }

    @Test
    fun validUConfigWithUFields_throwsInT() {
        FileSdk.maxVersionOverride = UPSIDE_DOWN_CAKE
        lint()
            .files(
                xml("res/raw/safety_center_config.xml", VALID_UDC_CONFIG),
                STRINGS_WITH_REFERENCE_XML
            )
            .run()
            .expect(
                "res/raw/safety_center_config.xml: Error: Parser exception at sdk=33: " +
                    "\"Unexpected attribute dynamic-safety-source.notificationsAllowed\", cause: " +
                    "\"null\" [InvalidSafetyCenterConfig]\n1 errors, 0 warnings"
            )
    }

    @Test
    fun unrelatedFile_doesNotThrow() {
        lint().files(xml("res/raw/some_other_config.xml", "<some-other-root/>")).run().expectClean()
    }

    @Test
    fun unrelatedFolder_doesNotThrow() {
        lint().files(xml("res/values/strings.xml", "<some-other-root/>")).run().expectClean()
    }

    private companion object {
        val STRINGS_WITH_REFERENCE_XML: TestFile =
            xml(
                "res/values/strings.xml",
                """
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="reference" translatable="false">Reference</string>
</resources>
                """
            )

        const val VALID_TIRAMISU_CONFIG =
            """
<safety-center-config>
    <safety-sources-config>
        <safety-sources-group
            id="group"
            title="@lint.test.pkg:string/reference"
            summary="@lint.test.pkg:string/reference">
            <static-safety-source
                id="source"
                title="@lint.test.pkg:string/reference"
                summary="@lint.test.pkg:string/reference"
                intentAction="intent"
                profile="primary_profile_only"/>
        </safety-sources-group>
    </safety-sources-config>
</safety-center-config>
            """

        const val VALID_UDC_CONFIG =
            """
<safety-center-config>
    <safety-sources-config>
        <safety-sources-group
            id="group"
            title="@lint.test.pkg:string/reference"
            summary="@lint.test.pkg:string/reference">
            <dynamic-safety-source
                id="source"
                packageName="package"
                title="@lint.test.pkg:string/reference"
                summary="@lint.test.pkg:string/reference"
                intentAction="intent"
                profile="primary_profile_only"
                notificationsAllowed="true"/>
        </safety-sources-group>
    </safety-sources-config>
</safety-center-config>
            """
    }
}