blob: be9e36ea567eec96477acfa92c9479497320bcd3 [file] [log] [blame]
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -08001// 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
15package bpf
16
17import (
18 "io/ioutil"
19 "os"
20 "testing"
21
22 "android/soong/android"
Colin Cross815daf92019-05-14 16:05:20 -070023 "android/soong/cc"
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -080024)
25
26var buildDir string
27
28func setUp() {
29 var err error
30 buildDir, err = ioutil.TempDir("", "genrule_test")
31 if err != nil {
32 panic(err)
33 }
34}
35
36func tearDown() {
37 os.RemoveAll(buildDir)
38}
39
40func 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
Colin Cross98be1bb2019-12-13 20:41:13 -080051func testConfig(buildDir string, env map[string]string, bp string) android.Config {
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -080052 mockFS := map[string][]byte{
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -080053 "bpf.c": nil,
54 "BpfTest.cpp": nil,
55 }
56
Colin Cross98be1bb2019-12-13 20:41:13 -080057 return cc.TestConfig(buildDir, android.Android, env, bp, mockFS)
58}
59
60func testContext(config android.Config) *android.TestContext {
Colin Crossae8600b2020-10-29 17:09:13 -070061 ctx := cc.CreateTestContext(config)
markchien2f59ec92020-09-02 16:23:38 +080062 ctx.RegisterModuleType("bpf", BpfFactory)
Colin Crossae8600b2020-10-29 17:09:13 -070063 ctx.Register()
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -080064
65 return ctx
66}
67
68func TestBpfDataDependency(t *testing.T) {
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -080069 bp := `
70 bpf {
71 name: "bpf.o",
72 srcs: ["bpf.c"],
73 }
74
75 cc_test {
76 name: "vts_test_binary_bpf_module",
77 srcs: ["BpfTest.cpp"],
78 data: [":bpf.o"],
Colin Cross815daf92019-05-14 16:05:20 -070079 gtest: false,
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -080080 }
81 `
82
Colin Cross98be1bb2019-12-13 20:41:13 -080083 config := testConfig(buildDir, nil, bp)
84 ctx := testContext(config)
85
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -080086 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
87 if errs == nil {
88 _, errs = ctx.PrepareBuildActions(config)
89 }
90 if errs != nil {
91 t.Fatal(errs)
92 }
93
94 // We only verify the above BP configuration is processed successfully since the data property
95 // value is not available for testing from this package.
96 // TODO(jungjw): Add a check for data or move this test to the cc package.
97}