Home | History | Annotate | Download | only in bpf
      1 // Copyright 2018 Google Inc. All rights reserved.
      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 bpf
     16 
     17 import (
     18 	"io/ioutil"
     19 	"os"
     20 	"testing"
     21 
     22 	"android/soong/android"
     23 	cc2 "android/soong/cc"
     24 )
     25 
     26 var buildDir string
     27 
     28 func setUp() {
     29 	var err error
     30 	buildDir, err = ioutil.TempDir("", "genrule_test")
     31 	if err != nil {
     32 		panic(err)
     33 	}
     34 }
     35 
     36 func tearDown() {
     37 	os.RemoveAll(buildDir)
     38 }
     39 
     40 func TestMain(m *testing.M) {
     41 	run := func() int {
     42 		setUp()
     43 		defer tearDown()
     44 
     45 		return m.Run()
     46 	}
     47 
     48 	os.Exit(run())
     49 }
     50 
     51 func testContext(bp string) *android.TestContext {
     52 	ctx := android.NewTestArchContext()
     53 	ctx.RegisterModuleType("bpf", android.ModuleFactoryAdaptor(bpfFactory))
     54 	ctx.RegisterModuleType("cc_test", android.ModuleFactoryAdaptor(cc2.TestFactory))
     55 	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc2.LibraryFactory))
     56 	ctx.RegisterModuleType("cc_library_static", android.ModuleFactoryAdaptor(cc2.LibraryStaticFactory))
     57 	ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc2.ObjectFactory))
     58 	ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc2.ToolchainLibraryFactory))
     59 	ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
     60 		ctx.BottomUp("link", cc2.LinkageMutator).Parallel()
     61 	})
     62 	ctx.Register()
     63 
     64 	// Add some modules that are required by the compiler and/or linker
     65 	bp = bp + `
     66 		toolchain_library {
     67 			name: "libatomic",
     68 			vendor_available: true,
     69 			recovery_available: true,
     70 			src: "",
     71 		}
     72 
     73 		toolchain_library {
     74 			name: "libclang_rt.builtins-arm-android",
     75 			vendor_available: true,
     76 			recovery_available: true,
     77 			src: "",
     78 		}
     79 
     80 		toolchain_library {
     81 			name: "libclang_rt.builtins-aarch64-android",
     82 			vendor_available: true,
     83 			recovery_available: true,
     84 			src: "",
     85 		}
     86 
     87 		toolchain_library {
     88 			name: "libgcc",
     89 			vendor_available: true,
     90 			recovery_available: true,
     91 			src: "",
     92 		}
     93 
     94 		cc_library {
     95 			name: "libc",
     96 			no_libgcc: true,
     97 			nocrt: true,
     98 			system_shared_libs: [],
     99 			recovery_available: true,
    100 		}
    101 
    102 		cc_library {
    103 			name: "libm",
    104 			no_libgcc: true,
    105 			nocrt: true,
    106 			system_shared_libs: [],
    107 			recovery_available: true,
    108 		}
    109 
    110 		cc_library {
    111 			name: "libdl",
    112 			no_libgcc: true,
    113 			nocrt: true,
    114 			system_shared_libs: [],
    115 			recovery_available: true,
    116 		}
    117 
    118 		cc_library {
    119 			name: "libgtest",
    120 			host_supported: true,
    121 			vendor_available: true,
    122 		}
    123 
    124 		cc_library {
    125 			name: "libgtest_main",
    126 			host_supported: true,
    127 			vendor_available: true,
    128 		}
    129 
    130 		cc_object {
    131 			name: "crtbegin_dynamic",
    132 			recovery_available: true,
    133 			vendor_available: true,
    134 		}
    135 
    136 		cc_object {
    137 			name: "crtend_android",
    138 			recovery_available: true,
    139 			vendor_available: true,
    140 		}
    141 
    142 		cc_object {
    143 			name: "crtbegin_so",
    144 			recovery_available: true,
    145 			vendor_available: true,
    146 		}
    147 
    148 		cc_object {
    149 			name: "crtend_so",
    150 			recovery_available: true,
    151 			vendor_available: true,
    152 		}
    153 	`
    154 	mockFS := map[string][]byte{
    155 		"Android.bp":  []byte(bp),
    156 		"bpf.c":       nil,
    157 		"BpfTest.cpp": nil,
    158 	}
    159 
    160 	ctx.MockFileSystem(mockFS)
    161 
    162 	return ctx
    163 }
    164 
    165 func TestBpfDataDependency(t *testing.T) {
    166 	config := android.TestArchConfig(buildDir, nil)
    167 	bp := `
    168 		bpf {
    169 			name: "bpf.o",
    170 			srcs: ["bpf.c"],
    171 		}
    172 
    173 		cc_test {
    174 			name: "vts_test_binary_bpf_module",
    175 			srcs: ["BpfTest.cpp"],
    176 			data: [":bpf.o"],
    177 		}
    178 	`
    179 
    180 	ctx := testContext(bp)
    181 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
    182 	if errs == nil {
    183 		_, errs = ctx.PrepareBuildActions(config)
    184 	}
    185 	if errs != nil {
    186 		t.Fatal(errs)
    187 	}
    188 
    189 	// We only verify the above BP configuration is processed successfully since the data property
    190 	// value is not available for testing from this package.
    191 	// TODO(jungjw): Add a check for data or move this test to the cc package.
    192 }
    193