1// Copyright 2019 The Android Open Source Project 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 "strings" 19 "testing" 20 21 "android/soong/android" 22) 23 24func TestRustTest(t *testing.T) { 25 ctx := testRust(t, ` 26 rust_test_host { 27 name: "my_test", 28 srcs: ["foo.rs"], 29 data: ["data.txt"], 30 }`) 31 32 testingModule := ctx.ModuleForTests("my_test", "linux_glibc_x86_64") 33 expectedOut := "my_test/linux_glibc_x86_64/my_test" 34 outPath := testingModule.Output("my_test").Output.String() 35 if !strings.Contains(outPath, expectedOut) { 36 t.Errorf("wrong output path: %v; expected: %v", outPath, expectedOut) 37 } 38 39 dataPaths := testingModule.Module().(*Module).compiler.(*testDecorator).dataPaths() 40 if len(dataPaths) != 1 { 41 t.Errorf("expected exactly one test data file. test data files: [%v]", dataPaths) 42 return 43 } 44} 45 46func TestRustTestLinkage(t *testing.T) { 47 ctx := testRust(t, ` 48 rust_test { 49 name: "my_test", 50 srcs: ["foo.rs"], 51 rustlibs: ["libfoo"], 52 rlibs: ["libbar"], 53 } 54 rust_library { 55 name: "libfoo", 56 srcs: ["foo.rs"], 57 crate_name: "foo", 58 } 59 rust_library { 60 name: "libbar", 61 srcs: ["foo.rs"], 62 crate_name: "bar", 63 }`) 64 65 testingModule := ctx.ModuleForTests("my_test", "android_arm64_armv8-a").Module().(*Module) 66 67 if !android.InList("libfoo.rlib-std", testingModule.Properties.AndroidMkRlibs) { 68 t.Errorf("rlib-std variant for libfoo not detected as a rustlib-defined rlib dependency for device rust_test module") 69 } 70 if !android.InList("libbar.rlib-std", testingModule.Properties.AndroidMkRlibs) { 71 t.Errorf("rlib-std variant for libbar not detected as an rlib dependency for device rust_test module") 72 } 73 if !android.InList("libstd", testingModule.Properties.AndroidMkRlibs) { 74 t.Errorf("Device rust_test module 'my_test' does not link libstd as an rlib") 75 } 76} 77 78func TestDataLibs(t *testing.T) { 79 bp := ` 80 cc_library { 81 name: "test_lib", 82 srcs: ["test_lib.cpp"], 83 } 84 85 rust_binary { 86 name: "rusty", 87 srcs: ["foo.rs"], 88 compile_multilib: "both", 89 } 90 91 rust_ffi { 92 name: "librust_test_lib", 93 crate_name: "rust_test_lib", 94 srcs: ["test_lib.rs"], 95 relative_install_path: "foo/bar/baz", 96 compile_multilib: "64", 97 } 98 99 rust_test { 100 name: "main_test", 101 srcs: ["foo.rs"], 102 data_libs: ["test_lib"], 103 data_bins: ["rusty"], 104 } 105 ` 106 107 ctx := testRust(t, bp) 108 109 module := ctx.ModuleForTests("main_test", "android_arm64_armv8-a").Module() 110 testBinary := module.(*Module).compiler.(*testDecorator) 111 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("") 112 if err != nil { 113 t.Fatalf("Expected rust_test to produce output files, error: %s", err) 114 } 115 if len(outputFiles) != 1 { 116 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles) 117 } 118 if len(testBinary.dataPaths()) != 2 { 119 t.Fatalf("expected exactly two test data files. test data files: [%v]", testBinary.dataPaths()) 120 } 121 122 outputPath := outputFiles[0].String() 123 dataLibraryPath := testBinary.dataPaths()[0].SrcPath.String() 124 dataBinaryPath := testBinary.dataPaths()[1].SrcPath.String() 125 126 if !strings.HasSuffix(outputPath, "/main_test") { 127 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath) 128 } 129 if !strings.HasSuffix(dataLibraryPath, "/test_lib.so") { 130 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataLibraryPath) 131 } 132 if !strings.HasSuffix(dataBinaryPath, "/rusty") { 133 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataBinaryPath) 134 } 135} 136 137func TestDataLibsRelativeInstallPath(t *testing.T) { 138 bp := ` 139 cc_library { 140 name: "test_lib", 141 srcs: ["test_lib.cpp"], 142 relative_install_path: "foo/bar/baz", 143 compile_multilib: "64", 144 } 145 146 rust_ffi { 147 name: "librust_test_lib", 148 crate_name: "rust_test_lib", 149 srcs: ["test_lib.rs"], 150 relative_install_path: "foo/bar/baz", 151 compile_multilib: "64", 152 } 153 154 rust_binary { 155 name: "rusty", 156 srcs: ["foo.rs"], 157 relative_install_path: "foo/bar/baz", 158 compile_multilib: "64", 159 } 160 161 rust_test { 162 name: "main_test", 163 srcs: ["foo.rs"], 164 data_libs: ["test_lib", "librust_test_lib"], 165 data_bins: ["rusty"], 166 compile_multilib: "64", 167 } 168 ` 169 170 ctx := testRust(t, bp) 171 module := ctx.ModuleForTests("main_test", "android_arm64_armv8-a").Module() 172 testBinary := module.(*Module).compiler.(*testDecorator) 173 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("") 174 if err != nil { 175 t.Fatalf("Expected rust_test to produce output files, error: %s", err) 176 } 177 if len(outputFiles) != 1 { 178 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles) 179 } 180 if len(testBinary.dataPaths()) != 3 { 181 t.Fatalf("expected exactly two test data files. test data files: [%v]", testBinary.dataPaths()) 182 } 183 184 outputPath := outputFiles[0].String() 185 186 if !strings.HasSuffix(outputPath, "/main_test") { 187 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath) 188 } 189 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0] 190 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:lib64/foo/bar/baz") { 191 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:lib64/foo/bar/baz`,"+ 192 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0]) 193 } 194 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":librust_test_lib.so:lib64/foo/bar/baz") { 195 t.Errorf("expected LOCAL_TEST_DATA to end with `:librust_test_lib.so:lib64/foo/bar/baz`,"+ 196 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1]) 197 } 198 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][2], ":rusty:foo/bar/baz") { 199 t.Errorf("expected LOCAL_TEST_DATA to end with `:rusty:foo/bar/baz`,"+ 200 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][2]) 201 } 202} 203