summaryrefslogtreecommitdiff
path: root/android/license_kind_test.go
blob: 767b64e82dd6b13bdd8305bbb0c421bba82bb82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package android

import (
	"testing"

	"github.com/google/blueprint"
)

var licenseKindTests = []struct {
	name           string
	fs             map[string][]byte
	expectedErrors []string
}{
	{
		name: "license_kind must not accept licenses property",
		fs: map[string][]byte{
			"top/Blueprints": []byte(`
				license_kind {
					name: "top_license",
					licenses: ["other_license"],
				}`),
		},
		expectedErrors: []string{
			`top/Blueprints:4:14: unrecognized property "licenses"`,
		},
	},
	{
		name: "bad license_kind",
		fs: map[string][]byte{
			"top/Blueprints": []byte(`
				license_kind {
					name: "top_notice",
					conditions: ["notice"],
				}`),
			"other/Blueprints": []byte(`
				mock_license {
					name: "other_notice",
					license_kinds: ["notice"],
				}`),
		},
		expectedErrors: []string{
			`other/Blueprints:2:5: "other_notice" depends on undefined module "notice"`,
		},
	},
	{
		name: "good license kind",
		fs: map[string][]byte{
			"top/Blueprints": []byte(`
				license_kind {
					name: "top_by_exception_only",
					conditions: ["by_exception_only"],
				}

				mock_license {
					name: "top_proprietary",
					license_kinds: ["top_by_exception_only"],
				}`),
			"other/Blueprints": []byte(`
				mock_license {
					name: "other_proprietary",
					license_kinds: ["top_proprietary"],
				}`),
		},
	},
	{
		name: "multiple license kinds",
		fs: map[string][]byte{
			"top/Blueprints": []byte(`
				license_kind {
					name: "top_notice",
					conditions: ["notice"],
				}

				license_kind {
					name: "top_by_exception_only",
					conditions: ["by_exception_only"],
				}

				mock_license {
					name: "top_allowed_as_notice",
					license_kinds: ["top_notice"],
				}

				mock_license {
					name: "top_proprietary",
					license_kinds: ["top_by_exception_only"],
				}`),
			"other/Blueprints": []byte(`
				mock_license {
					name: "other_rule",
					license_kinds: ["top_by_exception_only"],
				}`),
		},
	},
}

func TestLicenseKind(t *testing.T) {
	for _, test := range licenseKindTests {
		t.Run(test.name, func(t *testing.T) {
			_, errs := testLicenseKind(test.fs)

			expectedErrors := test.expectedErrors
			if expectedErrors == nil {
				FailIfErrored(t, errs)
			} else {
				for _, expectedError := range expectedErrors {
					FailIfNoMatchingErrors(t, expectedError, errs)
				}
				if len(errs) > len(expectedErrors) {
					t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
					for i, expectedError := range expectedErrors {
						t.Errorf("expectedErrors[%d] = %s", i, expectedError)
					}
					for i, err := range errs {
						t.Errorf("errs[%d] = %s", i, err)
					}
				}
			}
		})
	}
}

func testLicenseKind(fs map[string][]byte) (*TestContext, []error) {

	// Create a new config per test as license_kind information is stored in the config.
	config := TestArchConfig(buildDir, nil, "", fs)

	ctx := NewTestArchContext(config)
	RegisterLicenseKindBuildComponents(ctx)
	ctx.RegisterModuleType("mock_license", newMockLicenseModule)
	ctx.Register()

	_, errs := ctx.ParseBlueprintsFiles(".")
	if len(errs) > 0 {
		return ctx, errs
	}

	_, errs = ctx.PrepareBuildActions(config)
	return ctx, errs
}

type mockLicenseProperties struct {
	License_kinds []string
}

type mockLicenseModule struct {
	ModuleBase
	DefaultableModuleBase

	properties mockLicenseProperties
}

func newMockLicenseModule() Module {
	m := &mockLicenseModule{}
	m.AddProperties(&m.properties)
	InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
	InitDefaultableModule(m)
	return m
}

type licensekindTag struct {
	blueprint.BaseDependencyTag
}

func (j *mockLicenseModule) DepsMutator(ctx BottomUpMutatorContext) {
	m, ok := ctx.Module().(Module)
	if !ok {
		return
	}
	ctx.AddDependency(m, licensekindTag{}, j.properties.License_kinds...)
}

func (p *mockLicenseModule) GenerateAndroidBuildActions(ModuleContext) {
}