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 15package rust 16 17import ( 18 "encoding/json" 19 "io/ioutil" 20 "path/filepath" 21 "sort" 22 "strings" 23 "testing" 24 25 "android/soong/android" 26) 27 28// testProjectJson run the generation of rust-project.json. It returns the raw 29// content of the generated file. 30func testProjectJson(t *testing.T, bp string) []byte { 31 result := android.GroupFixturePreparers( 32 prepareForRustTest, 33 android.FixtureMergeEnv(map[string]string{"SOONG_GEN_RUST_PROJECT": "1"}), 34 ).RunTestWithBp(t, bp) 35 36 // The JSON file is generated via WriteFileToOutputDir. Therefore, it 37 // won't appear in the Output of the TestingSingleton. Manually verify 38 // it exists. 39 content, err := ioutil.ReadFile(filepath.Join(result.Config.BuildDir(), rustProjectJsonFileName)) 40 if err != nil { 41 t.Errorf("rust-project.json has not been generated") 42 } 43 return content 44} 45 46// validateJsonCrates validates that content follows the basic structure of 47// rust-project.json. It returns the crates attribute if the validation 48// succeeded. 49// It uses an empty interface instead of relying on a defined structure to 50// avoid a strong dependency on our implementation. 51func validateJsonCrates(t *testing.T, rawContent []byte) []interface{} { 52 var content interface{} 53 err := json.Unmarshal(rawContent, &content) 54 if err != nil { 55 t.Errorf("Unable to parse the rust-project.json as JSON: %v", err) 56 } 57 root, ok := content.(map[string]interface{}) 58 if !ok { 59 t.Errorf("Unexpected JSON format: %v", content) 60 } 61 if _, ok = root["crates"]; !ok { 62 t.Errorf("No crates attribute in rust-project.json: %v", root) 63 } 64 crates, ok := root["crates"].([]interface{}) 65 if !ok { 66 t.Errorf("Unexpected crates format: %v", root["crates"]) 67 } 68 return crates 69} 70 71// validateCrate ensures that a crate can be parsed as a map. 72func validateCrate(t *testing.T, crate interface{}) map[string]interface{} { 73 c, ok := crate.(map[string]interface{}) 74 if !ok { 75 t.Fatalf("Unexpected type for crate: %v", c) 76 } 77 return c 78} 79 80// validateDependencies parses the dependencies for a crate. It returns a list 81// of the dependencies name. 82func validateDependencies(t *testing.T, crate map[string]interface{}) []string { 83 var dependencies []string 84 deps, ok := crate["deps"].([]interface{}) 85 if !ok { 86 t.Errorf("Unexpected format for deps: %v", crate["deps"]) 87 } 88 for _, dep := range deps { 89 d, ok := dep.(map[string]interface{}) 90 if !ok { 91 t.Errorf("Unexpected format for dependency: %v", dep) 92 } 93 name, ok := d["name"].(string) 94 if !ok { 95 t.Errorf("Dependency is missing the name key: %v", d) 96 } 97 dependencies = append(dependencies, name) 98 } 99 return dependencies 100} 101 102func TestProjectJsonDep(t *testing.T) { 103 bp := ` 104 rust_library { 105 name: "liba", 106 srcs: ["a/src/lib.rs"], 107 crate_name: "a" 108 } 109 rust_library { 110 name: "libb", 111 srcs: ["b/src/lib.rs"], 112 crate_name: "b", 113 rlibs: ["liba"], 114 } 115 ` 116 jsonContent := testProjectJson(t, bp) 117 validateJsonCrates(t, jsonContent) 118} 119 120func TestProjectJsonFeature(t *testing.T) { 121 bp := ` 122 rust_library { 123 name: "liba", 124 srcs: ["a/src/lib.rs"], 125 crate_name: "a", 126 features: ["f1", "f2"] 127 } 128 ` 129 jsonContent := testProjectJson(t, bp) 130 crates := validateJsonCrates(t, jsonContent) 131 for _, c := range crates { 132 crate := validateCrate(t, c) 133 cfgs, ok := crate["cfg"].([]interface{}) 134 if !ok { 135 t.Fatalf("Unexpected type for cfgs: %v", crate) 136 } 137 expectedCfgs := []string{"feature=\"f1\"", "feature=\"f2\""} 138 foundCfgs := []string{} 139 for _, cfg := range cfgs { 140 cfg, ok := cfg.(string) 141 if !ok { 142 t.Fatalf("Unexpected type for cfg: %v", cfg) 143 } 144 foundCfgs = append(foundCfgs, cfg) 145 } 146 sort.Strings(foundCfgs) 147 for i, foundCfg := range foundCfgs { 148 if foundCfg != expectedCfgs[i] { 149 t.Errorf("Incorrect features: got %v; want %v", foundCfg, expectedCfgs[i]) 150 } 151 } 152 } 153} 154 155func TestProjectJsonBinary(t *testing.T) { 156 bp := ` 157 rust_binary { 158 name: "libz", 159 srcs: ["z/src/lib.rs"], 160 crate_name: "z" 161 } 162 ` 163 jsonContent := testProjectJson(t, bp) 164 crates := validateJsonCrates(t, jsonContent) 165 for _, c := range crates { 166 crate := validateCrate(t, c) 167 rootModule, ok := crate["root_module"].(string) 168 if !ok { 169 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"]) 170 } 171 if rootModule == "z/src/lib.rs" { 172 return 173 } 174 } 175 t.Errorf("Entry for binary %q not found: %s", "a", jsonContent) 176} 177 178func TestProjectJsonBindGen(t *testing.T) { 179 bp := ` 180 rust_library { 181 name: "libd", 182 srcs: ["d/src/lib.rs"], 183 rlibs: ["libbindings1"], 184 crate_name: "d" 185 } 186 rust_bindgen { 187 name: "libbindings1", 188 crate_name: "bindings1", 189 source_stem: "bindings1", 190 host_supported: true, 191 wrapper_src: "src/any.h", 192 } 193 rust_library_host { 194 name: "libe", 195 srcs: ["e/src/lib.rs"], 196 rustlibs: ["libbindings2"], 197 crate_name: "e" 198 } 199 rust_bindgen_host { 200 name: "libbindings2", 201 crate_name: "bindings2", 202 source_stem: "bindings2", 203 wrapper_src: "src/any.h", 204 } 205 ` 206 jsonContent := testProjectJson(t, bp) 207 crates := validateJsonCrates(t, jsonContent) 208 for _, c := range crates { 209 crate := validateCrate(t, c) 210 rootModule, ok := crate["root_module"].(string) 211 if !ok { 212 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"]) 213 } 214 if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") { 215 t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule) 216 } 217 if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, android.BuildOs.String()) { 218 t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v", 219 rootModule, android.BuildOs.String()) 220 } 221 // Check that libbindings1 does not depend on itself. 222 if strings.Contains(rootModule, "libbindings1") { 223 for _, depName := range validateDependencies(t, crate) { 224 if depName == "bindings1" { 225 t.Errorf("libbindings1 depends on itself") 226 } 227 } 228 } 229 if strings.Contains(rootModule, "d/src/lib.rs") { 230 // Check that libd depends on libbindings1 231 found := false 232 for _, depName := range validateDependencies(t, crate) { 233 if depName == "bindings1" { 234 found = true 235 break 236 } 237 } 238 if !found { 239 t.Errorf("libd does not depend on libbindings1: %v", crate) 240 } 241 // Check that OUT_DIR is populated. 242 env, ok := crate["env"].(map[string]interface{}) 243 if !ok { 244 t.Errorf("libd does not have its environment variables set: %v", crate) 245 } 246 if _, ok = env["OUT_DIR"]; !ok { 247 t.Errorf("libd does not have its OUT_DIR set: %v", env) 248 } 249 250 } 251 } 252} 253 254func TestProjectJsonMultiVersion(t *testing.T) { 255 bp := ` 256 rust_library { 257 name: "liba1", 258 srcs: ["a1/src/lib.rs"], 259 crate_name: "a" 260 } 261 rust_library { 262 name: "liba2", 263 srcs: ["a2/src/lib.rs"], 264 crate_name: "a", 265 } 266 rust_library { 267 name: "libb", 268 srcs: ["b/src/lib.rs"], 269 crate_name: "b", 270 rustlibs: ["liba1", "liba2"], 271 } 272 ` 273 jsonContent := testProjectJson(t, bp) 274 crates := validateJsonCrates(t, jsonContent) 275 for _, c := range crates { 276 crate := validateCrate(t, c) 277 rootModule, ok := crate["root_module"].(string) 278 if !ok { 279 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"]) 280 } 281 // Make sure that b has 2 different dependencies. 282 if rootModule == "b/src/lib.rs" { 283 aCount := 0 284 deps := validateDependencies(t, crate) 285 for _, depName := range deps { 286 if depName == "a" { 287 aCount++ 288 } 289 } 290 if aCount != 2 { 291 t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps) 292 } 293 return 294 } 295 } 296 t.Errorf("libb crate has not been found: %v", crates) 297} 298