blob: 68279800d624370fa8ad6c441dd7501be5907fa9 [file] [log] [blame]
Christopher Wiley90be4e32015-10-20 14:55:25 -07001/*
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
Christopher Wiley90be4e32015-10-20 14:55:25 -070017#include <memory>
Christopher Wiley12e894a2016-01-29 11:55:07 -080018#include <set>
19#include <string>
Christopher Wiley90be4e32015-10-20 14:55:25 -070020#include <vector>
21
Christopher Wileyec31a052016-01-25 07:28:51 -080022#include <android-base/stringprintf.h>
Christopher Wiley90be4e32015-10-20 14:55:25 -070023#include <gtest/gtest.h>
24
25#include "aidl.h"
Steven Moreland49585242019-12-18 16:06:49 -080026#include "aidl_checkapi.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070027#include "aidl_language.h"
Steven Moreland860b1942018-08-16 14:59:28 -070028#include "aidl_to_cpp.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090029#include "aidl_to_java.h"
30#include "options.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070031#include "tests/fake_io_delegate.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070032
Jeongik Cha2a5b4d82019-08-06 19:37:59 +090033using android::aidl::internals::parse_preprocessed_file;
Christopher Wiley90be4e32015-10-20 14:55:25 -070034using android::aidl::test::FakeIoDelegate;
Christopher Wileyec31a052016-01-25 07:28:51 -080035using android::base::StringPrintf;
Christopher Wiley12e894a2016-01-29 11:55:07 -080036using std::set;
Christopher Wiley90be4e32015-10-20 14:55:25 -070037using std::string;
38using std::unique_ptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -080039using std::vector;
Jeongik Cha2a5b4d82019-08-06 19:37:59 +090040using testing::internal::CaptureStderr;
41using testing::internal::GetCapturedStderr;
Christopher Wiley90be4e32015-10-20 14:55:25 -070042
43namespace android {
44namespace aidl {
Christopher Wileyf8136192016-04-12 14:19:35 -070045namespace {
46
47const char kExpectedDepFileContents[] =
48R"(place/for/output/p/IFoo.java : \
49 p/IFoo.aidl
50
51p/IFoo.aidl :
52)";
53
Dan Willemsen93298ee2016-11-10 23:55:55 -080054const char kExpectedNinjaDepFileContents[] =
55R"(place/for/output/p/IFoo.java : \
56 p/IFoo.aidl
57)";
58
Jiyong Parkdf202122019-09-30 20:48:35 +090059const char kExpectedParcelableDeclarationDepFileContents[] =
60 R"( : \
61 p/Foo.aidl
62
63p/Foo.aidl :
64)";
65
66const char kExpectedStructuredParcelableDepFileContents[] =
Steven Moreland2a9a7d62019-02-05 16:11:54 -080067 R"(place/for/output/p/Foo.java : \
Christopher Wileyb1bbdf82016-04-21 11:43:45 -070068 p/Foo.aidl
69
70p/Foo.aidl :
71)";
72
Jiyong Parkd4e73c82019-07-24 21:39:15 +090073const char kExpectedJavaParcelableOutputContests[] =
Jeongik Chaa755c2a2018-12-12 16:28:23 +090074 R"(/*
75 * This file is auto-generated. DO NOT MODIFY.
Jeongik Chaa755c2a2018-12-12 16:28:23 +090076 */
Makoto Onuki00be5c72020-03-04 16:57:23 -080077@android.annotation.Hide
Jeongik Chaa755c2a2018-12-12 16:28:23 +090078public class Rect implements android.os.Parcelable
79{
80 // Comment
81
Makoto Onuki00be5c72020-03-04 16:57:23 -080082 @android.annotation.Hide
Jeongik Chaa755c2a2018-12-12 16:28:23 +090083 public int x = 5;
84
Makoto Onuki00be5c72020-03-04 16:57:23 -080085 @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")
Jeongik Chaa755c2a2018-12-12 16:28:23 +090087 public int y;
Jiyong Parkd4e73c82019-07-24 21:39:15 +090088
89 public android.os.ParcelFileDescriptor fd;
Jeongik Chaa755c2a2018-12-12 16:28:23 +090090 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);
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900108 if ((fd!=null)) {
109 _aidl_parcel.writeInt(1);
110 fd.writeToParcel(_aidl_parcel, 0);
111 }
112 else {
113 _aidl_parcel.writeInt(0);
114 }
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900115 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;
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900130 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;
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900137 } finally {
138 _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size);
139 }
140 }
141 @Override public int describeContents()
142 {
Jeongik Cha372a8c82018-12-12 16:31:11 +0900143 return 0;
144 }
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900145}
146)";
147
Christopher Wileyf8136192016-04-12 14:19:35 -0700148} // namespace
Christopher Wiley90be4e32015-10-20 14:55:25 -0700149
150class AidlTest : public ::testing::Test {
151 protected:
Christopher Wiley56799522015-10-31 10:17:04 -0700152 void SetUp() override {
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900153 CaptureStderr();
Christopher Wiley56799522015-10-31 10:17:04 -0700154 }
155
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900156 void TearDown() override {
157 auto actual_stderr = GetCapturedStderr();
Steven Moreland48ae1a42019-10-03 12:29:46 -0700158 std::cerr << actual_stderr << std::endl;
159
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900160 if (expected_stderr_.size() > 0) {
161 EXPECT_EQ(android::base::Join(expected_stderr_, ""), actual_stderr);
162 }
163 }
164
165 void AddExpectedStderr(string expected) { expected_stderr_.push_back(expected); }
166
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900167 AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_,
168 Options::Language lang, AidlError* error = nullptr,
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700169 const vector<string> additional_arguments = {}) {
Christopher Wiley0522cd52015-10-28 15:39:44 -0700170 io_delegate_.SetFileContents(path, contents);
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900171 vector<string> args;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900172 if (lang == Options::Language::CPP) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900173 args.emplace_back("aidl-cpp");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900174 } else {
175 args.emplace_back("aidl");
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900176 }
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700177 for (const string& s : additional_arguments) {
178 args.emplace_back(s);
179 }
180 for (const string& f : preprocessed_files_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900181 args.emplace_back("--preprocessed=" + f);
182 }
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700183 for (const string& i : import_paths_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900184 args.emplace_back("--include=" + i);
185 }
186 args.emplace_back(path);
187 Options options = Options::From(args);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900188 vector<AidlDefinedType*> defined_types;
189 vector<string> imported_files;
Jiyong Park8c380532018-08-30 14:55:26 +0900190 ImportResolver import_resolver{io_delegate_, path, import_paths_, {}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700191 AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900192 path, options, io_delegate_, &typenames_, &defined_types, &imported_files);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900193
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700194 if (error != nullptr) {
195 *error = actual_error;
196 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900197
198 if (actual_error != AidlError::OK) {
199 return nullptr;
200 }
201
202 EXPECT_EQ(1ul, defined_types.size());
203
204 return defined_types.front();
Christopher Wiley90be4e32015-10-20 14:55:25 -0700205 }
Christopher Wiley0522cd52015-10-28 15:39:44 -0700206
207 FakeIoDelegate io_delegate_;
Christopher Wiley41544372015-11-03 14:52:29 -0800208 vector<string> preprocessed_files_;
Jiyong Park8c380532018-08-30 14:55:26 +0900209 set<string> import_paths_;
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900210 vector<string> expected_stderr_;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900211 AidlTypenames typenames_;
Christopher Wiley90be4e32015-10-20 14:55:25 -0700212};
213
Steven Morelandf3da0892018-10-05 14:52:01 -0700214TEST_F(AidlTest, AcceptMissingPackage) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900215 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));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700218}
219
Steven Moreland9c2988f2019-07-17 17:49:10 -0700220TEST_F(AidlTest, EndsInSingleLineComment) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900221 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));
Steven Moreland9c2988f2019-07-17 17:49:10 -0700226}
227
Christopher Wiley0522cd52015-10-28 15:39:44 -0700228TEST_F(AidlTest, RejectsArraysOfBinders) {
Jiyong Park8c380532018-08-30 14:55:26 +0900229 import_paths_.emplace("");
Christopher Wiley0522cd52015-10-28 15:39:44 -0700230 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); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900236 EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::JAVA));
237 typenames_.Reset();
238 EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::CPP));
Christopher Wiley0522cd52015-10-28 15:39:44 -0700239}
240
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900241TEST_F(AidlTest, SupportOnlyOutParameters) {
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900242 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); }";
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900244 // List without type isn't supported in cpp.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900245 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));
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900252}
253
Christopher Wiley90be4e32015-10-20 14:55:25 -0700254TEST_F(AidlTest, RejectsOnewayOutParameters) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700255 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); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900259 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));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700266}
267
268TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700269 string oneway_method = "package a; interface IFoo { oneway int f(); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900270 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));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700273}
274
Casey Dahlin57dbe242015-12-04 11:44:02 -0800275TEST_F(AidlTest, RejectsNullablePrimitive) {
276 string oneway_method = "package a; interface IFoo { @nullable int f(); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900277 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));
Casey Dahlin57dbe242015-12-04 11:44:02 -0800280}
281
Steven Morelandb3cd3c72018-10-11 12:37:45 -0700282TEST_F(AidlTest, RejectsDuplicatedArgumentNames) {
283 string method = "package a; interface IFoo { void f(int a, int a); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900284 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));
Steven Morelandb3cd3c72018-10-11 12:37:45 -0700287}
288
Andrei Onea9445fc62019-06-27 18:11:59 +0100289TEST_F(AidlTest, RejectsDuplicatedAnnotationParams) {
290 string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900291 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));
Andrei Onea9445fc62019-06-27 18:11:59 +0100294}
295
Christopher Wileyec31a052016-01-25 07:28:51 -0800296TEST_F(AidlTest, ParsesNullableAnnotation) {
297 for (auto is_nullable: {true, false}) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900298 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);
Christopher Wileyec31a052016-01-25 07:28:51 -0800302 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700303 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);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900307 typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800308 }
309}
310
311TEST_F(AidlTest, ParsesUtf8Annotations) {
312 for (auto is_utf8: {true, false}) {
313 auto parse_result = Parse(
314 "a/IFoo.aidl",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900315 StringPrintf("package a; interface IFoo {%s String f(); }", (is_utf8) ? "@utf8InCpp" : ""),
316 typenames_, Options::Language::CPP);
Christopher Wileyec31a052016-01-25 07:28:51 -0800317 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700318 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);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900322 typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800323 }
324}
325
Steven Morelanda57d0a62019-07-30 09:41:14 -0700326TEST_F(AidlTest, VintfRequiresStructuredAndStability) {
327 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900328 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
329 Options::Language::CPP, &error);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700330 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
331 ASSERT_EQ(nullptr, parse_result);
332}
333
334TEST_F(AidlTest, VintfRequiresStructured) {
335 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900336 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
337 Options::Language::CPP, &error, {"--stability", "vintf"});
Steven Morelanda57d0a62019-07-30 09:41:14 -0700338 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
339 ASSERT_EQ(nullptr, parse_result);
340}
341
342TEST_F(AidlTest, VintfRequiresSpecifiedStability) {
343 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900344 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
345 Options::Language::CPP, &error, {"--structured"});
Steven Morelanda57d0a62019-07-30 09:41:14 -0700346 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
347 ASSERT_EQ(nullptr, parse_result);
348}
349
350TEST_F(AidlTest, ParsesStabilityAnnotations) {
351 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900352 auto parse_result =
353 Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, Options::Language::CPP,
354 &error, {"--structured", "--stability", "vintf"});
Steven Morelanda57d0a62019-07-30 09:41:14 -0700355 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());
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900360 typenames_.Reset();
Steven Morelanda57d0a62019-07-30 09:41:14 -0700361}
362
Jeongik Cha64783ed2019-06-07 18:30:54 +0900363TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) {
364 Options java_options = Options::From("aidl -o out --structured a/Foo.aidl");
Jeongik Cha88f95a82020-01-15 13:02:16 +0900365 Options cpp_options = Options::From("aidl --lang=cpp -o out -h out/include a/Foo.aidl");
366 Options cpp_structured_options =
Jeongik Cha64783ed2019-06-07 18:30:54 +0900367 Options::From("aidl --lang=cpp --structured -o out -h out/include a/Foo.aidl");
368 io_delegate_.SetFileContents(
Jeongik Cha88f95a82020-01-15 13:02:16 +0900369 "a/Foo.aidl",
370 StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo cpp_header \"Foo.h\" ;"));
Jeongik Cha64783ed2019-06-07 18:30:54 +0900371
372 EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_));
Jeongik Cha88f95a82020-01-15 13:02:16 +0900373 EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_));
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900374 AddExpectedStderr(
Jeongik Cha88f95a82020-01-15 13:02:16 +0900375 "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_));
Jeongik Cha64783ed2019-06-07 18:30:54 +0900378}
379
Christopher Wiley90be4e32015-10-20 14:55:25 -0700380TEST_F(AidlTest, AcceptsOneway) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700381 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); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900384 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));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700391}
Christopher Wileyef140932015-11-03 09:29:19 -0800392
Artur Satayev91fe8712019-07-29 13:06:01 +0100393TEST_F(AidlTest, AcceptsAnnotatedOnewayMethod) {
394 string oneway_method = "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900395 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));
Artur Satayev91fe8712019-07-29 13:06:01 +0100398}
399
400TEST_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
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900407 auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, Options::Language::JAVA);
Artur Satayev91fe8712019-07-29 13:06:01 +0100408 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
Christopher Wileyef140932015-11-03 09:29:19 -0800417TEST_F(AidlTest, ParsesPreprocessedFile) {
418 string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
419 io_delegate_.SetFileContents("path", simple_content);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900420 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);
Christopher Wileyef140932015-11-03 09:29:19 -0800424}
425
426TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
427 string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
428 io_delegate_.SetFileContents("path", simple_content);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900429
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);
Christopher Wileyef140932015-11-03 09:29:19 -0800434}
435
Christopher Wiley41544372015-11-03 14:52:29 -0800436TEST_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");
Jiyong Park8c380532018-08-30 14:55:26 +0900441 import_paths_.emplace("");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900442 auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
443 typenames_, Options::Language::JAVA);
Christopher Wiley41544372015-11-03 14:52:29 -0800444 EXPECT_NE(nullptr, parse_result);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900445
Christopher Wiley41544372015-11-03 14:52:29 -0800446 // We expect to know about both kinds of IBar
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900447 EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").second);
448 EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").second);
Christopher Wiley41544372015-11-03 14:52:29 -0800449 // But if we request just "IBar" we should get our imported one.
Steven Moreland02e012e2018-08-02 14:58:10 -0700450 AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900451 ambiguous_type.Resolve(typenames_);
452 EXPECT_EQ("one.IBar", ambiguous_type.GetName());
Christopher Wiley41544372015-11-03 14:52:29 -0800453}
454
Jiyong Park8f6ec462020-01-19 20:52:47 +0900455// Special case of PreferImportToPreprocessed. Imported type should be preferred
456// even when the preprocessed file already has the same type.
457TEST_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
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800477TEST_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
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900483 vector<string> args {
484 "aidl",
485 "--preprocess",
486 "preprocessed",
487 "p/Outer.aidl",
488 "one/IBar.aidl"};
489 Options options = Options::From(args);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800490 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
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900497TEST_F(AidlTest, JavaParcelableOutput) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100498 io_delegate_.SetFileContents(
499 "Rect.aidl",
Makoto Onuki00be5c72020-03-04 16:57:23 -0800500 "@Hide\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100501 "parcelable Rect {\n"
502 " // Comment\n"
Makoto Onuki00be5c72020-03-04 16:57:23 -0800503 " @Hide\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100504 " int x=5;\n"
Makoto Onuki00be5c72020-03-04 16:57:23 -0800505 " @Hide\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100506 " @UnsupportedAppUsage(maxTargetSdk = 28, trackingBug = 42, implicitMember = \"dummy\", "
Andrei Onea07fa3e02019-07-04 17:55:19 +0100507 "expectedSignature = \"dummy\", publicAlternatives = \"d\" \n + \"u\" + \n \"m\" \n + \"m\" "
508 "+ \"y\")\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100509 " int y;\n"
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900510 " ParcelFileDescriptor fd;\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100511 "}");
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900512
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));
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900519 EXPECT_EQ(kExpectedJavaParcelableOutputContests, output);
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900520}
521
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800522TEST_F(AidlTest, RequireOuterClass) {
523 io_delegate_.SetFileContents("p/Outer.aidl",
524 "package p; parcelable Outer.Inner;");
Jiyong Park8c380532018-08-30 14:55:26 +0900525 import_paths_.emplace("");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900526 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);
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800529 EXPECT_EQ(nullptr, parse_result);
530}
531
532TEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) {
533 io_delegate_.SetFileContents("preprocessed",
534 "parcelable p.Outer.Inner;");
535 preprocessed_files_.push_back("preprocessed");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900536 auto parse_result = Parse("p/IFoo.aidl", "package p; interface IFoo { void f(in Inner c); }",
537 typenames_, Options::Language::JAVA);
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800538 // 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
Christopher Wiley632801d2015-11-05 14:15:49 -0800544TEST_F(AidlTest, FailOnParcelable) {
Steven Morelande2c64b42018-09-18 15:06:37 -0700545 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;");
546
Christopher Wiley632801d2015-11-05 14:15:49 -0800547 // By default, we shouldn't fail on parcelable.
Steven Morelande2c64b42018-09-18 15:06:37 -0700548 Options options1 = Options::From("aidl p/IFoo.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900549 EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_));
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900550
Steven Morelande2c64b42018-09-18 15:06:37 -0700551 // -b considers this an error
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900552 Options options2 = Options::From("aidl -b p/IFoo.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900553 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
Steven Morelande2c64b42018-09-18 15:06:37 -0700554
555 io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}");
556
Jiyong Parkda8c6932019-08-12 19:56:08 +0900557 // With '-b' option, a parcelable and an interface should fail.
Steven Morelande2c64b42018-09-18 15:06:37 -0700558 Options options3 = Options::From("aidl p/IBar.aidl");
Jiyong Parkda8c6932019-08-12 19:56:08 +0900559 EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_));
Steven Morelande2c64b42018-09-18 15:06:37 -0700560 Options options4 = Options::From("aidl -b p/IBar.aidl");
561 EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_));
Christopher Wiley632801d2015-11-05 14:15:49 -0800562}
563
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700564TEST_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); }",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900571 typenames_, Options::Language::JAVA, &reported_error, {"--structured"});
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700572 EXPECT_EQ(nullptr, parse_result);
573 EXPECT_EQ(AidlError::NOT_STRUCTURED, reported_error);
574}
575
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700576TEST_F(AidlTest, FailOnDuplicateConstantNames) {
577 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900578 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
579 R"(package p;
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700580 interface IFoo {
581 const String DUPLICATED = "d";
582 const int DUPLICATED = 1;
583 }
584 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900585 typenames_, Options::Language::CPP, &reported_error));
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700586 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700587}
588
Steven Morelandc258abc2018-07-10 14:03:38 -0700589TEST_F(AidlTest, FailOnManyDefinedTypes) {
590 AidlError reported_error;
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900591 AddExpectedStderr("ERROR: p/IFoo.aidl: You must declare only one type per a file.\n");
Steven Morelandc258abc2018-07-10 14:03:38 -0700592 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
593 R"(package p;
594 interface IFoo {}
Steven Morelandc258abc2018-07-10 14:03:38 -0700595 parcelable IBar {}
596 parcelable StructuredParcelable {}
597 interface IBaz {}
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900598 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900599 typenames_, Options::Language::CPP, &reported_error));
Steven Morelandc258abc2018-07-10 14:03:38 -0700600 // 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
605TEST_F(AidlTest, FailOnNoDefinedTypes) {
606 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900607 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", typenames_, Options::Language::CPP,
608 &reported_error));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900609 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
Steven Morelandc258abc2018-07-10 14:03:38 -0700610}
611
Roshan Pius3b2203d2016-07-22 16:13:20 -0700612TEST_F(AidlTest, FailOnMalformedConstHexValue) {
613 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900614 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
615 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700616 interface IFoo {
617 const int BAD_HEX_VALUE = 0xffffffffffffffffff;
618 }
619 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900620 typenames_, Options::Language::CPP, &reported_error));
Will McVickerd7d18df2019-09-12 13:40:50 -0700621 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
Roshan Pius3b2203d2016-07-22 16:13:20 -0700622}
623
624TEST_F(AidlTest, ParsePositiveConstHexValue) {
625 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900626 auto cpp_parse_result = Parse("p/IFoo.aidl",
627 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700628 interface IFoo {
629 const int POSITIVE_HEX_VALUE = 0xf5;
630 }
631 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900632 typenames_, Options::Language::CPP, &reported_error);
Roshan Pius3b2203d2016-07-22 16:13:20 -0700633 EXPECT_NE(nullptr, cpp_parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700634 const AidlInterface* interface = cpp_parse_result->AsInterface();
635 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700636 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());
Will McVickerd7d18df2019-09-12 13:40:50 -0700639 EXPECT_TRUE(cpp_constants[0]->CheckValid(typenames_));
Steven Moreland860b1942018-08-16 14:59:28 -0700640 EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700641}
642
643TEST_F(AidlTest, ParseNegativeConstHexValue) {
644 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900645 auto cpp_parse_result = Parse("p/IFoo.aidl",
646 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700647 interface IFoo {
648 const int NEGATIVE_HEX_VALUE = 0xffffffff;
649 }
650 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900651 typenames_, Options::Language::CPP, &reported_error);
Steven Moreland48ae1a42019-10-03 12:29:46 -0700652 ASSERT_NE(nullptr, cpp_parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700653 const AidlInterface* interface = cpp_parse_result->AsInterface();
654 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700655 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());
Will McVickerd7d18df2019-09-12 13:40:50 -0700658 EXPECT_EQ(true, cpp_constants[0]->CheckValid(typenames_));
Steven Moreland860b1942018-08-16 14:59:28 -0700659 EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700660}
661
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700662TEST_F(AidlTest, UnderstandsNestedParcelables) {
663 io_delegate_.SetFileContents(
664 "p/Outer.aidl",
665 "package p; parcelable Outer.Inner cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900666 import_paths_.emplace("");
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700667 const string input_path = "p/IFoo.aidl";
668 const string input = "package p; import p.Outer; interface IFoo"
669 " { Outer.Inner get(); }";
670
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900671 auto cpp_parse_result = Parse(input_path, input, typenames_, Options::Language::CPP);
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700672 EXPECT_NE(nullptr, cpp_parse_result);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900673
674 auto pair = typenames_.ResolveTypename("p.Outer.Inner");
675 EXPECT_TRUE(pair.second);
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700676 // C++ uses "::" instead of "." to refer to a inner class.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900677 AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, "");
678 EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_));
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700679}
680
Christopher Wiley9078d722015-11-17 10:23:49 -0800681TEST_F(AidlTest, UnderstandsNativeParcelables) {
682 io_delegate_.SetFileContents(
683 "p/Bar.aidl",
Casey Dahlincd639212015-12-15 12:51:04 -0800684 "package p; parcelable Bar cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900685 import_paths_.emplace("");
Christopher Wiley9078d722015-11-17 10:23:49 -0800686 const string input_path = "p/IFoo.aidl";
687 const string input = "package p; import p.Bar; interface IFoo { }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900688 {
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_);
Daniel Norman716d3112019-09-10 13:11:56 -0700711 EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type, typenames_));
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900712 }
Christopher Wiley9078d722015-11-17 10:23:49 -0800713}
714
Christopher Wileyf8136192016-04-12 14:19:35 -0700715TEST_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.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900719 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 {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900726 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyf8136192016-04-12 14:19:35 -0700727 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900728 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Christopher Wileyf8136192016-04-12 14:19:35 -0700729 EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
730}
731
Dan Willemsen93298ee2016-11-10 23:55:55 -0800732TEST_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.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900736 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 {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900744 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800745 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900746 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800747 EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents);
748}
749
Jiyong Parkdf202122019-09-30 20:48:35 +0900750TEST_F(AidlTest, WritesTrivialDependencyFileForParcelableDeclaration) {
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700751 // 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.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900756 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;");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900763 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700764 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900765 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Jiyong Parkdf202122019-09-30 20:48:35 +0900766 EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDeclarationDepFileContents);
767}
768
769TEST_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);
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700782}
783
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900784TEST_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
Jiyong Parkccf00f82018-07-17 01:39:23 +0900797/* not working until type_namespace.h is fixed
798TEST_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;}";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900803 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_,
806Options::Language::JAVA)); EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, typenames_,
807Options::Language::CPP));
Jiyong Parkccf00f82018-07-17 01:39:23 +0900808}
809*/
810
Jeongik Cha08ca2182019-11-21 14:01:13 +0900811// TODO(b/136048684)
812TEST_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
Jiyong Park02da7422018-07-16 16:00:26 +0900831TEST_F(AidlTest, ApiDump) {
832 io_delegate_.SetFileContents(
833 "foo/bar/IFoo.aidl",
834 "package foo.bar;\n"
835 "import foo.bar.Data;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900836 "// comment @hide\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900837 "interface IFoo {\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900838 " /* @hide */\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900839 " 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"
Jeongik Cha997281d2020-01-16 15:23:59 +0900843 " // @hide\n"
Jiyong Parka428d212018-08-29 22:26:30 +0900844 " const int A = 1;\n"
845 " const String STR = \"Hello\";\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900846 "}\n");
847 io_delegate_.SetFileContents("foo/bar/Data.aidl",
848 "package foo.bar;\n"
849 "import foo.bar.IFoo;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900850 "/* @hide*/\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900851 "parcelable Data {\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900852 " // @hide\n"
Jiyong Parka468e2a2018-08-29 21:25:18 +0900853 " int x = 10;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900854 " // @hide\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900855 " int y;\n"
856 " IFoo foo;\n"
857 " List<IFoo> a;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900858 " /*@hide2*/\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900859 " List<foo.bar.IFoo> b;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900860 " // It should be @hide property\n"
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900861 " @nullable String[] c;\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900862 "}\n");
863 io_delegate_.SetFileContents("api.aidl", "");
Jiyong Park633246c2019-11-25 10:50:05 +0900864 vector<string> args = {"aidl", "--dumpapi", "--out=dump", "--include=.",
865 "foo/bar/IFoo.aidl", "foo/bar/Data.aidl"};
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900866 Options options = Options::From(args);
Jiyong Park02da7422018-07-16 16:00:26 +0900867 bool result = dump_api(options, io_delegate_);
868 ASSERT_TRUE(result);
869 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +0900870 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
Paul Trautrimb5ff1a22020-02-27 13:10:16 +0900871 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
Jeongik Cha997281d2020-01-16 15:23:59 +0900872/* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +0900873interface IFoo {
Jeongik Cha997281d2020-01-16 15:23:59 +0900874 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +0900875 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();
Jeongik Cha997281d2020-01-16 15:23:59 +0900879 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +0900880 const int A = 1;
881 const String STR = "Hello";
882}
Paul Trautrimb5ff1a22020-02-27 13:10:16 +0900883)"));
Jiyong Park02da7422018-07-16 16:00:26 +0900884
Jiyong Parke59c3682018-09-11 23:10:25 +0900885 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual));
Paul Trautrimb5ff1a22020-02-27 13:10:16 +0900886 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
Jeongik Cha997281d2020-01-16 15:23:59 +0900887/* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +0900888parcelable Data {
Jeongik Cha997281d2020-01-16 15:23:59 +0900889 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +0900890 int x = 10;
Jeongik Cha997281d2020-01-16 15:23:59 +0900891 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +0900892 int y;
893 foo.bar.IFoo foo;
894 List<foo.bar.IFoo> a;
895 List<foo.bar.IFoo> b;
Jeongik Cha997281d2020-01-16 15:23:59 +0900896 /* @hide */
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900897 @nullable String[] c;
Jiyong Park02da7422018-07-16 16:00:26 +0900898}
Paul Trautrimb5ff1a22020-02-27 13:10:16 +0900899)"));
Jiyong Park02da7422018-07-16 16:00:26 +0900900}
901
Jiyong Parked65bf42018-08-28 15:43:27 +0900902TEST_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
Jiyong Parke59c3682018-09-11 23:10:25 +0900912 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +0900913 Options options = Options::From(args);
914 bool result = dump_api(options, io_delegate_);
915 ASSERT_TRUE(result);
916 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +0900917 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
Paul Trautrimb5ff1a22020-02-27 13:10:16 +0900918 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
Jiyong Parke59c3682018-09-11 23:10:25 +0900919interface IFoo {
920 int foo() = 1;
921 int bar() = 2;
922 int baz() = 10;
Jiyong Parked65bf42018-08-28 15:43:27 +0900923}
Paul Trautrimb5ff1a22020-02-27 13:10:16 +0900924)"));
Jiyong Parked65bf42018-08-28 15:43:27 +0900925}
926
927TEST_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
Jiyong Parke59c3682018-09-11 23:10:25 +0900937 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +0900938 Options options = Options::From(args);
939 EXPECT_FALSE(dump_api(options, io_delegate_));
940}
941
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900942TEST_F(AidlTest, CheckNumGenericTypeSecifier) {
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900943 Options options = Options::From("aidl p/IFoo.aidl IFoo.java");
944 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900945 "package p; interface IFoo {"
946 "void foo(List<String, String> a);}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900947 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900948
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900949 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900950 "package p; interface IFoo {"
951 "void foo(Map<String> a);}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900952 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900953
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900954 Options options2 = Options::From("aidl p/Data.aidl Data.java");
955 io_delegate_.SetFileContents(options2.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900956 "package p; parcelable Data {"
957 "List<String, String> foo;}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900958 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900959
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900960 io_delegate_.SetFileContents(options2.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900961 "package p; parcelable Data {"
962 "Map<String> foo;}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900963 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
964}
965
Jeongik Chae48d9942020-01-02 17:39:00 +0900966TEST_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
Jeongik Chadf76dc72019-11-28 00:08:47 +0900991TEST_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
999TEST_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_));
Jeongik Chae74c86d2019-12-12 16:54:03 +09001018
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_));
Jeongik Chadf76dc72019-11-28 00:08:47 +09001028}
1029
Jeongik Cha0e426012019-07-29 15:57:02 +09001030TEST_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");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001041
Jeongik Cha0e426012019-07-29 15:57:02 +09001042 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Parkb034bf02018-07-30 17:44:33 +09001043
Jeongik Cha0e426012019-07-29 15:57:02 +09001044 io_delegate_.SetFileContents(options.InputFiles().front(),
1045 "package foo.bar;\n"
1046 "interface IFoo1 { int foo(); }\n"
1047 "interface IFoo2 { int foo(); }\n");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001048
Jeongik Cha0e426012019-07-29 15:57:02 +09001049 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Parkb034bf02018-07-30 17:44:33 +09001050
Jeongik Cha0e426012019-07-29 15:57:02 +09001051 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");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001055
Jeongik Cha0e426012019-07-29 15:57:02 +09001056 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Parkb034bf02018-07-30 17:44:33 +09001057 }
1058}
1059
1060TEST_F(AidlTest, MultipleInputFiles) {
1061 Options options = Options::From(
Jiyong Park633246c2019-11-25 10:50:05 +09001062 "aidl --lang=java -o out -I . foo/bar/IFoo.aidl foo/bar/Data.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001063
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
1085TEST_F(AidlTest, MultipleInputFilesCpp) {
1086 Options options = Options::From("aidl --lang=cpp -o out -h out/include "
Jiyong Park633246c2019-11-25 10:50:05 +09001087 "-I . foo/bar/IFoo.aidl foo/bar/Data.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001088
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 : {
Jiyong Parkb03551f2018-08-06 19:20:51 +09001103 "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp",
Jiyong Parkb034bf02018-07-30 17:44:33 +09001104 "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 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001111}
1112
Jiyong Park309668e2018-07-28 16:55:44 +09001113TEST_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(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001119 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +09001120
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(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001126 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +09001127
1128 // this is another reserved name
1129 io_delegate_.SetFileContents(options.InputFiles().front(),
1130 "package p; interface IFoo {"
1131 "String getTransactionName(int code); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001132 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +09001133
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(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001138 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +09001139}
1140
Daniel Norman85aed542019-08-21 12:01:14 -07001141TEST_F(AidlTest, DifferentOrderAnnotationsInCheckAPI) {
Jeongik Cha3271ffa2018-12-04 15:19:20 +09001142 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
Jiyong Park3656c3c2018-08-01 20:02:01 +09001151TEST_F(AidlTest, SuccessOnIdenticalApiDumps) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001152 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();}");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001155
1156 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1157}
1158
Daniel Norman85aed542019-08-21 12:01:14 -07001159class AidlTestCompatibleChanges : public AidlTest {
1160 protected:
1161 Options options_ = Options::From("aidl --checkapi old new");
1162};
1163
1164TEST_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
1183TEST_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
1198TEST_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
1213TEST_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
1228TEST_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
1244TEST_F(AidlTestCompatibleChanges, NewPackage) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001245 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1246 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001247 "interface IFoo {"
1248 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001249 "}");
1250 io_delegate_.SetFileContents("old/p/Data.aidl",
1251 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001252 "parcelable Data {"
1253 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001254 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001255 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1256 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001257 "interface IFoo {"
1258 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001259 "}");
1260 io_delegate_.SetFileContents("new/p/Data.aidl",
1261 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001262 "parcelable Data {"
1263 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +09001264 "}");
1265 io_delegate_.SetFileContents("new/q/IFoo.aidl",
1266 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001267 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001268 " void foo(int a);"
1269 "}");
1270 io_delegate_.SetFileContents("new/q/Data.aidl",
1271 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001272 "parcelable Data {"
1273 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001274 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001275 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1276}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001277
Daniel Norman85aed542019-08-21 12:01:14 -07001278TEST_F(AidlTestCompatibleChanges, ArgNameChange) {
1279 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1280 "package p;"
1281 "interface IFoo {"
1282 " void foo(int a);"
1283 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001284 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1285 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001286 "interface IFoo {"
1287 " void foo(int b);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001288 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001289 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1290}
Jiyong Parke59c3682018-09-11 23:10:25 +09001291
Daniel Norman85aed542019-08-21 12:01:14 -07001292TEST_F(AidlTestCompatibleChanges, AddedConstValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001293 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;}");
Daniel Norman85aed542019-08-21 12:01:14 -07001299 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1300}
Jiyong Parka428d212018-08-29 22:26:30 +09001301
Daniel Norman85aed542019-08-21 12:01:14 -07001302TEST_F(AidlTestCompatibleChanges, ChangedConstValueOrder) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001303 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;}");
Daniel Norman85aed542019-08-21 12:01:14 -07001309 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
Jiyong Park3656c3c2018-08-01 20:02:01 +09001310}
1311
Daniel Norman85aed542019-08-21 12:01:14 -07001312class AidlTestIncompatibleChanges : public AidlTest {
1313 protected:
1314 Options options_ = Options::From("aidl --checkapi old new");
1315};
1316
1317TEST_F(AidlTestIncompatibleChanges, RemovedType) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001318 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1319 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001320 "interface IFoo {"
1321 " void foo(in String[] str);"
1322 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001323 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001324 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1325 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1326}
1327
1328TEST_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
1343TEST_F(AidlTestIncompatibleChanges, RemovedField) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001344 io_delegate_.SetFileContents("old/p/Data.aidl",
1345 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001346 "parcelable Data {"
1347 " int foo;"
1348 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001349 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001350 io_delegate_.SetFileContents("new/p/Data.aidl",
1351 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001352 "parcelable Data {"
1353 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001354 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001355 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1356}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001357
Daniel Norman85aed542019-08-21 12:01:14 -07001358TEST_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
1373TEST_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 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001380 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1381 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001382 "interface IFoo {"
1383 " void foo(in String[] str);"
1384 " void bar2(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001385 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001386 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1387}
1388
Daniel Norman85aed542019-08-21 12:01:14 -07001389TEST_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 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001396 io_delegate_.SetFileContents("new/p/IFoo2.aidl",
1397 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001398 "interface IFoo2 {"
1399 " void foo(in String[] str);"
1400 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001401 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001402 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1403}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001404
Daniel Norman85aed542019-08-21 12:01:14 -07001405TEST_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
1421TEST_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 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001428 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1429 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001430 "interface IFoo {"
1431 " void bar(@utf8InCpp String str);"
1432 " void foo(in String[] str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001433 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001434 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1435}
1436
1437TEST_F(AidlTestIncompatibleChanges, ReorderedField) {
1438 io_delegate_.SetFileContents("old/p/Data.aidl",
Jiyong Parke59c3682018-09-11 23:10:25 +09001439 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001440 "parcelable Data {"
1441 " int foo;"
1442 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001443 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001444 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}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001452
Daniel Norman85aed542019-08-21 12:01:14 -07001453TEST_F(AidlTestIncompatibleChanges, ChangedDirectionSpecifier) {
1454 io_delegate_.SetFileContents("old/p/IFoo.aidl",
Jiyong Parke59c3682018-09-11 23:10:25 +09001455 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001456 "interface IFoo {"
1457 " void foo(in String[] str);"
1458 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001459 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001460 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1461 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001462 "interface IFoo {"
1463 " void foo(out String[] str);"
1464 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001465 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001466 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1467}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001468
Daniel Norman85aed542019-08-21 12:01:14 -07001469TEST_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 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001476 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1477 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001478 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001479 " void foo(in @utf8InCpp String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001480 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001481 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001482 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1483}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001484
Daniel Norman85aed542019-08-21 12:01:14 -07001485TEST_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 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001492 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1493 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001494 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001495 " void foo(in String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001496 " void bar(String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001497 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001498 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1499}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001500
Daniel Norman85aed542019-08-21 12:01:14 -07001501TEST_F(AidlTestIncompatibleChanges, RemovedPackage) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001502 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}");
1503 io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001504 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}");
Daniel Norman85aed542019-08-21 12:01:14 -07001505 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1506}
Jiyong Parka468e2a2018-08-29 21:25:18 +09001507
Daniel Norman85aed542019-08-21 12:01:14 -07001508TEST_F(AidlTestIncompatibleChanges, ChangedDefaultValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001509 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; }");
Daniel Norman85aed542019-08-21 12:01:14 -07001511 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1512}
Jiyong Parka428d212018-08-29 22:26:30 +09001513
Daniel Norman85aed542019-08-21 12:01:14 -07001514TEST_F(AidlTestIncompatibleChanges, RemovedConstValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001515 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; }");
Daniel Norman85aed542019-08-21 12:01:14 -07001519 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1520}
Jiyong Parka428d212018-08-29 22:26:30 +09001521
Daniel Norman85aed542019-08-21 12:01:14 -07001522TEST_F(AidlTestIncompatibleChanges, ChangedConstValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001523 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; }");
Daniel Norman85aed542019-08-21 12:01:14 -07001525 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Jiyong Park3c35e392018-08-30 13:10:30 +09001526}
1527
Jiyong Park8c380532018-08-30 14:55:26 +09001528TEST_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
Jiyong Parked65bf42018-08-28 15:43:27 +09001537TEST_F(AidlTest, HandleManualIdAssignments) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001538 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;}");
Jiyong Parked65bf42018-08-28 15:43:27 +09001541
1542 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1543
Jiyong Parke59c3682018-09-11 23:10:25 +09001544 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 11;}");
Jiyong Parked65bf42018-08-28 15:43:27 +09001545 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
1546}
1547
Jiyong Parke05195e2018-10-08 18:24:23 +09001548TEST_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}
Jiyong Parked65bf42018-08-28 15:43:27 +09001575
Jiyong Park3633b722019-04-11 15:38:26 +09001576TEST_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
1586TEST_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
1596TEST_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
1606TEST_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
1617TEST_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
Jiyong Parkc6816252019-07-08 08:12:28 +09001627TEST_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
1634TEST_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
Jiyong Park56f73d72019-06-11 12:20:28 +09001642class AidlOutputPathTest : public AidlTest {
1643 protected:
1644 void SetUp() override {
1645 AidlTest::SetUp();
1646 io_delegate_.SetFileContents("sub/dir/foo/bar/IFoo.aidl", "package foo.bar; interface IFoo {}");
1647 }
1648
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
1656TEST_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
1661TEST_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
1667TEST_F(AidlOutputPathTest, NoOutDirWithOutputFile) {
1668 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java");
1669}
1670
1671TEST_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
Will McVickera4e5b132019-10-03 13:52:21 -07001676TEST_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
1688TEST_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
1700TEST_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));
Will McVickerd7d18df2019-09-12 13:40:50 -07001709 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
Will McVickera4e5b132019-10-03 13:52:21 -07001710}
1711
1712TEST_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));
Will McVickerd7d18df2019-09-12 13:40:50 -07001721 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
Will McVickera4e5b132019-10-03 13:52:21 -07001722}
1723
Daniel Normanb28684e2019-10-17 15:31:39 -07001724TEST_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
Christopher Wiley90be4e32015-10-20 14:55:25 -07001738} // namespace aidl
1739} // namespace android