1 /*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <memory>
18 #include <set>
19 #include <string>
20 #include <vector>
21
22 #include <android-base/stringprintf.h>
23 #include <gtest/gtest.h>
24
25 #include "aidl.h"
26 #include "aidl_checkapi.h"
27 #include "aidl_language.h"
28 #include "aidl_to_cpp.h"
29 #include "aidl_to_java.h"
30 #include "options.h"
31 #include "tests/fake_io_delegate.h"
32
33 using android::aidl::internals::parse_preprocessed_file;
34 using android::aidl::test::FakeIoDelegate;
35 using android::base::StringPrintf;
36 using std::set;
37 using std::string;
38 using std::unique_ptr;
39 using std::vector;
40 using testing::internal::CaptureStderr;
41 using testing::internal::GetCapturedStderr;
42
43 namespace android {
44 namespace aidl {
45 namespace {
46
47 const char kExpectedDepFileContents[] =
48 R"(place/for/output/p/IFoo.java : \
49 p/IFoo.aidl
50
51 p/IFoo.aidl :
52 )";
53
54 const char kExpectedNinjaDepFileContents[] =
55 R"(place/for/output/p/IFoo.java : \
56 p/IFoo.aidl
57 )";
58
59 const char kExpectedParcelableDeclarationDepFileContents[] =
60 R"( : \
61 p/Foo.aidl
62
63 p/Foo.aidl :
64 )";
65
66 const char kExpectedStructuredParcelableDepFileContents[] =
67 R"(place/for/output/p/Foo.java : \
68 p/Foo.aidl
69
70 p/Foo.aidl :
71 )";
72
73 const char kExpectedJavaParcelableOutputContests[] =
74 R"(/*
75 * This file is auto-generated. DO NOT MODIFY.
76 */
77 @android.annotation.Hide
78 public class Rect implements android.os.Parcelable
79 {
80 // Comment
81
82 @android.annotation.Hide
83 public int x = 5;
84
85 @android.annotation.Hide
86 @android.compat.annotation.UnsupportedAppUsage(expectedSignature = "dummy", implicitMember = "dummy", maxTargetSdk = 28, publicAlternatives = "dummy", trackingBug = 42L, overrideSourcePosition="Rect.aidl:7:1:10:14")
87 public int y;
88
89 public android.os.ParcelFileDescriptor fd;
90 public static final android.os.Parcelable.Creator<Rect> CREATOR = new android.os.Parcelable.Creator<Rect>() {
91 @Override
92 public Rect createFromParcel(android.os.Parcel _aidl_source) {
93 Rect _aidl_out = new Rect();
94 _aidl_out.readFromParcel(_aidl_source);
95 return _aidl_out;
96 }
97 @Override
98 public Rect[] newArray(int _aidl_size) {
99 return new Rect[_aidl_size];
100 }
101 };
102 @Override public final void writeToParcel(android.os.Parcel _aidl_parcel, int _aidl_flag)
103 {
104 int _aidl_start_pos = _aidl_parcel.dataPosition();
105 _aidl_parcel.writeInt(0);
106 _aidl_parcel.writeInt(x);
107 _aidl_parcel.writeInt(y);
108 if ((fd!=null)) {
109 _aidl_parcel.writeInt(1);
110 fd.writeToParcel(_aidl_parcel, 0);
111 }
112 else {
113 _aidl_parcel.writeInt(0);
114 }
115 int _aidl_end_pos = _aidl_parcel.dataPosition();
116 _aidl_parcel.setDataPosition(_aidl_start_pos);
117 _aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos);
118 _aidl_parcel.setDataPosition(_aidl_end_pos);
119 }
120 public final void readFromParcel(android.os.Parcel _aidl_parcel)
121 {
122 int _aidl_start_pos = _aidl_parcel.dataPosition();
123 int _aidl_parcelable_size = _aidl_parcel.readInt();
124 if (_aidl_parcelable_size < 0) return;
125 try {
126 x = _aidl_parcel.readInt();
127 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
128 y = _aidl_parcel.readInt();
129 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
130 if ((0!=_aidl_parcel.readInt())) {
131 fd = android.os.ParcelFileDescriptor.CREATOR.createFromParcel(_aidl_parcel);
132 }
133 else {
134 fd = null;
135 }
136 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
137 } finally {
138 _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size);
139 }
140 }
141 @Override public int describeContents()
142 {
143 return 0;
144 }
145 }
146 )";
147
148 } // namespace
149
150 class AidlTest : public ::testing::Test {
151 protected:
SetUp()152 void SetUp() override {
153 CaptureStderr();
154 }
155
TearDown()156 void TearDown() override {
157 auto actual_stderr = GetCapturedStderr();
158 std::cerr << actual_stderr << std::endl;
159
160 if (expected_stderr_.size() > 0) {
161 EXPECT_EQ(android::base::Join(expected_stderr_, ""), actual_stderr);
162 }
163 }
164
AddExpectedStderr(string expected)165 void AddExpectedStderr(string expected) { expected_stderr_.push_back(expected); }
166
Parse(const string & path,const string & contents,AidlTypenames & typenames_,Options::Language lang,AidlError * error=nullptr,const vector<string> additional_arguments={})167 AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_,
168 Options::Language lang, AidlError* error = nullptr,
169 const vector<string> additional_arguments = {}) {
170 io_delegate_.SetFileContents(path, contents);
171 vector<string> args;
172 if (lang == Options::Language::CPP) {
173 args.emplace_back("aidl-cpp");
174 } else {
175 args.emplace_back("aidl");
176 }
177 for (const string& s : additional_arguments) {
178 args.emplace_back(s);
179 }
180 for (const string& f : preprocessed_files_) {
181 args.emplace_back("--preprocessed=" + f);
182 }
183 for (const string& i : import_paths_) {
184 args.emplace_back("--include=" + i);
185 }
186 args.emplace_back(path);
187 Options options = Options::From(args);
188 vector<AidlDefinedType*> defined_types;
189 vector<string> imported_files;
190 ImportResolver import_resolver{io_delegate_, path, import_paths_, {}};
191 AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
192 path, options, io_delegate_, &typenames_, &defined_types, &imported_files);
193
194 if (error != nullptr) {
195 *error = actual_error;
196 }
197
198 if (actual_error != AidlError::OK) {
199 return nullptr;
200 }
201
202 EXPECT_EQ(1ul, defined_types.size());
203
204 return defined_types.front();
205 }
206
207 FakeIoDelegate io_delegate_;
208 vector<string> preprocessed_files_;
209 set<string> import_paths_;
210 vector<string> expected_stderr_;
211 AidlTypenames typenames_;
212 };
213
TEST_F(AidlTest,AcceptMissingPackage)214 TEST_F(AidlTest, AcceptMissingPackage) {
215 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, Options::Language::JAVA));
216 typenames_.Reset();
217 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, Options::Language::CPP));
218 }
219
TEST_F(AidlTest,EndsInSingleLineComment)220 TEST_F(AidlTest, EndsInSingleLineComment) {
221 EXPECT_NE(nullptr,
222 Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, Options::Language::JAVA));
223 typenames_.Reset();
224 EXPECT_NE(nullptr,
225 Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, Options::Language::CPP));
226 }
227
TEST_F(AidlTest,RejectsArraysOfBinders)228 TEST_F(AidlTest, RejectsArraysOfBinders) {
229 import_paths_.emplace("");
230 io_delegate_.SetFileContents("bar/IBar.aidl",
231 "package bar; interface IBar {}");
232 string path = "foo/IFoo.aidl";
233 string contents = "package foo;\n"
234 "import bar.IBar;\n"
235 "interface IFoo { void f(in IBar[] input); }";
236 EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::JAVA));
237 typenames_.Reset();
238 EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::CPP));
239 }
240
TEST_F(AidlTest,SupportOnlyOutParameters)241 TEST_F(AidlTest, SupportOnlyOutParameters) {
242 string interface_list = "package a; interface IBar { void f(out List bar); }";
243 string interface_ibinder = "package a; interface IBaz { void f(out IBinder bar); }";
244 // List without type isn't supported in cpp.
245 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, Options::Language::CPP));
246 typenames_.Reset();
247 EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, Options::Language::JAVA));
248 typenames_.Reset();
249 EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, Options::Language::CPP));
250 typenames_.Reset();
251 EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, Options::Language::JAVA));
252 }
253
TEST_F(AidlTest,RejectsOnewayOutParameters)254 TEST_F(AidlTest, RejectsOnewayOutParameters) {
255 string oneway_interface =
256 "package a; oneway interface IFoo { void f(out int bar); }";
257 string oneway_method =
258 "package a; interface IBar { oneway void f(out int bar); }";
259 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, typenames_, Options::Language::CPP));
260 typenames_.Reset();
261 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, typenames_, Options::Language::JAVA));
262 typenames_.Reset();
263 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, Options::Language::CPP));
264 typenames_.Reset();
265 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, Options::Language::JAVA));
266 }
267
TEST_F(AidlTest,RejectsOnewayNonVoidReturn)268 TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
269 string oneway_method = "package a; interface IFoo { oneway int f(); }";
270 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
271 typenames_.Reset();
272 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
273 }
274
TEST_F(AidlTest,RejectsNullablePrimitive)275 TEST_F(AidlTest, RejectsNullablePrimitive) {
276 string oneway_method = "package a; interface IFoo { @nullable int f(); }";
277 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
278 typenames_.Reset();
279 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
280 }
281
TEST_F(AidlTest,RejectsDuplicatedArgumentNames)282 TEST_F(AidlTest, RejectsDuplicatedArgumentNames) {
283 string method = "package a; interface IFoo { void f(int a, int a); }";
284 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::CPP));
285 typenames_.Reset();
286 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::JAVA));
287 }
288
TEST_F(AidlTest,RejectsDuplicatedAnnotationParams)289 TEST_F(AidlTest, RejectsDuplicatedAnnotationParams) {
290 string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }";
291 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::CPP));
292 typenames_.Reset();
293 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::JAVA));
294 }
295
TEST_F(AidlTest,ParsesNullableAnnotation)296 TEST_F(AidlTest, ParsesNullableAnnotation) {
297 for (auto is_nullable: {true, false}) {
298 auto parse_result = Parse("a/IFoo.aidl",
299 StringPrintf("package a; interface IFoo {%s String f(); }",
300 (is_nullable) ? "@nullable" : ""),
301 typenames_, Options::Language::CPP);
302 ASSERT_NE(nullptr, parse_result);
303 const AidlInterface* interface = parse_result->AsInterface();
304 ASSERT_NE(nullptr, interface);
305 ASSERT_FALSE(interface->GetMethods().empty());
306 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsNullable(), is_nullable);
307 typenames_.Reset();
308 }
309 }
310
TEST_F(AidlTest,ParsesUtf8Annotations)311 TEST_F(AidlTest, ParsesUtf8Annotations) {
312 for (auto is_utf8: {true, false}) {
313 auto parse_result = Parse(
314 "a/IFoo.aidl",
315 StringPrintf("package a; interface IFoo {%s String f(); }", (is_utf8) ? "@utf8InCpp" : ""),
316 typenames_, Options::Language::CPP);
317 ASSERT_NE(nullptr, parse_result);
318 const AidlInterface* interface = parse_result->AsInterface();
319 ASSERT_NE(nullptr, interface);
320 ASSERT_FALSE(interface->GetMethods().empty());
321 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsUtf8InCpp(), is_utf8);
322 typenames_.Reset();
323 }
324 }
325
TEST_F(AidlTest,VintfRequiresStructuredAndStability)326 TEST_F(AidlTest, VintfRequiresStructuredAndStability) {
327 AidlError error;
328 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
329 Options::Language::CPP, &error);
330 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
331 ASSERT_EQ(nullptr, parse_result);
332 }
333
TEST_F(AidlTest,VintfRequiresStructured)334 TEST_F(AidlTest, VintfRequiresStructured) {
335 AidlError error;
336 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
337 Options::Language::CPP, &error, {"--stability", "vintf"});
338 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
339 ASSERT_EQ(nullptr, parse_result);
340 }
341
TEST_F(AidlTest,VintfRequiresSpecifiedStability)342 TEST_F(AidlTest, VintfRequiresSpecifiedStability) {
343 AidlError error;
344 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
345 Options::Language::CPP, &error, {"--structured"});
346 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
347 ASSERT_EQ(nullptr, parse_result);
348 }
349
TEST_F(AidlTest,ParsesStabilityAnnotations)350 TEST_F(AidlTest, ParsesStabilityAnnotations) {
351 AidlError error;
352 auto parse_result =
353 Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, Options::Language::CPP,
354 &error, {"--structured", "--stability", "vintf"});
355 ASSERT_EQ(AidlError::OK, error);
356 ASSERT_NE(nullptr, parse_result);
357 const AidlInterface* interface = parse_result->AsInterface();
358 ASSERT_NE(nullptr, interface);
359 ASSERT_TRUE(interface->IsVintfStability());
360 typenames_.Reset();
361 }
362
TEST_F(AidlTest,ParsesJavaOnlyStableParcelable)363 TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) {
364 Options java_options = Options::From("aidl -o out --structured a/Foo.aidl");
365 Options cpp_options = Options::From("aidl --lang=cpp -o out -h out/include a/Foo.aidl");
366 Options cpp_structured_options =
367 Options::From("aidl --lang=cpp --structured -o out -h out/include a/Foo.aidl");
368 io_delegate_.SetFileContents(
369 "a/Foo.aidl",
370 StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo cpp_header \"Foo.h\" ;"));
371
372 EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_));
373 EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_));
374 AddExpectedStderr(
375 "ERROR: a/Foo.aidl:1.48-52: Cannot declared parcelable in a --structured interface. "
376 "Parcelable must be defined in AIDL directly.\n");
377 EXPECT_NE(0, ::android::aidl::compile_aidl(cpp_structured_options, io_delegate_));
378 }
379
TEST_F(AidlTest,AcceptsOneway)380 TEST_F(AidlTest, AcceptsOneway) {
381 string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
382 string oneway_interface =
383 "package a; oneway interface IBar { void f(int a); }";
384 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
385 typenames_.Reset();
386 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
387 typenames_.Reset();
388 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, Options::Language::CPP));
389 typenames_.Reset();
390 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, Options::Language::JAVA));
391 }
392
TEST_F(AidlTest,AcceptsAnnotatedOnewayMethod)393 TEST_F(AidlTest, AcceptsAnnotatedOnewayMethod) {
394 string oneway_method = "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }";
395 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
396 typenames_.Reset();
397 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
398 }
399
TEST_F(AidlTest,WritesComments)400 TEST_F(AidlTest, WritesComments) {
401 string foo_interface =
402 "package a; /* foo */ interface IFoo {"
403 " /* i */ int i();"
404 " /* j */ @nullable String j();"
405 " /* k */ @UnsupportedAppUsage oneway void k(int a); }";
406
407 auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, Options::Language::JAVA);
408 EXPECT_NE(nullptr, parse_result);
409 EXPECT_EQ("/* foo */", parse_result->GetComments());
410
411 const AidlInterface* interface = parse_result->AsInterface();
412 EXPECT_EQ("/* i */", interface->GetMethods()[0]->GetComments());
413 EXPECT_EQ("/* j */", interface->GetMethods()[1]->GetComments());
414 EXPECT_EQ("/* k */", interface->GetMethods()[2]->GetComments());
415 }
416
TEST_F(AidlTest,ParsesPreprocessedFile)417 TEST_F(AidlTest, ParsesPreprocessedFile) {
418 string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
419 io_delegate_.SetFileContents("path", simple_content);
420 EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").second);
421 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
422 EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").second);
423 EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").second);
424 }
425
TEST_F(AidlTest,ParsesPreprocessedFileWithWhitespace)426 TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
427 string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
428 io_delegate_.SetFileContents("path", simple_content);
429
430 EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").second);
431 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
432 EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").second);
433 EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").second);
434 }
435
TEST_F(AidlTest,PreferImportToPreprocessed)436 TEST_F(AidlTest, PreferImportToPreprocessed) {
437 io_delegate_.SetFileContents("preprocessed", "interface another.IBar;");
438 io_delegate_.SetFileContents("one/IBar.aidl", "package one; "
439 "interface IBar {}");
440 preprocessed_files_.push_back("preprocessed");
441 import_paths_.emplace("");
442 auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
443 typenames_, Options::Language::JAVA);
444 EXPECT_NE(nullptr, parse_result);
445
446 // We expect to know about both kinds of IBar
447 EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").second);
448 EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").second);
449 // But if we request just "IBar" we should get our imported one.
450 AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
451 ambiguous_type.Resolve(typenames_);
452 EXPECT_EQ("one.IBar", ambiguous_type.GetName());
453 }
454
455 // Special case of PreferImportToPreprocessed. Imported type should be preferred
456 // even when the preprocessed file already has the same type.
TEST_F(AidlTest,B147918827)457 TEST_F(AidlTest, B147918827) {
458 io_delegate_.SetFileContents("preprocessed", "interface another.IBar;\ninterface one.IBar;");
459 io_delegate_.SetFileContents("one/IBar.aidl",
460 "package one; "
461 "interface IBar {}");
462 preprocessed_files_.push_back("preprocessed");
463 import_paths_.emplace("");
464 auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
465 typenames_, Options::Language::JAVA);
466 EXPECT_NE(nullptr, parse_result);
467
468 // We expect to know about both kinds of IBar
469 EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").second);
470 EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").second);
471 // But if we request just "IBar" we should get our imported one.
472 AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
473 ambiguous_type.Resolve(typenames_);
474 EXPECT_EQ("one.IBar", ambiguous_type.GetName());
475 }
476
TEST_F(AidlTest,WritePreprocessedFile)477 TEST_F(AidlTest, WritePreprocessedFile) {
478 io_delegate_.SetFileContents("p/Outer.aidl",
479 "package p; parcelable Outer.Inner;");
480 io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;"
481 "interface IBar {}");
482
483 vector<string> args {
484 "aidl",
485 "--preprocess",
486 "preprocessed",
487 "p/Outer.aidl",
488 "one/IBar.aidl"};
489 Options options = Options::From(args);
490 EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_));
491
492 string output;
493 EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output));
494 EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output);
495 }
496
TEST_F(AidlTest,JavaParcelableOutput)497 TEST_F(AidlTest, JavaParcelableOutput) {
498 io_delegate_.SetFileContents(
499 "Rect.aidl",
500 "@Hide\n"
501 "parcelable Rect {\n"
502 " // Comment\n"
503 " @Hide\n"
504 " int x=5;\n"
505 " @Hide\n"
506 " @UnsupportedAppUsage(maxTargetSdk = 28, trackingBug = 42, implicitMember = \"dummy\", "
507 "expectedSignature = \"dummy\", publicAlternatives = \"d\" \n + \"u\" + \n \"m\" \n + \"m\" "
508 "+ \"y\")\n"
509 " int y;\n"
510 " ParcelFileDescriptor fd;\n"
511 "}");
512
513 vector<string> args{"aidl", "Rect.aidl"};
514 Options options = Options::From(args);
515 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
516
517 string output;
518 EXPECT_TRUE(io_delegate_.GetWrittenContents("Rect.java", &output));
519 EXPECT_EQ(kExpectedJavaParcelableOutputContests, output);
520 }
521
TEST_F(AidlTest,RequireOuterClass)522 TEST_F(AidlTest, RequireOuterClass) {
523 io_delegate_.SetFileContents("p/Outer.aidl",
524 "package p; parcelable Outer.Inner;");
525 import_paths_.emplace("");
526 auto parse_result =
527 Parse("p/IFoo.aidl", "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
528 typenames_, Options::Language::JAVA);
529 EXPECT_EQ(nullptr, parse_result);
530 }
531
TEST_F(AidlTest,ParseCompoundParcelableFromPreprocess)532 TEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) {
533 io_delegate_.SetFileContents("preprocessed",
534 "parcelable p.Outer.Inner;");
535 preprocessed_files_.push_back("preprocessed");
536 auto parse_result = Parse("p/IFoo.aidl", "package p; interface IFoo { void f(in Inner c); }",
537 typenames_, Options::Language::JAVA);
538 // TODO(wiley): This should actually return nullptr because we require
539 // the outer class name. However, for legacy reasons,
540 // this behavior must be maintained. b/17415692
541 EXPECT_NE(nullptr, parse_result);
542 }
543
TEST_F(AidlTest,FailOnParcelable)544 TEST_F(AidlTest, FailOnParcelable) {
545 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;");
546
547 // By default, we shouldn't fail on parcelable.
548 Options options1 = Options::From("aidl p/IFoo.aidl");
549 EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_));
550
551 // -b considers this an error
552 Options options2 = Options::From("aidl -b p/IFoo.aidl");
553 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
554
555 io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}");
556
557 // With '-b' option, a parcelable and an interface should fail.
558 Options options3 = Options::From("aidl p/IBar.aidl");
559 EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_));
560 Options options4 = Options::From("aidl -b p/IBar.aidl");
561 EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_));
562 }
563
TEST_F(AidlTest,StructuredFailOnUnstructuredParcelable)564 TEST_F(AidlTest, StructuredFailOnUnstructuredParcelable) {
565 io_delegate_.SetFileContents("o/WhoKnowsWhat.aidl", "package o; parcelable WhoKnowsWhat;");
566 import_paths_.emplace("");
567 AidlError reported_error;
568 auto parse_result =
569 Parse("p/IFoo.aidl",
570 "package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }",
571 typenames_, Options::Language::JAVA, &reported_error, {"--structured"});
572 EXPECT_EQ(nullptr, parse_result);
573 EXPECT_EQ(AidlError::NOT_STRUCTURED, reported_error);
574 }
575
TEST_F(AidlTest,FailOnDuplicateConstantNames)576 TEST_F(AidlTest, FailOnDuplicateConstantNames) {
577 AidlError reported_error;
578 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
579 R"(package p;
580 interface IFoo {
581 const String DUPLICATED = "d";
582 const int DUPLICATED = 1;
583 }
584 )",
585 typenames_, Options::Language::CPP, &reported_error));
586 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
587 }
588
TEST_F(AidlTest,FailOnManyDefinedTypes)589 TEST_F(AidlTest, FailOnManyDefinedTypes) {
590 AidlError reported_error;
591 AddExpectedStderr("ERROR: p/IFoo.aidl: You must declare only one type per a file.\n");
592 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
593 R"(package p;
594 interface IFoo {}
595 parcelable IBar {}
596 parcelable StructuredParcelable {}
597 interface IBaz {}
598 )",
599 typenames_, Options::Language::CPP, &reported_error));
600 // Parse success is important for clear error handling even if the cases aren't
601 // actually supported in code generation.
602 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
603 }
604
TEST_F(AidlTest,FailOnNoDefinedTypes)605 TEST_F(AidlTest, FailOnNoDefinedTypes) {
606 AidlError reported_error;
607 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", typenames_, Options::Language::CPP,
608 &reported_error));
609 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
610 }
611
TEST_F(AidlTest,FailOnMalformedConstHexValue)612 TEST_F(AidlTest, FailOnMalformedConstHexValue) {
613 AidlError reported_error;
614 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
615 R"(package p;
616 interface IFoo {
617 const int BAD_HEX_VALUE = 0xffffffffffffffffff;
618 }
619 )",
620 typenames_, Options::Language::CPP, &reported_error));
621 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
622 }
623
TEST_F(AidlTest,ParsePositiveConstHexValue)624 TEST_F(AidlTest, ParsePositiveConstHexValue) {
625 AidlError reported_error;
626 auto cpp_parse_result = Parse("p/IFoo.aidl",
627 R"(package p;
628 interface IFoo {
629 const int POSITIVE_HEX_VALUE = 0xf5;
630 }
631 )",
632 typenames_, Options::Language::CPP, &reported_error);
633 EXPECT_NE(nullptr, cpp_parse_result);
634 const AidlInterface* interface = cpp_parse_result->AsInterface();
635 ASSERT_NE(nullptr, interface);
636 const auto& cpp_constants = interface->GetConstantDeclarations();
637 EXPECT_EQ((size_t)1, cpp_constants.size());
638 EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_constants[0]->GetName());
639 EXPECT_TRUE(cpp_constants[0]->CheckValid(typenames_));
640 EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
641 }
642
TEST_F(AidlTest,ParseNegativeConstHexValue)643 TEST_F(AidlTest, ParseNegativeConstHexValue) {
644 AidlError reported_error;
645 auto cpp_parse_result = Parse("p/IFoo.aidl",
646 R"(package p;
647 interface IFoo {
648 const int NEGATIVE_HEX_VALUE = 0xffffffff;
649 }
650 )",
651 typenames_, Options::Language::CPP, &reported_error);
652 ASSERT_NE(nullptr, cpp_parse_result);
653 const AidlInterface* interface = cpp_parse_result->AsInterface();
654 ASSERT_NE(nullptr, interface);
655 const auto& cpp_constants = interface->GetConstantDeclarations();
656 EXPECT_EQ((size_t)1, cpp_constants.size());
657 EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_constants[0]->GetName());
658 EXPECT_EQ(true, cpp_constants[0]->CheckValid(typenames_));
659 EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
660 }
661
TEST_F(AidlTest,UnderstandsNestedParcelables)662 TEST_F(AidlTest, UnderstandsNestedParcelables) {
663 io_delegate_.SetFileContents(
664 "p/Outer.aidl",
665 "package p; parcelable Outer.Inner cpp_header \"baz/header\";");
666 import_paths_.emplace("");
667 const string input_path = "p/IFoo.aidl";
668 const string input = "package p; import p.Outer; interface IFoo"
669 " { Outer.Inner get(); }";
670
671 auto cpp_parse_result = Parse(input_path, input, typenames_, Options::Language::CPP);
672 EXPECT_NE(nullptr, cpp_parse_result);
673
674 auto pair = typenames_.ResolveTypename("p.Outer.Inner");
675 EXPECT_TRUE(pair.second);
676 // C++ uses "::" instead of "." to refer to a inner class.
677 AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, "");
678 EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_));
679 }
680
TEST_F(AidlTest,UnderstandsNativeParcelables)681 TEST_F(AidlTest, UnderstandsNativeParcelables) {
682 io_delegate_.SetFileContents(
683 "p/Bar.aidl",
684 "package p; parcelable Bar cpp_header \"baz/header\";");
685 import_paths_.emplace("");
686 const string input_path = "p/IFoo.aidl";
687 const string input = "package p; import p.Bar; interface IFoo { }";
688 {
689 // C++ understands C++ specific stuff
690 auto cpp_parse_result = Parse(input_path, input, typenames_, Options::Language::CPP);
691 EXPECT_NE(nullptr, cpp_parse_result);
692 auto pair = typenames_.ResolveTypename("p.Bar");
693 EXPECT_TRUE(pair.second);
694 AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, "");
695 native_type.Resolve(typenames_);
696 EXPECT_EQ("::p::Bar", cpp::CppNameOf(native_type, typenames_));
697 set<string> headers;
698 cpp::AddHeaders(native_type, typenames_, headers);
699 EXPECT_EQ(1u, headers.size());
700 EXPECT_EQ(1u, headers.count("baz/header"));
701 }
702 typenames_.Reset();
703 {
704 // Java ignores C++ specific stuff
705 auto java_parse_result = Parse(input_path, input, typenames_, Options::Language::JAVA);
706 EXPECT_NE(nullptr, java_parse_result);
707 auto pair = typenames_.ResolveTypename("p.Bar");
708 EXPECT_TRUE(pair.second);
709 AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, "");
710 native_type.Resolve(typenames_);
711 EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type, typenames_));
712 }
713 }
714
TEST_F(AidlTest,WritesCorrectDependencyFile)715 TEST_F(AidlTest, WritesCorrectDependencyFile) {
716 // While the in tree build system always gives us an output file name,
717 // other android tools take advantage of our ability to infer the intended
718 // file name. This test makes sure we handle this correctly.
719 vector<string> args = {
720 "aidl",
721 "-d dep/file/path",
722 "-o place/for/output",
723 "p/IFoo.aidl"};
724 Options options = Options::From(args);
725 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
726 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
727 string actual_dep_file_contents;
728 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
729 EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
730 }
731
TEST_F(AidlTest,WritesCorrectDependencyFileNinja)732 TEST_F(AidlTest, WritesCorrectDependencyFileNinja) {
733 // While the in tree build system always gives us an output file name,
734 // other android tools take advantage of our ability to infer the intended
735 // file name. This test makes sure we handle this correctly.
736 vector<string> args = {
737 "aidl",
738 "-d dep/file/path",
739 "--ninja",
740 "-o place/for/output",
741 "p/IFoo.aidl"};
742 Options options = Options::From(args);
743 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
744 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
745 string actual_dep_file_contents;
746 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
747 EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents);
748 }
749
TEST_F(AidlTest,WritesTrivialDependencyFileForParcelableDeclaration)750 TEST_F(AidlTest, WritesTrivialDependencyFileForParcelableDeclaration) {
751 // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does
752 // this by calling aidl with every .aidl file it finds, then parsing the
753 // generated dependency files. Those that reference .java output files are
754 // for interfaces and those that do not are parcelables. However, for both
755 // parcelables and interfaces, we *must* generate a non-empty dependency file.
756 vector<string> args = {
757 "aidl",
758 "-o place/for/output",
759 "-d dep/file/path",
760 "p/Foo.aidl"};
761 Options options = Options::From(args);
762 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;");
763 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
764 string actual_dep_file_contents;
765 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
766 EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDeclarationDepFileContents);
767 }
768
TEST_F(AidlTest,WritesDependencyFileForStructuredParcelable)769 TEST_F(AidlTest, WritesDependencyFileForStructuredParcelable) {
770 vector<string> args = {
771 "aidl",
772 "--structured",
773 "-o place/for/output",
774 "-d dep/file/path",
775 "p/Foo.aidl"};
776 Options options = Options::From(args);
777 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo {int a;}");
778 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
779 string actual_dep_file_contents;
780 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
781 EXPECT_EQ(actual_dep_file_contents, kExpectedStructuredParcelableDepFileContents);
782 }
783
TEST_F(AidlTest,NoJavaOutputForParcelableDeclaration)784 TEST_F(AidlTest, NoJavaOutputForParcelableDeclaration) {
785 vector<string> args = {
786 "aidl",
787 "--lang=java",
788 "-o place/for/output",
789 "p/Foo.aidl"};
790 Options options = Options::From(args);
791 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;");
792 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
793 string output_file_contents;
794 EXPECT_FALSE(io_delegate_.GetWrittenContents(options.OutputFile(), &output_file_contents));
795 }
796
797 /* not working until type_namespace.h is fixed
798 TEST_F(AidlTest, AcceptsNestedContainerType) {
799 string nested_in_iface = "package a; interface IFoo {\n"
800 " List<int, List<String, bool>> foo(); }";
801 string nested_in_parcelable = "package a; parcelable IData {\n"
802 " List<int, List<String, bool>> foo;}";
803 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, typenames_, Options::Language::JAVA));
804 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, typenames_, Options::Language::CPP));
805 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, typenames_,
806 Options::Language::JAVA)); EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, typenames_,
807 Options::Language::CPP));
808 }
809 */
810
811 // TODO(b/136048684)
TEST_F(AidlTest,PrimitiveList)812 TEST_F(AidlTest, PrimitiveList) {
813 string primitive_interface =
814 "package a; interface IFoo {\n"
815 " List<int> foo(); }";
816 string primitive_parcelable =
817 "package a; parcelable IData {\n"
818 " List<int> foo;}";
819 EXPECT_EQ(nullptr,
820 Parse("a/IFoo.aidl", primitive_interface, typenames_, Options::Language::JAVA));
821 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, typenames_, Options::Language::CPP));
822 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, typenames_, Options::Language::NDK));
823 EXPECT_EQ(nullptr,
824 Parse("a/IFoo.aidl", primitive_parcelable, typenames_, Options::Language::JAVA));
825 EXPECT_EQ(nullptr,
826 Parse("a/IFoo.aidl", primitive_parcelable, typenames_, Options::Language::CPP));
827 EXPECT_EQ(nullptr,
828 Parse("a/IFoo.aidl", primitive_parcelable, typenames_, Options::Language::NDK));
829 }
830
TEST_F(AidlTest,ApiDump)831 TEST_F(AidlTest, ApiDump) {
832 io_delegate_.SetFileContents(
833 "foo/bar/IFoo.aidl",
834 "package foo.bar;\n"
835 "import foo.bar.Data;\n"
836 "// comment @hide\n"
837 "interface IFoo {\n"
838 " /* @hide */\n"
839 " int foo(out int[] a, String b, boolean c, inout List<String> d);\n"
840 " int foo2(@utf8InCpp String x, inout List<String> y);\n"
841 " IFoo foo3(IFoo foo);\n"
842 " Data getData();\n"
843 " // @hide\n"
844 " const int A = 1;\n"
845 " const String STR = \"Hello\";\n"
846 "}\n");
847 io_delegate_.SetFileContents("foo/bar/Data.aidl",
848 "package foo.bar;\n"
849 "import foo.bar.IFoo;\n"
850 "/* @hide*/\n"
851 "parcelable Data {\n"
852 " // @hide\n"
853 " int x = 10;\n"
854 " // @hide\n"
855 " int y;\n"
856 " IFoo foo;\n"
857 " List<IFoo> a;\n"
858 " /*@hide2*/\n"
859 " List<foo.bar.IFoo> b;\n"
860 " // It should be @hide property\n"
861 " @nullable String[] c;\n"
862 "}\n");
863 io_delegate_.SetFileContents("api.aidl", "");
864 vector<string> args = {"aidl", "--dumpapi", "--out=dump", "--include=.",
865 "foo/bar/IFoo.aidl", "foo/bar/Data.aidl"};
866 Options options = Options::From(args);
867 bool result = dump_api(options, io_delegate_);
868 ASSERT_TRUE(result);
869 string actual;
870 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
871 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
872 /* @hide */
873 interface IFoo {
874 /* @hide */
875 int foo(out int[] a, String b, boolean c, inout List<String> d);
876 int foo2(@utf8InCpp String x, inout List<String> y);
877 foo.bar.IFoo foo3(foo.bar.IFoo foo);
878 foo.bar.Data getData();
879 /* @hide */
880 const int A = 1;
881 const String STR = "Hello";
882 }
883 )"));
884
885 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual));
886 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
887 /* @hide */
888 parcelable Data {
889 /* @hide */
890 int x = 10;
891 /* @hide */
892 int y;
893 foo.bar.IFoo foo;
894 List<foo.bar.IFoo> a;
895 List<foo.bar.IFoo> b;
896 /* @hide */
897 @nullable String[] c;
898 }
899 )"));
900 }
901
TEST_F(AidlTest,ApiDumpWithManualIds)902 TEST_F(AidlTest, ApiDumpWithManualIds) {
903 io_delegate_.SetFileContents(
904 "foo/bar/IFoo.aidl",
905 "package foo.bar;\n"
906 "interface IFoo {\n"
907 " int foo() = 1;\n"
908 " int bar() = 2;\n"
909 " int baz() = 10;\n"
910 "}\n");
911
912 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
913 Options options = Options::From(args);
914 bool result = dump_api(options, io_delegate_);
915 ASSERT_TRUE(result);
916 string actual;
917 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
918 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
919 interface IFoo {
920 int foo() = 1;
921 int bar() = 2;
922 int baz() = 10;
923 }
924 )"));
925 }
926
TEST_F(AidlTest,ApiDumpWithManualIdsOnlyOnSomeMethods)927 TEST_F(AidlTest, ApiDumpWithManualIdsOnlyOnSomeMethods) {
928 io_delegate_.SetFileContents(
929 "foo/bar/IFoo.aidl",
930 "package foo.bar;\n"
931 "interface IFoo {\n"
932 " int foo() = 1;\n"
933 " int bar();\n"
934 " int baz() = 10;\n"
935 "}\n");
936
937 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
938 Options options = Options::From(args);
939 EXPECT_FALSE(dump_api(options, io_delegate_));
940 }
941
TEST_F(AidlTest,CheckNumGenericTypeSecifier)942 TEST_F(AidlTest, CheckNumGenericTypeSecifier) {
943 Options options = Options::From("aidl p/IFoo.aidl IFoo.java");
944 io_delegate_.SetFileContents(options.InputFiles().front(),
945 "package p; interface IFoo {"
946 "void foo(List<String, String> a);}");
947 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
948
949 io_delegate_.SetFileContents(options.InputFiles().front(),
950 "package p; interface IFoo {"
951 "void foo(Map<String> a);}");
952 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
953
954 Options options2 = Options::From("aidl p/Data.aidl Data.java");
955 io_delegate_.SetFileContents(options2.InputFiles().front(),
956 "package p; parcelable Data {"
957 "List<String, String> foo;}");
958 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
959
960 io_delegate_.SetFileContents(options2.InputFiles().front(),
961 "package p; parcelable Data {"
962 "Map<String> foo;}");
963 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
964 }
965
TEST_F(AidlTest,CheckTypeParameterInMapType)966 TEST_F(AidlTest, CheckTypeParameterInMapType) {
967 Options options = Options::From("aidl -I p p/IFoo.aidl");
968 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar { String s; }");
969
970 io_delegate_.SetFileContents("p/IFoo.aidl",
971 "package p; interface IFoo {"
972 "Map<String, Bar> foo();}");
973 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
974
975 io_delegate_.SetFileContents("p/IFoo.aidl",
976 "package p; interface IFoo {"
977 "Map<Bar, Bar> foo();}");
978 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
979
980 io_delegate_.SetFileContents("p/IFoo.aidl",
981 "package p; interface IFoo {"
982 "Map<String, String> foo();}");
983 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
984
985 io_delegate_.SetFileContents("p/IFoo.aidl",
986 "package p; interface IFoo {"
987 "Map<String, ParcelFileDescriptor> foo();}");
988 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
989 }
990
TEST_F(AidlTest,WrongGenericType)991 TEST_F(AidlTest, WrongGenericType) {
992 Options options = Options::From("aidl p/IFoo.aidl IFoo.java");
993 io_delegate_.SetFileContents(options.InputFiles().front(),
994 "package p; interface IFoo {"
995 "String<String> foo(); }");
996 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
997 }
998
TEST_F(AidlTest,UserDefinedUnstructuredGenericParcelableType)999 TEST_F(AidlTest, UserDefinedUnstructuredGenericParcelableType) {
1000 Options optionsForParcelable = Options::From("aidl -I p p/Bar.aidl");
1001 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, T>;");
1002 EXPECT_NE(0, ::android::aidl::compile_aidl(optionsForParcelable, io_delegate_));
1003
1004 Options options = Options::From("aidl -I p p/IFoo.aidl");
1005 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar;");
1006 io_delegate_.SetFileContents("p/IFoo.aidl",
1007 "package p; interface IFoo {"
1008 "Bar<String, String> foo();}");
1009 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1010 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T>;");
1011 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1012 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, V>;");
1013 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1014 io_delegate_.SetFileContents("p/IFoo.aidl",
1015 "package p; interface IFoo {"
1016 "Bar<String, ParcelFileDescriptor> foo();}");
1017 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1018
1019 io_delegate_.SetFileContents("p/IFoo.aidl",
1020 "package p; interface IFoo {"
1021 "Bar<int, long> foo();}");
1022
1023 io_delegate_.SetFileContents("p/IFoo.aidl",
1024 "package p; interface IFoo {"
1025 "Bar<int[], long[]> foo();}");
1026
1027 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1028 }
1029
TEST_F(AidlTest,FailOnMultipleTypesInSingleFile)1030 TEST_F(AidlTest, FailOnMultipleTypesInSingleFile) {
1031 std::vector<std::string> rawOptions{"aidl --lang=java -o out foo/bar/Foo.aidl",
1032 "aidl --lang=cpp -o out -h out/include foo/bar/Foo.aidl"};
1033 for (auto& rawOption : rawOptions) {
1034 Options options = Options::From(rawOption);
1035 io_delegate_.SetFileContents(options.InputFiles().front(),
1036 "package foo.bar;\n"
1037 "interface IFoo1 { int foo(); }\n"
1038 "interface IFoo2 { int foo(); }\n"
1039 "parcelable Data1 { int a; int b;}\n"
1040 "parcelable Data2 { int a; int b;}\n");
1041
1042 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1043
1044 io_delegate_.SetFileContents(options.InputFiles().front(),
1045 "package foo.bar;\n"
1046 "interface IFoo1 { int foo(); }\n"
1047 "interface IFoo2 { int foo(); }\n");
1048
1049 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1050
1051 io_delegate_.SetFileContents(options.InputFiles().front(),
1052 "package foo.bar;\n"
1053 "parcelable Data1 { int a; int b;}\n"
1054 "parcelable Data2 { int a; int b;}\n");
1055
1056 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1057 }
1058 }
1059
TEST_F(AidlTest,MultipleInputFiles)1060 TEST_F(AidlTest, MultipleInputFiles) {
1061 Options options = Options::From(
1062 "aidl --lang=java -o out -I . foo/bar/IFoo.aidl foo/bar/Data.aidl");
1063
1064 io_delegate_.SetFileContents(options.InputFiles().at(0),
1065 "package foo.bar;\n"
1066 "import foo.bar.Data;\n"
1067 "interface IFoo { Data getData(); }\n");
1068
1069 io_delegate_.SetFileContents(options.InputFiles().at(1),
1070 "package foo.bar;\n"
1071 "import foo.bar.IFoo;\n"
1072 "parcelable Data { IFoo foo; }\n");
1073
1074 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1075
1076 string content;
1077 for (const auto file : {
1078 "out/foo/bar/IFoo.java", "out/foo/bar/Data.java"}) {
1079 content.clear();
1080 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
1081 EXPECT_FALSE(content.empty());
1082 }
1083 }
1084
TEST_F(AidlTest,MultipleInputFilesCpp)1085 TEST_F(AidlTest, MultipleInputFilesCpp) {
1086 Options options = Options::From("aidl --lang=cpp -o out -h out/include "
1087 "-I . foo/bar/IFoo.aidl foo/bar/Data.aidl");
1088
1089 io_delegate_.SetFileContents(options.InputFiles().at(0),
1090 "package foo.bar;\n"
1091 "import foo.bar.Data;\n"
1092 "interface IFoo { Data getData(); }\n");
1093
1094 io_delegate_.SetFileContents(options.InputFiles().at(1),
1095 "package foo.bar;\n"
1096 "import foo.bar.IFoo;\n"
1097 "parcelable Data { IFoo foo; }\n");
1098
1099 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1100
1101 string content;
1102 for (const auto file : {
1103 "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp",
1104 "out/include/foo/bar/IFoo.h", "out/include/foo/bar/Data.h",
1105 "out/include/foo/bar/BpFoo.h", "out/include/foo/bar/BpData.h",
1106 "out/include/foo/bar/BnFoo.h", "out/include/foo/bar/BnData.h"}) {
1107 content.clear();
1108 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
1109 EXPECT_FALSE(content.empty());
1110 }
1111 }
1112
TEST_F(AidlTest,ConflictWithMetaTransactions)1113 TEST_F(AidlTest, ConflictWithMetaTransactions) {
1114 Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl");
1115 // int getInterfaceVersion() is one of the meta transactions
1116 io_delegate_.SetFileContents(options.InputFiles().front(),
1117 "package p; interface IFoo {"
1118 "int getInterfaceVersion(); }");
1119 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1120
1121 // boolean getInterfaceVersion() is not, but should be prevented
1122 // because return type is not part of a method signature
1123 io_delegate_.SetFileContents(options.InputFiles().front(),
1124 "package p; interface IFoo {"
1125 "boolean getInterfaceVersion(); }");
1126 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1127
1128 // this is another reserved name
1129 io_delegate_.SetFileContents(options.InputFiles().front(),
1130 "package p; interface IFoo {"
1131 "String getTransactionName(int code); }");
1132 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1133
1134 // this is not a meta interface method as it differs type arguments
1135 io_delegate_.SetFileContents(options.InputFiles().front(),
1136 "package p; interface IFoo {"
1137 "String getTransactionName(); }");
1138 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1139 }
1140
TEST_F(AidlTest,DifferentOrderAnnotationsInCheckAPI)1141 TEST_F(AidlTest, DifferentOrderAnnotationsInCheckAPI) {
1142 Options options = Options::From("aidl --checkapi old new");
1143 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1144 "package p; interface IFoo{ @utf8InCpp @nullable String foo();}");
1145 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1146 "package p; interface IFoo{ @nullable @utf8InCpp String foo();}");
1147
1148 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1149 }
1150
TEST_F(AidlTest,SuccessOnIdenticalApiDumps)1151 TEST_F(AidlTest, SuccessOnIdenticalApiDumps) {
1152 Options options = Options::From("aidl --checkapi old new");
1153 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
1154 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
1155
1156 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1157 }
1158
1159 class AidlTestCompatibleChanges : public AidlTest {
1160 protected:
1161 Options options_ = Options::From("aidl --checkapi old new");
1162 };
1163
TEST_F(AidlTestCompatibleChanges,NewType)1164 TEST_F(AidlTestCompatibleChanges, NewType) {
1165 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1166 "package p;"
1167 "interface IFoo {"
1168 " void foo(int a);"
1169 "}");
1170 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1171 "package p;"
1172 "interface IFoo {"
1173 " void foo(int a);"
1174 "}");
1175 io_delegate_.SetFileContents("new/p/IBar.aidl",
1176 "package p;"
1177 "interface IBar {"
1178 " void bar();"
1179 "}");
1180 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1181 }
1182
TEST_F(AidlTestCompatibleChanges,NewMethod)1183 TEST_F(AidlTestCompatibleChanges, NewMethod) {
1184 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1185 "package p;"
1186 "interface IFoo {"
1187 " void foo(int a);"
1188 "}");
1189 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1190 "package p;"
1191 "interface IFoo {"
1192 " void foo(int a);"
1193 " void bar();"
1194 "}");
1195 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1196 }
1197
TEST_F(AidlTestCompatibleChanges,NewField)1198 TEST_F(AidlTestCompatibleChanges, NewField) {
1199 io_delegate_.SetFileContents("old/p/Data.aidl",
1200 "package p;"
1201 "parcelable Data {"
1202 " int foo;"
1203 "}");
1204 io_delegate_.SetFileContents("new/p/Data.aidl",
1205 "package p;"
1206 "parcelable Data {"
1207 " int foo;"
1208 " int bar;"
1209 "}");
1210 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1211 }
1212
TEST_F(AidlTestCompatibleChanges,NewEnumerator)1213 TEST_F(AidlTestCompatibleChanges, NewEnumerator) {
1214 io_delegate_.SetFileContents("old/p/Enum.aidl",
1215 "package p;"
1216 "enum Enum {"
1217 " FOO = 1,"
1218 "}");
1219 io_delegate_.SetFileContents("new/p/Enum.aidl",
1220 "package p;"
1221 "enum Enum {"
1222 " FOO = 1,"
1223 " BAR = 2,"
1224 "}");
1225 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1226 }
1227
TEST_F(AidlTestCompatibleChanges,ReorderedEnumerator)1228 TEST_F(AidlTestCompatibleChanges, ReorderedEnumerator) {
1229 io_delegate_.SetFileContents("old/p/Enum.aidl",
1230 "package p;"
1231 "enum Enum {"
1232 " FOO = 1,"
1233 " BAR = 2,"
1234 "}");
1235 io_delegate_.SetFileContents("new/p/Enum.aidl",
1236 "package p;"
1237 "enum Enum {"
1238 " BAR = 2,"
1239 " FOO = 1,"
1240 "}");
1241 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1242 }
1243
TEST_F(AidlTestCompatibleChanges,NewPackage)1244 TEST_F(AidlTestCompatibleChanges, NewPackage) {
1245 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1246 "package p;"
1247 "interface IFoo {"
1248 " void foo(int a);"
1249 "}");
1250 io_delegate_.SetFileContents("old/p/Data.aidl",
1251 "package p;"
1252 "parcelable Data {"
1253 " int foo;"
1254 "}");
1255 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1256 "package p;"
1257 "interface IFoo {"
1258 " void foo(int a);"
1259 "}");
1260 io_delegate_.SetFileContents("new/p/Data.aidl",
1261 "package p;"
1262 "parcelable Data {"
1263 " int foo;"
1264 "}");
1265 io_delegate_.SetFileContents("new/q/IFoo.aidl",
1266 "package q;"
1267 "interface IFoo {"
1268 " void foo(int a);"
1269 "}");
1270 io_delegate_.SetFileContents("new/q/Data.aidl",
1271 "package q;"
1272 "parcelable Data {"
1273 " int foo;"
1274 "}");
1275 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1276 }
1277
TEST_F(AidlTestCompatibleChanges,ArgNameChange)1278 TEST_F(AidlTestCompatibleChanges, ArgNameChange) {
1279 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1280 "package p;"
1281 "interface IFoo {"
1282 " void foo(int a);"
1283 "}");
1284 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1285 "package p;"
1286 "interface IFoo {"
1287 " void foo(int b);"
1288 "}");
1289 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1290 }
1291
TEST_F(AidlTestCompatibleChanges,AddedConstValue)1292 TEST_F(AidlTestCompatibleChanges, AddedConstValue) {
1293 io_delegate_.SetFileContents("old/p/I.aidl",
1294 "package p; interface I {"
1295 "const int A = 1; }");
1296 io_delegate_.SetFileContents("new/p/I.aidl",
1297 "package p ; interface I {"
1298 "const int A = 1; const int B = 2;}");
1299 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1300 }
1301
TEST_F(AidlTestCompatibleChanges,ChangedConstValueOrder)1302 TEST_F(AidlTestCompatibleChanges, ChangedConstValueOrder) {
1303 io_delegate_.SetFileContents("old/p/I.aidl",
1304 "package p; interface I {"
1305 "const int A = 1; const int B = 2;}");
1306 io_delegate_.SetFileContents("new/p/I.aidl",
1307 "package p ; interface I {"
1308 "const int B = 2; const int A = 1;}");
1309 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1310 }
1311
1312 class AidlTestIncompatibleChanges : public AidlTest {
1313 protected:
1314 Options options_ = Options::From("aidl --checkapi old new");
1315 };
1316
TEST_F(AidlTestIncompatibleChanges,RemovedType)1317 TEST_F(AidlTestIncompatibleChanges, RemovedType) {
1318 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1319 "package p;"
1320 "interface IFoo {"
1321 " void foo(in String[] str);"
1322 " void bar(@utf8InCpp String str);"
1323 "}");
1324 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1325 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1326 }
1327
TEST_F(AidlTestIncompatibleChanges,RemovedMethod)1328 TEST_F(AidlTestIncompatibleChanges, RemovedMethod) {
1329 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1330 "package p;"
1331 "interface IFoo {"
1332 " void foo(in String[] str);"
1333 " void bar(@utf8InCpp String str);"
1334 "}");
1335 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1336 "package p;"
1337 "interface IFoo {"
1338 " void foo(in String[] str);"
1339 "}");
1340 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1341 }
1342
TEST_F(AidlTestIncompatibleChanges,RemovedField)1343 TEST_F(AidlTestIncompatibleChanges, RemovedField) {
1344 io_delegate_.SetFileContents("old/p/Data.aidl",
1345 "package p;"
1346 "parcelable Data {"
1347 " int foo;"
1348 " int bar;"
1349 "}");
1350 io_delegate_.SetFileContents("new/p/Data.aidl",
1351 "package p;"
1352 "parcelable Data {"
1353 " int foo;"
1354 "}");
1355 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1356 }
1357
TEST_F(AidlTestIncompatibleChanges,RemovedEnumerator)1358 TEST_F(AidlTestIncompatibleChanges, RemovedEnumerator) {
1359 io_delegate_.SetFileContents("old/p/Enum.aidl",
1360 "package p;"
1361 "enum Enum {"
1362 " FOO = 1,"
1363 " BAR = 2,"
1364 "}");
1365 io_delegate_.SetFileContents("new/p/Enum.aidl",
1366 "package p;"
1367 "enum Enum {"
1368 " BAR = 2,"
1369 "}");
1370 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1371 }
1372
TEST_F(AidlTestIncompatibleChanges,RenamedMethod)1373 TEST_F(AidlTestIncompatibleChanges, RenamedMethod) {
1374 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1375 "package p;"
1376 "interface IFoo {"
1377 " void foo(in String[] str);"
1378 " void bar(@utf8InCpp String str);"
1379 "}");
1380 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1381 "package p;"
1382 "interface IFoo {"
1383 " void foo(in String[] str);"
1384 " void bar2(@utf8InCpp String str);"
1385 "}");
1386 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1387 }
1388
TEST_F(AidlTestIncompatibleChanges,RenamedType)1389 TEST_F(AidlTestIncompatibleChanges, RenamedType) {
1390 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1391 "package p;"
1392 "interface IFoo {"
1393 " void foo(in String[] str);"
1394 " void bar(@utf8InCpp String str);"
1395 "}");
1396 io_delegate_.SetFileContents("new/p/IFoo2.aidl",
1397 "package p;"
1398 "interface IFoo2 {"
1399 " void foo(in String[] str);"
1400 " void bar(@utf8InCpp String str);"
1401 "}");
1402 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1403 }
1404
TEST_F(AidlTestIncompatibleChanges,ChangedEnumerator)1405 TEST_F(AidlTestIncompatibleChanges, ChangedEnumerator) {
1406 io_delegate_.SetFileContents("old/p/Enum.aidl",
1407 "package p;"
1408 "enum Enum {"
1409 " FOO = 1,"
1410 " BAR = 2,"
1411 "}");
1412 io_delegate_.SetFileContents("new/p/Enum.aidl",
1413 "package p;"
1414 "enum Enum {"
1415 " FOO = 3,"
1416 " BAR = 2,"
1417 "}");
1418 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1419 }
1420
TEST_F(AidlTestIncompatibleChanges,ReorderedMethod)1421 TEST_F(AidlTestIncompatibleChanges, ReorderedMethod) {
1422 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1423 "package p;"
1424 "interface IFoo {"
1425 " void foo(in String[] str);"
1426 " void bar(@utf8InCpp String str);"
1427 "}");
1428 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1429 "package p;"
1430 "interface IFoo {"
1431 " void bar(@utf8InCpp String str);"
1432 " void foo(in String[] str);"
1433 "}");
1434 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1435 }
1436
TEST_F(AidlTestIncompatibleChanges,ReorderedField)1437 TEST_F(AidlTestIncompatibleChanges, ReorderedField) {
1438 io_delegate_.SetFileContents("old/p/Data.aidl",
1439 "package p;"
1440 "parcelable Data {"
1441 " int foo;"
1442 " int bar;"
1443 "}");
1444 io_delegate_.SetFileContents("new/p/Data.aidl",
1445 "package p;"
1446 "parcelable Data {"
1447 " int bar;"
1448 " int foo;"
1449 "}");
1450 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1451 }
1452
TEST_F(AidlTestIncompatibleChanges,ChangedDirectionSpecifier)1453 TEST_F(AidlTestIncompatibleChanges, ChangedDirectionSpecifier) {
1454 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1455 "package p;"
1456 "interface IFoo {"
1457 " void foo(in String[] str);"
1458 " void bar(@utf8InCpp String str);"
1459 "}");
1460 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1461 "package p;"
1462 "interface IFoo {"
1463 " void foo(out String[] str);"
1464 " void bar(@utf8InCpp String str);"
1465 "}");
1466 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1467 }
1468
TEST_F(AidlTestIncompatibleChanges,AddedAnnotation)1469 TEST_F(AidlTestIncompatibleChanges, AddedAnnotation) {
1470 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1471 "package p;"
1472 "interface IFoo {"
1473 " void foo(in String[] str);"
1474 " void bar(@utf8InCpp String str);"
1475 "}");
1476 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1477 "package p;"
1478 "interface IFoo {"
1479 " void foo(in @utf8InCpp String[] str);"
1480 " void bar(@utf8InCpp String str);"
1481 "}");
1482 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1483 }
1484
TEST_F(AidlTestIncompatibleChanges,RemovedAnnotation)1485 TEST_F(AidlTestIncompatibleChanges, RemovedAnnotation) {
1486 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1487 "package p;"
1488 "interface IFoo {"
1489 " void foo(in String[] str);"
1490 " void bar(@utf8InCpp String str);"
1491 "}");
1492 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1493 "package p;"
1494 "interface IFoo {"
1495 " void foo(in String[] str);"
1496 " void bar(String str);"
1497 "}");
1498 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1499 }
1500
TEST_F(AidlTestIncompatibleChanges,RemovedPackage)1501 TEST_F(AidlTestIncompatibleChanges, RemovedPackage) {
1502 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}");
1503 io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}");
1504 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}");
1505 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1506 }
1507
TEST_F(AidlTestIncompatibleChanges,ChangedDefaultValue)1508 TEST_F(AidlTestIncompatibleChanges, ChangedDefaultValue) {
1509 io_delegate_.SetFileContents("old/p/D.aidl", "package p; parcelable D { int a = 1; }");
1510 io_delegate_.SetFileContents("new/p/D.aidl", "package p; parcelable D { int a = 2; }");
1511 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1512 }
1513
TEST_F(AidlTestIncompatibleChanges,RemovedConstValue)1514 TEST_F(AidlTestIncompatibleChanges, RemovedConstValue) {
1515 io_delegate_.SetFileContents("old/p/I.aidl",
1516 "package p; interface I {"
1517 "const int A = 1; const int B = 2;}");
1518 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 1; }");
1519 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1520 }
1521
TEST_F(AidlTestIncompatibleChanges,ChangedConstValue)1522 TEST_F(AidlTestIncompatibleChanges, ChangedConstValue) {
1523 io_delegate_.SetFileContents("old/p/I.aidl", "package p; interface I { const int A = 1; }");
1524 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 2; }");
1525 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1526 }
1527
TEST_F(AidlTest,RejectAmbiguousImports)1528 TEST_F(AidlTest, RejectAmbiguousImports) {
1529 Options options = Options::From("aidl --lang=java -o out -I dir1 -I dir2 p/IFoo.aidl");
1530 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; import q.IBar; interface IFoo{}");
1531 io_delegate_.SetFileContents("dir1/q/IBar.aidl", "package q; interface IBar{}");
1532 io_delegate_.SetFileContents("dir2/q/IBar.aidl", "package q; interface IBar{}");
1533
1534 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1535 }
1536
TEST_F(AidlTest,HandleManualIdAssignments)1537 TEST_F(AidlTest, HandleManualIdAssignments) {
1538 Options options = Options::From("aidl --checkapi old new");
1539 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
1540 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
1541
1542 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1543
1544 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 11;}");
1545 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
1546 }
1547
TEST_F(AidlTest,ParcelFileDescriptorIsBuiltinType)1548 TEST_F(AidlTest, ParcelFileDescriptorIsBuiltinType) {
1549 Options javaOptions = Options::From("aidl --lang=java -o out p/IFoo.aidl");
1550 Options cppOptions = Options::From("aidl --lang=cpp -h out -o out p/IFoo.aidl");
1551
1552 // use without import
1553 io_delegate_.SetFileContents("p/IFoo.aidl",
1554 "package p; interface IFoo{ void foo(in ParcelFileDescriptor fd);}");
1555 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1556 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1557
1558 // use without impot but with full name
1559 io_delegate_.SetFileContents(
1560 "p/IFoo.aidl",
1561 "package p; interface IFoo{ void foo(in android.os.ParcelFileDescriptor fd);}");
1562 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1563 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1564
1565 // use with import (as before)
1566 io_delegate_.SetFileContents("p/IFoo.aidl",
1567 "package p;"
1568 "import android.os.ParcelFileDescriptor;"
1569 "interface IFoo{"
1570 " void foo(in ParcelFileDescriptor fd);"
1571 "}");
1572 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1573 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1574 }
1575
TEST_F(AidlTest,ManualIds)1576 TEST_F(AidlTest, ManualIds) {
1577 Options options = Options::From("aidl --lang=java -o out IFoo.aidl");
1578 io_delegate_.SetFileContents("IFoo.aidl",
1579 "interface IFoo {\n"
1580 " void foo() = 0;\n"
1581 " void bar() = 1;\n"
1582 "}");
1583 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1584 }
1585
TEST_F(AidlTest,ManualIdsWithMetaTransactions)1586 TEST_F(AidlTest, ManualIdsWithMetaTransactions) {
1587 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1588 io_delegate_.SetFileContents("IFoo.aidl",
1589 "interface IFoo {\n"
1590 " void foo() = 0;\n"
1591 " void bar() = 1;\n"
1592 "}");
1593 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1594 }
1595
TEST_F(AidlTest,FailOnDuplicatedIds)1596 TEST_F(AidlTest, FailOnDuplicatedIds) {
1597 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1598 io_delegate_.SetFileContents("IFoo.aidl",
1599 "interface IFoo {\n"
1600 " void foo() = 3;\n"
1601 " void bar() = 3;\n"
1602 "}");
1603 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1604 }
1605
TEST_F(AidlTest,FailOnOutOfRangeIds)1606 TEST_F(AidlTest, FailOnOutOfRangeIds) {
1607 // 16777115 is kLastMetaMethodId + 1
1608 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1609 io_delegate_.SetFileContents("IFoo.aidl",
1610 "interface IFoo {\n"
1611 " void foo() = 3;\n"
1612 " void bar() = 16777115;\n"
1613 "}");
1614 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1615 }
1616
TEST_F(AidlTest,FailOnPartiallyAssignedIds)1617 TEST_F(AidlTest, FailOnPartiallyAssignedIds) {
1618 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1619 io_delegate_.SetFileContents("IFoo.aidl",
1620 "interface IFoo {\n"
1621 " void foo() = 3;\n"
1622 " void bar();\n"
1623 "}");
1624 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1625 }
1626
TEST_F(AidlTest,AllowDuplicatedImportPaths)1627 TEST_F(AidlTest, AllowDuplicatedImportPaths) {
1628 Options options = Options::From("aidl --lang=java -I dir -I dir IFoo.aidl");
1629 io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}");
1630 io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}");
1631 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1632 }
1633
TEST_F(AidlTest,FailOnAmbiguousImports)1634 TEST_F(AidlTest, FailOnAmbiguousImports) {
1635 Options options = Options::From("aidl --lang=java -I dir -I dir2 IFoo.aidl");
1636 io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}");
1637 io_delegate_.SetFileContents("dir2/IBar.aidl", "interface IBar{}");
1638 io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}");
1639 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1640 }
1641
1642 class AidlOutputPathTest : public AidlTest {
1643 protected:
SetUp()1644 void SetUp() override {
1645 AidlTest::SetUp();
1646 io_delegate_.SetFileContents("sub/dir/foo/bar/IFoo.aidl", "package foo.bar; interface IFoo {}");
1647 }
1648
Test(const Options & options,const std::string expected_output_path)1649 void Test(const Options& options, const std::string expected_output_path) {
1650 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1651 // check the existence
1652 EXPECT_TRUE(io_delegate_.GetWrittenContents(expected_output_path, nullptr));
1653 }
1654 };
1655
TEST_F(AidlOutputPathTest,OutDirWithNoOutputFile)1656 TEST_F(AidlOutputPathTest, OutDirWithNoOutputFile) {
1657 // <out_dir> / <package_name> / <type_name>.java
1658 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl"), "out/foo/bar/IFoo.java");
1659 }
1660
TEST_F(AidlOutputPathTest,OutDirWithOutputFile)1661 TEST_F(AidlOutputPathTest, OutDirWithOutputFile) {
1662 // when output file is explicitly set, it is always respected. -o option is
1663 // ignored.
1664 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java");
1665 }
1666
TEST_F(AidlOutputPathTest,NoOutDirWithOutputFile)1667 TEST_F(AidlOutputPathTest, NoOutDirWithOutputFile) {
1668 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java");
1669 }
1670
TEST_F(AidlOutputPathTest,NoOutDirWithNoOutputFile)1671 TEST_F(AidlOutputPathTest, NoOutDirWithNoOutputFile) {
1672 // output is the same as the input file except for the suffix
1673 Test(Options::From("aidl sub/dir/foo/bar/IFoo.aidl"), "sub/dir/foo/bar/IFoo.java");
1674 }
1675
TEST_F(AidlTest,FailOnOutOfBoundsInt32MaxConstInt)1676 TEST_F(AidlTest, FailOnOutOfBoundsInt32MaxConstInt) {
1677 AidlError reported_error;
1678 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
1679 R"(package p;
1680 interface IFoo {
1681 const int int32_max_oob = 2147483650;
1682 }
1683 )",
1684 typenames_, Options::Language::CPP, &reported_error));
1685 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
1686 }
1687
TEST_F(AidlTest,FailOnOutOfBoundsInt32MinConstInt)1688 TEST_F(AidlTest, FailOnOutOfBoundsInt32MinConstInt) {
1689 AidlError reported_error;
1690 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
1691 R"(package p;
1692 interface IFoo {
1693 const int int32_min_oob = -2147483650;
1694 }
1695 )",
1696 typenames_, Options::Language::CPP, &reported_error));
1697 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
1698 }
1699
TEST_F(AidlTest,FailOnOutOfBoundsInt64MaxConstInt)1700 TEST_F(AidlTest, FailOnOutOfBoundsInt64MaxConstInt) {
1701 AidlError reported_error;
1702 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
1703 R"(package p;
1704 interface IFoo {
1705 const long int64_max_oob = 21474836509999999999999999;
1706 }
1707 )",
1708 typenames_, Options::Language::CPP, &reported_error));
1709 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
1710 }
1711
TEST_F(AidlTest,FailOnOutOfBoundsInt64MinConstInt)1712 TEST_F(AidlTest, FailOnOutOfBoundsInt64MinConstInt) {
1713 AidlError reported_error;
1714 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
1715 R"(package p;
1716 interface IFoo {
1717 const long int64_min_oob = -21474836509999999999999999;
1718 }
1719 )",
1720 typenames_, Options::Language::CPP, &reported_error));
1721 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
1722 }
1723
TEST_F(AidlTest,FailOnOutOfBoundsAutofilledEnum)1724 TEST_F(AidlTest, FailOnOutOfBoundsAutofilledEnum) {
1725 AidlError reported_error;
1726 EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl",
1727 R"(package p;
1728 @Backing(type="byte")
1729 enum TestEnum {
1730 FOO = 127,
1731 BAR,
1732 }
1733 )",
1734 typenames_, Options::Language::CPP, &reported_error));
1735 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
1736 }
1737
1738 } // namespace aidl
1739 } // namespace android
1740