1// Copyright 2024 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 selinux 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 prepareForTest = android.GroupFixturePreparers( 30 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { 31 buildFlags := make(map[string]string) 32 buildFlags["RELEASE_FLAGS_BAR"] = "true" 33 buildFlags["RELEASE_FLAGS_FOO1"] = "false" 34 // "RELEASE_FLAGS_FOO2" is missing 35 buildFlags["RELEASE_AVF_ENABLE_DEVICE_ASSIGNMENT"] = "true" 36 variables.BuildFlags = buildFlags 37 }), 38 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { 39 ctx.RegisterModuleType("se_flags", flagsFactory) 40 ctx.RegisterModuleType("se_flags_collector", flagsCollectorFactory) 41 }), 42) 43 44func TestFlagCollector(t *testing.T) { 45 t.Parallel() 46 47 ctx := android.GroupFixturePreparers( 48 prepareForTest, 49 android.FixtureAddTextFile("package_bar/Android.bp", ` 50 se_flags { 51 name: "se_flags_bar", 52 flags: ["RELEASE_FLAGS_BAR"], 53 export_to: ["se_flags_collector"], 54 } 55 `), 56 android.FixtureAddTextFile("package_foo/Android.bp", ` 57 se_flags { 58 name: "se_flags_foo", 59 flags: ["RELEASE_FLAGS_FOO1", "RELEASE_FLAGS_FOO2"], 60 export_to: ["se_flags_collector"], 61 } 62 `), 63 android.FixtureAddTextFile("system/sepolicy/Android.bp", ` 64 se_flags { 65 name: "se_flags", 66 flags: ["RELEASE_AVF_ENABLE_DEVICE_ASSIGNMENT"], 67 export_to: ["se_flags_collector"], 68 } 69 se_flags_collector { 70 name: "se_flags_collector", 71 } 72 `), 73 ).RunTest(t).TestContext 74 75 collectorModule := ctx.ModuleForTests("se_flags_collector", "").Module() 76 collectorData, ok := android.OtherModuleProvider(ctx.OtherModuleProviderAdaptor(), collectorModule, buildFlagsProviderKey) 77 if !ok { 78 t.Errorf("se_flags_collector must provide buildFlags") 79 return 80 } 81 82 actual := flagsToM4Macros(collectorData.BuildFlags) 83 expected := []string{ 84 "-D target_flag_RELEASE_AVF_ENABLE_DEVICE_ASSIGNMENT=true", 85 "-D target_flag_RELEASE_FLAGS_BAR=true", 86 "-D target_flag_RELEASE_FLAGS_FOO1=false", 87 } 88 if !reflect.DeepEqual(actual, expected) { 89 t.Errorf("M4 macros were not exported correctly"+ 90 "\nactual: %v"+ 91 "\nexpected: %v", 92 actual, 93 expected, 94 ) 95 } 96} 97