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 testingModule := ctx.ModuleForTests("main_test", "android_arm64_armv8-a") 110 testBinary := testingModule.Module().(*Module).compiler.(*testDecorator) 111 outputFiles := testingModule.OutputFiles(ctx, t, "") 112 if len(outputFiles) != 1 { 113 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles) 114 } 115 if len(testBinary.dataPaths()) != 2 { 116 t.Fatalf("expected exactly two test data files. test data files: [%v]", testBinary.dataPaths()) 117 } 118 119 outputPath := outputFiles[0].String() 120 dataLibraryPath := testBinary.dataPaths()[0].SrcPath.String() 121 dataBinaryPath := testBinary.dataPaths()[1].SrcPath.String() 122 123 if !strings.HasSuffix(outputPath, "/main_test") { 124 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath) 125 } 126 if !strings.HasSuffix(dataLibraryPath, "/test_lib.so") { 127 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataLibraryPath) 128 } 129 if !strings.HasSuffix(dataBinaryPath, "/rusty") { 130 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataBinaryPath) 131 } 132} 133 134func TestDataLibsRelativeInstallPath(t *testing.T) { 135 bp := ` 136 cc_library { 137 name: "test_lib", 138 srcs: ["test_lib.cpp"], 139 relative_install_path: "foo/bar/baz", 140 compile_multilib: "64", 141 } 142 143 rust_ffi { 144 name: "librust_test_lib", 145 crate_name: "rust_test_lib", 146 srcs: ["test_lib.rs"], 147 relative_install_path: "foo/bar/baz", 148 compile_multilib: "64", 149 } 150 151 rust_binary { 152 name: "rusty", 153 srcs: ["foo.rs"], 154 relative_install_path: "foo/bar/baz", 155 compile_multilib: "64", 156 } 157 158 rust_test { 159 name: "main_test", 160 srcs: ["foo.rs"], 161 data_libs: ["test_lib", "librust_test_lib"], 162 data_bins: ["rusty"], 163 compile_multilib: "64", 164 } 165 ` 166 167 ctx := testRust(t, bp) 168 testingModule := ctx.ModuleForTests("main_test", "android_arm64_armv8-a") 169 module := testingModule.Module() 170 testBinary := module.(*Module).compiler.(*testDecorator) 171 outputFiles := testingModule.OutputFiles(ctx, t, "") 172 if len(outputFiles) != 1 { 173 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles) 174 } 175 if len(testBinary.dataPaths()) != 3 { 176 t.Fatalf("expected exactly two test data files. test data files: [%v]", testBinary.dataPaths()) 177 } 178 179 outputPath := outputFiles[0].String() 180 181 if !strings.HasSuffix(outputPath, "/main_test") { 182 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath) 183 } 184 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0] 185 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:lib64/foo/bar/baz") { 186 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:lib64/foo/bar/baz`,"+ 187 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0]) 188 } 189 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":librust_test_lib.so:lib64/foo/bar/baz") { 190 t.Errorf("expected LOCAL_TEST_DATA to end with `:librust_test_lib.so:lib64/foo/bar/baz`,"+ 191 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1]) 192 } 193 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][2], ":rusty:foo/bar/baz") { 194 t.Errorf("expected LOCAL_TEST_DATA to end with `:rusty:foo/bar/baz`,"+ 195 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][2]) 196 } 197} 198