xref: /aosp_15_r20/system/sepolicy/build/soong/selinux_test.go (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1*e4a36f41SAndroid Build Coastguard Worker// Copyright 2024 The Android Open Source Project
2*e4a36f41SAndroid Build Coastguard Worker//
3*e4a36f41SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*e4a36f41SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*e4a36f41SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*e4a36f41SAndroid Build Coastguard Worker//
7*e4a36f41SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*e4a36f41SAndroid Build Coastguard Worker//
9*e4a36f41SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*e4a36f41SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*e4a36f41SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e4a36f41SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*e4a36f41SAndroid Build Coastguard Worker// limitations under the License.
14*e4a36f41SAndroid Build Coastguard Worker
15*e4a36f41SAndroid Build Coastguard Workerpackage selinux
16*e4a36f41SAndroid Build Coastguard Worker
17*e4a36f41SAndroid Build Coastguard Workerimport (
18*e4a36f41SAndroid Build Coastguard Worker	"os"
19*e4a36f41SAndroid Build Coastguard Worker	"reflect"
20*e4a36f41SAndroid Build Coastguard Worker	"testing"
21*e4a36f41SAndroid Build Coastguard Worker
22*e4a36f41SAndroid Build Coastguard Worker	"android/soong/android"
23*e4a36f41SAndroid Build Coastguard Worker)
24*e4a36f41SAndroid Build Coastguard Worker
25*e4a36f41SAndroid Build Coastguard Workerfunc TestMain(m *testing.M) {
26*e4a36f41SAndroid Build Coastguard Worker	os.Exit(m.Run())
27*e4a36f41SAndroid Build Coastguard Worker}
28*e4a36f41SAndroid Build Coastguard Worker
29*e4a36f41SAndroid Build Coastguard Workervar prepareForTest = android.GroupFixturePreparers(
30*e4a36f41SAndroid Build Coastguard Worker	android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
31*e4a36f41SAndroid Build Coastguard Worker		buildFlags := make(map[string]string)
32*e4a36f41SAndroid Build Coastguard Worker		buildFlags["RELEASE_FLAGS_BAR"] = "true"
33*e4a36f41SAndroid Build Coastguard Worker		buildFlags["RELEASE_FLAGS_FOO1"] = "false"
34*e4a36f41SAndroid Build Coastguard Worker		// "RELEASE_FLAGS_FOO2" is missing
35*e4a36f41SAndroid Build Coastguard Worker		buildFlags["RELEASE_AVF_ENABLE_DEVICE_ASSIGNMENT"] = "true"
36*e4a36f41SAndroid Build Coastguard Worker		variables.BuildFlags = buildFlags
37*e4a36f41SAndroid Build Coastguard Worker	}),
38*e4a36f41SAndroid Build Coastguard Worker	android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
39*e4a36f41SAndroid Build Coastguard Worker		ctx.RegisterModuleType("se_flags", flagsFactory)
40*e4a36f41SAndroid Build Coastguard Worker		ctx.RegisterModuleType("se_flags_collector", flagsCollectorFactory)
41*e4a36f41SAndroid Build Coastguard Worker	}),
42*e4a36f41SAndroid Build Coastguard Worker)
43*e4a36f41SAndroid Build Coastguard Worker
44*e4a36f41SAndroid Build Coastguard Workerfunc TestFlagCollector(t *testing.T) {
45*e4a36f41SAndroid Build Coastguard Worker	t.Parallel()
46*e4a36f41SAndroid Build Coastguard Worker
47*e4a36f41SAndroid Build Coastguard Worker	ctx := android.GroupFixturePreparers(
48*e4a36f41SAndroid Build Coastguard Worker		prepareForTest,
49*e4a36f41SAndroid Build Coastguard Worker		android.FixtureAddTextFile("package_bar/Android.bp", `
50*e4a36f41SAndroid Build Coastguard Worker			se_flags {
51*e4a36f41SAndroid Build Coastguard Worker				name: "se_flags_bar",
52*e4a36f41SAndroid Build Coastguard Worker				flags: ["RELEASE_FLAGS_BAR"],
53*e4a36f41SAndroid Build Coastguard Worker				export_to: ["se_flags_collector"],
54*e4a36f41SAndroid Build Coastguard Worker			}
55*e4a36f41SAndroid Build Coastguard Worker			`),
56*e4a36f41SAndroid Build Coastguard Worker		android.FixtureAddTextFile("package_foo/Android.bp", `
57*e4a36f41SAndroid Build Coastguard Worker			se_flags {
58*e4a36f41SAndroid Build Coastguard Worker				name: "se_flags_foo",
59*e4a36f41SAndroid Build Coastguard Worker				flags: ["RELEASE_FLAGS_FOO1", "RELEASE_FLAGS_FOO2"],
60*e4a36f41SAndroid Build Coastguard Worker				export_to: ["se_flags_collector"],
61*e4a36f41SAndroid Build Coastguard Worker			}
62*e4a36f41SAndroid Build Coastguard Worker			`),
63*e4a36f41SAndroid Build Coastguard Worker		android.FixtureAddTextFile("system/sepolicy/Android.bp", `
64*e4a36f41SAndroid Build Coastguard Worker			se_flags {
65*e4a36f41SAndroid Build Coastguard Worker				name: "se_flags",
66*e4a36f41SAndroid Build Coastguard Worker				flags: ["RELEASE_AVF_ENABLE_DEVICE_ASSIGNMENT"],
67*e4a36f41SAndroid Build Coastguard Worker				export_to: ["se_flags_collector"],
68*e4a36f41SAndroid Build Coastguard Worker			}
69*e4a36f41SAndroid Build Coastguard Worker			se_flags_collector {
70*e4a36f41SAndroid Build Coastguard Worker				name: "se_flags_collector",
71*e4a36f41SAndroid Build Coastguard Worker			}
72*e4a36f41SAndroid Build Coastguard Worker			`),
73*e4a36f41SAndroid Build Coastguard Worker	).RunTest(t).TestContext
74*e4a36f41SAndroid Build Coastguard Worker
75*e4a36f41SAndroid Build Coastguard Worker	collectorModule := ctx.ModuleForTests("se_flags_collector", "").Module()
76*e4a36f41SAndroid Build Coastguard Worker	collectorData, ok := android.OtherModuleProvider(ctx.OtherModuleProviderAdaptor(), collectorModule, buildFlagsProviderKey)
77*e4a36f41SAndroid Build Coastguard Worker	if !ok {
78*e4a36f41SAndroid Build Coastguard Worker		t.Errorf("se_flags_collector must provide buildFlags")
79*e4a36f41SAndroid Build Coastguard Worker		return
80*e4a36f41SAndroid Build Coastguard Worker	}
81*e4a36f41SAndroid Build Coastguard Worker
82*e4a36f41SAndroid Build Coastguard Worker	actual := flagsToM4Macros(collectorData.BuildFlags)
83*e4a36f41SAndroid Build Coastguard Worker	expected := []string{
84*e4a36f41SAndroid Build Coastguard Worker		"-D target_flag_RELEASE_AVF_ENABLE_DEVICE_ASSIGNMENT=true",
85*e4a36f41SAndroid Build Coastguard Worker		"-D target_flag_RELEASE_FLAGS_BAR=true",
86*e4a36f41SAndroid Build Coastguard Worker		"-D target_flag_RELEASE_FLAGS_FOO1=false",
87*e4a36f41SAndroid Build Coastguard Worker	}
88*e4a36f41SAndroid Build Coastguard Worker	if !reflect.DeepEqual(actual, expected) {
89*e4a36f41SAndroid Build Coastguard Worker		t.Errorf("M4 macros were not exported correctly"+
90*e4a36f41SAndroid Build Coastguard Worker			"\nactual:   %v"+
91*e4a36f41SAndroid Build Coastguard Worker			"\nexpected: %v",
92*e4a36f41SAndroid Build Coastguard Worker			actual,
93*e4a36f41SAndroid Build Coastguard Worker			expected,
94*e4a36f41SAndroid Build Coastguard Worker		)
95*e4a36f41SAndroid Build Coastguard Worker	}
96*e4a36f41SAndroid Build Coastguard Worker}
97