xref: /aosp_15_r20/build/make/tools/compliance/cmd/rtrace/rtrace_test.go (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1// Copyright 2021 Google LLC
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 main
16
17import (
18	"bytes"
19	"fmt"
20	"os"
21	"strings"
22	"testing"
23
24	"android/soong/tools/compliance"
25)
26
27func TestMain(m *testing.M) {
28	// Change into the parent directory before running the tests
29	// so they can find the testdata directory.
30	if err := os.Chdir(".."); err != nil {
31		fmt.Printf("failed to change to testdata directory: %s\n", err)
32		os.Exit(1)
33	}
34	os.Exit(m.Run())
35}
36
37func Test_plaintext(t *testing.T) {
38	tests := []struct {
39		condition   string
40		name        string
41		outDir      string
42		roots       []string
43		ctx         context
44		expectedOut []string
45	}{
46		{
47			condition:   "firstparty",
48			name:        "apex",
49			roots:       []string{"highest.apex.meta_lic"},
50			expectedOut: []string{},
51		},
52		{
53			condition: "firstparty",
54			name:      "apex_trimmed",
55			roots:     []string{"highest.apex.meta_lic"},
56			ctx: context{
57				sources:     []string{"testdata/firstparty/bin/bin1.meta_lic"},
58				stripPrefix: []string{"testdata/firstparty/"},
59			},
60			expectedOut: []string{},
61		},
62		{
63			condition:   "firstparty",
64			name:        "container",
65			roots:       []string{"container.zip.meta_lic"},
66			expectedOut: []string{},
67		},
68		{
69			condition:   "firstparty",
70			name:        "application",
71			roots:       []string{"application.meta_lic"},
72			expectedOut: []string{},
73		},
74		{
75			condition:   "firstparty",
76			name:        "binary",
77			roots:       []string{"bin/bin1.meta_lic"},
78			expectedOut: []string{},
79		},
80		{
81			condition:   "firstparty",
82			name:        "library",
83			roots:       []string{"lib/libd.so.meta_lic"},
84			expectedOut: []string{},
85		},
86		{
87			condition:   "notice",
88			name:        "apex",
89			roots:       []string{"highest.apex.meta_lic"},
90			expectedOut: []string{},
91		},
92		{
93			condition: "notice",
94			name:      "apex_trimmed",
95			roots:     []string{"highest.apex.meta_lic"},
96			ctx: context{
97				sources:     []string{"testdata/notice/bin/bin1.meta_lic"},
98				stripPrefix: []string{"testdata/notice/"},
99			},
100			expectedOut: []string{},
101		},
102		{
103			condition:   "notice",
104			name:        "container",
105			roots:       []string{"container.zip.meta_lic"},
106			expectedOut: []string{},
107		},
108		{
109			condition:   "notice",
110			name:        "application",
111			roots:       []string{"application.meta_lic"},
112			expectedOut: []string{},
113		},
114		{
115			condition:   "notice",
116			name:        "binary",
117			roots:       []string{"bin/bin1.meta_lic"},
118			expectedOut: []string{},
119		},
120		{
121			condition:   "notice",
122			name:        "library",
123			roots:       []string{"lib/libd.so.meta_lic"},
124			expectedOut: []string{},
125		},
126		{
127			condition:   "reciprocal",
128			name:        "apex",
129			roots:       []string{"highest.apex.meta_lic"},
130			expectedOut: []string{},
131		},
132		{
133			condition: "reciprocal",
134			name:      "apex_trimmed",
135			roots:     []string{"highest.apex.meta_lic"},
136			ctx: context{
137				sources:     []string{"testdata/reciprocal/bin/bin1.meta_lic"},
138				stripPrefix: []string{"testdata/reciprocal/"},
139			},
140			expectedOut: []string{},
141		},
142		{
143			condition:   "reciprocal",
144			name:        "container",
145			roots:       []string{"container.zip.meta_lic"},
146			expectedOut: []string{},
147		},
148		{
149			condition:   "reciprocal",
150			name:        "application",
151			roots:       []string{"application.meta_lic"},
152			expectedOut: []string{},
153		},
154		{
155			condition:   "reciprocal",
156			name:        "binary",
157			roots:       []string{"bin/bin1.meta_lic"},
158			expectedOut: []string{},
159		},
160		{
161			condition:   "reciprocal",
162			name:        "library",
163			roots:       []string{"lib/libd.so.meta_lic"},
164			expectedOut: []string{},
165		},
166		{
167			condition: "restricted",
168			name:      "apex",
169			roots:     []string{"highest.apex.meta_lic"},
170			expectedOut: []string{
171				"testdata/restricted/lib/liba.so.meta_lic restricted_if_statically_linked",
172				"testdata/restricted/lib/libb.so.meta_lic restricted",
173			},
174		},
175		{
176			condition: "restricted",
177			name:      "apex_trimmed_bin1",
178			roots:     []string{"highest.apex.meta_lic"},
179			ctx: context{
180				sources:     []string{"testdata/restricted/bin/bin1.meta_lic"},
181				stripPrefix: []string{"testdata/restricted/"},
182			},
183			expectedOut: []string{"lib/liba.so.meta_lic restricted_if_statically_linked"},
184		},
185		{
186			condition: "restricted",
187			name:      "apex_trimmed_bin2",
188			roots:     []string{"highest.apex.meta_lic"},
189			ctx: context{
190				sources:     []string{"testdata/restricted/bin/bin2.meta_lic"},
191				stripPrefix: []string{"testdata/restricted/"},
192			},
193			expectedOut: []string{"lib/libb.so.meta_lic restricted"},
194		},
195		{
196			condition: "restricted",
197			name:      "container",
198			roots:     []string{"container.zip.meta_lic"},
199			expectedOut: []string{
200				"testdata/restricted/lib/liba.so.meta_lic restricted_if_statically_linked",
201				"testdata/restricted/lib/libb.so.meta_lic restricted",
202			},
203		},
204		{
205			condition:   "restricted",
206			name:        "application",
207			roots:       []string{"application.meta_lic"},
208			expectedOut: []string{"testdata/restricted/lib/liba.so.meta_lic restricted_if_statically_linked"},
209		},
210		{
211			condition:   "restricted",
212			name:        "binary",
213			roots:       []string{"bin/bin1.meta_lic"},
214			expectedOut: []string{"testdata/restricted/lib/liba.so.meta_lic restricted_if_statically_linked"},
215		},
216		{
217			condition:   "restricted",
218			name:        "library",
219			roots:       []string{"lib/libd.so.meta_lic"},
220			expectedOut: []string{},
221		},
222		{
223			condition:   "proprietary",
224			name:        "apex",
225			roots:       []string{"highest.apex.meta_lic"},
226			expectedOut: []string{"testdata/proprietary/lib/libb.so.meta_lic restricted"},
227		},
228		{
229			condition: "proprietary",
230			name:      "apex_trimmed_bin1",
231			roots:     []string{"highest.apex.meta_lic"},
232			ctx: context{
233				sources:     []string{"testdata/proprietary/bin/bin1.meta_lic"},
234				stripPrefix: []string{"testdata/proprietary/"},
235			},
236			expectedOut: []string{},
237		},
238		{
239			condition: "proprietary",
240			name:      "apex_trimmed_bin2",
241			roots:     []string{"highest.apex.meta_lic"},
242			ctx: context{
243				sources:     []string{"testdata/proprietary/bin/bin2.meta_lic"},
244				stripPrefix: []string{"testdata/proprietary/"},
245			},
246			expectedOut: []string{"lib/libb.so.meta_lic restricted"},
247		},
248		{
249			condition:   "proprietary",
250			name:        "container",
251			roots:       []string{"container.zip.meta_lic"},
252			expectedOut: []string{"testdata/proprietary/lib/libb.so.meta_lic restricted"},
253		},
254		{
255			condition:   "proprietary",
256			name:        "application",
257			roots:       []string{"application.meta_lic"},
258			expectedOut: []string{},
259		},
260		{
261			condition:   "proprietary",
262			name:        "binary",
263			roots:       []string{"bin/bin1.meta_lic"},
264			expectedOut: []string{},
265		},
266		{
267			condition:   "proprietary",
268			name:        "library",
269			roots:       []string{"lib/libd.so.meta_lic"},
270			expectedOut: []string{},
271		},
272	}
273	for _, tt := range tests {
274		t.Run(tt.condition+" "+tt.name, func(t *testing.T) {
275			expectedOut := &bytes.Buffer{}
276			for _, eo := range tt.expectedOut {
277				expectedOut.WriteString(eo)
278				expectedOut.WriteString("\n")
279			}
280			fmt.Fprintf(expectedOut, "restricted conditions trace to %d targets\n", len(tt.expectedOut))
281			if 0 == len(tt.expectedOut) {
282				fmt.Fprintln(expectedOut, "  (check for typos in project names or metadata files)")
283			}
284
285			stdout := &bytes.Buffer{}
286			stderr := &bytes.Buffer{}
287
288			rootFiles := make([]string, 0, len(tt.roots))
289			for _, r := range tt.roots {
290				rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r)
291			}
292			if len(tt.ctx.sources) < 1 {
293				tt.ctx.sources = rootFiles
294			}
295			_, err := traceRestricted(&tt.ctx, stdout, stderr, compliance.GetFS(tt.outDir), rootFiles...)
296			t.Logf("rtrace: stderr = %v", stderr)
297			t.Logf("rtrace: stdout = %v", stdout)
298			if err != nil {
299				t.Fatalf("rtrace: error = %v", err)
300				return
301			}
302			if stderr.Len() > 0 {
303				t.Errorf("rtrace: gotStderr = %v, want none", stderr)
304			}
305			out := stdout.String()
306			expected := expectedOut.String()
307			if out != expected {
308				outList := strings.Split(out, "\n")
309				expectedList := strings.Split(expected, "\n")
310				startLine := 0
311				for startLine < len(outList) && startLine < len(expectedList) && outList[startLine] == expectedList[startLine] {
312					startLine++
313				}
314				t.Errorf("rtrace: gotStdout = %v, want %v, somewhere near line %d Stdout = %v, want %v",
315					out, expected, startLine+1, outList[startLine], expectedList[startLine])
316			}
317		})
318	}
319}
320