xref: /aosp_15_r20/external/bazelbuild-rules_android/src/tools/ak/extractaar/validator_test.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2022 The Bazel Authors. 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 extractaar
16
17import (
18	"testing"
19
20	"github.com/google/go-cmp/cmp"
21	"github.com/google/go-cmp/cmp/cmpopts"
22)
23
24func TestValidateManifest(t *testing.T) {
25	tests := []struct {
26		name          string
27		files         []*aarFile
28		dest          string
29		expectedFiles []*toCopy
30	}{
31		{
32			name: "one manifest",
33			files: []*aarFile{
34				&aarFile{path: "/tmp/aar/AndroidManifest.xml"},
35			},
36			dest: "/dest/outputManifest.xml",
37			expectedFiles: []*toCopy{
38				&toCopy{src: "/tmp/aar/AndroidManifest.xml", dest: "/dest/outputManifest.xml"},
39			},
40		},
41	}
42
43	for _, tc := range tests {
44		t.Run(tc.name, func(t *testing.T) {
45			validator := manifestValidator{dest: tc.dest}
46			files, err := validator.validate(tc.files)
47			if err != nil {
48				t.Fatalf("manifestValidator.validate(%s) unexpected error: %v", tc.files, err)
49			}
50			if diff := cmp.Diff(tc.expectedFiles, files, cmp.AllowUnexported(toCopy{})); diff != "" {
51				t.Errorf("manifestValidator.validate(%s) returned diff (-want, +got):\n%v", tc.files, diff)
52			}
53		})
54	}
55}
56
57func TestValidateManifestError(t *testing.T) {
58	tests := []struct {
59		name  string
60		files []*aarFile
61	}{
62		{
63			name:  "no manifest",
64			files: []*aarFile{},
65		},
66		{
67			name: "multiple manifests",
68			files: []*aarFile{
69				&aarFile{path: "/tmp/aar/AndroidManifest.xml"},
70				&aarFile{path: "/tmp/aar/SecondAndroidManifest.xml"},
71			},
72		},
73	}
74
75	for _, tc := range tests {
76		t.Run(tc.name, func(t *testing.T) {
77			validator := manifestValidator{}
78			if _, err := validator.validate(tc.files); err == nil {
79				t.Errorf("manifestValidator.validate(%s) expected error but test succeeded: %v", tc.files, err)
80			}
81		})
82	}
83}
84
85func TestValidateResources(t *testing.T) {
86	tests := []struct {
87		name          string
88		files         []*aarFile
89		dest          string
90		hasRes        tristate
91		expectedFiles []*toCopy
92	}{
93		{
94			name: "has resources with valid hasRes attribute",
95			files: []*aarFile{
96				&aarFile{path: "/tmp/aar/res/values/strings.xml", relPath: "res/values/strings.xml"},
97				&aarFile{path: "/tmp/aar/res/layout/activity.xml", relPath: "res/layout/activity.xml"},
98			},
99			hasRes: tristate(1),
100			dest:   "/dest/outputres",
101			expectedFiles: []*toCopy{
102				&toCopy{src: "/tmp/aar/res/values/strings.xml", dest: "/dest/outputres/res/values/strings.xml"},
103				&toCopy{src: "/tmp/aar/res/layout/activity.xml", dest: "/dest/outputres/res/layout/activity.xml"},
104			},
105		},
106		{
107			name:          "does not have resources with valid hasRes attribute",
108			files:         []*aarFile{},
109			hasRes:        tristate(0),
110			dest:          "/dest/outputres",
111			expectedFiles: nil,
112		},
113		{
114			name:          "no resources and checks disabled",
115			files:         []*aarFile{},
116			hasRes:        tristate(-1),
117			dest:          "/dest/outputres",
118			expectedFiles: nil,
119		},
120	}
121
122	for _, tc := range tests {
123		t.Run(tc.name, func(t *testing.T) {
124			validator := resourceValidator{dest: tc.dest, hasRes: tc.hasRes}
125			files, err := validator.validate(tc.files)
126			if err != nil {
127				t.Fatalf("resourceValidator.validate(%s) unexpected error: %v", tc.files, err)
128			}
129			if diff := cmp.Diff(tc.expectedFiles, files, cmp.AllowUnexported(toCopy{})); diff != "" {
130				t.Errorf("resourceValidator.validate(%s) returned diff (-want, +got):\n%v", tc.files, diff)
131			}
132		})
133	}
134}
135
136func TestValidateResourcesError(t *testing.T) {
137	tests := []struct {
138		name          string
139		files         []*aarFile
140		hasRes        tristate
141		ruleAttr      string
142		expectedError *BuildozerError
143	}{
144		{
145			name: "has resources with invalid hasRes attribute",
146			files: []*aarFile{
147				&aarFile{path: "/tmp/aar/res/values/strings.xml", relPath: "res/values/strings.xml"},
148				&aarFile{path: "/tmp/aar/res/layout/activity.xml", relPath: "res/layout/activity.xml"},
149			},
150			hasRes:        tristate(-1),
151			ruleAttr:      "test",
152			expectedError: &BuildozerError{RuleAttr: "test", NewValue: "True"},
153		},
154		{
155			name:          "no resources with invalid hasRes attribute",
156			files:         []*aarFile{},
157			hasRes:        tristate(1),
158			ruleAttr:      "test",
159			expectedError: &BuildozerError{RuleAttr: "test", NewValue: "False"},
160		},
161	}
162
163	for _, tc := range tests {
164		t.Run(tc.name, func(t *testing.T) {
165			validator := resourceValidator{ruleAttr: tc.ruleAttr, hasRes: tc.hasRes}
166			_, err := validator.validate(tc.files)
167			if err == nil {
168				t.Fatalf("resourceValidator.validate(%s) expected error but test succeeded: %v", tc.files, err)
169			}
170			if diff := cmp.Diff(tc.expectedError, err, cmpopts.IgnoreFields(BuildozerError{}, "Msg")); diff != "" {
171				t.Errorf("resourceValidator.validate(%s) returned diff (-want, +got):\n%v", tc.files, diff)
172			}
173		})
174	}
175}
176