Jihoon Kang | f86fe9a | 2024-06-26 22:18:10 +0000 | [diff] [blame^] | 1 | // Copyright 2024 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package apex |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/java" |
| 20 | "fmt" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | var checkContainerMatch = func(t *testing.T, name string, container string, expected bool, actual bool) { |
| 25 | errorMessage := fmt.Sprintf("module %s container %s value differ", name, container) |
| 26 | android.AssertBoolEquals(t, errorMessage, expected, actual) |
| 27 | } |
| 28 | |
| 29 | func TestApexDepsContainers(t *testing.T) { |
| 30 | result := android.GroupFixturePreparers( |
| 31 | prepareForApexTest, |
| 32 | java.PrepareForTestWithJavaSdkLibraryFiles, |
| 33 | java.FixtureWithLastReleaseApis("mybootclasspathlib"), |
| 34 | ).RunTestWithBp(t, ` |
| 35 | apex { |
| 36 | name: "myapex", |
| 37 | key: "myapex.key", |
| 38 | bootclasspath_fragments: [ |
| 39 | "mybootclasspathfragment", |
| 40 | ], |
| 41 | updatable: true, |
| 42 | min_sdk_version: "30", |
| 43 | } |
| 44 | apex_key { |
| 45 | name: "myapex.key", |
| 46 | public_key: "testkey.avbpubkey", |
| 47 | private_key: "testkey.pem", |
| 48 | } |
| 49 | bootclasspath_fragment { |
| 50 | name: "mybootclasspathfragment", |
| 51 | contents: [ |
| 52 | "mybootclasspathlib", |
| 53 | ], |
| 54 | apex_available: [ |
| 55 | "myapex", |
| 56 | ], |
| 57 | hidden_api: { |
| 58 | split_packages: ["*"], |
| 59 | }, |
| 60 | } |
| 61 | java_sdk_library { |
| 62 | name: "mybootclasspathlib", |
| 63 | srcs: [ |
| 64 | "mybootclasspathlib.java", |
| 65 | ], |
| 66 | apex_available: [ |
| 67 | "myapex", |
| 68 | ], |
| 69 | compile_dex: true, |
| 70 | static_libs: [ |
| 71 | "foo", |
| 72 | "baz", |
| 73 | ], |
| 74 | libs: [ |
| 75 | "bar", |
| 76 | ], |
| 77 | min_sdk_version: "30", |
| 78 | } |
| 79 | java_library { |
| 80 | name: "foo", |
| 81 | srcs:[ |
| 82 | "A.java", |
| 83 | ], |
| 84 | apex_available: [ |
| 85 | "myapex", |
| 86 | ], |
| 87 | min_sdk_version: "30", |
| 88 | } |
| 89 | java_library { |
| 90 | name: "bar", |
| 91 | srcs:[ |
| 92 | "A.java", |
| 93 | ], |
| 94 | min_sdk_version: "30", |
| 95 | } |
| 96 | java_library { |
| 97 | name: "baz", |
| 98 | srcs:[ |
| 99 | "A.java", |
| 100 | ], |
| 101 | apex_available: [ |
| 102 | "//apex_available:platform", |
| 103 | "myapex", |
| 104 | ], |
| 105 | min_sdk_version: "30", |
| 106 | } |
| 107 | `) |
| 108 | testcases := []struct { |
| 109 | moduleName string |
| 110 | variant string |
| 111 | isSystemContainer bool |
| 112 | isApexContainer bool |
| 113 | }{ |
| 114 | { |
| 115 | moduleName: "mybootclasspathlib", |
| 116 | variant: "android_common_myapex", |
| 117 | isSystemContainer: true, |
| 118 | isApexContainer: true, |
| 119 | }, |
| 120 | { |
| 121 | moduleName: "mybootclasspathlib.impl", |
| 122 | variant: "android_common_apex30", |
| 123 | isSystemContainer: true, |
| 124 | isApexContainer: true, |
| 125 | }, |
| 126 | { |
| 127 | moduleName: "mybootclasspathlib.stubs", |
| 128 | variant: "android_common", |
| 129 | isSystemContainer: true, |
| 130 | isApexContainer: false, |
| 131 | }, |
| 132 | { |
| 133 | moduleName: "foo", |
| 134 | variant: "android_common_apex30", |
| 135 | isSystemContainer: true, |
| 136 | isApexContainer: true, |
| 137 | }, |
| 138 | { |
| 139 | moduleName: "bar", |
| 140 | variant: "android_common", |
| 141 | isSystemContainer: true, |
| 142 | isApexContainer: false, |
| 143 | }, |
| 144 | { |
| 145 | moduleName: "baz", |
| 146 | variant: "android_common_apex30", |
| 147 | isSystemContainer: true, |
| 148 | isApexContainer: true, |
| 149 | }, |
| 150 | } |
| 151 | |
| 152 | for _, c := range testcases { |
| 153 | m := result.ModuleForTests(c.moduleName, c.variant) |
| 154 | containers, _ := android.OtherModuleProvider(result.TestContext.OtherModuleProviderAdaptor(), m.Module(), android.ContainersInfoProvider) |
| 155 | belongingContainers := containers.BelongingContainers() |
| 156 | checkContainerMatch(t, c.moduleName, "system", c.isSystemContainer, android.InList(android.SystemContainer, belongingContainers)) |
| 157 | checkContainerMatch(t, c.moduleName, "apex", c.isApexContainer, android.InList(android.ApexContainer, belongingContainers)) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestNonUpdatableApexDepsContainers(t *testing.T) { |
| 162 | result := android.GroupFixturePreparers( |
| 163 | prepareForApexTest, |
| 164 | java.PrepareForTestWithJavaSdkLibraryFiles, |
| 165 | java.FixtureWithLastReleaseApis("mybootclasspathlib"), |
| 166 | ).RunTestWithBp(t, ` |
| 167 | apex { |
| 168 | name: "myapex", |
| 169 | key: "myapex.key", |
| 170 | bootclasspath_fragments: [ |
| 171 | "mybootclasspathfragment", |
| 172 | ], |
| 173 | updatable: false, |
| 174 | } |
| 175 | apex_key { |
| 176 | name: "myapex.key", |
| 177 | public_key: "testkey.avbpubkey", |
| 178 | private_key: "testkey.pem", |
| 179 | } |
| 180 | bootclasspath_fragment { |
| 181 | name: "mybootclasspathfragment", |
| 182 | contents: [ |
| 183 | "mybootclasspathlib", |
| 184 | ], |
| 185 | apex_available: [ |
| 186 | "myapex", |
| 187 | ], |
| 188 | hidden_api: { |
| 189 | split_packages: ["*"], |
| 190 | }, |
| 191 | } |
| 192 | java_sdk_library { |
| 193 | name: "mybootclasspathlib", |
| 194 | srcs: [ |
| 195 | "mybootclasspathlib.java", |
| 196 | ], |
| 197 | apex_available: [ |
| 198 | "myapex", |
| 199 | ], |
| 200 | compile_dex: true, |
| 201 | static_libs: [ |
| 202 | "foo", |
| 203 | ], |
| 204 | libs: [ |
| 205 | "bar", |
| 206 | ], |
| 207 | } |
| 208 | java_library { |
| 209 | name: "foo", |
| 210 | srcs:[ |
| 211 | "A.java", |
| 212 | ], |
| 213 | apex_available: [ |
| 214 | "myapex", |
| 215 | ], |
| 216 | } |
| 217 | java_library { |
| 218 | name: "bar", |
| 219 | srcs:[ |
| 220 | "A.java", |
| 221 | ], |
| 222 | } |
| 223 | `) |
| 224 | testcases := []struct { |
| 225 | moduleName string |
| 226 | variant string |
| 227 | isSystemContainer bool |
| 228 | isApexContainer bool |
| 229 | }{ |
| 230 | { |
| 231 | moduleName: "mybootclasspathlib", |
| 232 | variant: "android_common_myapex", |
| 233 | isSystemContainer: true, |
| 234 | isApexContainer: true, |
| 235 | }, |
| 236 | { |
| 237 | moduleName: "mybootclasspathlib.impl", |
| 238 | variant: "android_common_apex10000", |
| 239 | isSystemContainer: true, |
| 240 | isApexContainer: true, |
| 241 | }, |
| 242 | { |
| 243 | moduleName: "mybootclasspathlib.stubs", |
| 244 | variant: "android_common", |
| 245 | isSystemContainer: true, |
| 246 | isApexContainer: false, |
| 247 | }, |
| 248 | { |
| 249 | moduleName: "foo", |
| 250 | variant: "android_common_apex10000", |
| 251 | isSystemContainer: true, |
| 252 | isApexContainer: true, |
| 253 | }, |
| 254 | { |
| 255 | moduleName: "bar", |
| 256 | variant: "android_common", |
| 257 | isSystemContainer: true, |
| 258 | isApexContainer: false, |
| 259 | }, |
| 260 | } |
| 261 | |
| 262 | for _, c := range testcases { |
| 263 | m := result.ModuleForTests(c.moduleName, c.variant) |
| 264 | containers, _ := android.OtherModuleProvider(result.TestContext.OtherModuleProviderAdaptor(), m.Module(), android.ContainersInfoProvider) |
| 265 | belongingContainers := containers.BelongingContainers() |
| 266 | checkContainerMatch(t, c.moduleName, "system", c.isSystemContainer, android.InList(android.SystemContainer, belongingContainers)) |
| 267 | checkContainerMatch(t, c.moduleName, "apex", c.isApexContainer, android.InList(android.ApexContainer, belongingContainers)) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | func TestUpdatableAndNonUpdatableApexesIdenticalMinSdkVersion(t *testing.T) { |
| 272 | result := android.GroupFixturePreparers( |
| 273 | prepareForApexTest, |
| 274 | java.PrepareForTestWithJavaSdkLibraryFiles, |
| 275 | android.FixtureMergeMockFs(android.MockFS{ |
| 276 | "system/sepolicy/apex/myapex_non_updatable-file_contexts": nil, |
| 277 | "system/sepolicy/apex/myapex_updatable-file_contexts": nil, |
| 278 | }), |
| 279 | ).RunTestWithBp(t, ` |
| 280 | apex { |
| 281 | name: "myapex_non_updatable", |
| 282 | key: "myapex_non_updatable.key", |
| 283 | java_libs: [ |
| 284 | "foo", |
| 285 | ], |
| 286 | updatable: false, |
| 287 | min_sdk_version: "30", |
| 288 | } |
| 289 | apex_key { |
| 290 | name: "myapex_non_updatable.key", |
| 291 | public_key: "testkey.avbpubkey", |
| 292 | private_key: "testkey.pem", |
| 293 | } |
| 294 | |
| 295 | apex { |
| 296 | name: "myapex_updatable", |
| 297 | key: "myapex_updatable.key", |
| 298 | java_libs: [ |
| 299 | "foo", |
| 300 | ], |
| 301 | updatable: true, |
| 302 | min_sdk_version: "30", |
| 303 | } |
| 304 | apex_key { |
| 305 | name: "myapex_updatable.key", |
| 306 | public_key: "testkey.avbpubkey", |
| 307 | private_key: "testkey.pem", |
| 308 | } |
| 309 | |
| 310 | java_library { |
| 311 | name: "foo", |
| 312 | srcs:[ |
| 313 | "A.java", |
| 314 | ], |
| 315 | apex_available: [ |
| 316 | "myapex_non_updatable", |
| 317 | "myapex_updatable", |
| 318 | ], |
| 319 | min_sdk_version: "30", |
| 320 | sdk_version: "current", |
| 321 | } |
| 322 | `) |
| 323 | |
| 324 | fooApexVariant := result.ModuleForTests("foo", "android_common_apex30") |
| 325 | containers, _ := android.OtherModuleProvider(result.TestContext.OtherModuleProviderAdaptor(), fooApexVariant.Module(), android.ContainersInfoProvider) |
| 326 | belongingContainers := containers.BelongingContainers() |
| 327 | checkContainerMatch(t, "foo", "system", true, android.InList(android.SystemContainer, belongingContainers)) |
| 328 | checkContainerMatch(t, "foo", "apex", true, android.InList(android.ApexContainer, belongingContainers)) |
| 329 | } |