-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics_unittest.cpp
283 lines (249 loc) · 9.56 KB
/
diagnostics_unittest.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (C) 2020, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "diagnostics.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include "aidl.h"
#include "parser.h"
#include "tests/fake_io_delegate.h"
using android::aidl::AidlTypenames;
using android::aidl::DiagnosticID;
using android::aidl::Options;
using android::aidl::internals::load_and_validate_aidl;
using android::aidl::test::FakeIoDelegate;
using testing::internal::CaptureStderr;
using testing::internal::GetCapturedStderr;
struct DiagnosticsTest : testing::Test {
void ParseFiles(std::vector<std::pair<std::string, std::string>>&& files) {
ASSERT_TRUE(files.size() > 0);
const std::string main = files.begin()->first;
for (const auto& [file, contents] : files) {
io.SetFileContents(file, contents);
}
if (!enable_diagnostic) {
ASSERT_TRUE(expect_diagnostic);
enable_diagnostic = expect_diagnostic;
}
// emit diagnostics as warnings.
// "java" has no specific meaning here because we're testing CheckValid()
const Options options = Options::From("aidl " + optional_args + " -I . --lang java -o out -W" +
to_string(*enable_diagnostic) + " " + main);
CaptureStderr();
load_and_validate_aidl(main, options, io, &typenames, nullptr);
const std::string err = GetCapturedStderr();
if (expect_diagnostic) {
EXPECT_THAT(err, testing::HasSubstr("-W" + to_string(*expect_diagnostic)));
} else {
EXPECT_EQ("", err);
}
}
AidlTypenames typenames;
FakeIoDelegate io;
std::string optional_args;
// The type of diagnostic to enable for the test. If expect_diagnostic is
// set, use the same value.
std::optional<DiagnosticID> enable_diagnostic;
// The expected diagnostic. Must be set.
std::optional<DiagnosticID> expect_diagnostic;
};
TEST_F(DiagnosticsTest, const_name_ForEnumerator) {
expect_diagnostic = DiagnosticID::const_name;
ParseFiles({{"Foo.aidl", "enum Foo { foo }"}});
}
TEST_F(DiagnosticsTest, const_name_ForConstants) {
expect_diagnostic = DiagnosticID::const_name;
ParseFiles({{"IFoo.aidl", "interface IFoo { const int foo = 1; }"}});
}
TEST_F(DiagnosticsTest, interface_name) {
expect_diagnostic = DiagnosticID::interface_name;
ParseFiles({{"Foo.aidl", "interface Foo { }"}});
}
TEST_F(DiagnosticsTest, enum_explicit_default) {
expect_diagnostic = DiagnosticID::enum_explicit_default;
ParseFiles({{"Foo.aidl", "parcelable Foo { E e; }"}, {"E.aidl", "enum E { A }"}});
}
TEST_F(DiagnosticsTest, inout_parameter) {
expect_diagnostic = DiagnosticID::inout_parameter;
ParseFiles({{"IFoo.aidl", "interface IFoo { void foo(inout Bar bar); }"},
{"Bar.aidl", "parcelable Bar {}"}});
}
TEST_F(DiagnosticsTest, inout_parameter_SuppressAtMethodLevel) {
enable_diagnostic = DiagnosticID::inout_parameter;
expect_diagnostic = {};
ParseFiles({
{"IFoo.aidl",
"interface IFoo { @SuppressWarnings(value={\"inout-parameter\"}) void foo(inout Bar b); }"},
{"Bar.aidl", "parcelable Bar {}"},
});
}
TEST_F(DiagnosticsTest, inout_parameter_SuppressAtDeclLevel) {
enable_diagnostic = DiagnosticID::inout_parameter;
expect_diagnostic = {};
ParseFiles({
{"IFoo.aidl",
"@SuppressWarnings(value={\"inout-parameter\"}) interface IFoo { void foo(inout Bar b); }"},
{"Bar.aidl", "parcelable Bar {}"},
});
}
TEST_F(DiagnosticsTest, UnknownWarning) {
expect_diagnostic = DiagnosticID::unknown_warning;
ParseFiles({
{"IFoo.aidl", "@SuppressWarnings(value={\"blahblah\"}) interface IFoo { void foo(); }"},
});
}
TEST_F(DiagnosticsTest, CantSuppressUnknownWarning) {
expect_diagnostic = DiagnosticID::unknown_warning;
ParseFiles({
{"IFoo.aidl",
"@SuppressWarnings(value={\"unknown-warning\"})\n"
"interface IFoo { @SuppressWarnings(value={\"blah-blah\"}) void foo(); }"},
});
}
TEST_F(DiagnosticsTest, DontMixOnewayWithTwowayMethods) {
expect_diagnostic = DiagnosticID::mixed_oneway;
ParseFiles({
{"IFoo.aidl", "interface IFoo { void foo(); oneway void bar(); }"},
});
}
TEST_F(DiagnosticsTest, DontMixOnewayWithTwowayMethodsSuppressedAtMethod) {
enable_diagnostic = DiagnosticID::mixed_oneway;
expect_diagnostic = {};
ParseFiles({
{"IFoo.aidl",
"interface IFoo {\n"
" void foo();\n"
" @SuppressWarnings(value={\"mixed-oneway\"}) oneway void bar();\n"
"}"},
});
}
TEST_F(DiagnosticsTest, OnewayInterfaceIsOkayWithSyntheticMethods) {
optional_args = "--version 2"; // will add getInterfaceVersion() synthetic method
enable_diagnostic = DiagnosticID::mixed_oneway;
expect_diagnostic = {};
ParseFiles({
{"IFoo.aidl", "oneway interface IFoo { void foo(); }"},
});
}
TEST_F(DiagnosticsTest, ArraysAsOutputParametersConsideredHarmful) {
expect_diagnostic = DiagnosticID::out_array;
ParseFiles({
{"IFoo.aidl", "interface IFoo { void foo(out String[] ret); }"},
});
}
TEST_F(DiagnosticsTest, file_descriptor) {
expect_diagnostic = DiagnosticID::file_descriptor;
ParseFiles({{"IFoo.aidl",
"interface IFoo {\n"
" void foo(in FileDescriptor fd);\n"
"}"}});
}
TEST_F(DiagnosticsTest, out_nullable) {
expect_diagnostic = DiagnosticID::out_nullable;
ParseFiles({{"IFoo.aidl",
"interface IFoo {\n"
" void foo(out @nullable Bar bar);\n"
"}"},
{"Bar.aidl", "parcelable Bar {}"}});
}
TEST_F(DiagnosticsTest, inout_nullable) {
expect_diagnostic = DiagnosticID::out_nullable;
ParseFiles({{"IFoo.aidl",
"interface IFoo {\n"
" void foo(inout @nullable Bar bar);\n"
"}"},
{"Bar.aidl", "parcelable Bar {}"}});
}
TEST_F(DiagnosticsTest, out_nullable_OkayForArrays) {
expect_diagnostic = DiagnosticID::out_array; // not triggering out_nullable
ParseFiles({{"IFoo.aidl",
"interface IFoo {\n"
" void foo(inout @nullable Bar[] bar1, out @nullable Bar[] bar2);\n"
"}"},
{"Bar.aidl", "parcelable Bar {}"}});
}
TEST_F(DiagnosticsTest, RejectImportsCollisionWithTopLevelDecl) {
expect_diagnostic = DiagnosticID::unique_import;
ParseFiles({{"p/IFoo.aidl",
"package p;\n"
"import q.IFoo;\n" // should collide with previous import
"interface IFoo{}"},
{"q/IFoo.aidl", "package q; interface IFoo{}"}});
}
TEST_F(DiagnosticsTest, RejectImportsCollision) {
expect_diagnostic = DiagnosticID::unique_import;
ParseFiles({{"p/IFoo.aidl",
"package p;\n"
"import q.IBar;\n"
"import r.IBar;\n" // should collide with previous import
"interface IFoo{}"},
{"q/IBar.aidl", "package q; interface IBar{}"},
{"r/IBar.aidl", "package r; interface IBar{}"}});
}
TEST_F(DiagnosticsTest, AllowImportingSelf) {
enable_diagnostic = DiagnosticID::unique_import;
expect_diagnostic = {};
ParseFiles({{"p/IFoo.aidl",
"package p;\n"
"import p.IFoo;\n"
"interface IFoo{}"}});
}
TEST_F(DiagnosticsTest, RedundantImports) {
expect_diagnostic = DiagnosticID::unique_import;
ParseFiles({{"p/IFoo.aidl",
"package p;\n"
"import q.IBar;\n"
"import q.IBar;\n"
"interface IFoo{}"},
{"q/IBar.aidl", "package q; interface IBar{}"}});
}
TEST_F(DiagnosticsTest, UntypedCollectionInterface) {
expect_diagnostic = DiagnosticID::untyped_collection;
ParseFiles({{"IFoo.aidl", "interface IFoo { void foo(in Map m); }"}});
}
TEST_F(DiagnosticsTest, UntypedCollectionParcelable) {
expect_diagnostic = DiagnosticID::untyped_collection;
ParseFiles({{"Foo.aidl", "parcelable Foo { Map m; }"}});
}
TEST_F(DiagnosticsTest, UntypedCollectionUnion) {
expect_diagnostic = DiagnosticID::untyped_collection;
ParseFiles({{"Foo.aidl", "union Foo { List l; }"}});
}
TEST_F(DiagnosticsTest, UntypedCollectionInTypeArg) {
expect_diagnostic = DiagnosticID::untyped_collection;
ParseFiles({{"IFoo.aidl", "interface IFoo { void foo(in Bar<Map> m); }"},
{"Bar.aidl", "parcelable Bar<T> {}"}});
}
TEST_F(DiagnosticsTest, PermissionMissing) {
expect_diagnostic = DiagnosticID::missing_permission_annotation;
ParseFiles({{"IFoo.aidl", "interface IFoo { void food(); }"}});
}
TEST_F(DiagnosticsTest, PermissionMethod) {
enable_diagnostic = DiagnosticID::missing_permission_annotation;
expect_diagnostic = {};
ParseFiles({{"IFoo.aidl", "interface IFoo { @EnforcePermission(\"INTERNET\") void food(); }"}});
}
TEST_F(DiagnosticsTest, PermissionMethodMissing) {
expect_diagnostic = DiagnosticID::missing_permission_annotation;
ParseFiles({{"IFoo.aidl",
"interface IFoo { @EnforcePermission(\"INTERNET\") void food(); void foo2(); }"}});
}
TEST_F(DiagnosticsTest, PermissionInterface) {
enable_diagnostic = DiagnosticID::missing_permission_annotation;
expect_diagnostic = {};
ParseFiles({{"IFoo.aidl", "@EnforcePermission(\"INTERNET\") interface IFoo { void food(); }"}});
}