Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 android |
| 16 | |
| 17 | import ( |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 18 | "bufio" |
| 19 | "bytes" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 20 | "io/ioutil" |
| 21 | "os" |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 22 | "path/filepath" |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 23 | "strings" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 24 | "testing" |
| 25 | ) |
| 26 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 27 | func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) { |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 28 | config, buildDir := setUp(t) |
| 29 | defer tearDown(buildDir) |
| 30 | ctx := NewTestArchContext() |
| 31 | ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory)) |
Jaewoong Jung | 4b44fcd | 2019-02-07 08:28:03 -0800 | [diff] [blame] | 32 | ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory)) |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 33 | ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory)) |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 34 | ctx.RegisterModuleType("prebuilt_usr_share_host", ModuleFactoryAdaptor(PrebuiltUserShareHostFactory)) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 35 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 36 | ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel() |
| 37 | }) |
| 38 | ctx.Register() |
| 39 | mockFiles := map[string][]byte{ |
| 40 | "Android.bp": []byte(bp), |
| 41 | "foo.conf": nil, |
| 42 | "bar.conf": nil, |
| 43 | "baz.conf": nil, |
| 44 | } |
| 45 | ctx.MockFileSystem(mockFiles) |
| 46 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 47 | FailIfErrored(t, errs) |
| 48 | _, errs = ctx.PrepareBuildActions(config) |
| 49 | FailIfErrored(t, errs) |
| 50 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 51 | return ctx, config |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func setUp(t *testing.T) (config Config, buildDir string) { |
| 55 | buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test") |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | config = TestArchConfig(buildDir, nil) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | func tearDown(buildDir string) { |
| 65 | os.RemoveAll(buildDir) |
| 66 | } |
| 67 | |
| 68 | func TestPrebuiltEtcVariants(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 69 | ctx, _ := testPrebuiltEtc(t, ` |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 70 | prebuilt_etc { |
| 71 | name: "foo.conf", |
| 72 | src: "foo.conf", |
| 73 | } |
| 74 | prebuilt_etc { |
| 75 | name: "bar.conf", |
| 76 | src: "bar.conf", |
| 77 | recovery_available: true, |
| 78 | } |
| 79 | prebuilt_etc { |
| 80 | name: "baz.conf", |
| 81 | src: "baz.conf", |
| 82 | recovery: true, |
| 83 | } |
| 84 | `) |
| 85 | |
| 86 | foo_variants := ctx.ModuleVariantsForTests("foo.conf") |
| 87 | if len(foo_variants) != 1 { |
| 88 | t.Errorf("expected 1, got %#v", foo_variants) |
| 89 | } |
| 90 | |
| 91 | bar_variants := ctx.ModuleVariantsForTests("bar.conf") |
| 92 | if len(bar_variants) != 2 { |
| 93 | t.Errorf("expected 2, got %#v", bar_variants) |
| 94 | } |
| 95 | |
| 96 | baz_variants := ctx.ModuleVariantsForTests("baz.conf") |
| 97 | if len(baz_variants) != 1 { |
| 98 | t.Errorf("expected 1, got %#v", bar_variants) |
| 99 | } |
| 100 | } |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 101 | |
| 102 | func TestPrebuiltEtcOutputPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 103 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 104 | prebuilt_etc { |
| 105 | name: "foo.conf", |
| 106 | src: "foo.conf", |
| 107 | filename: "foo.installed.conf", |
| 108 | } |
| 109 | `) |
| 110 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 111 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 112 | if p.outputFilePath.Base() != "foo.installed.conf" { |
| 113 | t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base()) |
| 114 | } |
| 115 | } |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 116 | |
| 117 | func TestPrebuiltEtcGlob(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 118 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 119 | prebuilt_etc { |
| 120 | name: "my_foo", |
| 121 | src: "foo.*", |
| 122 | } |
| 123 | prebuilt_etc { |
| 124 | name: "my_bar", |
| 125 | src: "bar.*", |
| 126 | filename_from_src: true, |
| 127 | } |
| 128 | `) |
| 129 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 130 | p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 131 | if p.outputFilePath.Base() != "my_foo" { |
| 132 | t.Errorf("expected my_foo, got %q", p.outputFilePath.Base()) |
| 133 | } |
| 134 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 135 | p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 136 | if p.outputFilePath.Base() != "bar.conf" { |
| 137 | t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base()) |
| 138 | } |
| 139 | } |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 140 | |
| 141 | func TestPrebuiltEtcAndroidMk(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 142 | ctx, _ := testPrebuiltEtc(t, ` |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 143 | prebuilt_etc { |
| 144 | name: "foo", |
| 145 | src: "foo.conf", |
| 146 | owner: "abc", |
| 147 | filename_from_src: true, |
| 148 | } |
| 149 | `) |
| 150 | |
| 151 | data := AndroidMkData{} |
| 152 | data.Required = append(data.Required, "modA", "moduleB") |
| 153 | |
| 154 | expected := map[string]string{ |
| 155 | "LOCAL_MODULE": "foo", |
| 156 | "LOCAL_MODULE_CLASS": "ETC", |
| 157 | "LOCAL_MODULE_OWNER": "abc", |
| 158 | "LOCAL_INSTALLED_MODULE_STEM": "foo.conf", |
| 159 | "LOCAL_REQUIRED_MODULES": "modA moduleB", |
| 160 | } |
| 161 | |
| 162 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
| 163 | buf := &bytes.Buffer{} |
| 164 | mod.AndroidMk().Custom(buf, "foo", "", "", data) |
| 165 | for k, expected := range expected { |
| 166 | found := false |
| 167 | scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes())) |
| 168 | for scanner.Scan() { |
| 169 | line := scanner.Text() |
| 170 | tok := strings.Split(line, " := ") |
| 171 | if tok[0] == k { |
| 172 | found = true |
| 173 | if tok[1] != expected { |
| 174 | t.Errorf("Incorrect %s '%s', expected '%s'", k, tok[1], expected) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if !found { |
| 180 | t.Errorf("No %s defined, saw %s", k, buf.String()) |
| 181 | } |
| 182 | } |
| 183 | } |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 184 | |
| 185 | func TestPrebuiltEtcHost(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 186 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 187 | prebuilt_etc_host { |
| 188 | name: "foo.conf", |
| 189 | src: "foo.conf", |
| 190 | } |
| 191 | `) |
| 192 | |
| 193 | buildOS := BuildOs.String() |
| 194 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
| 195 | if !p.Host() { |
| 196 | t.Errorf("host bit is not set for a prebuilt_etc_host module.") |
| 197 | } |
| 198 | } |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 199 | |
| 200 | func TestPrebuiltUserShareInstallDirPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 201 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 202 | prebuilt_usr_share { |
| 203 | name: "foo.conf", |
| 204 | src: "foo.conf", |
| 205 | sub_dir: "bar", |
| 206 | } |
| 207 | `) |
| 208 | |
| 209 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
| 210 | expected := "target/product/test_device/system/usr/share/bar" |
| 211 | if p.installDirPath.RelPathString() != expected { |
| 212 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) |
| 213 | } |
| 214 | } |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame^] | 215 | |
| 216 | func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) { |
| 217 | ctx, config := testPrebuiltEtc(t, ` |
| 218 | prebuilt_usr_share_host { |
| 219 | name: "foo.conf", |
| 220 | src: "foo.conf", |
| 221 | sub_dir: "bar", |
| 222 | } |
| 223 | `) |
| 224 | |
| 225 | buildOS := BuildOs.String() |
| 226 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
| 227 | expected := filepath.Join("host", config.PrebuiltOS(), "usr", "share", "bar") |
| 228 | if p.installDirPath.RelPathString() != expected { |
| 229 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) |
| 230 | } |
| 231 | } |