xref: /aosp_15_r20/build/make/tools/compliance/cmd/shippedlibs/shippedlibs_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	"bufio"
19	"bytes"
20	"fmt"
21	"os"
22	"strings"
23	"testing"
24
25	"android/soong/tools/compliance"
26)
27
28func TestMain(m *testing.M) {
29	// Change into the parent directory before running the tests
30	// so they can find the testdata directory.
31	if err := os.Chdir(".."); err != nil {
32		fmt.Printf("failed to change to testdata directory: %s\n", err)
33		os.Exit(1)
34	}
35	os.Exit(m.Run())
36}
37
38func Test(t *testing.T) {
39	tests := []struct {
40		condition   string
41		name        string
42		outDir      string
43		roots       []string
44		expectedOut []string
45	}{
46		{
47			condition:   "firstparty",
48			name:        "apex",
49			roots:       []string{"highest.apex.meta_lic"},
50			expectedOut: []string{"Android"},
51		},
52		{
53			condition:   "firstparty",
54			name:        "container",
55			roots:       []string{"container.zip.meta_lic"},
56			expectedOut: []string{"Android"},
57		},
58		{
59			condition:   "firstparty",
60			name:        "application",
61			roots:       []string{"application.meta_lic"},
62			expectedOut: []string{"Android"},
63		},
64		{
65			condition:   "firstparty",
66			name:        "binary",
67			roots:       []string{"bin/bin1.meta_lic"},
68			expectedOut: []string{"Android"},
69		},
70		{
71			condition:   "firstparty",
72			name:        "library",
73			roots:       []string{"lib/libd.so.meta_lic"},
74			expectedOut: []string{"Android"},
75		},
76		{
77			condition:   "notice",
78			name:        "apex",
79			roots:       []string{"highest.apex.meta_lic"},
80			expectedOut: []string{"Android", "Device", "External"},
81		},
82		{
83			condition:   "notice",
84			name:        "container",
85			roots:       []string{"container.zip.meta_lic"},
86			expectedOut: []string{"Android", "Device", "External"},
87		},
88		{
89			condition:   "notice",
90			name:        "application",
91			roots:       []string{"application.meta_lic"},
92			expectedOut: []string{"Android", "Device"},
93		},
94		{
95			condition:   "notice",
96			name:        "binary",
97			roots:       []string{"bin/bin1.meta_lic"},
98			expectedOut: []string{"Android", "Device", "External"},
99		},
100		{
101			condition:   "notice",
102			name:        "library",
103			roots:       []string{"lib/libd.so.meta_lic"},
104			expectedOut: []string{"External"},
105		},
106		{
107			condition:   "reciprocal",
108			name:        "apex",
109			roots:       []string{"highest.apex.meta_lic"},
110			expectedOut: []string{"Android", "Device", "External"},
111		},
112		{
113			condition:   "reciprocal",
114			name:        "container",
115			roots:       []string{"container.zip.meta_lic"},
116			expectedOut: []string{"Android", "Device", "External"},
117		},
118		{
119			condition:   "reciprocal",
120			name:        "application",
121			roots:       []string{"application.meta_lic"},
122			expectedOut: []string{"Android", "Device"},
123		},
124		{
125			condition:   "reciprocal",
126			name:        "binary",
127			roots:       []string{"bin/bin1.meta_lic"},
128			expectedOut: []string{"Android", "Device", "External"},
129		},
130		{
131			condition:   "reciprocal",
132			name:        "library",
133			roots:       []string{"lib/libd.so.meta_lic"},
134			expectedOut: []string{"External"},
135		},
136		{
137			condition:   "restricted",
138			name:        "apex",
139			roots:       []string{"highest.apex.meta_lic"},
140			expectedOut: []string{"Android", "Device", "External"},
141		},
142		{
143			condition:   "restricted",
144			name:        "container",
145			roots:       []string{"container.zip.meta_lic"},
146			expectedOut: []string{"Android", "Device", "External"},
147		},
148		{
149			condition:   "restricted",
150			name:        "application",
151			roots:       []string{"application.meta_lic"},
152			expectedOut: []string{"Android", "Device"},
153		},
154		{
155			condition:   "restricted",
156			name:        "binary",
157			roots:       []string{"bin/bin1.meta_lic"},
158			expectedOut: []string{"Android", "Device", "External"},
159		},
160		{
161			condition:   "restricted",
162			name:        "library",
163			roots:       []string{"lib/libd.so.meta_lic"},
164			expectedOut: []string{"External"},
165		},
166		{
167			condition:   "proprietary",
168			name:        "apex",
169			roots:       []string{"highest.apex.meta_lic"},
170			expectedOut: []string{"Android", "Device", "External"},
171		},
172		{
173			condition:   "proprietary",
174			name:        "container",
175			roots:       []string{"container.zip.meta_lic"},
176			expectedOut: []string{"Android", "Device", "External"},
177		},
178		{
179			condition:   "proprietary",
180			name:        "application",
181			roots:       []string{"application.meta_lic"},
182			expectedOut: []string{"Android", "Device"},
183		},
184		{
185			condition:   "proprietary",
186			name:        "binary",
187			roots:       []string{"bin/bin1.meta_lic"},
188			expectedOut: []string{"Android", "Device", "External"},
189		},
190		{
191			condition:   "proprietary",
192			name:        "library",
193			roots:       []string{"lib/libd.so.meta_lic"},
194			expectedOut: []string{"External"},
195		},
196	}
197	for _, tt := range tests {
198		t.Run(tt.condition+" "+tt.name, func(t *testing.T) {
199			stdout := &bytes.Buffer{}
200			stderr := &bytes.Buffer{}
201
202			rootFiles := make([]string, 0, len(tt.roots))
203			for _, r := range tt.roots {
204				rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r)
205			}
206
207			ctx := context{stdout, stderr, compliance.GetFS(tt.outDir)}
208
209			err := shippedLibs(&ctx, rootFiles...)
210			if err != nil {
211				t.Fatalf("shippedLibs: error = %v, stderr = %v", err, stderr)
212				return
213			}
214			if stderr.Len() > 0 {
215				t.Errorf("shippedLibs: gotStderr = %v, want none", stderr)
216			}
217
218			t.Logf("got stdout: %s", stdout.String())
219
220			t.Logf("want stdout: %s", strings.Join(tt.expectedOut, "\n"))
221
222			out := bufio.NewScanner(stdout)
223			lineno := 0
224			for out.Scan() {
225				line := out.Text()
226				if strings.TrimLeft(line, " ") == "" {
227					continue
228				}
229				if len(tt.expectedOut) <= lineno {
230					t.Errorf("shippedLibs: unexpected output at line %d: got %q, want nothing (wanted %d lines)", lineno+1, line, len(tt.expectedOut))
231				} else if tt.expectedOut[lineno] != line {
232					t.Errorf("shippedLibs: unexpected output at line %d: got %q, want %q", lineno+1, line, tt.expectedOut[lineno])
233				}
234				lineno++
235			}
236			for ; lineno < len(tt.expectedOut); lineno++ {
237				t.Errorf("shippedLibs: missing output line %d: ended early, want %q", lineno+1, tt.expectedOut[lineno])
238			}
239		})
240	}
241}
242