| 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") | 
| Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 153 | data.Host_required = append(data.Host_required, "hostModA", "hostModB") | 
|  | 154 | data.Target_required = append(data.Target_required, "targetModA") | 
| Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 155 |  | 
|  | 156 | expected := map[string]string{ | 
| Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 157 | "LOCAL_MODULE":                  "foo", | 
|  | 158 | "LOCAL_MODULE_CLASS":            "ETC", | 
|  | 159 | "LOCAL_MODULE_OWNER":            "abc", | 
|  | 160 | "LOCAL_INSTALLED_MODULE_STEM":   "foo.conf", | 
|  | 161 | "LOCAL_REQUIRED_MODULES":        "modA moduleB", | 
|  | 162 | "LOCAL_HOST_REQUIRED_MODULES":   "hostModA hostModB", | 
|  | 163 | "LOCAL_TARGET_REQUIRED_MODULES": "targetModA", | 
| Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
|  | 166 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) | 
|  | 167 | buf := &bytes.Buffer{} | 
|  | 168 | mod.AndroidMk().Custom(buf, "foo", "", "", data) | 
|  | 169 | for k, expected := range expected { | 
|  | 170 | found := false | 
|  | 171 | scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes())) | 
|  | 172 | for scanner.Scan() { | 
|  | 173 | line := scanner.Text() | 
|  | 174 | tok := strings.Split(line, " := ") | 
|  | 175 | if tok[0] == k { | 
|  | 176 | found = true | 
|  | 177 | if tok[1] != expected { | 
|  | 178 | t.Errorf("Incorrect %s '%s', expected '%s'", k, tok[1], expected) | 
|  | 179 | } | 
|  | 180 | } | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | if !found { | 
|  | 184 | t.Errorf("No %s defined, saw %s", k, buf.String()) | 
|  | 185 | } | 
|  | 186 | } | 
|  | 187 | } | 
| Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 188 |  | 
|  | 189 | func TestPrebuiltEtcHost(t *testing.T) { | 
| Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 190 | ctx, _ := testPrebuiltEtc(t, ` | 
| Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 191 | prebuilt_etc_host { | 
|  | 192 | name: "foo.conf", | 
|  | 193 | src: "foo.conf", | 
|  | 194 | } | 
|  | 195 | `) | 
|  | 196 |  | 
|  | 197 | buildOS := BuildOs.String() | 
|  | 198 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) | 
|  | 199 | if !p.Host() { | 
|  | 200 | t.Errorf("host bit is not set for a prebuilt_etc_host module.") | 
|  | 201 | } | 
|  | 202 | } | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 203 |  | 
|  | 204 | func TestPrebuiltUserShareInstallDirPath(t *testing.T) { | 
| Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 205 | ctx, _ := testPrebuiltEtc(t, ` | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 206 | prebuilt_usr_share { | 
|  | 207 | name: "foo.conf", | 
|  | 208 | src: "foo.conf", | 
|  | 209 | sub_dir: "bar", | 
|  | 210 | } | 
|  | 211 | `) | 
|  | 212 |  | 
|  | 213 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) | 
|  | 214 | expected := "target/product/test_device/system/usr/share/bar" | 
|  | 215 | if p.installDirPath.RelPathString() != expected { | 
|  | 216 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) | 
|  | 217 | } | 
|  | 218 | } | 
| Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 219 |  | 
|  | 220 | func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) { | 
|  | 221 | ctx, config := testPrebuiltEtc(t, ` | 
|  | 222 | prebuilt_usr_share_host { | 
|  | 223 | name: "foo.conf", | 
|  | 224 | src: "foo.conf", | 
|  | 225 | sub_dir: "bar", | 
|  | 226 | } | 
|  | 227 | `) | 
|  | 228 |  | 
|  | 229 | buildOS := BuildOs.String() | 
|  | 230 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) | 
|  | 231 | expected := filepath.Join("host", config.PrebuiltOS(), "usr", "share", "bar") | 
|  | 232 | if p.installDirPath.RelPathString() != expected { | 
|  | 233 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) | 
|  | 234 | } | 
|  | 235 | } |