blob: 9421dc9ce29ae9a621c28cf6289b2e3aa70b79ef [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"
26#include "aidl_language.h"
27#include "tests/fake_io_delegate.h"
28#include "type_cpp.h"
29#include "type_java.h"
30#include "type_namespace.h"
31
32using android::aidl::test::FakeIoDelegate;
Christopher Wileyec31a052016-01-25 07:28:51 -080033using android::base::StringPrintf;
Christopher Wiley12e894a2016-01-29 11:55:07 -080034using std::set;
Christopher Wiley90be4e32015-10-20 14:55:25 -070035using std::string;
36using std::unique_ptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -080037using std::vector;
Christopher Wileyef140932015-11-03 09:29:19 -080038using android::aidl::internals::parse_preprocessed_file;
Christopher Wiley90be4e32015-10-20 14:55:25 -070039
40namespace android {
41namespace aidl {
42
43class AidlTest : public ::testing::Test {
44 protected:
Christopher Wiley56799522015-10-31 10:17:04 -070045 void SetUp() override {
46 java_types_.Init();
47 cpp_types_.Init();
48 }
49
Christopher Wiley90be4e32015-10-20 14:55:25 -070050 unique_ptr<AidlInterface> Parse(const string& path,
51 const string& contents,
52 TypeNamespace* types) {
Christopher Wiley0522cd52015-10-28 15:39:44 -070053 io_delegate_.SetFileContents(path, contents);
Christopher Wiley90be4e32015-10-20 14:55:25 -070054 unique_ptr<AidlInterface> ret;
55 std::vector<std::unique_ptr<AidlImport>> imports;
56 ::android::aidl::internals::load_and_validate_aidl(
Christopher Wiley41544372015-11-03 14:52:29 -080057 preprocessed_files_,
Christopher Wiley0522cd52015-10-28 15:39:44 -070058 import_paths_,
Christopher Wiley90be4e32015-10-20 14:55:25 -070059 path,
Christopher Wiley0522cd52015-10-28 15:39:44 -070060 io_delegate_,
Christopher Wiley90be4e32015-10-20 14:55:25 -070061 types,
62 &ret,
63 &imports);
64 return ret;
65 }
Christopher Wiley0522cd52015-10-28 15:39:44 -070066
67 FakeIoDelegate io_delegate_;
Christopher Wiley41544372015-11-03 14:52:29 -080068 vector<string> preprocessed_files_;
Christopher Wiley0522cd52015-10-28 15:39:44 -070069 vector<string> import_paths_;
Christopher Wiley56799522015-10-31 10:17:04 -070070 java::JavaTypeNamespace java_types_;
71 cpp::TypeNamespace cpp_types_;
Christopher Wiley90be4e32015-10-20 14:55:25 -070072};
73
74TEST_F(AidlTest, JavaAcceptsMissingPackage) {
Christopher Wiley56799522015-10-31 10:17:04 -070075 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &java_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -070076}
77
Christopher Wiley0522cd52015-10-28 15:39:44 -070078TEST_F(AidlTest, RejectsArraysOfBinders) {
79 import_paths_.push_back("");
80 io_delegate_.SetFileContents("bar/IBar.aidl",
81 "package bar; interface IBar {}");
82 string path = "foo/IFoo.aidl";
83 string contents = "package foo;\n"
84 "import bar.IBar;\n"
85 "interface IFoo { void f(in IBar[] input); }";
Christopher Wiley56799522015-10-31 10:17:04 -070086 EXPECT_EQ(nullptr, Parse(path, contents, &java_types_));
87 EXPECT_EQ(nullptr, Parse(path, contents, &cpp_types_));
Christopher Wiley0522cd52015-10-28 15:39:44 -070088}
89
Christopher Wiley90be4e32015-10-20 14:55:25 -070090TEST_F(AidlTest, CppRejectsMissingPackage) {
Christopher Wiley56799522015-10-31 10:17:04 -070091 EXPECT_EQ(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &cpp_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -070092 EXPECT_NE(nullptr,
Christopher Wiley56799522015-10-31 10:17:04 -070093 Parse("a/IFoo.aidl", "package a; interface IFoo { }", &cpp_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -070094}
95
96TEST_F(AidlTest, RejectsOnewayOutParameters) {
Christopher Wiley90be4e32015-10-20 14:55:25 -070097 string oneway_interface =
98 "package a; oneway interface IFoo { void f(out int bar); }";
99 string oneway_method =
100 "package a; interface IBar { oneway void f(out int bar); }";
Christopher Wiley56799522015-10-31 10:17:04 -0700101 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types_));
102 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types_));
103 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types_));
104 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700105}
106
107TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700108 string oneway_method = "package a; interface IFoo { oneway int f(); }";
Christopher Wiley56799522015-10-31 10:17:04 -0700109 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
110 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700111}
112
Casey Dahlin57dbe242015-12-04 11:44:02 -0800113TEST_F(AidlTest, RejectsNullablePrimitive) {
114 string oneway_method = "package a; interface IFoo { @nullable int f(); }";
115 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
116 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
117}
118
Christopher Wileyec31a052016-01-25 07:28:51 -0800119TEST_F(AidlTest, ParsesNullableAnnotation) {
120 for (auto is_nullable: {true, false}) {
121 auto parse_result = Parse(
122 "a/IFoo.aidl",
123 StringPrintf( "package a; interface IFoo {%s String f(); }",
124 (is_nullable) ? "@nullable" : ""),
125 &cpp_types_);
126 ASSERT_NE(nullptr, parse_result);
127 ASSERT_FALSE(parse_result->GetMethods().empty());
128 EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsNullable(),
129 is_nullable);
130 }
131}
132
133TEST_F(AidlTest, ParsesUtf8Annotations) {
134 for (auto is_utf8: {true, false}) {
135 auto parse_result = Parse(
136 "a/IFoo.aidl",
137 StringPrintf( "package a; interface IFoo {%s String f(); }",
Christopher Wiley9f403722016-01-27 16:04:11 -0800138 (is_utf8) ? "@utf8InCpp" : ""),
Christopher Wileyb7e01172016-01-28 16:32:34 -0800139 &cpp_types_);
Christopher Wileyec31a052016-01-25 07:28:51 -0800140 ASSERT_NE(nullptr, parse_result);
141 ASSERT_FALSE(parse_result->GetMethods().empty());
Christopher Wiley9f403722016-01-27 16:04:11 -0800142 EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsUtf8InCpp(),
Christopher Wileyec31a052016-01-25 07:28:51 -0800143 is_utf8);
144 }
145}
146
Christopher Wiley90be4e32015-10-20 14:55:25 -0700147TEST_F(AidlTest, AcceptsOneway) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700148 string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
149 string oneway_interface =
150 "package a; oneway interface IBar { void f(int a); }";
Christopher Wiley56799522015-10-31 10:17:04 -0700151 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
152 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
153 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types_));
154 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types_));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700155}
Christopher Wileyef140932015-11-03 09:29:19 -0800156
157TEST_F(AidlTest, ParsesPreprocessedFile) {
158 string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
159 io_delegate_.SetFileContents("path", simple_content);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800160 EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
Christopher Wileyef140932015-11-03 09:29:19 -0800161 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_));
Christopher Wiley9ab06232016-01-27 14:55:18 -0800162 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
163 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
Christopher Wileyef140932015-11-03 09:29:19 -0800164}
165
166TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
167 string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
168 io_delegate_.SetFileContents("path", simple_content);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800169 EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
Christopher Wileyef140932015-11-03 09:29:19 -0800170 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_));
Christopher Wiley9ab06232016-01-27 14:55:18 -0800171 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
172 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
Christopher Wileyef140932015-11-03 09:29:19 -0800173}
174
Christopher Wiley41544372015-11-03 14:52:29 -0800175TEST_F(AidlTest, PreferImportToPreprocessed) {
176 io_delegate_.SetFileContents("preprocessed", "interface another.IBar;");
177 io_delegate_.SetFileContents("one/IBar.aidl", "package one; "
178 "interface IBar {}");
179 preprocessed_files_.push_back("preprocessed");
180 import_paths_.push_back("");
181 auto parse_result = Parse(
182 "p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
183 &java_types_);
184 EXPECT_NE(nullptr, parse_result);
185 // We expect to know about both kinds of IBar
Christopher Wiley9ab06232016-01-27 14:55:18 -0800186 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("one.IBar"));
187 EXPECT_TRUE(java_types_.HasTypeByCanonicalName("another.IBar"));
Christopher Wiley41544372015-11-03 14:52:29 -0800188 // But if we request just "IBar" we should get our imported one.
Christopher Wiley9ab06232016-01-27 14:55:18 -0800189 AidlType ambiguous_type("IBar", 0, "", false /* not an array */);
190 const java::Type* type = java_types_.Find(ambiguous_type);
Christopher Wiley41544372015-11-03 14:52:29 -0800191 ASSERT_TRUE(type);
Christopher Wileyd21bfee2016-01-29 15:11:38 -0800192 EXPECT_EQ("one.IBar", type->CanonicalName());
Christopher Wiley41544372015-11-03 14:52:29 -0800193}
194
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800195TEST_F(AidlTest, WritePreprocessedFile) {
196 io_delegate_.SetFileContents("p/Outer.aidl",
197 "package p; parcelable Outer.Inner;");
198 io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;"
199 "interface IBar {}");
200
201 JavaOptions options;
202 options.output_file_name_ = "preprocessed";
203 options.files_to_preprocess_.resize(2);
204 options.files_to_preprocess_[0] = "p/Outer.aidl";
205 options.files_to_preprocess_[1] = "one/IBar.aidl";
206 EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_));
207
208 string output;
209 EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output));
210 EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output);
211}
212
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800213TEST_F(AidlTest, RequireOuterClass) {
214 io_delegate_.SetFileContents("p/Outer.aidl",
215 "package p; parcelable Outer.Inner;");
216 import_paths_.push_back("");
217 auto parse_result = Parse(
218 "p/IFoo.aidl",
219 "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
220 &java_types_);
221 EXPECT_EQ(nullptr, parse_result);
222}
223
224TEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) {
225 io_delegate_.SetFileContents("preprocessed",
226 "parcelable p.Outer.Inner;");
227 preprocessed_files_.push_back("preprocessed");
228 auto parse_result = Parse(
229 "p/IFoo.aidl",
230 "package p; interface IFoo { void f(in Inner c); }",
231 &java_types_);
232 // TODO(wiley): This should actually return nullptr because we require
233 // the outer class name. However, for legacy reasons,
234 // this behavior must be maintained. b/17415692
235 EXPECT_NE(nullptr, parse_result);
236}
237
Christopher Wiley632801d2015-11-05 14:15:49 -0800238TEST_F(AidlTest, FailOnParcelable) {
239 JavaOptions options;
240 options.input_file_name_ = "p/IFoo.aidl";
241 io_delegate_.SetFileContents(options.input_file_name_,
242 "package p; parcelable IFoo;");
243 // By default, we shouldn't fail on parcelable.
244 EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
245 options.fail_on_parcelable_ = true;
246 EXPECT_NE(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
247}
248
Christopher Wiley9078d722015-11-17 10:23:49 -0800249TEST_F(AidlTest, UnderstandsNativeParcelables) {
250 io_delegate_.SetFileContents(
251 "p/Bar.aidl",
Casey Dahlincd639212015-12-15 12:51:04 -0800252 "package p; parcelable Bar cpp_header \"baz/header\";");
Christopher Wiley9078d722015-11-17 10:23:49 -0800253 import_paths_.push_back("");
254 const string input_path = "p/IFoo.aidl";
255 const string input = "package p; import p.Bar; interface IFoo { }";
256
257 // C++ understands C++ specific stuff
258 auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
259 EXPECT_NE(nullptr, cpp_parse_result);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800260 auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Bar");
Christopher Wiley9078d722015-11-17 10:23:49 -0800261 ASSERT_NE(nullptr, cpp_type);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800262 EXPECT_EQ("::p::Bar", cpp_type->CppType());
Christopher Wiley9078d722015-11-17 10:23:49 -0800263 set<string> headers;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800264 cpp_type->GetHeaders(&headers);
Christopher Wiley9078d722015-11-17 10:23:49 -0800265 EXPECT_EQ(1u, headers.size());
266 EXPECT_EQ(1u, headers.count("baz/header"));
267
268 // Java ignores C++ specific stuff
269 auto java_parse_result = Parse(input_path, input, &java_types_);
270 EXPECT_NE(nullptr, java_parse_result);
Christopher Wiley9ab06232016-01-27 14:55:18 -0800271 auto java_type = java_types_.FindTypeByCanonicalName("p.Bar");
Christopher Wiley9078d722015-11-17 10:23:49 -0800272 ASSERT_NE(nullptr, java_type);
273 EXPECT_EQ("p.Bar", java_type->InstantiableName());
274}
275
Christopher Wiley90be4e32015-10-20 14:55:25 -0700276} // namespace aidl
277} // namespace android