Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 1 | // Copyright 2020 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 dexpreopt |
| 16 | |
| 17 | // This file contains unit tests for class loader context structure. |
| 18 | // For class loader context tests involving .bp files, see TestUsesLibraries in java package. |
| 19 | |
| 20 | import ( |
| 21 | "reflect" |
| 22 | "strings" |
| 23 | "testing" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 28 | const ( |
| 29 | shared = true // dependencies are not added to uses libs |
| 30 | nonshared = false // dependencies are added to uses libs |
| 31 | ) |
| 32 | |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 33 | func TestCLC(t *testing.T) { |
| 34 | // Construct class loader context with the following structure: |
| 35 | // . |
| 36 | // ├── 29 |
| 37 | // │ ├── android.hidl.manager |
| 38 | // │ └── android.hidl.base |
| 39 | // │ |
| 40 | // └── any |
| 41 | // ├── a |
| 42 | // ├── b |
| 43 | // ├── c |
| 44 | // ├── d |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 45 | // │ ├── a2 |
| 46 | // │ ├── b2 |
| 47 | // │ └── c2 |
| 48 | // │ ├── a1 |
| 49 | // │ └── b1 |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 50 | // ├── f |
| 51 | // ├── a3 |
| 52 | // └── b3 |
| 53 | // |
| 54 | ctx := testContext() |
| 55 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 56 | m := make(ClassLoaderContextMap) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 57 | |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 58 | m.AddContext(ctx, "a", nonshared, buildPath(ctx, "a"), installPath(ctx, "a")) |
| 59 | m.AddContext(ctx, "b", shared, buildPath(ctx, "b"), installPath(ctx, "b")) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 60 | |
| 61 | // "Maybe" variant in the good case: add as usual. |
| 62 | c := "c" |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 63 | m.MaybeAddContext(ctx, &c, nonshared, buildPath(ctx, "c"), installPath(ctx, "c")) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 64 | |
| 65 | // "Maybe" variant in the bad case: don't add library with unknown name, keep going. |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 66 | m.MaybeAddContext(ctx, nil, nonshared, nil, nil) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 67 | |
| 68 | // Add some libraries with nested subcontexts. |
| 69 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 70 | m1 := make(ClassLoaderContextMap) |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 71 | m1.AddContext(ctx, "a1", nonshared, buildPath(ctx, "a1"), installPath(ctx, "a1")) |
| 72 | m1.AddContext(ctx, "b1", shared, buildPath(ctx, "b1"), installPath(ctx, "b1")) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 73 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 74 | m2 := make(ClassLoaderContextMap) |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 75 | m2.AddContext(ctx, "a2", nonshared, buildPath(ctx, "a2"), installPath(ctx, "a2")) |
| 76 | m2.AddContext(ctx, "b2", shared, buildPath(ctx, "b2"), installPath(ctx, "b2")) |
| 77 | m2.AddContextForSdk(ctx, AnySdkVersion, "c2", shared, buildPath(ctx, "c2"), installPath(ctx, "c2"), m1) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 78 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 79 | m3 := make(ClassLoaderContextMap) |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 80 | m3.AddContext(ctx, "a3", nonshared, buildPath(ctx, "a3"), installPath(ctx, "a3")) |
| 81 | m3.AddContext(ctx, "b3", shared, buildPath(ctx, "b3"), installPath(ctx, "b3")) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 82 | |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 83 | m.AddContextForSdk(ctx, AnySdkVersion, "d", nonshared, buildPath(ctx, "d"), installPath(ctx, "d"), m2) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 84 | // When the same library is both in conditional and unconditional context, it should be removed |
| 85 | // from conditional context. |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 86 | m.AddContextForSdk(ctx, 42, "f", nonshared, buildPath(ctx, "f"), installPath(ctx, "f"), nil) |
| 87 | m.AddContextForSdk(ctx, AnySdkVersion, "f", nonshared, buildPath(ctx, "f"), installPath(ctx, "f"), nil) |
Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 88 | |
| 89 | // Merge map with implicit root library that is among toplevel contexts => does nothing. |
| 90 | m.AddContextMap(m1, "c") |
| 91 | // Merge map with implicit root library that is not among toplevel contexts => all subcontexts |
| 92 | // of the other map are added as toplevel contexts. |
| 93 | m.AddContextMap(m3, "m_g") |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 94 | |
| 95 | // Compatibility libraries with unknown install paths get default paths. |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 96 | m.AddContextForSdk(ctx, 29, AndroidHidlManager, nonshared, buildPath(ctx, AndroidHidlManager), nil, nil) |
| 97 | m.AddContextForSdk(ctx, 29, AndroidHidlBase, nonshared, buildPath(ctx, AndroidHidlBase), nil, nil) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 98 | |
| 99 | // Add "android.test.mock" to conditional CLC, observe that is gets removed because it is only |
| 100 | // needed as a compatibility library if "android.test.runner" is in CLC as well. |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 101 | m.AddContextForSdk(ctx, 30, AndroidTestMock, nonshared, buildPath(ctx, AndroidTestMock), nil, nil) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 102 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 103 | valid, validationError := validateClassLoaderContext(m) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 104 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 105 | fixClassLoaderContext(m) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 106 | |
| 107 | var haveStr string |
| 108 | var havePaths android.Paths |
| 109 | var haveUsesLibs []string |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 110 | if valid && validationError == nil { |
| 111 | haveStr, havePaths = ComputeClassLoaderContext(m) |
| 112 | haveUsesLibs = m.UsesLibs() |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Test that validation is successful (all paths are known). |
| 116 | t.Run("validate", func(t *testing.T) { |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 117 | if !(valid && validationError == nil) { |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 118 | t.Errorf("invalid class loader context") |
| 119 | } |
| 120 | }) |
| 121 | |
| 122 | // Test that class loader context structure is correct. |
| 123 | t.Run("string", func(t *testing.T) { |
| 124 | wantStr := " --host-context-for-sdk 29 " + |
| 125 | "PCL[out/" + AndroidHidlManager + ".jar]#" + |
| 126 | "PCL[out/" + AndroidHidlBase + ".jar]" + |
| 127 | " --target-context-for-sdk 29 " + |
| 128 | "PCL[/system/framework/" + AndroidHidlManager + ".jar]#" + |
| 129 | "PCL[/system/framework/" + AndroidHidlBase + ".jar]" + |
| 130 | " --host-context-for-sdk any " + |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 131 | "PCL[out/a.jar]#PCL[out/b.jar]#PCL[out/c.jar]#PCL[out/d.jar]" + |
| 132 | "{PCL[out/a2.jar]#PCL[out/b2.jar]#PCL[out/c2.jar]" + |
| 133 | "{PCL[out/a1.jar]#PCL[out/b1.jar]}}#" + |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 134 | "PCL[out/f.jar]#PCL[out/a3.jar]#PCL[out/b3.jar]" + |
| 135 | " --target-context-for-sdk any " + |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 136 | "PCL[/system/a.jar]#PCL[/system/b.jar]#PCL[/system/c.jar]#PCL[/system/d.jar]" + |
| 137 | "{PCL[/system/a2.jar]#PCL[/system/b2.jar]#PCL[/system/c2.jar]" + |
| 138 | "{PCL[/system/a1.jar]#PCL[/system/b1.jar]}}#" + |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 139 | "PCL[/system/f.jar]#PCL[/system/a3.jar]#PCL[/system/b3.jar]" |
| 140 | if wantStr != haveStr { |
| 141 | t.Errorf("\nwant class loader context: %s\nhave class loader context: %s", wantStr, haveStr) |
| 142 | } |
| 143 | }) |
| 144 | |
| 145 | // Test that all expected build paths are gathered. |
| 146 | t.Run("paths", func(t *testing.T) { |
| 147 | wantPaths := []string{ |
| 148 | "out/android.hidl.manager-V1.0-java.jar", "out/android.hidl.base-V1.0-java.jar", |
| 149 | "out/a.jar", "out/b.jar", "out/c.jar", "out/d.jar", |
| 150 | "out/a2.jar", "out/b2.jar", "out/c2.jar", |
| 151 | "out/a1.jar", "out/b1.jar", |
| 152 | "out/f.jar", "out/a3.jar", "out/b3.jar", |
| 153 | } |
| 154 | if !reflect.DeepEqual(wantPaths, havePaths.Strings()) { |
| 155 | t.Errorf("\nwant paths: %s\nhave paths: %s", wantPaths, havePaths) |
| 156 | } |
| 157 | }) |
| 158 | |
| 159 | // Test for libraries that are added by the manifest_fixer. |
| 160 | t.Run("uses libs", func(t *testing.T) { |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 161 | wantUsesLibs := []string{"a", "b", "c", "d", "a2", "b2", "c2", "f", "a3", "b3"} |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 162 | if !reflect.DeepEqual(wantUsesLibs, haveUsesLibs) { |
| 163 | t.Errorf("\nwant uses libs: %s\nhave uses libs: %s", wantUsesLibs, haveUsesLibs) |
| 164 | } |
| 165 | }) |
| 166 | } |
| 167 | |
| 168 | // Test that an unexpected unknown build path causes immediate error. |
| 169 | func TestCLCUnknownBuildPath(t *testing.T) { |
| 170 | ctx := testContext() |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 171 | m := make(ClassLoaderContextMap) |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 172 | err := m.addContext(ctx, AnySdkVersion, "a", nonshared, nil, nil, true, nil) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 173 | checkError(t, err, "unknown build path to <uses-library> \"a\"") |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Test that an unexpected unknown install path causes immediate error. |
| 177 | func TestCLCUnknownInstallPath(t *testing.T) { |
| 178 | ctx := testContext() |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 179 | m := make(ClassLoaderContextMap) |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 180 | err := m.addContext(ctx, AnySdkVersion, "a", nonshared, buildPath(ctx, "a"), nil, true, nil) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 181 | checkError(t, err, "unknown install path to <uses-library> \"a\"") |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | func TestCLCMaybeAdd(t *testing.T) { |
| 185 | ctx := testContext() |
| 186 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 187 | m := make(ClassLoaderContextMap) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 188 | a := "a" |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 189 | m.MaybeAddContext(ctx, &a, nonshared, nil, nil) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 190 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 191 | // The library should be added to <uses-library> tags by the manifest_fixer. |
| 192 | t.Run("maybe add", func(t *testing.T) { |
| 193 | haveUsesLibs := m.UsesLibs() |
| 194 | wantUsesLibs := []string{"a"} |
| 195 | if !reflect.DeepEqual(wantUsesLibs, haveUsesLibs) { |
| 196 | t.Errorf("\nwant uses libs: %s\nhave uses libs: %s", wantUsesLibs, haveUsesLibs) |
| 197 | } |
| 198 | }) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 199 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 200 | // But class loader context in such cases should raise an error on validation. |
| 201 | t.Run("validate", func(t *testing.T) { |
| 202 | _, err := validateClassLoaderContext(m) |
| 203 | checkError(t, err, "invalid path for <uses-library> \"a\"") |
| 204 | }) |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Ulya Trafimovich | 5e13a73 | 2020-11-03 15:33:03 +0000 | [diff] [blame] | 207 | // An attempt to add conditional nested subcontext should fail. |
| 208 | func TestCLCNestedConditional(t *testing.T) { |
| 209 | ctx := testContext() |
| 210 | m1 := make(ClassLoaderContextMap) |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 211 | m1.AddContextForSdk(ctx, 42, "a", nonshared, buildPath(ctx, "a"), installPath(ctx, "a"), nil) |
Ulya Trafimovich | 5e13a73 | 2020-11-03 15:33:03 +0000 | [diff] [blame] | 212 | m := make(ClassLoaderContextMap) |
Ulya Trafimovich | a8c28e2 | 2020-10-06 17:24:19 +0100 | [diff] [blame^] | 213 | err := m.addContext(ctx, AnySdkVersion, "b", nonshared, buildPath(ctx, "b"), installPath(ctx, "b"), true, m1) |
Ulya Trafimovich | 5e13a73 | 2020-11-03 15:33:03 +0000 | [diff] [blame] | 214 | checkError(t, err, "nested class loader context shouldn't have conditional part") |
| 215 | } |
| 216 | |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 217 | func checkError(t *testing.T, have error, want string) { |
| 218 | if have == nil { |
| 219 | t.Errorf("\nwant error: '%s'\nhave: none", want) |
| 220 | } else if msg := have.Error(); !strings.HasPrefix(msg, want) { |
| 221 | t.Errorf("\nwant error: '%s'\nhave error: '%s'\n", want, msg) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | func testContext() android.ModuleInstallPathContext { |
| 226 | config := android.TestConfig("out", nil, "", nil) |
| 227 | return android.ModuleInstallPathContextForTesting(config) |
| 228 | } |
| 229 | |
| 230 | func buildPath(ctx android.PathContext, lib string) android.Path { |
| 231 | return android.PathForOutput(ctx, lib+".jar") |
| 232 | } |
| 233 | |
| 234 | func installPath(ctx android.ModuleInstallPathContext, lib string) android.InstallPath { |
| 235 | return android.PathForModuleInstall(ctx, lib+".jar") |
| 236 | } |