Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame^] | 1 | // Copyright (C) 2020 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 | |
| 15 | package linkerconfig |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "reflect" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | var buildDir string |
| 26 | |
| 27 | func setUp() { |
| 28 | var err error |
| 29 | buildDir, err = ioutil.TempDir("", "soong_etc_test") |
| 30 | if err != nil { |
| 31 | panic(err) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func tearDown() { |
| 36 | os.RemoveAll(buildDir) |
| 37 | } |
| 38 | |
| 39 | func TestMain(m *testing.M) { |
| 40 | run := func() int { |
| 41 | setUp() |
| 42 | defer tearDown() |
| 43 | |
| 44 | return m.Run() |
| 45 | } |
| 46 | |
| 47 | os.Exit(run()) |
| 48 | } |
| 49 | |
| 50 | func testContext(t *testing.T, bp string) (*android.TestContext, android.Config) { |
| 51 | t.Helper() |
| 52 | |
| 53 | fs := map[string][]byte{ |
| 54 | "linker.config.json": nil, |
| 55 | } |
| 56 | |
| 57 | config := android.TestArchConfig(buildDir, nil, bp, fs) |
| 58 | |
| 59 | ctx := android.NewTestArchContext() |
| 60 | ctx.RegisterModuleType("linker_config", linkerConfigFactory) |
| 61 | ctx.Register(config) |
| 62 | |
| 63 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 64 | android.FailIfErrored(t, errs) |
| 65 | _, errs = ctx.PrepareBuildActions(config) |
| 66 | android.FailIfErrored(t, errs) |
| 67 | |
| 68 | return ctx, config |
| 69 | } |
| 70 | |
| 71 | func TestBaseLinkerConfig(t *testing.T) { |
| 72 | ctx, config := testContext(t, ` |
| 73 | linker_config { |
| 74 | name: "linker-config-base", |
| 75 | src: "linker.config.json", |
| 76 | } |
| 77 | `) |
| 78 | |
| 79 | expected := map[string][]string{ |
| 80 | "LOCAL_MODULE": {"linker-config-base"}, |
| 81 | "LOCAL_MODULE_CLASS": {"ETC"}, |
| 82 | "LOCAL_INSTALLED_MODULE_STEM": {"linker.config.pb"}, |
| 83 | } |
| 84 | |
| 85 | p := ctx.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig) |
| 86 | |
| 87 | if p.outputFilePath.Base() != "linker.config.pb" { |
| 88 | t.Errorf("expected linker.config.pb, got %q", p.outputFilePath.Base()) |
| 89 | } |
| 90 | |
| 91 | entries := android.AndroidMkEntriesForTest(t, config, "", p)[0] |
| 92 | for k, expectedValue := range expected { |
| 93 | if value, ok := entries.EntryMap[k]; ok { |
| 94 | if !reflect.DeepEqual(value, expectedValue) { |
| 95 | t.Errorf("Value of %s is '%s', but expected as '%s'", k, value, expectedValue) |
| 96 | } |
| 97 | } else { |
| 98 | t.Errorf("%s is not defined", k) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if value, ok := entries.EntryMap["LOCAL_UNINSTALLABLE_MODULE"]; ok { |
| 103 | t.Errorf("Value of LOCAL_UNINSTALLABLE_MODULE is %s, but expected as empty", value) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestUninstallableLinkerConfig(t *testing.T) { |
| 108 | ctx, config := testContext(t, ` |
| 109 | linker_config { |
| 110 | name: "linker-config-base", |
| 111 | src: "linker.config.json", |
| 112 | installable: false, |
| 113 | } |
| 114 | `) |
| 115 | |
| 116 | expected := []string{"true"} |
| 117 | |
| 118 | p := ctx.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig) |
| 119 | entries := android.AndroidMkEntriesForTest(t, config, "", p)[0] |
| 120 | if value, ok := entries.EntryMap["LOCAL_UNINSTALLABLE_MODULE"]; ok { |
| 121 | if !reflect.DeepEqual(value, expected) { |
| 122 | t.Errorf("LOCAL_UNINSTALLABLE_MODULE is expected to be true but %s", value) |
| 123 | } |
| 124 | } else { |
| 125 | t.Errorf("LOCAL_UNINSTALLABLE_MODULE is not defined") |
| 126 | } |
| 127 | } |