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
15package linkerconfig
16
17import (
18	"os"
19	"reflect"
20	"testing"
21
22	"android/soong/android"
23)
24
25func TestMain(m *testing.M) {
26	os.Exit(m.Run())
27}
28
29var prepareForLinkerConfigTest = android.GroupFixturePreparers(
30	android.PrepareForTestWithAndroidBuildComponents,
31	android.FixtureRegisterWithContext(registerLinkerConfigBuildComponent),
32	android.FixtureAddFile("linker.config.json", nil),
33)
34
35func TestBaseLinkerConfig(t *testing.T) {
36	result := prepareForLinkerConfigTest.RunTestWithBp(t, `
37		linker_config {
38			name: "linker-config-base",
39			src: "linker.config.json",
40		}
41	`)
42
43	expected := map[string][]string{
44		"LOCAL_MODULE":                {"linker-config-base"},
45		"LOCAL_MODULE_CLASS":          {"ETC"},
46		"LOCAL_INSTALLED_MODULE_STEM": {"linker.config.pb"},
47	}
48
49	p := result.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig)
50
51	if p.outputFilePath.Base() != "linker.config.pb" {
52		t.Errorf("expected linker.config.pb, got %q", p.outputFilePath.Base())
53	}
54
55	entries := android.AndroidMkEntriesForTest(t, result.TestContext, p)[0]
56	for k, expectedValue := range expected {
57		if value, ok := entries.EntryMap[k]; ok {
58			if !reflect.DeepEqual(value, expectedValue) {
59				t.Errorf("Value of %s is '%s', but expected as '%s'", k, value, expectedValue)
60			}
61		} else {
62			t.Errorf("%s is not defined", k)
63		}
64	}
65
66	if value, ok := entries.EntryMap["LOCAL_UNINSTALLABLE_MODULE"]; ok {
67		t.Errorf("Value of LOCAL_UNINSTALLABLE_MODULE is %s, but expected as empty", value)
68	}
69}
70
71func TestUninstallableLinkerConfig(t *testing.T) {
72	result := prepareForLinkerConfigTest.RunTestWithBp(t, `
73		linker_config {
74			name: "linker-config-base",
75			src: "linker.config.json",
76			installable: false,
77		}
78	`)
79
80	expected := []string{"true"}
81
82	p := result.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig)
83	entries := android.AndroidMkEntriesForTest(t, result.TestContext, p)[0]
84	if value, ok := entries.EntryMap["LOCAL_UNINSTALLABLE_MODULE"]; ok {
85		if !reflect.DeepEqual(value, expected) {
86			t.Errorf("LOCAL_UNINSTALLABLE_MODULE is expected to be true but %s", value)
87		}
88	} else {
89		t.Errorf("LOCAL_UNINSTALLABLE_MODULE is not defined")
90	}
91}
92