blob: 6266ee3e83a40f4ab754ab7ed2fc17ee1fb65f63 [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>
Jiyong Parkf1f5c802020-05-19 17:33:00 +090023#include <gmock/gmock.h>
Christopher Wiley90be4e32015-10-20 14:55:25 -070024#include <gtest/gtest.h>
25
26#include "aidl.h"
Steven Moreland49585242019-12-18 16:06:49 -080027#include "aidl_checkapi.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070028#include "aidl_language.h"
Steven Moreland860b1942018-08-16 14:59:28 -070029#include "aidl_to_cpp.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090030#include "aidl_to_java.h"
31#include "options.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070032#include "tests/fake_io_delegate.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070033
Jeongik Cha2a5b4d82019-08-06 19:37:59 +090034using android::aidl::internals::parse_preprocessed_file;
Christopher Wiley90be4e32015-10-20 14:55:25 -070035using android::aidl::test::FakeIoDelegate;
Christopher Wileyec31a052016-01-25 07:28:51 -080036using android::base::StringPrintf;
Christopher Wiley12e894a2016-01-29 11:55:07 -080037using std::set;
Christopher Wiley90be4e32015-10-20 14:55:25 -070038using std::string;
39using std::unique_ptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -080040using std::vector;
Devin Moore7b8d5c92020-03-17 14:14:08 -070041using testing::TestParamInfo;
Jeongik Cha2a5b4d82019-08-06 19:37:59 +090042using testing::internal::CaptureStderr;
43using testing::internal::GetCapturedStderr;
Christopher Wiley90be4e32015-10-20 14:55:25 -070044
45namespace android {
46namespace aidl {
Christopher Wileyf8136192016-04-12 14:19:35 -070047namespace {
48
49const char kExpectedDepFileContents[] =
50R"(place/for/output/p/IFoo.java : \
51 p/IFoo.aidl
52
53p/IFoo.aidl :
54)";
55
Dan Willemsen93298ee2016-11-10 23:55:55 -080056const char kExpectedNinjaDepFileContents[] =
57R"(place/for/output/p/IFoo.java : \
58 p/IFoo.aidl
59)";
60
Jiyong Parkdf202122019-09-30 20:48:35 +090061const char kExpectedParcelableDeclarationDepFileContents[] =
62 R"( : \
63 p/Foo.aidl
64
65p/Foo.aidl :
66)";
67
68const char kExpectedStructuredParcelableDepFileContents[] =
Steven Moreland2a9a7d62019-02-05 16:11:54 -080069 R"(place/for/output/p/Foo.java : \
Christopher Wileyb1bbdf82016-04-21 11:43:45 -070070 p/Foo.aidl
71
72p/Foo.aidl :
73)";
74
Jiyong Parkd4e73c82019-07-24 21:39:15 +090075const char kExpectedJavaParcelableOutputContests[] =
Jeongik Chaa755c2a2018-12-12 16:28:23 +090076 R"(/*
77 * This file is auto-generated. DO NOT MODIFY.
Jeongik Chaa755c2a2018-12-12 16:28:23 +090078 */
Makoto Onuki78a1c1c2020-03-04 16:57:23 -080079@android.annotation.Hide
Jeongik Chaa755c2a2018-12-12 16:28:23 +090080public class Rect implements android.os.Parcelable
81{
82 // Comment
83
Makoto Onuki78a1c1c2020-03-04 16:57:23 -080084 @android.annotation.Hide
Jeongik Chaa755c2a2018-12-12 16:28:23 +090085 public int x = 5;
86
Makoto Onuki78a1c1c2020-03-04 16:57:23 -080087 @android.annotation.Hide
88 @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 +090089 public int y;
Jiyong Parkd4e73c82019-07-24 21:39:15 +090090
91 public android.os.ParcelFileDescriptor fd;
Jeongik Chaa755c2a2018-12-12 16:28:23 +090092 public static final android.os.Parcelable.Creator<Rect> CREATOR = new android.os.Parcelable.Creator<Rect>() {
93 @Override
94 public Rect createFromParcel(android.os.Parcel _aidl_source) {
95 Rect _aidl_out = new Rect();
96 _aidl_out.readFromParcel(_aidl_source);
97 return _aidl_out;
98 }
99 @Override
100 public Rect[] newArray(int _aidl_size) {
101 return new Rect[_aidl_size];
102 }
103 };
104 @Override public final void writeToParcel(android.os.Parcel _aidl_parcel, int _aidl_flag)
105 {
106 int _aidl_start_pos = _aidl_parcel.dataPosition();
107 _aidl_parcel.writeInt(0);
108 _aidl_parcel.writeInt(x);
109 _aidl_parcel.writeInt(y);
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900110 if ((fd!=null)) {
111 _aidl_parcel.writeInt(1);
112 fd.writeToParcel(_aidl_parcel, 0);
113 }
114 else {
115 _aidl_parcel.writeInt(0);
116 }
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900117 int _aidl_end_pos = _aidl_parcel.dataPosition();
118 _aidl_parcel.setDataPosition(_aidl_start_pos);
119 _aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos);
120 _aidl_parcel.setDataPosition(_aidl_end_pos);
121 }
122 public final void readFromParcel(android.os.Parcel _aidl_parcel)
123 {
124 int _aidl_start_pos = _aidl_parcel.dataPosition();
125 int _aidl_parcelable_size = _aidl_parcel.readInt();
126 if (_aidl_parcelable_size < 0) return;
127 try {
128 x = _aidl_parcel.readInt();
129 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
130 y = _aidl_parcel.readInt();
131 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900132 if ((0!=_aidl_parcel.readInt())) {
133 fd = android.os.ParcelFileDescriptor.CREATOR.createFromParcel(_aidl_parcel);
134 }
135 else {
136 fd = null;
137 }
138 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900139 } finally {
140 _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size);
141 }
142 }
143 @Override public int describeContents()
144 {
Jeongik Cha372a8c82018-12-12 16:31:11 +0900145 return 0;
146 }
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900147}
148)";
149
Christopher Wileyf8136192016-04-12 14:19:35 -0700150} // namespace
Christopher Wiley90be4e32015-10-20 14:55:25 -0700151
Devin Moore7b8d5c92020-03-17 14:14:08 -0700152class AidlTest : public ::testing::TestWithParam<Options::Language> {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700153 protected:
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900154 AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_,
155 Options::Language lang, AidlError* error = nullptr,
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700156 const vector<string> additional_arguments = {}) {
Christopher Wiley0522cd52015-10-28 15:39:44 -0700157 io_delegate_.SetFileContents(path, contents);
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900158 vector<string> args;
Devin Moore7b8d5c92020-03-17 14:14:08 -0700159 args.emplace_back("aidl");
160 args.emplace_back("--lang=" + Options::LanguageToString(lang));
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700161 for (const string& s : additional_arguments) {
162 args.emplace_back(s);
163 }
164 for (const string& f : preprocessed_files_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900165 args.emplace_back("--preprocessed=" + f);
166 }
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700167 for (const string& i : import_paths_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900168 args.emplace_back("--include=" + i);
169 }
170 args.emplace_back(path);
171 Options options = Options::From(args);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900172 vector<AidlDefinedType*> defined_types;
173 vector<string> imported_files;
Jiyong Park8c380532018-08-30 14:55:26 +0900174 ImportResolver import_resolver{io_delegate_, path, import_paths_, {}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700175 AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900176 path, options, io_delegate_, &typenames_, &defined_types, &imported_files);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900177
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700178 if (error != nullptr) {
179 *error = actual_error;
180 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900181
182 if (actual_error != AidlError::OK) {
183 return nullptr;
184 }
185
186 EXPECT_EQ(1ul, defined_types.size());
187
188 return defined_types.front();
Christopher Wiley90be4e32015-10-20 14:55:25 -0700189 }
Christopher Wiley0522cd52015-10-28 15:39:44 -0700190
Devin Moore7b8d5c92020-03-17 14:14:08 -0700191 Options::Language GetLanguage() { return GetParam(); }
192
Christopher Wiley0522cd52015-10-28 15:39:44 -0700193 FakeIoDelegate io_delegate_;
Christopher Wiley41544372015-11-03 14:52:29 -0800194 vector<string> preprocessed_files_;
Jiyong Park8c380532018-08-30 14:55:26 +0900195 set<string> import_paths_;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900196 AidlTypenames typenames_;
Christopher Wiley90be4e32015-10-20 14:55:25 -0700197};
198
Devin Moore7b8d5c92020-03-17 14:14:08 -0700199// Instantiate the AidlTest parameterized suite, calling all of the TEST_P
200// tests with each of the supported languages as a parameter.
201INSTANTIATE_TEST_SUITE_P(AidlTestSuite, AidlTest,
202 testing::Values(Options::Language::CPP, Options::Language::JAVA,
203 Options::Language::NDK),
204 [](const testing::TestParamInfo<Options::Language>& info) {
205 return Options::LanguageToString(info.param);
206 });
207
208TEST_P(AidlTest, AcceptMissingPackage) {
209 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, GetLanguage()));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700210}
211
Devin Moore7b8d5c92020-03-17 14:14:08 -0700212TEST_P(AidlTest, EndsInSingleLineComment) {
213 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, GetLanguage()));
Steven Moreland9c2988f2019-07-17 17:49:10 -0700214}
215
Devin Moore7b8d5c92020-03-17 14:14:08 -0700216TEST_P(AidlTest, RejectsArraysOfBinders) {
Jiyong Park8c380532018-08-30 14:55:26 +0900217 import_paths_.emplace("");
Christopher Wiley0522cd52015-10-28 15:39:44 -0700218 io_delegate_.SetFileContents("bar/IBar.aidl",
219 "package bar; interface IBar {}");
Devin Moore097a3ab2020-03-11 16:08:44 -0700220 const string path = "foo/IFoo.aidl";
221 const string contents =
222 "package foo;\n"
223 "import bar.IBar;\n"
224 "interface IFoo { void f(in IBar[] input); }";
225 const string expected_stderr = "ERROR: foo/IFoo.aidl:3.27-32: Binder type cannot be an array\n";
226 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700227 EXPECT_EQ(nullptr, Parse(path, contents, typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700228 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Christopher Wiley0522cd52015-10-28 15:39:44 -0700229}
230
Devin Moore7b8d5c92020-03-17 14:14:08 -0700231TEST_P(AidlTest, SupportOnlyOutParameters) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700232 const string interface_list = "package a; interface IBar { void f(out List<String> bar); }";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700233 EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, GetLanguage()));
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900234 typenames_.Reset();
Devin Moore097a3ab2020-03-11 16:08:44 -0700235
236 const string interface_ibinder = "package a; interface IBaz { void f(out IBinder bar); }";
237 const string expected_ibinder_stderr =
238 "ERROR: a/IBaz.aidl:1.47-51: 'out IBinder bar' can only be an in parameter.\n";
239 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700240 EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700241 EXPECT_EQ(expected_ibinder_stderr, GetCapturedStderr());
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900242}
243
Devin Moore7b8d5c92020-03-17 14:14:08 -0700244TEST_P(AidlTest, RejectsOnewayOutParameters) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700245 const string oneway_interface = "package a; oneway interface IBar { void f(out int bar); }";
246 const string expected_stderr =
247 "ERROR: a/IBar.aidl:1.40-42: oneway method 'f' cannot have out parameters\n";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700248 CaptureStderr();
249 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage()));
250 EXPECT_EQ(expected_stderr, GetCapturedStderr());
251 typenames_.Reset();
252
Devin Moore097a3ab2020-03-11 16:08:44 -0700253 const string oneway_method = "package a; interface IBar { oneway void f(out int bar); }";
254 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700255 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700256 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Christopher Wiley90be4e32015-10-20 14:55:25 -0700257}
258
Devin Moore7b8d5c92020-03-17 14:14:08 -0700259TEST_P(AidlTest, RejectsOnewayNonVoidReturn) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700260 const string oneway_method = "package a; interface IFoo { oneway int f(); }";
261 const string expected_stderr =
262 "ERROR: a/IFoo.aidl:1.39-41: oneway method 'f' cannot return a value\n";
263 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700264 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700265 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Christopher Wiley90be4e32015-10-20 14:55:25 -0700266}
267
Devin Moore7b8d5c92020-03-17 14:14:08 -0700268TEST_P(AidlTest, RejectsNullablePrimitive) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700269 const string oneway_method = "package a; interface IFoo { @nullable int f(); }";
270 const string expected_stderr =
271 "ERROR: a/IFoo.aidl:1.38-42: Primitive type cannot get nullable annotation\n";
272 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700273 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700274 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Casey Dahlin57dbe242015-12-04 11:44:02 -0800275}
276
Devin Moore7b8d5c92020-03-17 14:14:08 -0700277TEST_P(AidlTest, RejectsDuplicatedArgumentNames) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700278 const string method = "package a; interface IFoo { void f(int a, int a); }";
279 const string expected_stderr =
280 "ERROR: a/IFoo.aidl:1.33-35: method 'f' has duplicate argument name 'a'\n";
281 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700282 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700283 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Steven Morelandb3cd3c72018-10-11 12:37:45 -0700284}
285
Devin Moore7b8d5c92020-03-17 14:14:08 -0700286TEST_P(AidlTest, RejectsDuplicatedAnnotationParams) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700287 const string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }";
288 const string expected_stderr = "ERROR: a/IFoo.aidl:1.56-62: Trying to redefine parameter foo.\n";
289 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700290 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700291 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Andrei Onea9445fc62019-06-27 18:11:59 +0100292}
293
Devin Moore7b8d5c92020-03-17 14:14:08 -0700294TEST_P(AidlTest, RejectUnsupportedInterfaceAnnotations) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700295 AidlError error;
296 const string method = "package a; @nullable interface IFoo { int f(); }";
297 const string expected_stderr =
Devin Moore24f68572020-02-26 13:20:59 -0800298 "ERROR: a/IFoo.aidl:1.21-31: 'nullable' is not a supported annotation for this node. "
299 "It must be one of: Hide, UnsupportedAppUsage, VintfStability\n";
Devin Moorec054bf82020-03-10 16:31:48 -0700300 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700301 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error));
Devin Moorec054bf82020-03-10 16:31:48 -0700302 EXPECT_EQ(expected_stderr, GetCapturedStderr());
303 EXPECT_EQ(AidlError::BAD_TYPE, error);
Devin Moore24f68572020-02-26 13:20:59 -0800304}
305
Devin Moore7b8d5c92020-03-17 14:14:08 -0700306TEST_P(AidlTest, RejectUnsupportedTypeAnnotations) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700307 AidlError error;
308 const string method = "package a; interface IFoo { @JavaOnlyStableParcelable int f(); }";
309 const string expected_stderr =
Devin Moore24f68572020-02-26 13:20:59 -0800310 "ERROR: a/IFoo.aidl:1.54-58: 'JavaOnlyStableParcelable' is not a supported annotation "
311 "for this node. It must be one of: Hide, UnsupportedAppUsage, nullable, utf8InCpp\n";
Devin Moorec054bf82020-03-10 16:31:48 -0700312 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700313 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error));
Devin Moorec054bf82020-03-10 16:31:48 -0700314 EXPECT_EQ(expected_stderr, GetCapturedStderr());
315 EXPECT_EQ(AidlError::BAD_TYPE, error);
Devin Moore24f68572020-02-26 13:20:59 -0800316}
317
Devin Moore7b8d5c92020-03-17 14:14:08 -0700318TEST_P(AidlTest, RejectUnsupportedParcelableAnnotations) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700319 AidlError error;
320 const string method = "package a; @nullable parcelable IFoo cpp_header \"IFoo.h\";";
321 const string expected_stderr =
Devin Moore24f68572020-02-26 13:20:59 -0800322 "ERROR: a/Foo.aidl:1.32-37: 'nullable' is not a supported annotation for this node. "
323 "It must be one of: Hide, JavaOnlyStableParcelable, UnsupportedAppUsage, VintfStability\n";
Devin Moorec054bf82020-03-10 16:31:48 -0700324 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700325 EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage(), &error));
Devin Moorec054bf82020-03-10 16:31:48 -0700326 EXPECT_EQ(expected_stderr, GetCapturedStderr());
327 EXPECT_EQ(AidlError::BAD_TYPE, error);
Devin Moore24f68572020-02-26 13:20:59 -0800328}
329
Devin Moore7b8d5c92020-03-17 14:14:08 -0700330TEST_P(AidlTest, RejectUnsupportedParcelableDefineAnnotations) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700331 AidlError error;
332 const string method = "package a; @nullable parcelable Foo { String a; String b; }";
333 const string expected_stderr =
Devin Moore24f68572020-02-26 13:20:59 -0800334 "ERROR: a/Foo.aidl:1.32-36: 'nullable' is not a supported annotation for this node. "
335 "It must be one of: Hide, UnsupportedAppUsage, VintfStability\n";
Devin Moorec054bf82020-03-10 16:31:48 -0700336 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700337 EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage(), &error));
Devin Moorec054bf82020-03-10 16:31:48 -0700338 EXPECT_EQ(expected_stderr, GetCapturedStderr());
339 EXPECT_EQ(AidlError::BAD_TYPE, error);
Devin Moore24f68572020-02-26 13:20:59 -0800340}
341
Devin Moore7b8d5c92020-03-17 14:14:08 -0700342TEST_P(AidlTest, ParsesNullableAnnotation) {
Christopher Wileyec31a052016-01-25 07:28:51 -0800343 for (auto is_nullable: {true, false}) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900344 auto parse_result = Parse("a/IFoo.aidl",
345 StringPrintf("package a; interface IFoo {%s String f(); }",
346 (is_nullable) ? "@nullable" : ""),
Devin Moore7b8d5c92020-03-17 14:14:08 -0700347 typenames_, GetLanguage());
Christopher Wileyec31a052016-01-25 07:28:51 -0800348 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700349 const AidlInterface* interface = parse_result->AsInterface();
350 ASSERT_NE(nullptr, interface);
351 ASSERT_FALSE(interface->GetMethods().empty());
352 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsNullable(), is_nullable);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900353 typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800354 }
355}
356
Devin Moore7b8d5c92020-03-17 14:14:08 -0700357TEST_P(AidlTest, ParsesUtf8Annotations) {
Christopher Wileyec31a052016-01-25 07:28:51 -0800358 for (auto is_utf8: {true, false}) {
359 auto parse_result = Parse(
360 "a/IFoo.aidl",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900361 StringPrintf("package a; interface IFoo {%s String f(); }", (is_utf8) ? "@utf8InCpp" : ""),
Devin Moore7b8d5c92020-03-17 14:14:08 -0700362 typenames_, GetLanguage());
Christopher Wileyec31a052016-01-25 07:28:51 -0800363 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700364 const AidlInterface* interface = parse_result->AsInterface();
365 ASSERT_NE(nullptr, interface);
366 ASSERT_FALSE(interface->GetMethods().empty());
367 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsUtf8InCpp(), is_utf8);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900368 typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800369 }
370}
371
Devin Moore7b8d5c92020-03-17 14:14:08 -0700372TEST_P(AidlTest, VintfRequiresStructuredAndStability) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700373 AidlError error;
Devin Moore0d0e3f62020-03-30 17:45:39 -0700374 const string expected_stderr =
375 "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: "
376 "\"vintf\"'\n"
377 "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface "
378 "--structured\n";
379 CaptureStderr();
Devin Moore097a3ab2020-03-11 16:08:44 -0700380 ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
Devin Moore7b8d5c92020-03-17 14:14:08 -0700381 GetLanguage(), &error));
Devin Moore0d0e3f62020-03-30 17:45:39 -0700382 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Steven Morelanda57d0a62019-07-30 09:41:14 -0700383 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700384}
385
Devin Moore7b8d5c92020-03-17 14:14:08 -0700386TEST_P(AidlTest, VintfRequiresStructured) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700387 AidlError error;
Devin Moore0d0e3f62020-03-30 17:45:39 -0700388 const string expected_stderr =
389 "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface "
390 "--structured\n";
391 CaptureStderr();
Devin Moore097a3ab2020-03-11 16:08:44 -0700392 ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
Devin Moore7b8d5c92020-03-17 14:14:08 -0700393 GetLanguage(), &error, {"--stability", "vintf"}));
Devin Moore0d0e3f62020-03-30 17:45:39 -0700394 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Steven Morelanda57d0a62019-07-30 09:41:14 -0700395 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700396}
397
Devin Moore7b8d5c92020-03-17 14:14:08 -0700398TEST_P(AidlTest, VintfRequiresSpecifiedStability) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700399 AidlError error;
Devin Moore097a3ab2020-03-11 16:08:44 -0700400 const string expected_stderr =
401 "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: "
402 "\"vintf\"'\n";
403 CaptureStderr();
404 ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
Devin Moore7b8d5c92020-03-17 14:14:08 -0700405 GetLanguage(), &error, {"--structured"}));
Devin Moore097a3ab2020-03-11 16:08:44 -0700406 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Steven Morelanda57d0a62019-07-30 09:41:14 -0700407 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700408}
409
Devin Moore7b8d5c92020-03-17 14:14:08 -0700410TEST_P(AidlTest, ParsesStabilityAnnotations) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700411 AidlError error;
Devin Moore7b8d5c92020-03-17 14:14:08 -0700412 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
413 GetLanguage(), &error, {"--structured", "--stability", "vintf"});
Steven Morelanda57d0a62019-07-30 09:41:14 -0700414 ASSERT_EQ(AidlError::OK, error);
415 ASSERT_NE(nullptr, parse_result);
416 const AidlInterface* interface = parse_result->AsInterface();
417 ASSERT_NE(nullptr, interface);
418 ASSERT_TRUE(interface->IsVintfStability());
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900419 typenames_.Reset();
Steven Morelanda57d0a62019-07-30 09:41:14 -0700420}
421
Jeongik Cha64783ed2019-06-07 18:30:54 +0900422TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) {
423 Options java_options = Options::From("aidl -o out --structured a/Foo.aidl");
Jeongik Cha88f95a82020-01-15 13:02:16 +0900424 Options cpp_options = Options::From("aidl --lang=cpp -o out -h out/include a/Foo.aidl");
425 Options cpp_structured_options =
Jeongik Cha64783ed2019-06-07 18:30:54 +0900426 Options::From("aidl --lang=cpp --structured -o out -h out/include a/Foo.aidl");
427 io_delegate_.SetFileContents(
Jeongik Cha88f95a82020-01-15 13:02:16 +0900428 "a/Foo.aidl",
429 StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo cpp_header \"Foo.h\" ;"));
Jeongik Cha64783ed2019-06-07 18:30:54 +0900430
431 EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_));
Jeongik Cha88f95a82020-01-15 13:02:16 +0900432 EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -0700433 const string expected_stderr =
Jeongik Cha88f95a82020-01-15 13:02:16 +0900434 "ERROR: a/Foo.aidl:1.48-52: Cannot declared parcelable in a --structured interface. "
Devin Moorec054bf82020-03-10 16:31:48 -0700435 "Parcelable must be defined in AIDL directly.\n";
436 CaptureStderr();
Jeongik Cha88f95a82020-01-15 13:02:16 +0900437 EXPECT_NE(0, ::android::aidl::compile_aidl(cpp_structured_options, io_delegate_));
Devin Moorec054bf82020-03-10 16:31:48 -0700438 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jeongik Cha64783ed2019-06-07 18:30:54 +0900439}
440
Devin Moore7b8d5c92020-03-17 14:14:08 -0700441TEST_P(AidlTest, AcceptsOneway) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700442 const string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
443 const string oneway_interface = "package a; oneway interface IBar { void f(int a); }";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700444 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage()));
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900445 typenames_.Reset();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700446 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage()));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700447}
Christopher Wileyef140932015-11-03 09:29:19 -0800448
Devin Moore7b8d5c92020-03-17 14:14:08 -0700449TEST_P(AidlTest, AcceptsAnnotatedOnewayMethod) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700450 const string oneway_method =
451 "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700452 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage()));
Artur Satayev91fe8712019-07-29 13:06:01 +0100453}
454
Steven Moreland65297cc2020-04-20 20:17:36 -0700455TEST_P(AidlTest, AnnotationsInMultiplePlaces) {
456 const string oneway_method =
457 "package a; interface IFoo { @UnsupportedAppUsage oneway @Hide void f(int a); }";
458 const AidlDefinedType* defined = Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage());
459 ASSERT_NE(nullptr, defined);
460 const AidlInterface* iface = defined->AsInterface();
461 ASSERT_NE(nullptr, iface);
462
463 const auto& methods = iface->GetMethods();
464 ASSERT_EQ(1u, methods.size());
465 const auto& method = methods[0];
466 const AidlTypeSpecifier& ret_type = method->GetType();
467
468 // TODO(b/151102494): these annotations should be on the method
469 ASSERT_NE(nullptr, ret_type.UnsupportedAppUsage());
470 ASSERT_TRUE(ret_type.IsHide());
471}
472
Devin Moore7b8d5c92020-03-17 14:14:08 -0700473TEST_P(AidlTest, WritesComments) {
Artur Satayev91fe8712019-07-29 13:06:01 +0100474 string foo_interface =
475 "package a; /* foo */ interface IFoo {"
476 " /* i */ int i();"
477 " /* j */ @nullable String j();"
478 " /* k */ @UnsupportedAppUsage oneway void k(int a); }";
479
Devin Moore7b8d5c92020-03-17 14:14:08 -0700480 auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, GetLanguage());
Artur Satayev91fe8712019-07-29 13:06:01 +0100481 EXPECT_NE(nullptr, parse_result);
482 EXPECT_EQ("/* foo */", parse_result->GetComments());
483
484 const AidlInterface* interface = parse_result->AsInterface();
485 EXPECT_EQ("/* i */", interface->GetMethods()[0]->GetComments());
486 EXPECT_EQ("/* j */", interface->GetMethods()[1]->GetComments());
487 EXPECT_EQ("/* k */", interface->GetMethods()[2]->GetComments());
488}
489
Christopher Wileyef140932015-11-03 09:29:19 -0800490TEST_F(AidlTest, ParsesPreprocessedFile) {
491 string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
492 io_delegate_.SetFileContents("path", simple_content);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700493 EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900494 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700495 EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved);
496 EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved);
Christopher Wileyef140932015-11-03 09:29:19 -0800497}
498
499TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
500 string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
501 io_delegate_.SetFileContents("path", simple_content);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900502
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700503 EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900504 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700505 EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved);
506 EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved);
Christopher Wileyef140932015-11-03 09:29:19 -0800507}
508
Devin Moore7b8d5c92020-03-17 14:14:08 -0700509TEST_P(AidlTest, PreferImportToPreprocessed) {
Christopher Wiley41544372015-11-03 14:52:29 -0800510 io_delegate_.SetFileContents("preprocessed", "interface another.IBar;");
511 io_delegate_.SetFileContents("one/IBar.aidl", "package one; "
512 "interface IBar {}");
513 preprocessed_files_.push_back("preprocessed");
Jiyong Park8c380532018-08-30 14:55:26 +0900514 import_paths_.emplace("");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900515 auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700516 typenames_, GetLanguage());
Christopher Wiley41544372015-11-03 14:52:29 -0800517 EXPECT_NE(nullptr, parse_result);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900518
Christopher Wiley41544372015-11-03 14:52:29 -0800519 // We expect to know about both kinds of IBar
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700520 EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved);
521 EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved);
Christopher Wiley41544372015-11-03 14:52:29 -0800522 // But if we request just "IBar" we should get our imported one.
Steven Moreland02e012e2018-08-02 14:58:10 -0700523 AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900524 ambiguous_type.Resolve(typenames_);
525 EXPECT_EQ("one.IBar", ambiguous_type.GetName());
Christopher Wiley41544372015-11-03 14:52:29 -0800526}
527
Jiyong Park8f6ec462020-01-19 20:52:47 +0900528// Special case of PreferImportToPreprocessed. Imported type should be preferred
529// even when the preprocessed file already has the same type.
Devin Moore7b8d5c92020-03-17 14:14:08 -0700530TEST_P(AidlTest, B147918827) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900531 io_delegate_.SetFileContents("preprocessed", "interface another.IBar;\ninterface one.IBar;");
532 io_delegate_.SetFileContents("one/IBar.aidl",
533 "package one; "
534 "interface IBar {}");
535 preprocessed_files_.push_back("preprocessed");
536 import_paths_.emplace("");
537 auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700538 typenames_, GetLanguage());
Jiyong Park8f6ec462020-01-19 20:52:47 +0900539 EXPECT_NE(nullptr, parse_result);
540
541 // We expect to know about both kinds of IBar
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700542 EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved);
543 EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved);
Jiyong Park8f6ec462020-01-19 20:52:47 +0900544 // But if we request just "IBar" we should get our imported one.
545 AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
546 ambiguous_type.Resolve(typenames_);
547 EXPECT_EQ("one.IBar", ambiguous_type.GetName());
548}
549
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800550TEST_F(AidlTest, WritePreprocessedFile) {
551 io_delegate_.SetFileContents("p/Outer.aidl",
552 "package p; parcelable Outer.Inner;");
553 io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;"
554 "interface IBar {}");
555
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900556 vector<string> args {
557 "aidl",
558 "--preprocess",
559 "preprocessed",
560 "p/Outer.aidl",
561 "one/IBar.aidl"};
562 Options options = Options::From(args);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800563 EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_));
564
565 string output;
566 EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output));
567 EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output);
568}
569
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900570TEST_F(AidlTest, JavaParcelableOutput) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100571 io_delegate_.SetFileContents(
572 "Rect.aidl",
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800573 "@Hide\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100574 "parcelable Rect {\n"
575 " // Comment\n"
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800576 " @Hide\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100577 " int x=5;\n"
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800578 " @Hide\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100579 " @UnsupportedAppUsage(maxTargetSdk = 28, trackingBug = 42, implicitMember = \"dummy\", "
Andrei Onea07fa3e02019-07-04 17:55:19 +0100580 "expectedSignature = \"dummy\", publicAlternatives = \"d\" \n + \"u\" + \n \"m\" \n + \"m\" "
581 "+ \"y\")\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100582 " int y;\n"
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900583 " ParcelFileDescriptor fd;\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100584 "}");
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900585
586 vector<string> args{"aidl", "Rect.aidl"};
587 Options options = Options::From(args);
588 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
589
590 string output;
591 EXPECT_TRUE(io_delegate_.GetWrittenContents("Rect.java", &output));
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900592 EXPECT_EQ(kExpectedJavaParcelableOutputContests, output);
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900593}
594
Devin Moore7b8d5c92020-03-17 14:14:08 -0700595TEST_P(AidlTest, RequireOuterClass) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700596 const string expected_stderr = "ERROR: p/IFoo.aidl:1.54-60: Failed to resolve 'Inner'\n";
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800597 io_delegate_.SetFileContents("p/Outer.aidl",
598 "package p; parcelable Outer.Inner;");
Jiyong Park8c380532018-08-30 14:55:26 +0900599 import_paths_.emplace("");
Devin Moore097a3ab2020-03-11 16:08:44 -0700600 CaptureStderr();
601 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
602 "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700603 typenames_, GetLanguage()));
Devin Moore097a3ab2020-03-11 16:08:44 -0700604 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800605}
606
Devin Moore7b8d5c92020-03-17 14:14:08 -0700607TEST_P(AidlTest, ParseCompoundParcelableFromPreprocess) {
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800608 io_delegate_.SetFileContents("preprocessed",
609 "parcelable p.Outer.Inner;");
610 preprocessed_files_.push_back("preprocessed");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900611 auto parse_result = Parse("p/IFoo.aidl", "package p; interface IFoo { void f(in Inner c); }",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700612 typenames_, GetLanguage());
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800613 // TODO(wiley): This should actually return nullptr because we require
614 // the outer class name. However, for legacy reasons,
615 // this behavior must be maintained. b/17415692
616 EXPECT_NE(nullptr, parse_result);
617}
618
Christopher Wiley632801d2015-11-05 14:15:49 -0800619TEST_F(AidlTest, FailOnParcelable) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700620 const string expected_foo_stderr =
621 "ERROR: p/IFoo.aidl:1.22-27: Refusing to generate code with unstructured parcelables. "
622 "Declared parcelables should be in their own file and/or cannot be used with --structured "
623 "interfaces.\n";
Steven Morelande2c64b42018-09-18 15:06:37 -0700624 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;");
625
Christopher Wiley632801d2015-11-05 14:15:49 -0800626 // By default, we shouldn't fail on parcelable.
Steven Morelande2c64b42018-09-18 15:06:37 -0700627 Options options1 = Options::From("aidl p/IFoo.aidl");
Devin Moore097a3ab2020-03-11 16:08:44 -0700628 CaptureStderr();
Jiyong Parkb034bf02018-07-30 17:44:33 +0900629 EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -0700630 EXPECT_EQ("", GetCapturedStderr());
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900631
Steven Morelande2c64b42018-09-18 15:06:37 -0700632 // -b considers this an error
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900633 Options options2 = Options::From("aidl -b p/IFoo.aidl");
Devin Moore097a3ab2020-03-11 16:08:44 -0700634 CaptureStderr();
Jiyong Parkb034bf02018-07-30 17:44:33 +0900635 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -0700636 EXPECT_EQ(expected_foo_stderr, GetCapturedStderr());
Steven Morelande2c64b42018-09-18 15:06:37 -0700637
Devin Moore097a3ab2020-03-11 16:08:44 -0700638 const string expected_bar_stderr =
639 "ERROR: p/IBar.aidl:1.22-26: Refusing to generate code with unstructured parcelables. "
640 "Declared parcelables should be in their own file and/or cannot be used with --structured "
641 "interfaces.\n";
Steven Morelande2c64b42018-09-18 15:06:37 -0700642 io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}");
643
Jiyong Parkda8c6932019-08-12 19:56:08 +0900644 // With '-b' option, a parcelable and an interface should fail.
Steven Morelande2c64b42018-09-18 15:06:37 -0700645 Options options3 = Options::From("aidl p/IBar.aidl");
Devin Moore097a3ab2020-03-11 16:08:44 -0700646 CaptureStderr();
Jiyong Parkda8c6932019-08-12 19:56:08 +0900647 EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -0700648 EXPECT_EQ("", GetCapturedStderr());
Steven Morelande2c64b42018-09-18 15:06:37 -0700649 Options options4 = Options::From("aidl -b p/IBar.aidl");
Devin Moore097a3ab2020-03-11 16:08:44 -0700650 CaptureStderr();
Steven Morelande2c64b42018-09-18 15:06:37 -0700651 EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -0700652 EXPECT_EQ(expected_bar_stderr, GetCapturedStderr());
Christopher Wiley632801d2015-11-05 14:15:49 -0800653}
654
Devin Moore7b8d5c92020-03-17 14:14:08 -0700655TEST_P(AidlTest, StructuredFailOnUnstructuredParcelable) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700656 const string expected_stderr =
657 "ERROR: ./o/WhoKnowsWhat.aidl:1.22-35: o.WhoKnowsWhat is not structured, but this is a "
658 "structured interface.\n";
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700659 io_delegate_.SetFileContents("o/WhoKnowsWhat.aidl", "package o; parcelable WhoKnowsWhat;");
660 import_paths_.emplace("");
Devin Moore097a3ab2020-03-11 16:08:44 -0700661 AidlError error;
662 CaptureStderr();
663 EXPECT_EQ(
664 nullptr,
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700665 Parse("p/IFoo.aidl",
666 "package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700667 typenames_, GetLanguage(), &error, {"--structured"}));
Devin Moore097a3ab2020-03-11 16:08:44 -0700668 EXPECT_EQ(expected_stderr, GetCapturedStderr());
669 EXPECT_EQ(AidlError::NOT_STRUCTURED, error);
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700670}
671
Devin Moore7b8d5c92020-03-17 14:14:08 -0700672TEST_P(AidlTest, FailOnDuplicateConstantNames) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700673 AidlError error;
674 const string expected_stderr =
675 "ERROR: p/IFoo.aidl:4.34-45: Found duplicate constant name 'DUPLICATED'\n";
676 CaptureStderr();
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900677 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
678 R"(package p;
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700679 interface IFoo {
680 const String DUPLICATED = "d";
681 const int DUPLICATED = 1;
682 }
683 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700684 typenames_, GetLanguage(), &error));
Devin Moore097a3ab2020-03-11 16:08:44 -0700685 EXPECT_EQ(expected_stderr, GetCapturedStderr());
686 EXPECT_EQ(AidlError::BAD_TYPE, error);
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700687}
688
Devin Moore7b8d5c92020-03-17 14:14:08 -0700689TEST_P(AidlTest, FailOnManyDefinedTypes) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700690 AidlError error;
Devin Moore5de18ed2020-04-02 13:52:29 -0700691 const string expected_stderr =
692 "ERROR: p/IFoo.aidl:3.33-38: You must declare only one type per file.\n";
Devin Moorec054bf82020-03-10 16:31:48 -0700693 CaptureStderr();
Steven Morelandc258abc2018-07-10 14:03:38 -0700694 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
695 R"(package p;
696 interface IFoo {}
Steven Morelandc258abc2018-07-10 14:03:38 -0700697 parcelable IBar {}
698 parcelable StructuredParcelable {}
699 interface IBaz {}
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900700 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700701 typenames_, GetLanguage(), &error));
Devin Moorec054bf82020-03-10 16:31:48 -0700702 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Steven Morelandc258abc2018-07-10 14:03:38 -0700703 // Parse success is important for clear error handling even if the cases aren't
704 // actually supported in code generation.
Devin Moore097a3ab2020-03-11 16:08:44 -0700705 EXPECT_EQ(AidlError::BAD_TYPE, error);
Steven Morelandc258abc2018-07-10 14:03:38 -0700706}
707
Devin Moore7b8d5c92020-03-17 14:14:08 -0700708TEST_P(AidlTest, FailOnNoDefinedTypes) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700709 AidlError error;
710 const string expected_stderr = "ERROR: p/IFoo.aidl:1.11-11: syntax error, unexpected $end\n";
711 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700712 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", typenames_, GetLanguage(), &error));
Devin Moore097a3ab2020-03-11 16:08:44 -0700713 EXPECT_EQ(expected_stderr, GetCapturedStderr());
714 EXPECT_EQ(AidlError::PARSE_ERROR, error);
Steven Morelandc258abc2018-07-10 14:03:38 -0700715}
716
Devin Moore7b8d5c92020-03-17 14:14:08 -0700717TEST_P(AidlTest, FailOnMalformedConstHexValue) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700718 AidlError error;
719 const string expected_stderr =
720 "ERROR: Could not parse hexvalue: 0xffffffffffffffffff at p/IFoo.aidl:3.50-70.\n";
721 CaptureStderr();
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900722 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
723 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700724 interface IFoo {
725 const int BAD_HEX_VALUE = 0xffffffffffffffffff;
726 }
727 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700728 typenames_, GetLanguage(), &error));
Devin Moore097a3ab2020-03-11 16:08:44 -0700729 EXPECT_EQ(expected_stderr, GetCapturedStderr());
730 EXPECT_EQ(AidlError::PARSE_ERROR, error);
Roshan Pius3b2203d2016-07-22 16:13:20 -0700731}
732
Devin Moore7b8d5c92020-03-17 14:14:08 -0700733TEST_P(AidlTest, ParsePositiveConstHexValue) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700734 AidlError error;
Devin Moore7b8d5c92020-03-17 14:14:08 -0700735 auto parse_result = Parse("p/IFoo.aidl",
736 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700737 interface IFoo {
738 const int POSITIVE_HEX_VALUE = 0xf5;
739 }
740 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700741 typenames_, GetLanguage(), &error);
742 EXPECT_NE(nullptr, parse_result);
743 const AidlInterface* interface = parse_result->AsInterface();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700744 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700745 const auto& cpp_constants = interface->GetConstantDeclarations();
746 EXPECT_EQ((size_t)1, cpp_constants.size());
747 EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_constants[0]->GetName());
Will McVickerd7d18df2019-09-12 13:40:50 -0700748 EXPECT_TRUE(cpp_constants[0]->CheckValid(typenames_));
Steven Moreland860b1942018-08-16 14:59:28 -0700749 EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700750}
751
Devin Moore7b8d5c92020-03-17 14:14:08 -0700752TEST_P(AidlTest, ParseNegativeConstHexValue) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700753 AidlError error;
Devin Moore7b8d5c92020-03-17 14:14:08 -0700754 auto parse_result = Parse("p/IFoo.aidl",
755 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700756 interface IFoo {
757 const int NEGATIVE_HEX_VALUE = 0xffffffff;
758 }
759 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -0700760 typenames_, GetLanguage(), &error);
761 ASSERT_NE(nullptr, parse_result);
762 const AidlInterface* interface = parse_result->AsInterface();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700763 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700764 const auto& cpp_constants = interface->GetConstantDeclarations();
765 EXPECT_EQ((size_t)1, cpp_constants.size());
766 EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_constants[0]->GetName());
Will McVickerd7d18df2019-09-12 13:40:50 -0700767 EXPECT_EQ(true, cpp_constants[0]->CheckValid(typenames_));
Steven Moreland860b1942018-08-16 14:59:28 -0700768 EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700769}
770
Devin Moore7b8d5c92020-03-17 14:14:08 -0700771TEST_P(AidlTest, UnderstandsNestedParcelables) {
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700772 io_delegate_.SetFileContents(
773 "p/Outer.aidl",
774 "package p; parcelable Outer.Inner cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900775 import_paths_.emplace("");
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700776 const string input_path = "p/IFoo.aidl";
777 const string input = "package p; import p.Outer; interface IFoo"
778 " { Outer.Inner get(); }";
779
Devin Moore7b8d5c92020-03-17 14:14:08 -0700780 auto parse_result = Parse(input_path, input, typenames_, GetLanguage());
781 EXPECT_NE(nullptr, parse_result);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900782
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700783 EXPECT_TRUE(typenames_.ResolveTypename("p.Outer.Inner").is_resolved);
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700784 // C++ uses "::" instead of "." to refer to a inner class.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900785 AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, "");
786 EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_));
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700787}
788
Devin Moore7b8d5c92020-03-17 14:14:08 -0700789TEST_P(AidlTest, UnderstandsNativeParcelables) {
Christopher Wiley9078d722015-11-17 10:23:49 -0800790 io_delegate_.SetFileContents(
791 "p/Bar.aidl",
Casey Dahlincd639212015-12-15 12:51:04 -0800792 "package p; parcelable Bar cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900793 import_paths_.emplace("");
Christopher Wiley9078d722015-11-17 10:23:49 -0800794 const string input_path = "p/IFoo.aidl";
795 const string input = "package p; import p.Bar; interface IFoo { }";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700796 auto parse_result = Parse(input_path, input, typenames_, GetLanguage());
797 EXPECT_NE(nullptr, parse_result);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700798 EXPECT_TRUE(typenames_.ResolveTypename("p.Bar").is_resolved);
Devin Moore7b8d5c92020-03-17 14:14:08 -0700799 AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, "");
800 native_type.Resolve(typenames_);
801
802 EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type, typenames_));
803 // C++ understands C++ specific stuff
804 EXPECT_EQ("::p::Bar", cpp::CppNameOf(native_type, typenames_));
805 set<string> headers;
806 cpp::AddHeaders(native_type, typenames_, headers);
807 EXPECT_EQ(1u, headers.size());
808 EXPECT_EQ(1u, headers.count("baz/header"));
Christopher Wiley9078d722015-11-17 10:23:49 -0800809}
810
Christopher Wileyf8136192016-04-12 14:19:35 -0700811TEST_F(AidlTest, WritesCorrectDependencyFile) {
812 // While the in tree build system always gives us an output file name,
813 // other android tools take advantage of our ability to infer the intended
814 // file name. This test makes sure we handle this correctly.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900815 vector<string> args = {
816 "aidl",
817 "-d dep/file/path",
818 "-o place/for/output",
819 "p/IFoo.aidl"};
820 Options options = Options::From(args);
821 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900822 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyf8136192016-04-12 14:19:35 -0700823 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900824 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Christopher Wileyf8136192016-04-12 14:19:35 -0700825 EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
826}
827
Dan Willemsen93298ee2016-11-10 23:55:55 -0800828TEST_F(AidlTest, WritesCorrectDependencyFileNinja) {
829 // While the in tree build system always gives us an output file name,
830 // other android tools take advantage of our ability to infer the intended
831 // file name. This test makes sure we handle this correctly.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900832 vector<string> args = {
833 "aidl",
834 "-d dep/file/path",
835 "--ninja",
836 "-o place/for/output",
837 "p/IFoo.aidl"};
838 Options options = Options::From(args);
839 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900840 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800841 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900842 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800843 EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents);
844}
845
Jiyong Parkdf202122019-09-30 20:48:35 +0900846TEST_F(AidlTest, WritesTrivialDependencyFileForParcelableDeclaration) {
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700847 // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does
848 // this by calling aidl with every .aidl file it finds, then parsing the
849 // generated dependency files. Those that reference .java output files are
850 // for interfaces and those that do not are parcelables. However, for both
851 // parcelables and interfaces, we *must* generate a non-empty dependency file.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900852 vector<string> args = {
853 "aidl",
854 "-o place/for/output",
855 "-d dep/file/path",
856 "p/Foo.aidl"};
857 Options options = Options::From(args);
858 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900859 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700860 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900861 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Jiyong Parkdf202122019-09-30 20:48:35 +0900862 EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDeclarationDepFileContents);
863}
864
865TEST_F(AidlTest, WritesDependencyFileForStructuredParcelable) {
866 vector<string> args = {
867 "aidl",
868 "--structured",
869 "-o place/for/output",
870 "-d dep/file/path",
871 "p/Foo.aidl"};
872 Options options = Options::From(args);
873 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo {int a;}");
874 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
875 string actual_dep_file_contents;
876 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
877 EXPECT_EQ(actual_dep_file_contents, kExpectedStructuredParcelableDepFileContents);
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700878}
879
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900880TEST_F(AidlTest, NoJavaOutputForParcelableDeclaration) {
881 vector<string> args = {
882 "aidl",
883 "--lang=java",
884 "-o place/for/output",
885 "p/Foo.aidl"};
886 Options options = Options::From(args);
887 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;");
888 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
889 string output_file_contents;
890 EXPECT_FALSE(io_delegate_.GetWrittenContents(options.OutputFile(), &output_file_contents));
891}
892
Jeongik Cha08ca2182019-11-21 14:01:13 +0900893// TODO(b/136048684)
Devin Moore7b8d5c92020-03-17 14:14:08 -0700894TEST_P(AidlTest, PrimitiveList) {
Devin Moore097a3ab2020-03-11 16:08:44 -0700895 const string primitive_interface =
Jeongik Cha08ca2182019-11-21 14:01:13 +0900896 "package a; interface IFoo {\n"
897 " List<int> foo(); }";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700898 string expected_stderr;
899 switch (GetLanguage()) {
900 case Options::Language::CPP:
901 expected_stderr =
902 "ERROR: a/IFoo.aidl:2.1-7: List<int> is not supported. List in cpp supports only "
903 "String and IBinder.\n";
904 break;
905 case Options::Language::JAVA:
906 expected_stderr =
907 "ERROR: a/IFoo.aidl:2.1-7: List<int> is not supported. List in Java supports only "
908 "String, IBinder, and ParcelFileDescriptor.\n";
909 break;
910 case Options::Language::NDK:
911 expected_stderr =
912 "ERROR: a/IFoo.aidl:2.1-7: A generic type cannot have any primitive type parameters.\n";
913 break;
914 default:
915 AIDL_FATAL(AIDL_LOCATION_HERE)
916 << "Unexpected Options::Language enumerator: " << static_cast<size_t>(GetLanguage());
917 }
918 CaptureStderr();
919 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, typenames_, GetLanguage()));
920 EXPECT_EQ(expected_stderr, GetCapturedStderr());
921 typenames_.Reset();
922
923 string primitive_parcelable =
924 "package a; parcelable IFoo {\n"
Jeongik Cha08ca2182019-11-21 14:01:13 +0900925 " List<int> foo;}";
Devin Moore097a3ab2020-03-11 16:08:44 -0700926 CaptureStderr();
Devin Moore7b8d5c92020-03-17 14:14:08 -0700927 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_parcelable, typenames_, GetLanguage()));
928 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jeongik Cha08ca2182019-11-21 14:01:13 +0900929}
930
Jiyong Parkf8d53612020-05-04 14:06:13 +0900931TEST_P(AidlTest, RejectsPrimitiveListInStableAidl) {
932 AidlError error;
933 string expected_stderr =
934 "ERROR: a/IFoo.aidl:2.7-11: "
935 "Encountered an untyped List or Map. The use of untyped List/Map is "
936 "prohibited because it is not guaranteed that the objects in the list are recognizable in "
937 "the receiving side. Consider switching to an array or a generic List/Map.\n";
938 if (GetLanguage() != Options::Language::JAVA) {
939 expected_stderr =
940 "ERROR: a/IFoo.aidl:2.1-7: "
941 "Currently, only the Java backend supports non-generic List.\n";
942 }
943
944 const string primitive_interface =
945 "package a; interface IFoo {\n"
946 " List foo(); }";
947 CaptureStderr();
948 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, typenames_, GetLanguage(), &error,
949 {"--structured"}));
950 EXPECT_EQ(expected_stderr, GetCapturedStderr());
951 typenames_.Reset();
952
953 string primitive_parcelable =
954 "package a; parcelable IFoo {\n"
955 " List foo;}";
956 CaptureStderr();
957 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_parcelable, typenames_, GetLanguage(), &error,
958 {"--structured"}));
959 EXPECT_EQ(expected_stderr, GetCapturedStderr());
960}
961
Jiyong Park02da7422018-07-16 16:00:26 +0900962TEST_F(AidlTest, ApiDump) {
963 io_delegate_.SetFileContents(
964 "foo/bar/IFoo.aidl",
965 "package foo.bar;\n"
966 "import foo.bar.Data;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900967 "// comment @hide\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900968 "interface IFoo {\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900969 " /* @hide */\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900970 " int foo(out int[] a, String b, boolean c, inout List<String> d);\n"
971 " int foo2(@utf8InCpp String x, inout List<String> y);\n"
972 " IFoo foo3(IFoo foo);\n"
973 " Data getData();\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900974 " // @hide\n"
Jiyong Parka428d212018-08-29 22:26:30 +0900975 " const int A = 1;\n"
976 " const String STR = \"Hello\";\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900977 "}\n");
978 io_delegate_.SetFileContents("foo/bar/Data.aidl",
979 "package foo.bar;\n"
980 "import foo.bar.IFoo;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900981 "/* @hide*/\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900982 "parcelable Data {\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900983 " // @hide\n"
Jiyong Parka468e2a2018-08-29 21:25:18 +0900984 " int x = 10;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900985 " // @hide\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900986 " int y;\n"
987 " IFoo foo;\n"
988 " List<IFoo> a;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900989 " /*@hide2*/\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900990 " List<foo.bar.IFoo> b;\n"
Jeongik Cha997281d2020-01-16 15:23:59 +0900991 " // It should be @hide property\n"
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900992 " @nullable String[] c;\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900993 "}\n");
994 io_delegate_.SetFileContents("api.aidl", "");
Jiyong Park633246c2019-11-25 10:50:05 +0900995 vector<string> args = {"aidl", "--dumpapi", "--out=dump", "--include=.",
996 "foo/bar/IFoo.aidl", "foo/bar/Data.aidl"};
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900997 Options options = Options::From(args);
Jiyong Park02da7422018-07-16 16:00:26 +0900998 bool result = dump_api(options, io_delegate_);
999 ASSERT_TRUE(result);
1000 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +09001001 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
Paul Trautrimb01451d2020-02-27 13:10:16 +09001002 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
Jeongik Cha997281d2020-01-16 15:23:59 +09001003/* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +09001004interface IFoo {
Jeongik Cha997281d2020-01-16 15:23:59 +09001005 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +09001006 int foo(out int[] a, String b, boolean c, inout List<String> d);
1007 int foo2(@utf8InCpp String x, inout List<String> y);
1008 foo.bar.IFoo foo3(foo.bar.IFoo foo);
1009 foo.bar.Data getData();
Jeongik Cha997281d2020-01-16 15:23:59 +09001010 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +09001011 const int A = 1;
1012 const String STR = "Hello";
1013}
Paul Trautrimb01451d2020-02-27 13:10:16 +09001014)"));
Jiyong Park02da7422018-07-16 16:00:26 +09001015
Jiyong Parke59c3682018-09-11 23:10:25 +09001016 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual));
Paul Trautrimb01451d2020-02-27 13:10:16 +09001017 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
Jeongik Cha997281d2020-01-16 15:23:59 +09001018/* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +09001019parcelable Data {
Jeongik Cha997281d2020-01-16 15:23:59 +09001020 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +09001021 int x = 10;
Jeongik Cha997281d2020-01-16 15:23:59 +09001022 /* @hide */
Jiyong Parke59c3682018-09-11 23:10:25 +09001023 int y;
1024 foo.bar.IFoo foo;
1025 List<foo.bar.IFoo> a;
1026 List<foo.bar.IFoo> b;
Jeongik Cha997281d2020-01-16 15:23:59 +09001027 /* @hide */
Jeongik Cha3271ffa2018-12-04 15:19:20 +09001028 @nullable String[] c;
Jiyong Park02da7422018-07-16 16:00:26 +09001029}
Paul Trautrimb01451d2020-02-27 13:10:16 +09001030)"));
Jiyong Park02da7422018-07-16 16:00:26 +09001031}
1032
Jiyong Parked65bf42018-08-28 15:43:27 +09001033TEST_F(AidlTest, ApiDumpWithManualIds) {
1034 io_delegate_.SetFileContents(
1035 "foo/bar/IFoo.aidl",
1036 "package foo.bar;\n"
1037 "interface IFoo {\n"
1038 " int foo() = 1;\n"
1039 " int bar() = 2;\n"
1040 " int baz() = 10;\n"
1041 "}\n");
1042
Jiyong Parke59c3682018-09-11 23:10:25 +09001043 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +09001044 Options options = Options::From(args);
1045 bool result = dump_api(options, io_delegate_);
1046 ASSERT_TRUE(result);
1047 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +09001048 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
Paul Trautrimb01451d2020-02-27 13:10:16 +09001049 EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar;
Jiyong Parke59c3682018-09-11 23:10:25 +09001050interface IFoo {
1051 int foo() = 1;
1052 int bar() = 2;
1053 int baz() = 10;
Jiyong Parked65bf42018-08-28 15:43:27 +09001054}
Paul Trautrimb01451d2020-02-27 13:10:16 +09001055)"));
Jiyong Parked65bf42018-08-28 15:43:27 +09001056}
1057
1058TEST_F(AidlTest, ApiDumpWithManualIdsOnlyOnSomeMethods) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001059 const string expected_stderr =
1060 "ERROR: foo/bar/IFoo.aidl:4.8-12: You must either assign id's to all methods or to none of "
1061 "them.\n";
Jiyong Parked65bf42018-08-28 15:43:27 +09001062 io_delegate_.SetFileContents(
1063 "foo/bar/IFoo.aidl",
1064 "package foo.bar;\n"
1065 "interface IFoo {\n"
1066 " int foo() = 1;\n"
1067 " int bar();\n"
1068 " int baz() = 10;\n"
1069 "}\n");
1070
Jiyong Parke59c3682018-09-11 23:10:25 +09001071 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +09001072 Options options = Options::From(args);
Devin Moore097a3ab2020-03-11 16:08:44 -07001073 CaptureStderr();
Jiyong Parked65bf42018-08-28 15:43:27 +09001074 EXPECT_FALSE(dump_api(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001075 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Parked65bf42018-08-28 15:43:27 +09001076}
1077
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001078TEST_F(AidlTest, CheckNumGenericTypeSecifier) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001079 const string expected_list_stderr =
1080 "ERROR: p/IFoo.aidl:1.37-41: List must have only one type parameter.\n";
1081 const string expected_map_stderr =
1082 "ERROR: p/IFoo.aidl:1.37-40: Map must have 0 or 2 type parameters, but got 'Map<String>'\n";
Jiyong Park6f77e0c2018-07-28 16:55:44 +09001083 Options options = Options::From("aidl p/IFoo.aidl IFoo.java");
1084 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001085 "package p; interface IFoo {"
1086 "void foo(List<String, String> a);}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001087 CaptureStderr();
Jiyong Parkb034bf02018-07-30 17:44:33 +09001088 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001089 EXPECT_EQ(expected_list_stderr, GetCapturedStderr());
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001090
Jiyong Park6f77e0c2018-07-28 16:55:44 +09001091 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001092 "package p; interface IFoo {"
1093 "void foo(Map<String> a);}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001094 CaptureStderr();
Jiyong Parkb034bf02018-07-30 17:44:33 +09001095 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001096 EXPECT_EQ(expected_map_stderr, GetCapturedStderr());
Jiyong Parkb034bf02018-07-30 17:44:33 +09001097}
1098
Jeongik Chae48d9942020-01-02 17:39:00 +09001099TEST_F(AidlTest, CheckTypeParameterInMapType) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001100 const string expected_stderr =
1101 "ERROR: p/IFoo.aidl:1.28-31: The type of key in map must be String, but it is 'p.Bar'\n";
Jeongik Chae48d9942020-01-02 17:39:00 +09001102 Options options = Options::From("aidl -I p p/IFoo.aidl");
1103 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar { String s; }");
1104
1105 io_delegate_.SetFileContents("p/IFoo.aidl",
1106 "package p; interface IFoo {"
1107 "Map<String, Bar> foo();}");
1108 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1109
1110 io_delegate_.SetFileContents("p/IFoo.aidl",
1111 "package p; interface IFoo {"
1112 "Map<Bar, Bar> foo();}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001113 CaptureStderr();
Jeongik Chae48d9942020-01-02 17:39:00 +09001114 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001115 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jeongik Chae48d9942020-01-02 17:39:00 +09001116
1117 io_delegate_.SetFileContents("p/IFoo.aidl",
1118 "package p; interface IFoo {"
1119 "Map<String, String> foo();}");
1120 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1121
1122 io_delegate_.SetFileContents("p/IFoo.aidl",
1123 "package p; interface IFoo {"
1124 "Map<String, ParcelFileDescriptor> foo();}");
1125 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1126}
1127
Jeongik Chadf76dc72019-11-28 00:08:47 +09001128TEST_F(AidlTest, WrongGenericType) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001129 const string expected_stderr = "ERROR: p/IFoo.aidl:1.28-34: String is not a generic type.\n";
Jeongik Chadf76dc72019-11-28 00:08:47 +09001130 Options options = Options::From("aidl p/IFoo.aidl IFoo.java");
1131 io_delegate_.SetFileContents(options.InputFiles().front(),
1132 "package p; interface IFoo {"
1133 "String<String> foo(); }");
Devin Moore097a3ab2020-03-11 16:08:44 -07001134 CaptureStderr();
Jeongik Chadf76dc72019-11-28 00:08:47 +09001135 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001136 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jeongik Chadf76dc72019-11-28 00:08:47 +09001137}
1138
1139TEST_F(AidlTest, UserDefinedUnstructuredGenericParcelableType) {
1140 Options optionsForParcelable = Options::From("aidl -I p p/Bar.aidl");
1141 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, T>;");
Devin Moore097a3ab2020-03-11 16:08:44 -07001142 CaptureStderr();
Jeongik Chadf76dc72019-11-28 00:08:47 +09001143 EXPECT_NE(0, ::android::aidl::compile_aidl(optionsForParcelable, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001144 EXPECT_EQ("ERROR: p/Bar.aidl:1.22-26: Every type parameter should be unique.\n",
1145 GetCapturedStderr());
Jeongik Chadf76dc72019-11-28 00:08:47 +09001146
1147 Options options = Options::From("aidl -I p p/IFoo.aidl");
1148 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar;");
1149 io_delegate_.SetFileContents("p/IFoo.aidl",
1150 "package p; interface IFoo {"
1151 "Bar<String, String> foo();}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001152 CaptureStderr();
Jeongik Chadf76dc72019-11-28 00:08:47 +09001153 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001154 EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar is not a generic type.\n", GetCapturedStderr());
Jeongik Chadf76dc72019-11-28 00:08:47 +09001155 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T>;");
Devin Moore097a3ab2020-03-11 16:08:44 -07001156 CaptureStderr();
Jeongik Chadf76dc72019-11-28 00:08:47 +09001157 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001158 EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar must have 1 type parameters, but got 2\n",
1159 GetCapturedStderr());
Jeongik Chadf76dc72019-11-28 00:08:47 +09001160 io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, V>;");
1161 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1162 io_delegate_.SetFileContents("p/IFoo.aidl",
1163 "package p; interface IFoo {"
1164 "Bar<String, ParcelFileDescriptor> foo();}");
1165 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jeongik Chae74c86d2019-12-12 16:54:03 +09001166
1167 io_delegate_.SetFileContents("p/IFoo.aidl",
1168 "package p; interface IFoo {"
1169 "Bar<int, long> foo();}");
1170
1171 io_delegate_.SetFileContents("p/IFoo.aidl",
1172 "package p; interface IFoo {"
1173 "Bar<int[], long[]> foo();}");
1174
1175 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jeongik Chadf76dc72019-11-28 00:08:47 +09001176}
1177
Jeongik Cha0e426012019-07-29 15:57:02 +09001178TEST_F(AidlTest, FailOnMultipleTypesInSingleFile) {
1179 std::vector<std::string> rawOptions{"aidl --lang=java -o out foo/bar/Foo.aidl",
1180 "aidl --lang=cpp -o out -h out/include foo/bar/Foo.aidl"};
Devin Moore5de18ed2020-04-02 13:52:29 -07001181 for (const auto& rawOption : rawOptions) {
1182 string expected_stderr =
1183 "ERROR: foo/bar/Foo.aidl:3.1-10: You must declare only one type per file.\n";
Jeongik Cha0e426012019-07-29 15:57:02 +09001184 Options options = Options::From(rawOption);
1185 io_delegate_.SetFileContents(options.InputFiles().front(),
1186 "package foo.bar;\n"
1187 "interface IFoo1 { int foo(); }\n"
1188 "interface IFoo2 { int foo(); }\n"
1189 "parcelable Data1 { int a; int b;}\n"
1190 "parcelable Data2 { int a; int b;}\n");
Devin Moore097a3ab2020-03-11 16:08:44 -07001191 CaptureStderr();
Jeongik Cha0e426012019-07-29 15:57:02 +09001192 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001193 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Parkb034bf02018-07-30 17:44:33 +09001194
Jeongik Cha0e426012019-07-29 15:57:02 +09001195 io_delegate_.SetFileContents(options.InputFiles().front(),
1196 "package foo.bar;\n"
1197 "interface IFoo1 { int foo(); }\n"
1198 "interface IFoo2 { int foo(); }\n");
Devin Moore097a3ab2020-03-11 16:08:44 -07001199 CaptureStderr();
Jeongik Cha0e426012019-07-29 15:57:02 +09001200 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001201 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Parkb034bf02018-07-30 17:44:33 +09001202
Devin Moore5de18ed2020-04-02 13:52:29 -07001203 expected_stderr = "ERROR: foo/bar/Foo.aidl:3.11-17: You must declare only one type per file.\n";
Jeongik Cha0e426012019-07-29 15:57:02 +09001204 io_delegate_.SetFileContents(options.InputFiles().front(),
1205 "package foo.bar;\n"
1206 "parcelable Data1 { int a; int b;}\n"
1207 "parcelable Data2 { int a; int b;}\n");
Devin Moore097a3ab2020-03-11 16:08:44 -07001208 CaptureStderr();
Jeongik Cha0e426012019-07-29 15:57:02 +09001209 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001210 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Parkb034bf02018-07-30 17:44:33 +09001211 }
1212}
1213
Devin Moorebdba2a82020-03-31 15:19:02 -07001214TEST_P(AidlTest, FailParseOnEmptyFile) {
1215 const string contents = "";
1216 const string expected_stderr = "ERROR: a/IFoo.aidl:1.1-1: syntax error, unexpected $end\n";
1217 CaptureStderr();
1218 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", contents, typenames_, GetLanguage()));
1219 EXPECT_EQ(expected_stderr, GetCapturedStderr());
1220}
1221
Jiyong Parkb034bf02018-07-30 17:44:33 +09001222TEST_F(AidlTest, MultipleInputFiles) {
1223 Options options = Options::From(
Jiyong Park633246c2019-11-25 10:50:05 +09001224 "aidl --lang=java -o out -I . foo/bar/IFoo.aidl foo/bar/Data.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001225
1226 io_delegate_.SetFileContents(options.InputFiles().at(0),
1227 "package foo.bar;\n"
1228 "import foo.bar.Data;\n"
1229 "interface IFoo { Data getData(); }\n");
1230
1231 io_delegate_.SetFileContents(options.InputFiles().at(1),
1232 "package foo.bar;\n"
1233 "import foo.bar.IFoo;\n"
1234 "parcelable Data { IFoo foo; }\n");
1235
1236 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1237
1238 string content;
1239 for (const auto file : {
1240 "out/foo/bar/IFoo.java", "out/foo/bar/Data.java"}) {
1241 content.clear();
1242 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
1243 EXPECT_FALSE(content.empty());
1244 }
1245}
1246
1247TEST_F(AidlTest, MultipleInputFilesCpp) {
1248 Options options = Options::From("aidl --lang=cpp -o out -h out/include "
Jiyong Park633246c2019-11-25 10:50:05 +09001249 "-I . foo/bar/IFoo.aidl foo/bar/Data.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001250
1251 io_delegate_.SetFileContents(options.InputFiles().at(0),
1252 "package foo.bar;\n"
1253 "import foo.bar.Data;\n"
1254 "interface IFoo { Data getData(); }\n");
1255
1256 io_delegate_.SetFileContents(options.InputFiles().at(1),
1257 "package foo.bar;\n"
1258 "import foo.bar.IFoo;\n"
1259 "parcelable Data { IFoo foo; }\n");
1260
1261 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1262
1263 string content;
1264 for (const auto file : {
Jiyong Parkb03551f2018-08-06 19:20:51 +09001265 "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp",
Jiyong Parkb034bf02018-07-30 17:44:33 +09001266 "out/include/foo/bar/IFoo.h", "out/include/foo/bar/Data.h",
1267 "out/include/foo/bar/BpFoo.h", "out/include/foo/bar/BpData.h",
1268 "out/include/foo/bar/BnFoo.h", "out/include/foo/bar/BnData.h"}) {
1269 content.clear();
1270 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
1271 EXPECT_FALSE(content.empty());
1272 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001273}
1274
Devin Moore097a3ab2020-03-11 16:08:44 -07001275TEST_F(AidlTest, ConflictWithMetaTransactionGetVersion) {
1276 const string expected_stderr =
1277 "ERROR: p/IFoo.aidl:1.31-51: method getInterfaceVersion() is reserved for internal use.\n";
Jiyong Park309668e2018-07-28 16:55:44 +09001278 Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl");
1279 // int getInterfaceVersion() is one of the meta transactions
1280 io_delegate_.SetFileContents(options.InputFiles().front(),
1281 "package p; interface IFoo {"
1282 "int getInterfaceVersion(); }");
Devin Moore097a3ab2020-03-11 16:08:44 -07001283 CaptureStderr();
Jiyong Parkb034bf02018-07-30 17:44:33 +09001284 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001285 EXPECT_EQ(expected_stderr, GetCapturedStderr());
1286}
Jiyong Park309668e2018-07-28 16:55:44 +09001287
Devin Moore097a3ab2020-03-11 16:08:44 -07001288TEST_F(AidlTest, ConflictWithSimilarMetaTransaction) {
1289 // boolean getInterfaceVersion() is not a meta transaction, but should be
1290 // prevented because return type is not part of a method signature
1291 const string expected_stderr =
1292 "ERROR: p/IFoo.aidl:1.35-55: method getInterfaceVersion() is reserved for internal use.\n";
1293 Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl");
Jiyong Park309668e2018-07-28 16:55:44 +09001294 io_delegate_.SetFileContents(options.InputFiles().front(),
1295 "package p; interface IFoo {"
1296 "boolean getInterfaceVersion(); }");
Devin Moore097a3ab2020-03-11 16:08:44 -07001297 CaptureStderr();
Jiyong Parkb034bf02018-07-30 17:44:33 +09001298 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001299 EXPECT_EQ(expected_stderr, GetCapturedStderr());
1300}
Jiyong Park309668e2018-07-28 16:55:44 +09001301
Devin Moore097a3ab2020-03-11 16:08:44 -07001302TEST_F(AidlTest, ConflictWithMetaTransactionGetName) {
Jiyong Park309668e2018-07-28 16:55:44 +09001303 // this is another reserved name
Devin Moore097a3ab2020-03-11 16:08:44 -07001304 const string expected_stderr =
1305 "ERROR: p/IFoo.aidl:1.34-53: method getTransactionName(int) is reserved for internal use.\n";
1306 Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl");
Jiyong Park309668e2018-07-28 16:55:44 +09001307 io_delegate_.SetFileContents(options.InputFiles().front(),
1308 "package p; interface IFoo {"
1309 "String getTransactionName(int code); }");
Devin Moore097a3ab2020-03-11 16:08:44 -07001310 CaptureStderr();
Jiyong Parkb034bf02018-07-30 17:44:33 +09001311 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001312 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Park309668e2018-07-28 16:55:44 +09001313
1314 // this is not a meta interface method as it differs type arguments
1315 io_delegate_.SetFileContents(options.InputFiles().front(),
1316 "package p; interface IFoo {"
1317 "String getTransactionName(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +09001318 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +09001319}
1320
Daniel Norman85aed542019-08-21 12:01:14 -07001321TEST_F(AidlTest, DifferentOrderAnnotationsInCheckAPI) {
Jeongik Cha3271ffa2018-12-04 15:19:20 +09001322 Options options = Options::From("aidl --checkapi old new");
1323 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1324 "package p; interface IFoo{ @utf8InCpp @nullable String foo();}");
1325 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1326 "package p; interface IFoo{ @nullable @utf8InCpp String foo();}");
1327
1328 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1329}
1330
Jiyong Park3656c3c2018-08-01 20:02:01 +09001331TEST_F(AidlTest, SuccessOnIdenticalApiDumps) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001332 Options options = Options::From("aidl --checkapi old new");
1333 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
1334 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001335
1336 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1337}
1338
Daniel Norman85aed542019-08-21 12:01:14 -07001339class AidlTestCompatibleChanges : public AidlTest {
1340 protected:
1341 Options options_ = Options::From("aidl --checkapi old new");
1342};
1343
1344TEST_F(AidlTestCompatibleChanges, NewType) {
1345 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1346 "package p;"
1347 "interface IFoo {"
1348 " void foo(int a);"
1349 "}");
1350 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1351 "package p;"
1352 "interface IFoo {"
1353 " void foo(int a);"
1354 "}");
1355 io_delegate_.SetFileContents("new/p/IBar.aidl",
1356 "package p;"
1357 "interface IBar {"
1358 " void bar();"
1359 "}");
1360 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1361}
1362
1363TEST_F(AidlTestCompatibleChanges, NewMethod) {
1364 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1365 "package p;"
1366 "interface IFoo {"
1367 " void foo(int a);"
1368 "}");
1369 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1370 "package p;"
1371 "interface IFoo {"
1372 " void foo(int a);"
1373 " void bar();"
Jiyong Parkf8d53612020-05-04 14:06:13 +09001374 " void baz(in List<IFoo> arg);"
Daniel Norman85aed542019-08-21 12:01:14 -07001375 "}");
1376 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1377}
1378
1379TEST_F(AidlTestCompatibleChanges, NewField) {
1380 io_delegate_.SetFileContents("old/p/Data.aidl",
1381 "package p;"
1382 "parcelable Data {"
1383 " int foo;"
1384 "}");
1385 io_delegate_.SetFileContents("new/p/Data.aidl",
1386 "package p;"
1387 "parcelable Data {"
1388 " int foo;"
Steven Moreland370ed342020-04-28 18:14:39 -07001389 " int bar = 0;"
Jiyong Parkf8d53612020-05-04 14:06:13 +09001390 " @nullable List<Data> list;"
Daniel Norman85aed542019-08-21 12:01:14 -07001391 "}");
1392 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1393}
1394
1395TEST_F(AidlTestCompatibleChanges, NewEnumerator) {
1396 io_delegate_.SetFileContents("old/p/Enum.aidl",
1397 "package p;"
1398 "enum Enum {"
1399 " FOO = 1,"
1400 "}");
1401 io_delegate_.SetFileContents("new/p/Enum.aidl",
1402 "package p;"
1403 "enum Enum {"
1404 " FOO = 1,"
1405 " BAR = 2,"
1406 "}");
1407 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1408}
1409
1410TEST_F(AidlTestCompatibleChanges, ReorderedEnumerator) {
1411 io_delegate_.SetFileContents("old/p/Enum.aidl",
1412 "package p;"
1413 "enum Enum {"
1414 " FOO = 1,"
1415 " BAR = 2,"
1416 "}");
1417 io_delegate_.SetFileContents("new/p/Enum.aidl",
1418 "package p;"
1419 "enum Enum {"
1420 " BAR = 2,"
1421 " FOO = 1,"
1422 "}");
1423 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1424}
1425
1426TEST_F(AidlTestCompatibleChanges, NewPackage) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001427 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1428 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001429 "interface IFoo {"
1430 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001431 "}");
1432 io_delegate_.SetFileContents("old/p/Data.aidl",
1433 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001434 "parcelable Data {"
1435 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001436 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001437 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1438 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001439 "interface IFoo {"
1440 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001441 "}");
1442 io_delegate_.SetFileContents("new/p/Data.aidl",
1443 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001444 "parcelable Data {"
1445 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +09001446 "}");
1447 io_delegate_.SetFileContents("new/q/IFoo.aidl",
1448 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001449 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001450 " void foo(int a);"
1451 "}");
1452 io_delegate_.SetFileContents("new/q/Data.aidl",
1453 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001454 "parcelable Data {"
1455 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001456 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001457 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1458}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001459
Daniel Norman85aed542019-08-21 12:01:14 -07001460TEST_F(AidlTestCompatibleChanges, ArgNameChange) {
1461 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1462 "package p;"
1463 "interface IFoo {"
1464 " void foo(int a);"
1465 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001466 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1467 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001468 "interface IFoo {"
1469 " void foo(int b);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001470 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001471 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1472}
Jiyong Parke59c3682018-09-11 23:10:25 +09001473
Daniel Norman85aed542019-08-21 12:01:14 -07001474TEST_F(AidlTestCompatibleChanges, AddedConstValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001475 io_delegate_.SetFileContents("old/p/I.aidl",
1476 "package p; interface I {"
1477 "const int A = 1; }");
1478 io_delegate_.SetFileContents("new/p/I.aidl",
1479 "package p ; interface I {"
1480 "const int A = 1; const int B = 2;}");
Daniel Norman85aed542019-08-21 12:01:14 -07001481 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1482}
Jiyong Parka428d212018-08-29 22:26:30 +09001483
Daniel Norman85aed542019-08-21 12:01:14 -07001484TEST_F(AidlTestCompatibleChanges, ChangedConstValueOrder) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001485 io_delegate_.SetFileContents("old/p/I.aidl",
1486 "package p; interface I {"
1487 "const int A = 1; const int B = 2;}");
1488 io_delegate_.SetFileContents("new/p/I.aidl",
1489 "package p ; interface I {"
1490 "const int B = 2; const int A = 1;}");
Daniel Norman85aed542019-08-21 12:01:14 -07001491 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
Jiyong Park3656c3c2018-08-01 20:02:01 +09001492}
1493
Daniel Norman85aed542019-08-21 12:01:14 -07001494class AidlTestIncompatibleChanges : public AidlTest {
1495 protected:
1496 Options options_ = Options::From("aidl --checkapi old new");
1497};
1498
1499TEST_F(AidlTestIncompatibleChanges, RemovedType) {
Devin Moored131d5e2020-03-31 10:39:10 -07001500 const string expected_stderr = "ERROR: new: API files have been removed: old/p/IFoo.aidl\n";
Jiyong Parke59c3682018-09-11 23:10:25 +09001501 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1502 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001503 "interface IFoo {"
1504 " void foo(in String[] str);"
1505 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001506 "}");
Devin Moored131d5e2020-03-31 10:39:10 -07001507 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001508 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moored131d5e2020-03-31 10:39:10 -07001509 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001510}
1511
1512TEST_F(AidlTestIncompatibleChanges, RemovedMethod) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001513 const string expected_stderr =
1514 "ERROR: old/p/IFoo.aidl:1.61-65: Removed or changed method: p.IFoo.bar(String)\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001515 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1516 "package p;"
1517 "interface IFoo {"
1518 " void foo(in String[] str);"
1519 " void bar(@utf8InCpp String str);"
1520 "}");
1521 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1522 "package p;"
1523 "interface IFoo {"
1524 " void foo(in String[] str);"
1525 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001526 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001527 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001528 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001529}
1530
Jiyong Parkf8d53612020-05-04 14:06:13 +09001531TEST_F(AidlTestIncompatibleChanges, UntypedListInInterface) {
1532 const string expected_stderr =
1533 "ERROR: new/p/IFoo.aidl:1.61-65: "
1534 "Encountered an untyped List or Map. The use of untyped List/Map is "
1535 "prohibited because it is not guaranteed that the objects in the list are recognizable in "
1536 "the receiving side. Consider switching to an array or a generic List/Map.\n"
1537 "ERROR: new/p/IFoo.aidl: Failed to read.\n";
1538 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1539 "package p;"
1540 "interface IFoo {"
1541 " void foo(in String[] str);"
1542 "}");
1543 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1544 "package p;"
1545 "interface IFoo {"
1546 " void foo(in String[] str);"
1547 " void bar(in List arg);"
1548 "}");
1549 CaptureStderr();
1550 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1551 EXPECT_EQ(expected_stderr, GetCapturedStderr());
1552}
1553
1554TEST_F(AidlTestCompatibleChanges, UntypedListInParcelable) {
1555 const string expected_stderr =
1556 "ERROR: new/p/Data.aidl:1.54-59: "
1557 "Encountered an untyped List or Map. The use of untyped List/Map is "
1558 "prohibited because it is not guaranteed that the objects in the list are recognizable in "
1559 "the receiving side. Consider switching to an array or a generic List/Map.\n"
1560 "ERROR: new/p/Data.aidl: Failed to read.\n";
1561 io_delegate_.SetFileContents("old/p/Data.aidl",
1562 "package p;"
1563 "parcelable Data {"
1564 " int foo;"
1565 "}");
1566 io_delegate_.SetFileContents("new/p/Data.aidl",
1567 "package p;"
1568 "parcelable Data {"
1569 " int foo;"
1570 " @nullable List list;"
1571 "}");
1572 CaptureStderr();
1573 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1574 EXPECT_EQ(expected_stderr, GetCapturedStderr());
1575}
1576
Daniel Norman85aed542019-08-21 12:01:14 -07001577TEST_F(AidlTestIncompatibleChanges, RemovedField) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001578 const string expected_stderr =
1579 "ERROR: new/p/Data.aidl:1.21-26: Number of fields in p.Data is reduced from 2 to 1.\n";
Jiyong Parke59c3682018-09-11 23:10:25 +09001580 io_delegate_.SetFileContents("old/p/Data.aidl",
1581 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001582 "parcelable Data {"
1583 " int foo;"
1584 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001585 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001586 io_delegate_.SetFileContents("new/p/Data.aidl",
1587 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001588 "parcelable Data {"
1589 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001590 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001591 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001592 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001593 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001594}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001595
Daniel Norman85aed542019-08-21 12:01:14 -07001596TEST_F(AidlTestIncompatibleChanges, RemovedEnumerator) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001597 const string expected_stderr =
1598 "ERROR: new/p/Enum.aidl:1.15-20: Removed enumerator from p.Enum: FOO\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001599 io_delegate_.SetFileContents("old/p/Enum.aidl",
1600 "package p;"
1601 "enum Enum {"
1602 " FOO = 1,"
1603 " BAR = 2,"
1604 "}");
1605 io_delegate_.SetFileContents("new/p/Enum.aidl",
1606 "package p;"
1607 "enum Enum {"
1608 " BAR = 2,"
1609 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001610 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001611 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001612 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001613}
1614
1615TEST_F(AidlTestIncompatibleChanges, RenamedMethod) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001616 const string expected_stderr =
1617 "ERROR: old/p/IFoo.aidl:1.61-65: Removed or changed method: p.IFoo.bar(String)\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001618 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1619 "package p;"
1620 "interface IFoo {"
1621 " void foo(in String[] str);"
1622 " void bar(@utf8InCpp String str);"
1623 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001624 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1625 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001626 "interface IFoo {"
1627 " void foo(in String[] str);"
1628 " void bar2(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001629 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001630 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001631 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001632 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001633}
1634
Daniel Norman85aed542019-08-21 12:01:14 -07001635TEST_F(AidlTestIncompatibleChanges, RenamedType) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001636 const string expected_stderr = "ERROR: old/p/IFoo.aidl:1.11-20: Removed type: p.IFoo\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001637 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1638 "package p;"
1639 "interface IFoo {"
1640 " void foo(in String[] str);"
1641 " void bar(@utf8InCpp String str);"
1642 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001643 io_delegate_.SetFileContents("new/p/IFoo2.aidl",
1644 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001645 "interface IFoo2 {"
1646 " void foo(in String[] str);"
1647 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001648 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001649 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001650 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001651 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001652}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001653
Daniel Norman85aed542019-08-21 12:01:14 -07001654TEST_F(AidlTestIncompatibleChanges, ChangedEnumerator) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001655 const string expected_stderr =
1656 "ERROR: new/p/Enum.aidl:1.15-20: Changed enumerator value: p.Enum::FOO from 1 to 3.\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001657 io_delegate_.SetFileContents("old/p/Enum.aidl",
1658 "package p;"
1659 "enum Enum {"
1660 " FOO = 1,"
1661 " BAR = 2,"
1662 "}");
1663 io_delegate_.SetFileContents("new/p/Enum.aidl",
1664 "package p;"
1665 "enum Enum {"
1666 " FOO = 3,"
1667 " BAR = 2,"
1668 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001669 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001670 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001671 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001672}
1673
1674TEST_F(AidlTestIncompatibleChanges, ReorderedMethod) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001675 const string expected_stderr =
1676 "ERROR: new/p/IFoo.aidl:1.67-71: Transaction ID changed: p.IFoo.foo(String[]) is changed "
1677 "from 0 to 1.\n"
1678 "ERROR: new/p/IFoo.aidl:1.33-37: Transaction ID changed: p.IFoo.bar(String) is changed from "
1679 "1 to 0.\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001680 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1681 "package p;"
1682 "interface IFoo {"
1683 " void foo(in String[] str);"
1684 " void bar(@utf8InCpp String str);"
1685 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001686 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1687 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001688 "interface IFoo {"
1689 " void bar(@utf8InCpp String str);"
1690 " void foo(in String[] str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001691 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001692 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001693 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001694 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001695}
1696
1697TEST_F(AidlTestIncompatibleChanges, ReorderedField) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001698 const string expected_stderr =
Jiyong Parkb07d9932020-05-15 12:56:54 +09001699 "ERROR: new/p/Data.aidl:1.33-37: Reordered bar from 1 to 0.\n"
1700 "ERROR: new/p/Data.aidl:1.43-47: Reordered foo from 0 to 1.\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001701 io_delegate_.SetFileContents("old/p/Data.aidl",
Jiyong Parke59c3682018-09-11 23:10:25 +09001702 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001703 "parcelable Data {"
1704 " int foo;"
1705 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001706 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001707 io_delegate_.SetFileContents("new/p/Data.aidl",
1708 "package p;"
1709 "parcelable Data {"
1710 " int bar;"
1711 " int foo;"
1712 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001713 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001714 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001715 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001716}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001717
Daniel Norman85aed542019-08-21 12:01:14 -07001718TEST_F(AidlTestIncompatibleChanges, ChangedDirectionSpecifier) {
Devin Mooreeccdb902020-03-24 16:22:40 -07001719 const string expected_stderr = "ERROR: new/p/IFoo.aidl:1.33-37: Direction changed: in to out.\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001720 io_delegate_.SetFileContents("old/p/IFoo.aidl",
Jiyong Parke59c3682018-09-11 23:10:25 +09001721 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001722 "interface IFoo {"
1723 " void foo(in String[] str);"
1724 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001725 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001726 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1727 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001728 "interface IFoo {"
1729 " void foo(out String[] str);"
1730 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001731 "}");
Devin Mooreeccdb902020-03-24 16:22:40 -07001732 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001733 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Mooreeccdb902020-03-24 16:22:40 -07001734 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001735}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001736
Daniel Norman85aed542019-08-21 12:01:14 -07001737TEST_F(AidlTestIncompatibleChanges, AddedAnnotation) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001738 const string expected_stderr =
1739 "ERROR: new/p/IFoo.aidl:1.51-58: Changed annotations: (empty) to @utf8InCpp\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001740 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1741 "package p;"
1742 "interface IFoo {"
1743 " void foo(in String[] str);"
1744 " void bar(@utf8InCpp String str);"
1745 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001746 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1747 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001748 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001749 " void foo(in @utf8InCpp String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001750 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001751 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001752 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001753 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001754 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001755}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001756
Daniel Norman85aed542019-08-21 12:01:14 -07001757TEST_F(AidlTestIncompatibleChanges, RemovedAnnotation) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001758 const string expected_stderr =
1759 "ERROR: new/p/IFoo.aidl:1.66-72: Changed annotations: @utf8InCpp to (empty)\n";
Daniel Norman85aed542019-08-21 12:01:14 -07001760 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1761 "package p;"
1762 "interface IFoo {"
1763 " void foo(in String[] str);"
1764 " void bar(@utf8InCpp String str);"
1765 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001766 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1767 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001768 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001769 " void foo(in String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001770 " void bar(String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001771 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001772 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001773 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001774 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001775}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001776
Daniel Norman85aed542019-08-21 12:01:14 -07001777TEST_F(AidlTestIncompatibleChanges, RemovedPackage) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001778 const string expected_stderr = "ERROR: old/q/IFoo.aidl:1.11-21: Removed type: q.IFoo\n";
Jiyong Parke59c3682018-09-11 23:10:25 +09001779 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}");
1780 io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001781 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001782 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001783 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001784 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001785}
Jiyong Parka468e2a2018-08-29 21:25:18 +09001786
Daniel Norman85aed542019-08-21 12:01:14 -07001787TEST_F(AidlTestIncompatibleChanges, ChangedDefaultValue) {
Steven Moreland370ed342020-04-28 18:14:39 -07001788 const string expected_stderr = "ERROR: new/p/D.aidl:1.30-32: Changed default value: 1 to 2.\n";
Jiyong Parke59c3682018-09-11 23:10:25 +09001789 io_delegate_.SetFileContents("old/p/D.aidl", "package p; parcelable D { int a = 1; }");
1790 io_delegate_.SetFileContents("new/p/D.aidl", "package p; parcelable D { int a = 2; }");
Devin Moore097a3ab2020-03-11 16:08:44 -07001791 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001792 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001793 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001794}
Jiyong Parka428d212018-08-29 22:26:30 +09001795
Daniel Norman85aed542019-08-21 12:01:14 -07001796TEST_F(AidlTestIncompatibleChanges, RemovedConstValue) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001797 const string expected_stderr =
1798 "ERROR: old/p/I.aidl:1.51-53: Removed constant declaration: p.I.B\n";
Jiyong Parke59c3682018-09-11 23:10:25 +09001799 io_delegate_.SetFileContents("old/p/I.aidl",
1800 "package p; interface I {"
1801 "const int A = 1; const int B = 2;}");
1802 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 1; }");
Devin Moore097a3ab2020-03-11 16:08:44 -07001803 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001804 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001805 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Daniel Norman85aed542019-08-21 12:01:14 -07001806}
Jiyong Parka428d212018-08-29 22:26:30 +09001807
Daniel Norman85aed542019-08-21 12:01:14 -07001808TEST_F(AidlTestIncompatibleChanges, ChangedConstValue) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001809 const string expected_stderr =
1810 "ERROR: new/p/I.aidl:1.11-21: Changed constant value: p.I.A from 1 to 2.\n";
Jiyong Parke59c3682018-09-11 23:10:25 +09001811 io_delegate_.SetFileContents("old/p/I.aidl", "package p; interface I { const int A = 1; }");
1812 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 2; }");
Devin Moore097a3ab2020-03-11 16:08:44 -07001813 CaptureStderr();
Daniel Norman85aed542019-08-21 12:01:14 -07001814 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001815 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Park3c35e392018-08-30 13:10:30 +09001816}
1817
Jiyong Park8c380532018-08-30 14:55:26 +09001818TEST_F(AidlTest, RejectAmbiguousImports) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001819 const string expected_stderr =
1820 "ERROR: p/IFoo.aidl: Duplicate files found for q.IBar from:\n"
1821 "dir1/q/IBar.aidl\n"
1822 "dir2/q/IBar.aidl\n"
Devin Moore5de18ed2020-04-02 13:52:29 -07001823 "ERROR: p/IFoo.aidl: Couldn't find import for class q.IBar\n";
Jiyong Park8c380532018-08-30 14:55:26 +09001824 Options options = Options::From("aidl --lang=java -o out -I dir1 -I dir2 p/IFoo.aidl");
1825 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; import q.IBar; interface IFoo{}");
1826 io_delegate_.SetFileContents("dir1/q/IBar.aidl", "package q; interface IBar{}");
1827 io_delegate_.SetFileContents("dir2/q/IBar.aidl", "package q; interface IBar{}");
1828
Devin Moore097a3ab2020-03-11 16:08:44 -07001829 CaptureStderr();
Jiyong Park8c380532018-08-30 14:55:26 +09001830 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001831 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Park8c380532018-08-30 14:55:26 +09001832}
1833
Jiyong Parked65bf42018-08-28 15:43:27 +09001834TEST_F(AidlTest, HandleManualIdAssignments) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001835 const string expected_stderr =
1836 "ERROR: new/p/IFoo.aidl:1.32-36: Transaction ID changed: p.IFoo.foo() is changed from 10 to "
1837 "11.\n";
Jiyong Parke59c3682018-09-11 23:10:25 +09001838 Options options = Options::From("aidl --checkapi old new");
1839 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
1840 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
Jiyong Parked65bf42018-08-28 15:43:27 +09001841
1842 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1843
Jiyong Parke59c3682018-09-11 23:10:25 +09001844 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 11;}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001845 CaptureStderr();
Jiyong Parked65bf42018-08-28 15:43:27 +09001846 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001847 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Parked65bf42018-08-28 15:43:27 +09001848}
1849
Jiyong Parke05195e2018-10-08 18:24:23 +09001850TEST_F(AidlTest, ParcelFileDescriptorIsBuiltinType) {
1851 Options javaOptions = Options::From("aidl --lang=java -o out p/IFoo.aidl");
1852 Options cppOptions = Options::From("aidl --lang=cpp -h out -o out p/IFoo.aidl");
1853
1854 // use without import
1855 io_delegate_.SetFileContents("p/IFoo.aidl",
1856 "package p; interface IFoo{ void foo(in ParcelFileDescriptor fd);}");
1857 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1858 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1859
Devin Moore7b8d5c92020-03-17 14:14:08 -07001860 // use without import but with full name
Jiyong Parke05195e2018-10-08 18:24:23 +09001861 io_delegate_.SetFileContents(
1862 "p/IFoo.aidl",
1863 "package p; interface IFoo{ void foo(in android.os.ParcelFileDescriptor fd);}");
1864 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1865 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1866
1867 // use with import (as before)
1868 io_delegate_.SetFileContents("p/IFoo.aidl",
1869 "package p;"
1870 "import android.os.ParcelFileDescriptor;"
1871 "interface IFoo{"
1872 " void foo(in ParcelFileDescriptor fd);"
1873 "}");
1874 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1875 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1876}
Jiyong Parked65bf42018-08-28 15:43:27 +09001877
Jiyong Park3633b722019-04-11 15:38:26 +09001878TEST_F(AidlTest, ManualIds) {
1879 Options options = Options::From("aidl --lang=java -o out IFoo.aidl");
1880 io_delegate_.SetFileContents("IFoo.aidl",
1881 "interface IFoo {\n"
1882 " void foo() = 0;\n"
1883 " void bar() = 1;\n"
1884 "}");
1885 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1886}
1887
1888TEST_F(AidlTest, ManualIdsWithMetaTransactions) {
1889 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1890 io_delegate_.SetFileContents("IFoo.aidl",
1891 "interface IFoo {\n"
1892 " void foo() = 0;\n"
1893 " void bar() = 1;\n"
1894 "}");
1895 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1896}
1897
1898TEST_F(AidlTest, FailOnDuplicatedIds) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001899 const string expected_stderr =
1900 "ERROR: IFoo.aidl:3.7-11: Found duplicate method id (3) for method bar\n";
Jiyong Park3633b722019-04-11 15:38:26 +09001901 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1902 io_delegate_.SetFileContents("IFoo.aidl",
1903 "interface IFoo {\n"
1904 " void foo() = 3;\n"
1905 " void bar() = 3;\n"
1906 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001907 CaptureStderr();
Jiyong Park3633b722019-04-11 15:38:26 +09001908 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001909 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Park3633b722019-04-11 15:38:26 +09001910}
1911
1912TEST_F(AidlTest, FailOnOutOfRangeIds) {
1913 // 16777115 is kLastMetaMethodId + 1
Devin Moore097a3ab2020-03-11 16:08:44 -07001914 const string expected_stderr =
1915 "ERROR: IFoo.aidl:3.7-11: Found out of bounds id (16777115) for method bar. "
1916 "Value for id must be between 0 and 16777114 inclusive.\n";
Jiyong Park3633b722019-04-11 15:38:26 +09001917 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1918 io_delegate_.SetFileContents("IFoo.aidl",
1919 "interface IFoo {\n"
1920 " void foo() = 3;\n"
1921 " void bar() = 16777115;\n"
1922 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001923 CaptureStderr();
Jiyong Park3633b722019-04-11 15:38:26 +09001924 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001925 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Park3633b722019-04-11 15:38:26 +09001926}
1927
1928TEST_F(AidlTest, FailOnPartiallyAssignedIds) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001929 const string expected_stderr =
1930 "ERROR: IFoo.aidl:3.7-11: You must either assign id's to all methods or to none of them.\n";
Jiyong Park3633b722019-04-11 15:38:26 +09001931 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1932 io_delegate_.SetFileContents("IFoo.aidl",
1933 "interface IFoo {\n"
1934 " void foo() = 3;\n"
1935 " void bar();\n"
1936 "}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001937 CaptureStderr();
Jiyong Park3633b722019-04-11 15:38:26 +09001938 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001939 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Park3633b722019-04-11 15:38:26 +09001940}
1941
Jiyong Parkc6816252019-07-08 08:12:28 +09001942TEST_F(AidlTest, AllowDuplicatedImportPaths) {
1943 Options options = Options::From("aidl --lang=java -I dir -I dir IFoo.aidl");
1944 io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}");
1945 io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}");
1946 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1947}
1948
1949TEST_F(AidlTest, FailOnAmbiguousImports) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001950 const string expected_stderr =
1951 "ERROR: IFoo.aidl: Duplicate files found for IBar from:\n"
1952 "dir/IBar.aidl\n"
1953 "dir2/IBar.aidl\n"
Devin Moore5de18ed2020-04-02 13:52:29 -07001954 "ERROR: IFoo.aidl: Couldn't find import for class IBar\n";
Devin Moore097a3ab2020-03-11 16:08:44 -07001955
Jiyong Parkc6816252019-07-08 08:12:28 +09001956 Options options = Options::From("aidl --lang=java -I dir -I dir2 IFoo.aidl");
1957 io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}");
1958 io_delegate_.SetFileContents("dir2/IBar.aidl", "interface IBar{}");
1959 io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}");
Devin Moore097a3ab2020-03-11 16:08:44 -07001960 CaptureStderr();
Jiyong Parkc6816252019-07-08 08:12:28 +09001961 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Devin Moore097a3ab2020-03-11 16:08:44 -07001962 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Jiyong Parkc6816252019-07-08 08:12:28 +09001963}
1964
Jiyong Parkf1f5c802020-05-19 17:33:00 +09001965TEST_F(AidlTest, UnusedImportDoesNotContributeInclude) {
1966 io_delegate_.SetFileContents("a/b/IFoo.aidl",
1967 "package a.b;\n"
1968 "import a.b.IBar;\n"
1969 "import a.b.IQux;\n"
1970 "interface IFoo { IQux foo(); }\n");
1971 io_delegate_.SetFileContents("a/b/IBar.aidl", "package a.b; interface IBar { void foo(); }");
1972 io_delegate_.SetFileContents("a/b/IQux.aidl", "package a.b; interface IQux { void foo(); }");
1973
1974 Options options = Options::From("aidl --lang=ndk a/b/IFoo.aidl -I . -o out -h out/include");
1975 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1976
1977 string output;
1978 EXPECT_TRUE(io_delegate_.GetWrittenContents("out/include/aidl/a/b/IFoo.h", &output));
1979 // IBar was imported but wasn't used. include is not expected.
1980 EXPECT_THAT(output, Not(testing::HasSubstr("#include <aidl/a/b/IBar.h>")));
1981 // IBar was imported and used. include is expected.
1982 EXPECT_THAT(output, (testing::HasSubstr("#include <aidl/a/b/IQux.h>")));
1983}
1984
Jiyong Park56f73d72019-06-11 12:20:28 +09001985class AidlOutputPathTest : public AidlTest {
1986 protected:
1987 void SetUp() override {
1988 AidlTest::SetUp();
1989 io_delegate_.SetFileContents("sub/dir/foo/bar/IFoo.aidl", "package foo.bar; interface IFoo {}");
1990 }
1991
1992 void Test(const Options& options, const std::string expected_output_path) {
1993 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1994 // check the existence
1995 EXPECT_TRUE(io_delegate_.GetWrittenContents(expected_output_path, nullptr));
1996 }
1997};
1998
1999TEST_F(AidlOutputPathTest, OutDirWithNoOutputFile) {
2000 // <out_dir> / <package_name> / <type_name>.java
2001 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl"), "out/foo/bar/IFoo.java");
2002}
2003
2004TEST_F(AidlOutputPathTest, OutDirWithOutputFile) {
2005 // when output file is explicitly set, it is always respected. -o option is
2006 // ignored.
2007 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java");
2008}
2009
2010TEST_F(AidlOutputPathTest, NoOutDirWithOutputFile) {
2011 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java");
2012}
2013
2014TEST_F(AidlOutputPathTest, NoOutDirWithNoOutputFile) {
2015 // output is the same as the input file except for the suffix
2016 Test(Options::From("aidl sub/dir/foo/bar/IFoo.aidl"), "sub/dir/foo/bar/IFoo.java");
2017}
2018
Devin Moore7b8d5c92020-03-17 14:14:08 -07002019TEST_P(AidlTest, FailOnOutOfBoundsInt32MaxConstInt) {
Devin Moore097a3ab2020-03-11 16:08:44 -07002020 AidlError error;
2021 const string expected_stderr =
2022 "ERROR: p/IFoo.aidl:3.58-69: Invalid type specifier for an int64 literal: int\n";
2023 CaptureStderr();
Will McVickera4e5b132019-10-03 13:52:21 -07002024 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
2025 R"(package p;
2026 interface IFoo {
2027 const int int32_max_oob = 2147483650;
2028 }
2029 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -07002030 typenames_, GetLanguage(), &error));
Devin Moore097a3ab2020-03-11 16:08:44 -07002031 EXPECT_EQ(expected_stderr, GetCapturedStderr());
2032 EXPECT_EQ(AidlError::BAD_TYPE, error);
Will McVickera4e5b132019-10-03 13:52:21 -07002033}
2034
Devin Moore7b8d5c92020-03-17 14:14:08 -07002035TEST_P(AidlTest, FailOnOutOfBoundsInt32MinConstInt) {
Devin Moore097a3ab2020-03-11 16:08:44 -07002036 AidlError error;
2037 const string expected_stderr =
2038 "ERROR: p/IFoo.aidl:3.58-60: Invalid type specifier for an int64 literal: int\n";
2039 CaptureStderr();
Will McVickera4e5b132019-10-03 13:52:21 -07002040 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
2041 R"(package p;
2042 interface IFoo {
2043 const int int32_min_oob = -2147483650;
2044 }
2045 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -07002046 typenames_, GetLanguage(), &error));
Devin Moore097a3ab2020-03-11 16:08:44 -07002047 EXPECT_EQ(expected_stderr, GetCapturedStderr());
2048 EXPECT_EQ(AidlError::BAD_TYPE, error);
Will McVickera4e5b132019-10-03 13:52:21 -07002049}
2050
Devin Moore7b8d5c92020-03-17 14:14:08 -07002051TEST_P(AidlTest, FailOnOutOfBoundsInt64MaxConstInt) {
Devin Moore097a3ab2020-03-11 16:08:44 -07002052 AidlError error;
2053 const string expected_stderr =
2054 "ERROR: Could not parse integer: 21474836509999999999999999 at p/IFoo.aidl:3.59-85.\n";
2055 CaptureStderr();
Will McVickera4e5b132019-10-03 13:52:21 -07002056 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
2057 R"(package p;
2058 interface IFoo {
2059 const long int64_max_oob = 21474836509999999999999999;
2060 }
2061 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -07002062 typenames_, GetLanguage(), &error));
Devin Moore097a3ab2020-03-11 16:08:44 -07002063 EXPECT_EQ(expected_stderr, GetCapturedStderr());
2064 EXPECT_EQ(AidlError::PARSE_ERROR, error);
Will McVickera4e5b132019-10-03 13:52:21 -07002065}
2066
Devin Moore7b8d5c92020-03-17 14:14:08 -07002067TEST_P(AidlTest, FailOnOutOfBoundsInt64MinConstInt) {
Devin Moore097a3ab2020-03-11 16:08:44 -07002068 AidlError error;
2069 const string expected_stderr =
2070 "ERROR: Could not parse integer: 21474836509999999999999999 at p/IFoo.aidl:3.61-86.\n";
2071 CaptureStderr();
Will McVickera4e5b132019-10-03 13:52:21 -07002072 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
2073 R"(package p;
2074 interface IFoo {
2075 const long int64_min_oob = -21474836509999999999999999;
2076 }
2077 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -07002078 typenames_, GetLanguage(), &error));
Devin Moore097a3ab2020-03-11 16:08:44 -07002079 EXPECT_EQ(expected_stderr, GetCapturedStderr());
2080 EXPECT_EQ(AidlError::PARSE_ERROR, error);
Will McVickera4e5b132019-10-03 13:52:21 -07002081}
2082
Devin Moore7b8d5c92020-03-17 14:14:08 -07002083TEST_P(AidlTest, FailOnOutOfBoundsAutofilledEnum) {
Devin Moore097a3ab2020-03-11 16:08:44 -07002084 AidlError error;
Devin Mooredf93ebb2020-03-25 14:03:35 -07002085 const string expected_stderr =
2086 "ERROR: p/TestEnum.aidl:3.35-44: Invalid type specifier for an int32 "
2087 "literal: byte\n"
2088 "ERROR: p/TestEnum.aidl:5.1-36: Enumerator type differs from enum backing type.\n";
2089 CaptureStderr();
Daniel Normanb28684e2019-10-17 15:31:39 -07002090 EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl",
2091 R"(package p;
2092 @Backing(type="byte")
2093 enum TestEnum {
2094 FOO = 127,
2095 BAR,
2096 }
2097 )",
Devin Moore7b8d5c92020-03-17 14:14:08 -07002098 typenames_, GetLanguage(), &error));
Devin Mooredf93ebb2020-03-25 14:03:35 -07002099 EXPECT_EQ(expected_stderr, GetCapturedStderr());
Devin Moore097a3ab2020-03-11 16:08:44 -07002100 EXPECT_EQ(AidlError::BAD_TYPE, error);
Daniel Normanb28684e2019-10-17 15:31:39 -07002101}
2102
Devin Mooredecaf292020-04-30 09:16:40 -07002103TEST_P(AidlTest, UnsupportedBackingAnnotationParam) {
2104 AidlError error;
2105 const string expected_stderr =
2106 "ERROR: p/TestEnum.aidl:2.1-51: Parameter foo not supported for annotation Backing. It must "
2107 "be one of: type\n"
2108 "ERROR: p/TestEnum.aidl:2.1-51: Parameter foo not supported for annotation Backing. It must "
2109 "be one of: type\n";
2110 CaptureStderr();
2111 EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl",
2112 R"(package p;
2113 @Backing(foo="byte")
2114 enum TestEnum {
2115 FOO = 1,
2116 BAR,
2117 }
2118 )",
2119 typenames_, GetLanguage(), &error));
2120 EXPECT_EQ(expected_stderr, GetCapturedStderr());
2121 EXPECT_EQ(AidlError::BAD_TYPE, error);
2122}
2123
Christopher Wiley90be4e32015-10-20 14:55:25 -07002124} // namespace aidl
2125} // namespace android