Revert^2 "Migrate contexts tests to Android.bp"

This reverts commit baa93cc651227cf92760545d5283b021b853a774.

Reason for revert: amlogic build fixed

Change-Id: I8b046dc810d47a2d87012f02a668873889fce705
diff --git a/build/soong/selinux_contexts.go b/build/soong/selinux_contexts.go
index c55fba2..7424001 100644
--- a/build/soong/selinux_contexts.go
+++ b/build/soong/selinux_contexts.go
@@ -93,6 +93,11 @@
 	android.RegisterModuleType("service_contexts", serviceFactory)
 	android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory)
 	android.RegisterModuleType("seapp_contexts", seappFactory)
+
+	android.RegisterModuleType("file_contexts_test", fileContextsTestFactory)
+	android.RegisterModuleType("property_contexts_test", propertyContextsTestFactory)
+	android.RegisterModuleType("hwservice_contexts_test", hwserviceContextsTestFactory)
+	android.RegisterModuleType("service_contexts_test", serviceContextsTestFactory)
 }
 
 func (m *selinuxContextsModule) InstallInRoot() bool {
@@ -499,3 +504,142 @@
 	}
 	return nil, fmt.Errorf("unsupported module reference tag %q", tag)
 }
+
+type contextsTestProperties struct {
+	// Contexts files to be tested.
+	Srcs []string `android:"path"`
+
+	// Precompiled sepolicy binary to be tesed together.
+	Sepolicy *string `android:"path"`
+}
+
+type contextsTestModule struct {
+	android.ModuleBase
+
+	// Name of the test tool. "checkfc" or "property_info_checker"
+	tool string
+
+	// Additional flags to be passed to the tool.
+	flags []string
+
+	properties    contextsTestProperties
+	testTimestamp android.ModuleOutPath
+}
+
+// checkfc parses a context file and checks for syntax errors.
+// If -s is specified, the service backend is used to verify binder services.
+// If -l is specified, the service backend is used to verify hwbinder services.
+// Otherwise, context_file is assumed to be a file_contexts file
+// If -e is specified, then the context_file is allowed to be empty.
+
+// file_contexts_test tests given file_contexts files with checkfc.
+func fileContextsTestFactory() android.Module {
+	m := &contextsTestModule{tool: "checkfc" /* no flags: file_contexts file check */}
+	m.AddProperties(&m.properties)
+	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
+	return m
+}
+
+// property_contexts_test tests given property_contexts files with property_info_checker.
+func propertyContextsTestFactory() android.Module {
+	m := &contextsTestModule{tool: "property_info_checker"}
+	m.AddProperties(&m.properties)
+	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
+	return m
+}
+
+// hwservice_contexts_test tests given hwservice_contexts files with checkfc.
+func hwserviceContextsTestFactory() android.Module {
+	m := &contextsTestModule{tool: "checkfc", flags: []string{"-e" /* allow empty */, "-l" /* hwbinder services */}}
+	m.AddProperties(&m.properties)
+	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
+	return m
+}
+
+// service_contexts_test tests given service_contexts files with checkfc.
+func serviceContextsTestFactory() android.Module {
+	// checkfc -s: service_contexts test
+	m := &contextsTestModule{tool: "checkfc", flags: []string{"-s" /* binder services */}}
+	m.AddProperties(&m.properties)
+	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
+	return m
+}
+
+func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	tool := m.tool
+	if tool != "checkfc" && tool != "property_info_checker" {
+		panic(fmt.Errorf("%q: unknown tool name: %q", ctx.ModuleName(), tool))
+	}
+
+	if len(m.properties.Srcs) == 0 {
+		ctx.PropertyErrorf("srcs", "can't be empty")
+		return
+	}
+
+	if proptools.String(m.properties.Sepolicy) == "" {
+		ctx.PropertyErrorf("sepolicy", "can't be empty")
+		return
+	}
+
+	srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
+	sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy))
+
+	rule := android.NewRuleBuilder(pctx, ctx)
+	rule.Command().BuiltTool(tool).
+		Flags(m.flags).
+		Input(sepolicy).
+		Inputs(srcs)
+
+	m.testTimestamp = android.PathForModuleOut(ctx, "timestamp")
+	rule.Command().Text("touch").Output(m.testTimestamp)
+	rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName())
+}
+
+func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries {
+	return []android.AndroidMkEntries{android.AndroidMkEntries{
+		Class: "FAKE",
+		// OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it.
+		// Without OutputFile this module won't be exported to Makefile.
+		OutputFile: android.OptionalPathForPath(m.testTimestamp),
+		Include:    "$(BUILD_PHONY_PACKAGE)",
+		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
+			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
+				entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String())
+			},
+		},
+	}}
+}
+
+// contextsTestModule implements ImageInterface to be able to include recovery_available contexts
+// modules as its sources.
+func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
+}
+
+func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
+	return true
+}
+
+func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
+}
+
+func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
+}
+
+func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
+}
+
+func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
+}
+
+func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
+	return nil
+}
+
+func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
+}
+
+var _ android.ImageInterface = (*contextsTestModule)(nil)