blob: a5c40d613cec3c8e842d6d451577e780d9a36ea8 [file] [log] [blame]
Christopher Wiley90be4e32015-10-20 14:55:25 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Christopher Wiley90be4e32015-10-20 14:55:25 -070017#include <memory>
Christopher Wiley12e894a2016-01-29 11:55:07 -080018#include <set>
19#include <string>
Christopher Wiley90be4e32015-10-20 14:55:25 -070020#include <vector>
21
Christopher Wileyec31a052016-01-25 07:28:51 -080022#include <android-base/stringprintf.h>
Christopher Wiley90be4e32015-10-20 14:55:25 -070023#include <gtest/gtest.h>
24
25#include "aidl.h"
Jiyong Park3656c3c2018-08-01 20:02:01 +090026#include "aidl_apicheck.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070027#include "aidl_language.h"
Steven Moreland860b1942018-08-16 14:59:28 -070028#include "aidl_to_cpp.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070029#include "tests/fake_io_delegate.h"
30#include "type_cpp.h"
31#include "type_java.h"
32#include "type_namespace.h"
33
34using android::aidl::test::FakeIoDelegate;
Christopher Wileyec31a052016-01-25 07:28:51 -080035using android::base::StringPrintf;
Christopher Wiley12e894a2016-01-29 11:55:07 -080036using std::set;
Christopher Wiley90be4e32015-10-20 14:55:25 -070037using std::string;
38using std::unique_ptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -080039using std::vector;
Christopher Wileyef140932015-11-03 09:29:19 -080040using android::aidl::internals::parse_preprocessed_file;
Christopher Wiley90be4e32015-10-20 14:55:25 -070041
42namespace android {
43namespace aidl {
Christopher Wileyf8136192016-04-12 14:19:35 -070044namespace {
45
46const char kExpectedDepFileContents[] =
47R"(place/for/output/p/IFoo.java : \
48 p/IFoo.aidl
49
50p/IFoo.aidl :
51)";
52
Dan Willemsen93298ee2016-11-10 23:55:55 -080053const char kExpectedNinjaDepFileContents[] =
54R"(place/for/output/p/IFoo.java : \
55 p/IFoo.aidl
56)";
57
Christopher Wileyb1bbdf82016-04-21 11:43:45 -070058const char kExpectedParcelableDepFileContents[] =
59R"( : \
60 p/Foo.aidl
61
62p/Foo.aidl :
63)";
64
Jeongik Chaa755c2a2018-12-12 16:28:23 +090065const char kExepectedJavaParcelableOutputContests[] =
66 R"(/*
67 * This file is auto-generated. DO NOT MODIFY.
68 * Original file: Rect.aidl
69 */
70@android.annotation.SystemApi
71public class Rect implements android.os.Parcelable
72{
73 // Comment
74
75 @android.annotation.SystemApi
76 public int x = 5;
77
78 @android.annotation.UnsupportedAppUsage
79 @android.annotation.SystemApi
80 public int y;
81 public static final android.os.Parcelable.Creator<Rect> CREATOR = new android.os.Parcelable.Creator<Rect>() {
82 @Override
83 public Rect createFromParcel(android.os.Parcel _aidl_source) {
84 Rect _aidl_out = new Rect();
85 _aidl_out.readFromParcel(_aidl_source);
86 return _aidl_out;
87 }
88 @Override
89 public Rect[] newArray(int _aidl_size) {
90 return new Rect[_aidl_size];
91 }
92 };
93 @Override public final void writeToParcel(android.os.Parcel _aidl_parcel, int _aidl_flag)
94 {
95 int _aidl_start_pos = _aidl_parcel.dataPosition();
96 _aidl_parcel.writeInt(0);
97 _aidl_parcel.writeInt(x);
98 _aidl_parcel.writeInt(y);
99 int _aidl_end_pos = _aidl_parcel.dataPosition();
100 _aidl_parcel.setDataPosition(_aidl_start_pos);
101 _aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos);
102 _aidl_parcel.setDataPosition(_aidl_end_pos);
103 }
104 public final void readFromParcel(android.os.Parcel _aidl_parcel)
105 {
106 int _aidl_start_pos = _aidl_parcel.dataPosition();
107 int _aidl_parcelable_size = _aidl_parcel.readInt();
108 if (_aidl_parcelable_size < 0) return;
109 try {
110 x = _aidl_parcel.readInt();
111 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
112 y = _aidl_parcel.readInt();
113 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
114 } finally {
115 _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size);
116 }
117 }
118 @Override public int describeContents()
119 {
Jeongik Cha372a8c82018-12-12 16:31:11 +0900120 return 0;
121 }
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900122}
123)";
124
Christopher Wileyf8136192016-04-12 14:19:35 -0700125} // namespace
Christopher Wiley90be4e32015-10-20 14:55:25 -0700126
127class AidlTest : public ::testing::Test {
128 protected:
Christopher Wiley56799522015-10-31 10:17:04 -0700129 void SetUp() override {
130 java_types_.Init();
131 cpp_types_.Init();
132 }
133
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700134 AidlDefinedType* Parse(const string& path, const string& contents, TypeNamespace* types,
135 AidlError* error = nullptr,
136 const vector<string> additional_arguments = {}) {
Christopher Wiley0522cd52015-10-28 15:39:44 -0700137 io_delegate_.SetFileContents(path, contents);
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900138 vector<string> args;
139 if (types == &java_types_) {
140 args.emplace_back("aidl");
141 } else {
142 args.emplace_back("aidl-cpp");
143 }
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700144 for (const string& s : additional_arguments) {
145 args.emplace_back(s);
146 }
147 for (const string& f : preprocessed_files_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900148 args.emplace_back("--preprocessed=" + f);
149 }
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700150 for (const string& i : import_paths_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900151 args.emplace_back("--include=" + i);
152 }
153 args.emplace_back(path);
154 Options options = Options::From(args);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900155 vector<AidlDefinedType*> defined_types;
156 vector<string> imported_files;
Jiyong Park8c380532018-08-30 14:55:26 +0900157 ImportResolver import_resolver{io_delegate_, path, import_paths_, {}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700158 AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
Jiyong Parkb034bf02018-07-30 17:44:33 +0900159 path, options, io_delegate_, types, &defined_types, &imported_files);
160
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700161 if (error != nullptr) {
162 *error = actual_error;
163 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900164
165 if (actual_error != AidlError::OK) {
166 return nullptr;
167 }
168
169 EXPECT_EQ(1ul, defined_types.size());
170
171 return defined_types.front();
Christopher Wiley90be4e32015-10-20 14:55:25 -0700172 }
Christopher Wiley0522cd52015-10-28 15:39:44 -0700173
174 FakeIoDelegate io_delegate_;
Christopher Wiley41544372015-11-03 14:52:29 -0800175 vector<string> preprocessed_files_;
Jiyong Park8c380532018-08-30 14:55:26 +0900176 set<string> import_paths_;
Christopher Wiley56799522015-10-31 10:17:04 -0700177 java::JavaTypeNamespace java_types_;
178 cpp::TypeNamespace cpp_types_;
Christopher Wiley90be4e32015-10-20 14:55:25 -0700179};
180
Steven Morelandf3da0892018-10-05 14:52:01 -0700181TEST_F(AidlTest, AcceptMissingPackage) {
Christopher Wiley56799522015-10-31 10:17:04 -0700182 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &java_types_));
Steven Morelandf3da0892018-10-05 14:52:01 -0700183 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &cpp_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700184}
185
Christopher Wiley0522cd52015-10-28 15:39:44 -0700186TEST_F(AidlTest, RejectsArraysOfBinders) {
Jiyong Park8c380532018-08-30 14:55:26 +0900187 import_paths_.emplace("");
Christopher Wiley0522cd52015-10-28 15:39:44 -0700188 io_delegate_.SetFileContents("bar/IBar.aidl",
189 "package bar; interface IBar {}");
190 string path = "foo/IFoo.aidl";
191 string contents = "package foo;\n"
192 "import bar.IBar;\n"
193 "interface IFoo { void f(in IBar[] input); }";
Christopher Wiley56799522015-10-31 10:17:04 -0700194 EXPECT_EQ(nullptr, Parse(path, contents, &java_types_));
195 EXPECT_EQ(nullptr, Parse(path, contents, &cpp_types_));
Christopher Wiley0522cd52015-10-28 15:39:44 -0700196}
197
Christopher Wiley90be4e32015-10-20 14:55:25 -0700198TEST_F(AidlTest, RejectsOnewayOutParameters) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700199 string oneway_interface =
200 "package a; oneway interface IFoo { void f(out int bar); }";
201 string oneway_method =
202 "package a; interface IBar { oneway void f(out int bar); }";
Christopher Wiley56799522015-10-31 10:17:04 -0700203 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types_));
204 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types_));
205 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types_));
206 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700207}
208
209TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700210 string oneway_method = "package a; interface IFoo { oneway int f(); }";
Christopher Wiley56799522015-10-31 10:17:04 -0700211 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
212 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700213}
214
Casey Dahlin57dbe242015-12-04 11:44:02 -0800215TEST_F(AidlTest, RejectsNullablePrimitive) {
216 string oneway_method = "package a; interface IFoo { @nullable int f(); }";
217 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
218 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
219}
220
Steven Morelandb3cd3c72018-10-11 12:37:45 -0700221TEST_F(AidlTest, RejectsDuplicatedArgumentNames) {
222 string method = "package a; interface IFoo { void f(int a, int a); }";
223 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, &cpp_types_));
224 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, &java_types_));
225}
226
Christopher Wileyec31a052016-01-25 07:28:51 -0800227TEST_F(AidlTest, ParsesNullableAnnotation) {
228 for (auto is_nullable: {true, false}) {
229 auto parse_result = Parse(
230 "a/IFoo.aidl",
231 StringPrintf( "package a; interface IFoo {%s String f(); }",
232 (is_nullable) ? "@nullable" : ""),
233 &cpp_types_);
234 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700235 const AidlInterface* interface = parse_result->AsInterface();
236 ASSERT_NE(nullptr, interface);
237 ASSERT_FALSE(interface->GetMethods().empty());
238 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsNullable(), is_nullable);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900239 cpp_types_.typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800240 }
241}
242
243TEST_F(AidlTest, ParsesUtf8Annotations) {
244 for (auto is_utf8: {true, false}) {
245 auto parse_result = Parse(
246 "a/IFoo.aidl",
247 StringPrintf( "package a; interface IFoo {%s String f(); }",
Christopher Wiley9f403722016-01-27 16:04:11 -0800248 (is_utf8) ? "@utf8InCpp" : ""),
Christopher Wileyb7e01172016-01-28 16:32:34 -0800249 &cpp_types_);
Christopher Wileyec31a052016-01-25 07:28:51 -0800250 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700251 const AidlInterface* interface = parse_result->AsInterface();
252 ASSERT_NE(nullptr, interface);
253 ASSERT_FALSE(interface->GetMethods().empty());
254 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsUtf8InCpp(), is_utf8);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900255 cpp_types_.typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800256 }
257}
258
Christopher Wiley90be4e32015-10-20 14:55:25 -0700259TEST_F(AidlTest, AcceptsOneway) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700260 string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
261 string oneway_interface =
262 "package a; oneway interface IBar { void f(int a); }";
Christopher Wiley56799522015-10-31 10:17:04 -0700263 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
264 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
265 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types_));
266 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700267}
Christopher Wileyef140932015-11-03 09:29:19 -0800268
269TEST_F(AidlTest, ParsesPreprocessedFile) {
270 string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
271 io_delegate_.SetFileContents("path", simple_content);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800272 EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900273 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_, java_types_.typenames_));
Christopher Wiley9ab06232016-01-27 14:55:18 -0800274 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
275 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
Christopher Wileyef140932015-11-03 09:29:19 -0800276}
277
278TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
279 string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
280 io_delegate_.SetFileContents("path", simple_content);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800281 EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900282 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_, java_types_.typenames_));
Christopher Wiley9ab06232016-01-27 14:55:18 -0800283 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
284 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
Christopher Wileyef140932015-11-03 09:29:19 -0800285}
286
Christopher Wiley41544372015-11-03 14:52:29 -0800287TEST_F(AidlTest, PreferImportToPreprocessed) {
288 io_delegate_.SetFileContents("preprocessed", "interface another.IBar;");
289 io_delegate_.SetFileContents("one/IBar.aidl", "package one; "
290 "interface IBar {}");
291 preprocessed_files_.push_back("preprocessed");
Jiyong Park8c380532018-08-30 14:55:26 +0900292 import_paths_.emplace("");
Christopher Wiley41544372015-11-03 14:52:29 -0800293 auto parse_result = Parse(
294 "p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
295 &java_types_);
296 EXPECT_NE(nullptr, parse_result);
297 // We expect to know about both kinds of IBar
Christopher Wiley9ab06232016-01-27 14:55:18 -0800298 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("one.IBar"));
299 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("another.IBar"));
Christopher Wiley41544372015-11-03 14:52:29 -0800300 // But if we request just "IBar" we should get our imported one.
Steven Moreland02e012e2018-08-02 14:58:10 -0700301 AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
Christopher Wiley9ab06232016-01-27 14:55:18 -0800302 const java::Type* type = java_types_.Find(ambiguous_type);
Christopher Wiley41544372015-11-03 14:52:29 -0800303 ASSERT_TRUE(type);
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800304 EXPECT_EQ("one.IBar", type->CanonicalName());
Christopher Wiley41544372015-11-03 14:52:29 -0800305}
306
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800307TEST_F(AidlTest, WritePreprocessedFile) {
308 io_delegate_.SetFileContents("p/Outer.aidl",
309 "package p; parcelable Outer.Inner;");
310 io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;"
311 "interface IBar {}");
312
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900313 vector<string> args {
314 "aidl",
315 "--preprocess",
316 "preprocessed",
317 "p/Outer.aidl",
318 "one/IBar.aidl"};
319 Options options = Options::From(args);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800320 EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_));
321
322 string output;
323 EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output));
324 EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output);
325}
326
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900327TEST_F(AidlTest, JavaParcelableOutput) {
328 io_delegate_.SetFileContents("Rect.aidl",
329 "@SystemApi\n"
330 "parcelable Rect {\n"
331 " // Comment\n"
332 " @SystemApi\n"
333 " int x=5;\n"
334 " @SystemApi\n"
335 " @UnsupportedAppUsage\n"
336 " int y;\n"
337 "}");
338
339 vector<string> args{"aidl", "Rect.aidl"};
340 Options options = Options::From(args);
341 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
342
343 string output;
344 EXPECT_TRUE(io_delegate_.GetWrittenContents("Rect.java", &output));
345 EXPECT_EQ(kExepectedJavaParcelableOutputContests, output);
346}
347
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800348TEST_F(AidlTest, RequireOuterClass) {
349 io_delegate_.SetFileContents("p/Outer.aidl",
350 "package p; parcelable Outer.Inner;");
Jiyong Park8c380532018-08-30 14:55:26 +0900351 import_paths_.emplace("");
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800352 auto parse_result = Parse(
353 "p/IFoo.aidl",
354 "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
355 &java_types_);
356 EXPECT_EQ(nullptr, parse_result);
357}
358
359TEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) {
360 io_delegate_.SetFileContents("preprocessed",
361 "parcelable p.Outer.Inner;");
362 preprocessed_files_.push_back("preprocessed");
363 auto parse_result = Parse(
364 "p/IFoo.aidl",
365 "package p; interface IFoo { void f(in Inner c); }",
366 &java_types_);
367 // TODO(wiley): This should actually return nullptr because we require
368 // the outer class name. However, for legacy reasons,
369 // this behavior must be maintained. b/17415692
370 EXPECT_NE(nullptr, parse_result);
371}
372
Christopher Wiley632801d2015-11-05 14:15:49 -0800373TEST_F(AidlTest, FailOnParcelable) {
Steven Morelande2c64b42018-09-18 15:06:37 -0700374 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;");
375
Christopher Wiley632801d2015-11-05 14:15:49 -0800376 // By default, we shouldn't fail on parcelable.
Steven Morelande2c64b42018-09-18 15:06:37 -0700377 Options options1 = Options::From("aidl p/IFoo.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900378 EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_));
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900379
Steven Morelande2c64b42018-09-18 15:06:37 -0700380 // -b considers this an error
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900381 Options options2 = Options::From("aidl -b p/IFoo.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900382 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
Steven Morelande2c64b42018-09-18 15:06:37 -0700383
384 io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}");
385
386 // Regardless of '-b', a parcelable and an interface should fail.
387 Options options3 = Options::From("aidl p/IBar.aidl");
388 EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_));
389 Options options4 = Options::From("aidl -b p/IBar.aidl");
390 EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_));
Christopher Wiley632801d2015-11-05 14:15:49 -0800391}
392
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700393TEST_F(AidlTest, StructuredFailOnUnstructuredParcelable) {
394 io_delegate_.SetFileContents("o/WhoKnowsWhat.aidl", "package o; parcelable WhoKnowsWhat;");
395 import_paths_.emplace("");
396 AidlError reported_error;
397 auto parse_result =
398 Parse("p/IFoo.aidl",
399 "package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }",
400 &java_types_, &reported_error, {"--structured"});
401 EXPECT_EQ(nullptr, parse_result);
402 EXPECT_EQ(AidlError::NOT_STRUCTURED, reported_error);
403}
404
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700405TEST_F(AidlTest, FailOnDuplicateConstantNames) {
406 AidlError reported_error;
407 EXPECT_EQ(nullptr,
408 Parse("p/IFoo.aidl",
409 R"(package p;
410 interface IFoo {
411 const String DUPLICATED = "d";
412 const int DUPLICATED = 1;
413 }
414 )",
415 &cpp_types_,
416 &reported_error));
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700417 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700418}
419
Steven Morelandc258abc2018-07-10 14:03:38 -0700420TEST_F(AidlTest, FailOnManyDefinedTypes) {
421 AidlError reported_error;
422 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
423 R"(package p;
424 interface IFoo {}
425 parcelable Bar;
426 parcelable IBar {}
427 parcelable StructuredParcelable {}
428 interface IBaz {}
429 )",
430 &cpp_types_, &reported_error));
431 // Parse success is important for clear error handling even if the cases aren't
432 // actually supported in code generation.
433 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
434}
435
436TEST_F(AidlTest, FailOnNoDefinedTypes) {
437 AidlError reported_error;
438 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", &cpp_types_, &reported_error));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900439 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
Steven Morelandc258abc2018-07-10 14:03:38 -0700440}
441
Roshan Pius3b2203d2016-07-22 16:13:20 -0700442TEST_F(AidlTest, FailOnMalformedConstHexValue) {
443 AidlError reported_error;
444 EXPECT_EQ(nullptr,
445 Parse("p/IFoo.aidl",
446 R"(package p;
447 interface IFoo {
448 const int BAD_HEX_VALUE = 0xffffffffffffffffff;
449 }
450 )",
451 &cpp_types_,
452 &reported_error));
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700453 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
Roshan Pius3b2203d2016-07-22 16:13:20 -0700454}
455
456TEST_F(AidlTest, ParsePositiveConstHexValue) {
457 AidlError reported_error;
458 auto cpp_parse_result =
459 Parse("p/IFoo.aidl",
460 R"(package p;
461 interface IFoo {
462 const int POSITIVE_HEX_VALUE = 0xf5;
463 }
464 )",
465 &cpp_types_,
466 &reported_error);
467 EXPECT_NE(nullptr, cpp_parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700468 const AidlInterface* interface = cpp_parse_result->AsInterface();
469 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700470 const auto& cpp_constants = interface->GetConstantDeclarations();
471 EXPECT_EQ((size_t)1, cpp_constants.size());
472 EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_constants[0]->GetName());
Steven Moreland860b1942018-08-16 14:59:28 -0700473 EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700474}
475
476TEST_F(AidlTest, ParseNegativeConstHexValue) {
477 AidlError reported_error;
478 auto cpp_parse_result =
479 Parse("p/IFoo.aidl",
480 R"(package p;
481 interface IFoo {
482 const int NEGATIVE_HEX_VALUE = 0xffffffff;
483 }
484 )",
485 &cpp_types_,
486 &reported_error);
487 EXPECT_NE(nullptr, cpp_parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700488 const AidlInterface* interface = cpp_parse_result->AsInterface();
489 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700490 const auto& cpp_constants = interface->GetConstantDeclarations();
491 EXPECT_EQ((size_t)1, cpp_constants.size());
492 EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_constants[0]->GetName());
Steven Moreland860b1942018-08-16 14:59:28 -0700493 EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700494}
495
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700496TEST_F(AidlTest, UnderstandsNestedParcelables) {
497 io_delegate_.SetFileContents(
498 "p/Outer.aidl",
499 "package p; parcelable Outer.Inner cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900500 import_paths_.emplace("");
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700501 const string input_path = "p/IFoo.aidl";
502 const string input = "package p; import p.Outer; interface IFoo"
503 " { Outer.Inner get(); }";
504
505 auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
506 EXPECT_NE(nullptr, cpp_parse_result);
507 auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Outer.Inner");
508 ASSERT_NE(nullptr, cpp_type);
509 // C++ uses "::" instead of "." to refer to a inner class.
510 EXPECT_EQ("::p::Outer::Inner", cpp_type->CppType());
511}
512
Christopher Wiley9078d722015-11-17 10:23:49 -0800513TEST_F(AidlTest, UnderstandsNativeParcelables) {
514 io_delegate_.SetFileContents(
515 "p/Bar.aidl",
Casey Dahlincd639212015-12-15 12:51:04 -0800516 "package p; parcelable Bar cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900517 import_paths_.emplace("");
Christopher Wiley9078d722015-11-17 10:23:49 -0800518 const string input_path = "p/IFoo.aidl";
519 const string input = "package p; import p.Bar; interface IFoo { }";
520
521 // C++ understands C++ specific stuff
522 auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
523 EXPECT_NE(nullptr, cpp_parse_result);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800524 auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Bar");
Christopher Wiley9078d722015-11-17 10:23:49 -0800525 ASSERT_NE(nullptr, cpp_type);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800526 EXPECT_EQ("::p::Bar", cpp_type->CppType());
Christopher Wiley9078d722015-11-17 10:23:49 -0800527 set<string> headers;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800528 cpp_type->GetHeaders(&headers);
Christopher Wiley9078d722015-11-17 10:23:49 -0800529 EXPECT_EQ(1u, headers.size());
530 EXPECT_EQ(1u, headers.count("baz/header"));
531
532 // Java ignores C++ specific stuff
533 auto java_parse_result = Parse(input_path, input, &java_types_);
534 EXPECT_NE(nullptr, java_parse_result);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800535 auto java_type = java_types_.FindTypeByCanonicalName("p.Bar");
Christopher Wiley9078d722015-11-17 10:23:49 -0800536 ASSERT_NE(nullptr, java_type);
537 EXPECT_EQ("p.Bar", java_type->InstantiableName());
538}
539
Christopher Wileyf8136192016-04-12 14:19:35 -0700540TEST_F(AidlTest, WritesCorrectDependencyFile) {
541 // While the in tree build system always gives us an output file name,
542 // other android tools take advantage of our ability to infer the intended
543 // file name. This test makes sure we handle this correctly.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900544 vector<string> args = {
545 "aidl",
546 "-d dep/file/path",
547 "-o place/for/output",
548 "p/IFoo.aidl"};
549 Options options = Options::From(args);
550 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900551 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyf8136192016-04-12 14:19:35 -0700552 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900553 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Christopher Wileyf8136192016-04-12 14:19:35 -0700554 EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
555}
556
Dan Willemsen93298ee2016-11-10 23:55:55 -0800557TEST_F(AidlTest, WritesCorrectDependencyFileNinja) {
558 // While the in tree build system always gives us an output file name,
559 // other android tools take advantage of our ability to infer the intended
560 // file name. This test makes sure we handle this correctly.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900561 vector<string> args = {
562 "aidl",
563 "-d dep/file/path",
564 "--ninja",
565 "-o place/for/output",
566 "p/IFoo.aidl"};
567 Options options = Options::From(args);
568 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900569 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800570 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900571 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800572 EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents);
573}
574
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700575TEST_F(AidlTest, WritesTrivialDependencyFileForParcelable) {
576 // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does
577 // this by calling aidl with every .aidl file it finds, then parsing the
578 // generated dependency files. Those that reference .java output files are
579 // for interfaces and those that do not are parcelables. However, for both
580 // parcelables and interfaces, we *must* generate a non-empty dependency file.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900581 vector<string> args = {
582 "aidl",
583 "-o place/for/output",
584 "-d dep/file/path",
585 "p/Foo.aidl"};
586 Options options = Options::From(args);
587 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900588 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700589 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900590 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700591 EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDepFileContents);
592}
593
Jiyong Parkccf00f82018-07-17 01:39:23 +0900594/* not working until type_namespace.h is fixed
595TEST_F(AidlTest, AcceptsNestedContainerType) {
596 string nested_in_iface = "package a; interface IFoo {\n"
597 " List<int, List<String, bool>> foo(); }";
598 string nested_in_parcelable = "package a; parcelable IData {\n"
599 " List<int, List<String, bool>> foo;}";
600 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, &java_types_));
601 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, &cpp_types_));
602 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, &java_types_));
603 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, &cpp_types_));
604}
605*/
606
Jiyong Park02da7422018-07-16 16:00:26 +0900607TEST_F(AidlTest, ApiDump) {
608 io_delegate_.SetFileContents(
609 "foo/bar/IFoo.aidl",
610 "package foo.bar;\n"
611 "import foo.bar.Data;\n"
Jiyong Parke59c3682018-09-11 23:10:25 +0900612 "// comment\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900613 "interface IFoo {\n"
614 " int foo(out int[] a, String b, boolean c, inout List<String> d);\n"
615 " int foo2(@utf8InCpp String x, inout List<String> y);\n"
616 " IFoo foo3(IFoo foo);\n"
617 " Data getData();\n"
Jiyong Parka428d212018-08-29 22:26:30 +0900618 " const int A = 1;\n"
619 " const String STR = \"Hello\";\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900620 "}\n");
621 io_delegate_.SetFileContents("foo/bar/Data.aidl",
622 "package foo.bar;\n"
623 "import foo.bar.IFoo;\n"
624 "parcelable Data {\n"
Jiyong Parka468e2a2018-08-29 21:25:18 +0900625 " int x = 10;\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900626 " int y;\n"
627 " IFoo foo;\n"
628 " List<IFoo> a;\n"
629 " List<foo.bar.IFoo> b;\n"
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900630 " @nullable String[] c;\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900631 "}\n");
632 io_delegate_.SetFileContents("api.aidl", "");
Jiyong Parke59c3682018-09-11 23:10:25 +0900633 vector<string> args = {"aidl", "--dumpapi", "--out=dump", "foo/bar/IFoo.aidl",
634 "foo/bar/Data.aidl"};
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900635 Options options = Options::From(args);
Jiyong Park02da7422018-07-16 16:00:26 +0900636 bool result = dump_api(options, io_delegate_);
637 ASSERT_TRUE(result);
638 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +0900639 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
640 EXPECT_EQ(actual, R"(package foo.bar;
641interface IFoo {
642 int foo(out int[] a, String b, boolean c, inout List<String> d);
643 int foo2(@utf8InCpp String x, inout List<String> y);
644 foo.bar.IFoo foo3(foo.bar.IFoo foo);
645 foo.bar.Data getData();
646 const int A = 1;
647 const String STR = "Hello";
648}
649)");
Jiyong Park02da7422018-07-16 16:00:26 +0900650
Jiyong Parke59c3682018-09-11 23:10:25 +0900651 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual));
652 EXPECT_EQ(actual, R"(package foo.bar;
653parcelable Data {
654 int x = 10;
655 int y;
656 foo.bar.IFoo foo;
657 List<foo.bar.IFoo> a;
658 List<foo.bar.IFoo> b;
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900659 @nullable String[] c;
Jiyong Park02da7422018-07-16 16:00:26 +0900660}
661)");
662}
663
Jiyong Parked65bf42018-08-28 15:43:27 +0900664TEST_F(AidlTest, ApiDumpWithManualIds) {
665 io_delegate_.SetFileContents(
666 "foo/bar/IFoo.aidl",
667 "package foo.bar;\n"
668 "interface IFoo {\n"
669 " int foo() = 1;\n"
670 " int bar() = 2;\n"
671 " int baz() = 10;\n"
672 "}\n");
673
Jiyong Parke59c3682018-09-11 23:10:25 +0900674 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +0900675 Options options = Options::From(args);
676 bool result = dump_api(options, io_delegate_);
677 ASSERT_TRUE(result);
678 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +0900679 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
680 EXPECT_EQ(actual, R"(package foo.bar;
681interface IFoo {
682 int foo() = 1;
683 int bar() = 2;
684 int baz() = 10;
Jiyong Parked65bf42018-08-28 15:43:27 +0900685}
686)");
687}
688
689TEST_F(AidlTest, ApiDumpWithManualIdsOnlyOnSomeMethods) {
690 io_delegate_.SetFileContents(
691 "foo/bar/IFoo.aidl",
692 "package foo.bar;\n"
693 "interface IFoo {\n"
694 " int foo() = 1;\n"
695 " int bar();\n"
696 " int baz() = 10;\n"
697 "}\n");
698
Jiyong Parke59c3682018-09-11 23:10:25 +0900699 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +0900700 Options options = Options::From(args);
701 EXPECT_FALSE(dump_api(options, io_delegate_));
702}
703
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900704TEST_F(AidlTest, CheckNumGenericTypeSecifier) {
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900705 Options options = Options::From("aidl p/IFoo.aidl IFoo.java");
706 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900707 "package p; interface IFoo {"
708 "void foo(List<String, String> a);}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900709 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900710
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900711 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900712 "package p; interface IFoo {"
713 "void foo(Map<String> a);}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900714 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900715
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900716 Options options2 = Options::From("aidl p/Data.aidl Data.java");
717 io_delegate_.SetFileContents(options2.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900718 "package p; parcelable Data {"
719 "List<String, String> foo;}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900720 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900721
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900722 io_delegate_.SetFileContents(options2.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900723 "package p; parcelable Data {"
724 "Map<String> foo;}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900725 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
726}
727
728TEST_F(AidlTest, MultipleTypesInSingleFile) {
729 Options options = Options::From("aidl --lang=java -o out foo/bar/Foo.aidl");
730 io_delegate_.SetFileContents(options.InputFiles().front(),
731 "package foo.bar;\n"
732 "interface IFoo1 { int foo(); }\n"
733 "interface IFoo2 { int foo(); }\n"
734 "parcelable Data { int a; int b;}\n");
735
736 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
737
738 string content;
739 for (const auto file :
740 {"out/foo/bar/IFoo1.java", "out/foo/bar/IFoo2.java", "out/foo/bar/Data.java"}) {
741 content.clear();
742 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
743 EXPECT_FALSE(content.empty());
744 }
745}
746
747TEST_F(AidlTest, MultipleTypesInSingleFileCpp) {
748 Options options = Options::From("aidl --lang=cpp -o out -h out/include foo/bar/Foo.aidl");
749 io_delegate_.SetFileContents(options.InputFiles().front(),
750 "package foo.bar;\n"
751 "interface IFoo1 { int foo(); }\n"
752 "interface IFoo2 { int foo(); }\n"
753 "parcelable Data { int a; int b;}\n");
754
755 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
756
757 string content;
758 for (const auto file : {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900759 "out/foo/bar/IFoo1.cpp", "out/foo/bar/IFoo2.cpp", "out/foo/bar/Data.cpp",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900760 "out/include/foo/bar/IFoo1.h", "out/include/foo/bar/IFoo2.h", "out/include/foo/bar/Data.h",
761 "out/include/foo/bar/BpFoo1.h", "out/include/foo/bar/BpFoo2.h", "out/include/foo/bar/BpData.h",
762 "out/include/foo/bar/BnFoo1.h", "out/include/foo/bar/BnFoo2.h", "out/include/foo/bar/BnData.h"}) {
763 content.clear();
764 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
765 EXPECT_FALSE(content.empty());
766 }
767}
768
769TEST_F(AidlTest, MultipleInputFiles) {
770 Options options = Options::From(
771 "aidl --lang=java -o out foo/bar/IFoo.aidl foo/bar/Data.aidl");
772
773 io_delegate_.SetFileContents(options.InputFiles().at(0),
774 "package foo.bar;\n"
775 "import foo.bar.Data;\n"
776 "interface IFoo { Data getData(); }\n");
777
778 io_delegate_.SetFileContents(options.InputFiles().at(1),
779 "package foo.bar;\n"
780 "import foo.bar.IFoo;\n"
781 "parcelable Data { IFoo foo; }\n");
782
783 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
784
785 string content;
786 for (const auto file : {
787 "out/foo/bar/IFoo.java", "out/foo/bar/Data.java"}) {
788 content.clear();
789 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
790 EXPECT_FALSE(content.empty());
791 }
792}
793
794TEST_F(AidlTest, MultipleInputFilesCpp) {
795 Options options = Options::From("aidl --lang=cpp -o out -h out/include "
796 "foo/bar/IFoo.aidl foo/bar/Data.aidl");
797
798 io_delegate_.SetFileContents(options.InputFiles().at(0),
799 "package foo.bar;\n"
800 "import foo.bar.Data;\n"
801 "interface IFoo { Data getData(); }\n");
802
803 io_delegate_.SetFileContents(options.InputFiles().at(1),
804 "package foo.bar;\n"
805 "import foo.bar.IFoo;\n"
806 "parcelable Data { IFoo foo; }\n");
807
808 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
809
810 string content;
811 for (const auto file : {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900812 "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900813 "out/include/foo/bar/IFoo.h", "out/include/foo/bar/Data.h",
814 "out/include/foo/bar/BpFoo.h", "out/include/foo/bar/BpData.h",
815 "out/include/foo/bar/BnFoo.h", "out/include/foo/bar/BnData.h"}) {
816 content.clear();
817 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
818 EXPECT_FALSE(content.empty());
819 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900820}
821
Jiyong Park309668e2018-07-28 16:55:44 +0900822TEST_F(AidlTest, ConflictWithMetaTransactions) {
823 Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl");
824 // int getInterfaceVersion() is one of the meta transactions
825 io_delegate_.SetFileContents(options.InputFiles().front(),
826 "package p; interface IFoo {"
827 "int getInterfaceVersion(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900828 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900829
830 // boolean getInterfaceVersion() is not, but should be prevented
831 // because return type is not part of a method signature
832 io_delegate_.SetFileContents(options.InputFiles().front(),
833 "package p; interface IFoo {"
834 "boolean getInterfaceVersion(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900835 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900836
837 // this is another reserved name
838 io_delegate_.SetFileContents(options.InputFiles().front(),
839 "package p; interface IFoo {"
840 "String getTransactionName(int code); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900841 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900842
843 // this is not a meta interface method as it differs type arguments
844 io_delegate_.SetFileContents(options.InputFiles().front(),
845 "package p; interface IFoo {"
846 "String getTransactionName(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900847 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900848}
849
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900850TEST_F(AidlTest, DiffrentOrderAnnotationsInCheckAPI) {
851 Options options = Options::From("aidl --checkapi old new");
852 io_delegate_.SetFileContents("old/p/IFoo.aidl",
853 "package p; interface IFoo{ @utf8InCpp @nullable String foo();}");
854 io_delegate_.SetFileContents("new/p/IFoo.aidl",
855 "package p; interface IFoo{ @nullable @utf8InCpp String foo();}");
856
857 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
858}
859
Jiyong Park3656c3c2018-08-01 20:02:01 +0900860TEST_F(AidlTest, SuccessOnIdenticalApiDumps) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900861 Options options = Options::From("aidl --checkapi old new");
862 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
863 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
Jiyong Park3656c3c2018-08-01 20:02:01 +0900864
865 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
866}
867
868TEST_F(AidlTest, SuccessOnCompatibleChanges) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900869 Options options = Options::From("aidl --checkapi old new");
870 io_delegate_.SetFileContents("old/p/IFoo.aidl",
871 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900872 "interface IFoo {"
873 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +0900874 "}");
875 io_delegate_.SetFileContents("old/p/Data.aidl",
876 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900877 "parcelable Data {"
878 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900879 "}");
880
881 // new type
Jiyong Parke59c3682018-09-11 23:10:25 +0900882 io_delegate_.SetFileContents("new/p/IFoo.aidl",
883 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900884 "interface IFoo {"
885 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +0900886 "}");
887 io_delegate_.SetFileContents("new/p/Data.aidl",
888 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900889 "parcelable Data {"
890 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +0900891 "}");
892 io_delegate_.SetFileContents("new/p/IBar.aidl",
893 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900894 "interface IBar {"
895 " void bar();"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900896 "}");
897 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +0900898 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
899 io_delegate_.SetFileContents("new/p/Data.aidl", "");
900 io_delegate_.SetFileContents("new/p/IBar.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +0900901
902 // new method
Jiyong Parke59c3682018-09-11 23:10:25 +0900903 io_delegate_.SetFileContents("new/p/IFoo.aidl",
904 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900905 "interface IFoo {"
906 " void foo(int a);"
907 " void bar();"
Jiyong Parke59c3682018-09-11 23:10:25 +0900908 "}");
909 io_delegate_.SetFileContents("new/p/Data.aidl",
910 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900911 "parcelable Data {"
912 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900913 "}");
914 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +0900915 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
916 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +0900917
918 // new field
Jiyong Parke59c3682018-09-11 23:10:25 +0900919 io_delegate_.SetFileContents("new/p/IFoo.aidl",
920 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900921 "interface IFoo {"
922 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +0900923 "}");
924 io_delegate_.SetFileContents("new/p/Data.aidl",
925 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900926 "parcelable Data {"
927 " int foo;"
928 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900929 "}");
930 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +0900931 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
932 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +0900933
934 // new package
Jiyong Parke59c3682018-09-11 23:10:25 +0900935 io_delegate_.SetFileContents("new/p/IFoo.aidl",
936 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900937 "interface IFoo {"
938 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +0900939 "}");
940 io_delegate_.SetFileContents("new/p/Data.aidl",
941 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900942 "parcelable Data {"
943 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +0900944 "}");
945 io_delegate_.SetFileContents("new/q/IFoo.aidl",
946 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900947 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +0900948 " void foo(int a);"
949 "}");
950 io_delegate_.SetFileContents("new/q/Data.aidl",
951 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900952 "parcelable Data {"
953 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900954 "}");
955 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +0900956 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
957 io_delegate_.SetFileContents("new/p/Data.aidl", "");
958 io_delegate_.SetFileContents("new/q/IFoo.aidl", "");
959 io_delegate_.SetFileContents("new/q/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +0900960
961 // arg name change
Jiyong Parke59c3682018-09-11 23:10:25 +0900962 io_delegate_.SetFileContents("new/p/IFoo.aidl",
963 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900964 "interface IFoo {"
965 " void foo(int b);"
Jiyong Parke59c3682018-09-11 23:10:25 +0900966 "}");
967 io_delegate_.SetFileContents("new/p/Data.aidl",
968 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900969 "parcelable Data {"
970 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +0900971 "}");
972 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +0900973 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
974 io_delegate_.SetFileContents("new/p/Data.aidl", "");
975
976 io_delegate_.SetFileContents("old/p/IFoo.aidl", "");
977 io_delegate_.SetFileContents("old/p/Data.aidl", "");
Jiyong Parka428d212018-08-29 22:26:30 +0900978
979 // added const value
Jiyong Parke59c3682018-09-11 23:10:25 +0900980 io_delegate_.SetFileContents("old/p/I.aidl",
981 "package p; interface I {"
982 "const int A = 1; }");
983 io_delegate_.SetFileContents("new/p/I.aidl",
984 "package p ; interface I {"
985 "const int A = 1; const int B = 2;}");
Jiyong Parka428d212018-08-29 22:26:30 +0900986 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +0900987 io_delegate_.SetFileContents("old/p/I.aidl", "");
988 io_delegate_.SetFileContents("new/p/I.aidl", "");
Jiyong Parka428d212018-08-29 22:26:30 +0900989
990 // changed const value order
Jiyong Parke59c3682018-09-11 23:10:25 +0900991 io_delegate_.SetFileContents("old/p/I.aidl",
992 "package p; interface I {"
993 "const int A = 1; const int B = 2;}");
994 io_delegate_.SetFileContents("new/p/I.aidl",
995 "package p ; interface I {"
996 "const int B = 2; const int A = 1;}");
Jiyong Parka428d212018-08-29 22:26:30 +0900997 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
Jiyong Park3656c3c2018-08-01 20:02:01 +0900998}
999
1000TEST_F(AidlTest, FailOnIncompatibleChanges) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001001 Options options = Options::From("aidl --checkapi old new");
1002 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1003 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001004 "interface IFoo {"
1005 " void foo(in String[] str);"
1006 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001007 "}");
1008 io_delegate_.SetFileContents("old/p/Data.aidl",
1009 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001010 "parcelable Data {"
1011 " int foo;"
1012 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001013 "}");
1014
1015 // removed type
Jiyong Parke59c3682018-09-11 23:10:25 +09001016 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1017 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001018 "interface IFoo {"
1019 " void foo(in String[] str);"
1020 " void bar(@utf8InCpp String str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001021 "}");
1022 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001023 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001024
1025 // removed method
Jiyong Parke59c3682018-09-11 23:10:25 +09001026 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1027 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001028 "interface IFoo {"
1029 " void foo(in String[] str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001030 "}");
1031 io_delegate_.SetFileContents("new/p/Data.aidl",
1032 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001033 "parcelable Data {"
1034 " int foo;"
1035 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001036 "}");
1037 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001038 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1039 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001040
1041 // removed field
Jiyong Parke59c3682018-09-11 23:10:25 +09001042 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1043 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001044 "interface IFoo {"
1045 " void foo(in String[] str);"
1046 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001047 "}");
1048 io_delegate_.SetFileContents("new/p/Data.aidl",
1049 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001050 "parcelable Data {"
1051 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001052 "}");
1053 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001054 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1055 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001056
1057 // renamed method
Jiyong Parke59c3682018-09-11 23:10:25 +09001058 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1059 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001060 "interface IFoo {"
1061 " void foo(in String[] str);"
1062 " void bar2(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001063 "}");
1064 io_delegate_.SetFileContents("new/p/Data.aidl",
1065 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001066 "parcelable Data {"
1067 " int foo;"
1068 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001069 "}");
1070 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001071 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1072 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001073
1074 // renamed field
Jiyong Parke59c3682018-09-11 23:10:25 +09001075 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1076 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001077 "interface IFoo {"
1078 " void foo(in String[] str);"
1079 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001080 "}");
1081 io_delegate_.SetFileContents("new/p/Data.aidl",
1082 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001083 "parcelable Data {"
1084 " int foo;"
1085 " int bar2;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001086 "}");
1087 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001088 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1089 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001090
1091 // renamed type
Jiyong Parke59c3682018-09-11 23:10:25 +09001092 io_delegate_.SetFileContents("new/p/IFoo2.aidl",
1093 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001094 "interface IFoo2 {"
1095 " void foo(in String[] str);"
1096 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001097 "}");
1098 io_delegate_.SetFileContents("new/p/Data.aidl",
1099 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001100 "parcelable Data {"
1101 " int foo;"
1102 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001103 "}");
1104 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001105 io_delegate_.SetFileContents("new/p/IFoo2.aidl", "");
1106 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001107
1108 // reorderd method
Jiyong Parke59c3682018-09-11 23:10:25 +09001109 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1110 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001111 "interface IFoo {"
1112 " void bar(@utf8InCpp String str);"
1113 " void foo(in String[] str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001114 "}");
1115 io_delegate_.SetFileContents("new/p/Data.aidl",
1116 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001117 "parcelable Data {"
1118 " int foo;"
1119 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001120 "}");
1121 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001122 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1123 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001124
1125 // reorderd field
Jiyong Parke59c3682018-09-11 23:10:25 +09001126 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1127 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001128 "interface IFoo {"
1129 " void foo(in String[] str);"
1130 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001131 "}");
1132 io_delegate_.SetFileContents("new/p/Data.aidl",
1133 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001134 "parcelable Data {"
1135 " int bar;"
1136 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001137 "}");
1138 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001139 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1140 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001141
1142 // changed direction specifier
Jiyong Parke59c3682018-09-11 23:10:25 +09001143 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1144 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001145 "interface IFoo {"
1146 " void foo(out String[] str);"
1147 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001148 "}");
1149 io_delegate_.SetFileContents("new/p/Data.aidl",
1150 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001151 "parcelable Data {"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001152 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +09001153 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001154 "}");
1155 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001156 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1157 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001158
1159 // added annotation
Jiyong Parke59c3682018-09-11 23:10:25 +09001160 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1161 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001162 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001163 " void foo(in @utf8InCpp String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001164 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001165 "}");
1166 io_delegate_.SetFileContents("new/p/Data.aidl",
1167 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001168 "parcelable Data {"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001169 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +09001170 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001171 "}");
1172 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001173 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1174 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001175
1176 // removed annotation
Jiyong Parke59c3682018-09-11 23:10:25 +09001177 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1178 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001179 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001180 " void foo(in String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001181 " void bar(String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001182 "}");
1183 io_delegate_.SetFileContents("new/p/Data.aidl",
1184 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001185 "parcelable Data {"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001186 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +09001187 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001188 "}");
1189 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001190 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1191 io_delegate_.SetFileContents("new/p/Data.aidl", "");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001192
1193 // removed package
Jiyong Parke59c3682018-09-11 23:10:25 +09001194 io_delegate_.SetFileContents("old/p/Data.aidl", "");
1195 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}");
1196 io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}");
1197
1198 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}");
Jiyong Park3656c3c2018-08-01 20:02:01 +09001199 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001200 io_delegate_.SetFileContents("old/p/IFoo.aidl", "");
1201 io_delegate_.SetFileContents("old/q/IFoo.aidl", "");
1202 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
Jiyong Parka468e2a2018-08-29 21:25:18 +09001203
1204 // changed default value
Jiyong Parke59c3682018-09-11 23:10:25 +09001205 io_delegate_.SetFileContents("old/p/D.aidl", "package p; parcelable D { int a = 1; }");
1206 io_delegate_.SetFileContents("new/p/D.aidl", "package p; parcelable D { int a = 2; }");
Jiyong Parka468e2a2018-08-29 21:25:18 +09001207 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001208 io_delegate_.SetFileContents("old/p/D.aidl", "");
1209 io_delegate_.SetFileContents("new/p/D.aidl", "");
Jiyong Parka428d212018-08-29 22:26:30 +09001210
1211 // removed const value
Jiyong Parke59c3682018-09-11 23:10:25 +09001212 io_delegate_.SetFileContents("old/p/I.aidl",
1213 "package p; interface I {"
1214 "const int A = 1; const int B = 2;}");
1215 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 1; }");
Jiyong Parka428d212018-08-29 22:26:30 +09001216 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001217 io_delegate_.SetFileContents("old/p/I.aidl", "");
1218 io_delegate_.SetFileContents("new/p/I.aidl", "");
Jiyong Parka428d212018-08-29 22:26:30 +09001219
1220 // changed const value
Jiyong Parke59c3682018-09-11 23:10:25 +09001221 io_delegate_.SetFileContents("old/p/I.aidl", "package p; interface I { const int A = 1; }");
1222 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 2; }");
Jiyong Parka428d212018-08-29 22:26:30 +09001223 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
Jiyong Parke59c3682018-09-11 23:10:25 +09001224 io_delegate_.SetFileContents("old/p/I.aidl", "");
1225 io_delegate_.SetFileContents("new/p/I.aidl", "");
Jiyong Park3c35e392018-08-30 13:10:30 +09001226}
1227
Jiyong Park8c380532018-08-30 14:55:26 +09001228TEST_F(AidlTest, RejectAmbiguousImports) {
1229 Options options = Options::From("aidl --lang=java -o out -I dir1 -I dir2 p/IFoo.aidl");
1230 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; import q.IBar; interface IFoo{}");
1231 io_delegate_.SetFileContents("dir1/q/IBar.aidl", "package q; interface IBar{}");
1232 io_delegate_.SetFileContents("dir2/q/IBar.aidl", "package q; interface IBar{}");
1233
1234 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1235}
1236
Jiyong Parked65bf42018-08-28 15:43:27 +09001237TEST_F(AidlTest, HandleManualIdAssignments) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001238 Options options = Options::From("aidl --checkapi old new");
1239 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
1240 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
Jiyong Parked65bf42018-08-28 15:43:27 +09001241
1242 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1243
Jiyong Parke59c3682018-09-11 23:10:25 +09001244 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 11;}");
Jiyong Parked65bf42018-08-28 15:43:27 +09001245 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
1246}
1247
Jiyong Parke05195e2018-10-08 18:24:23 +09001248TEST_F(AidlTest, ParcelFileDescriptorIsBuiltinType) {
1249 Options javaOptions = Options::From("aidl --lang=java -o out p/IFoo.aidl");
1250 Options cppOptions = Options::From("aidl --lang=cpp -h out -o out p/IFoo.aidl");
1251
1252 // use without import
1253 io_delegate_.SetFileContents("p/IFoo.aidl",
1254 "package p; interface IFoo{ void foo(in ParcelFileDescriptor fd);}");
1255 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1256 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1257
1258 // use without impot but with full name
1259 io_delegate_.SetFileContents(
1260 "p/IFoo.aidl",
1261 "package p; interface IFoo{ void foo(in android.os.ParcelFileDescriptor fd);}");
1262 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1263 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1264
1265 // use with import (as before)
1266 io_delegate_.SetFileContents("p/IFoo.aidl",
1267 "package p;"
1268 "import android.os.ParcelFileDescriptor;"
1269 "interface IFoo{"
1270 " void foo(in ParcelFileDescriptor fd);"
1271 "}");
1272 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1273 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1274}
Jiyong Parked65bf42018-08-28 15:43:27 +09001275
Christopher Wiley90be4e32015-10-20 14:55:25 -07001276} // namespace aidl
1277} // namespace android