1 /*
2  * Copyright 2022 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 package androidx.build.dependencyallowlist
18 
19 import org.junit.Assert.assertEquals
20 import org.junit.Test
21 
22 class DependencyAllowlistTest {
23     @Test
allComponentsHaveBugsnull24     fun allComponentsHaveBugs() {
25         val verificationMetadataXml = """
26           <?xml version="1.0" encoding="UTF-8"?>
27           <verification-metadata xmlns="https://schema.gradle.org/dependency-verification"
28                                  xmlns:androidx="https://developer.android.com/jetpack/androidx">
29             <components>
30                <component group="g" name="g" version="3.1" androidx:reason="Unsigned b/8675309">
31                  <artifact name="g-3.1.jar">
32                    <sha256
33                       value="f5759b7fcdfc83a525a036deedcbd32e5b536b625ebc282426f16ca137eb5902"
34                       origin="Generated by Gradle"
35                    />
36                  </artifact>
37                </component>
38             </components>
39           </verification-metadata>
40         """.trimIndent()
41         assertEquals(listOf<String>(), allowlistWarnings(verificationMetadataXml))
42     }
43 
44     @Test
commentsDontCountAsReasonsnull45     fun commentsDontCountAsReasons() {
46         val verificationMetadataXml = """
47           <?xml version="1.0" encoding="UTF-8"?>
48           <verification-metadata xmlns="https://schema.gradle.org/dependency-verification"
49                                  xmlns:androidx="https://developer.android.com/jetpack/androidx">
50             <components>
51                <!-- Unsigned b/8675309 -->
52                <component group="g" name="g" version="3.1">
53                  <artifact name="g-3.1.jar">
54                    <sha256
55                       value="f5759b7fcdfc83a525a036deedcbd32e5b536b625ebc282426f16ca137eb5902"
56                       origin="Generated by Gradle"
57                    />
58                  </artifact>
59                </component>
60             </components>
61           </verification-metadata>
62         """.trimIndent()
63         assertEquals(
64             listOf(
65                 "Add androidx:reason for unsigned component 'g' (See go/androidx-unsigned-bugs)"
66             ),
67             allowlistWarnings(verificationMetadataXml)
68         )
69     }
70 
71     @Test
oneComponentHasGitHubBugnull72     fun oneComponentHasGitHubBug() {
73         val verificationMetadataXml = """
74           <?xml version="1.0" encoding="UTF-8"?>
75           <verification-metadata xmlns="https://schema.gradle.org/dependency-verification"
76                                  xmlns:androidx="https://developer.android.com/jetpack/androidx">
77             <components>
78                <component group="g" name="g" version="3.1"
79                           androidx:reason="Unsigned https://github.com/rerepo/somecode/issues/65">
80                  <artifact name="g-3.1.jar">
81                    <sha256
82                       value="f5759b7fcdfc83a525a036deedcbd32e5b536b625ebc282426f16ca137eb5902"
83                       origin="Generated by Gradle"
84                    />
85                  </artifact>
86                </component>
87 
88                <component group="ng" name="g" version="3.1"
89                           androidx:reason="Not a good bug comment">
90                  <artifact name="g-3.1.jar">
91                    <sha256
92                       value="f5759b7fcdfc83a525a036deedcbd32e5b536b625ebc282426f16ca137eb5902"
93                       origin="Generated by Gradle"
94                    />
95                  </artifact>
96                </component>
97             </components>
98           </verification-metadata>
99         """.trimIndent()
100         assertEquals(
101             listOf(
102                 "Add androidx:reason for unsigned component 'ng' (See go/androidx-unsigned-bugs)"
103             ),
104             allowlistWarnings(verificationMetadataXml)
105         )
106     }
107 
108     @Test
oneComponentNoReasonnull109     fun oneComponentNoReason() {
110         val verificationMetadataXml = """
111           <?xml version="1.0" encoding="UTF-8"?>
112           <verification-metadata xmlns="https://schema.gradle.org/dependency-verification"
113                                  xmlns:androidx="https://developer.android.com/jetpack/androidx">
114             <components>
115                <component group="g" name="g" version="3.1">
116                  <artifact name="g-3.1.jar">
117                    <sha256
118                       value="f5759b7fcdfc83a525a036deedcbd32e5b536b625ebc282426f16ca137eb5902"
119                       origin="Generated by Gradle"
120                    />
121                  </artifact>
122                </component>
123             </components>
124           </verification-metadata>
125         """.trimIndent()
126         assertEquals(
127             listOf(
128                 "Add androidx:reason for unsigned component 'g' (See go/androidx-unsigned-bugs)"
129             ),
130             allowlistWarnings(verificationMetadataXml)
131         )
132     }
133 
134     @Test
reasonThatIsNotABugnull135     fun reasonThatIsNotABug() {
136         val verificationMetadataXml = """
137           <?xml version="1.0" encoding="UTF-8"?>
138           <verification-metadata xmlns="https://schema.gradle.org/dependency-verification"
139                                  xmlns:androidx="https://developer.android.com/jetpack/androidx">
140             <components>
141                <component group="ggg" name="ggg" version="3.1"
142                           androidx:reason="This does not reference a bug">
143                  <artifact name="ggg-3.1.jar">
144                    <sha256
145                       value="f5759b7fcdfc83a525a036deedcbd32e5b536b625ebc282426f16ca137eb5902"
146                       origin="Generated by Gradle"
147                    />
148                  </artifact>
149                </component>
150             </components>
151           </verification-metadata>
152         """.trimIndent()
153         assertEquals(
154             listOf(
155                 "Add androidx:reason for unsigned component 'ggg' (See go/androidx-unsigned-bugs)"
156             ),
157             allowlistWarnings(verificationMetadataXml)
158         )
159     }
160 }
161