1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/*
* Copyright (C) 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <memory>
#include <set>
#include <string>
#include <vector>
#include <android-base/stringprintf.h>
#include <gtest/gtest.h>
#include "aidl.h"
#include "aidl_language.h"
#include "tests/fake_io_delegate.h"
#include "type_cpp.h"
#include "type_java.h"
#include "type_namespace.h"
using android::aidl::test::FakeIoDelegate;
using android::base::StringPrintf;
using std::set;
using std::string;
using std::unique_ptr;
using std::vector;
using android::aidl::internals::parse_preprocessed_file;
namespace android {
namespace aidl {
namespace {
const char kExpectedDepFileContents[] =
R"(place/for/output/p/IFoo.java : \
p/IFoo.aidl
p/IFoo.aidl :
)";
const char kExpectedNinjaDepFileContents[] =
R"(place/for/output/p/IFoo.java : \
p/IFoo.aidl
)";
const char kExpectedParcelableDepFileContents[] =
R"( : \
p/Foo.aidl
p/Foo.aidl :
)";
} // namespace
class AidlTest : public ::testing::Test {
protected:
void SetUp() override {
java_types_.Init();
cpp_types_.Init();
}
unique_ptr<AidlInterface> Parse(const string& path,
const string& contents,
TypeNamespace* types,
AidlError* error = nullptr) {
io_delegate_.SetFileContents(path, contents);
unique_ptr<AidlInterface> ret;
std::vector<std::unique_ptr<AidlImport>> imports;
AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
preprocessed_files_,
import_paths_,
path,
io_delegate_,
types,
&ret,
&imports);
if (error != nullptr) {
*error = actual_error;
}
return ret;
}
FakeIoDelegate io_delegate_;
vector<string> preprocessed_files_;
vector<string> import_paths_;
java::JavaTypeNamespace java_types_;
cpp::TypeNamespace cpp_types_;
};
TEST_F(AidlTest, JavaAcceptsMissingPackage) {
EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &java_types_));
}
TEST_F(AidlTest, RejectsArraysOfBinders) {
import_paths_.push_back("");
io_delegate_.SetFileContents("bar/IBar.aidl",
"package bar; interface IBar {}");
string path = "foo/IFoo.aidl";
string contents = "package foo;\n"
"import bar.IBar;\n"
"interface IFoo { void f(in IBar[] input); }";
EXPECT_EQ(nullptr, Parse(path, contents, &java_types_));
EXPECT_EQ(nullptr, Parse(path, contents, &cpp_types_));
}
TEST_F(AidlTest, CppRejectsMissingPackage) {
EXPECT_EQ(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &cpp_types_));
EXPECT_NE(nullptr,
Parse("a/IFoo.aidl", "package a; interface IFoo { }", &cpp_types_));
}
TEST_F(AidlTest, RejectsOnewayOutParameters) {
string oneway_interface =
"package a; oneway interface IFoo { void f(out int bar); }";
string oneway_method =
"package a; interface IBar { oneway void f(out int bar); }";
EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types_));
EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types_));
EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types_));
EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types_));
}
TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
string oneway_method = "package a; interface IFoo { oneway int f(); }";
EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
}
TEST_F(AidlTest, RejectsNullablePrimitive) {
string oneway_method = "package a; interface IFoo { @nullable int f(); }";
EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
}
TEST_F(AidlTest, ParsesNullableAnnotation) {
for (auto is_nullable: {true, false}) {
auto parse_result = Parse(
"a/IFoo.aidl",
StringPrintf( "package a; interface IFoo {%s String f(); }",
(is_nullable) ? "@nullable" : ""),
&cpp_types_);
ASSERT_NE(nullptr, parse_result);
ASSERT_FALSE(parse_result->GetMethods().empty());
EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsNullable(),
is_nullable);
}
}
TEST_F(AidlTest, ParsesUtf8Annotations) {
for (auto is_utf8: {true, false}) {
auto parse_result = Parse(
"a/IFoo.aidl",
StringPrintf( "package a; interface IFoo {%s String f(); }",
(is_utf8) ? "@utf8InCpp" : ""),
&cpp_types_);
ASSERT_NE(nullptr, parse_result);
ASSERT_FALSE(parse_result->GetMethods().empty());
EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsUtf8InCpp(),
is_utf8);
}
}
TEST_F(AidlTest, AcceptsOneway) {
string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
string oneway_interface =
"package a; oneway interface IBar { void f(int a); }";
EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types_));
EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types_));
}
TEST_F(AidlTest, ParsesPreprocessedFile) {
string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
io_delegate_.SetFileContents("path", simple_content);
EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_));
EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
}
TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
io_delegate_.SetFileContents("path", simple_content);
EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_));
EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
}
TEST_F(AidlTest, PreferImportToPreprocessed) {
io_delegate_.SetFileContents("preprocessed", "interface another.IBar;");
io_delegate_.SetFileContents("one/IBar.aidl", "package one; "
"interface IBar {}");
preprocessed_files_.push_back("preprocessed");
import_paths_.push_back("");
auto parse_result = Parse(
"p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
&java_types_);
EXPECT_NE(nullptr, parse_result);
// We expect to know about both kinds of IBar
EXPECT_TRUE(java_types_.HasTypeByCanonicalName("one.IBar"));
EXPECT_TRUE(java_types_.HasTypeByCanonicalName("another.IBar"));
// But if we request just "IBar" we should get our imported one.
AidlType ambiguous_type("IBar", 0, "", false /* not an array */);
const java::Type* type = java_types_.Find(ambiguous_type);
ASSERT_TRUE(type);
EXPECT_EQ("one.IBar", type->CanonicalName());
}
TEST_F(AidlTest, WritePreprocessedFile) {
io_delegate_.SetFileContents("p/Outer.aidl",
"package p; parcelable Outer.Inner;");
io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;"
"interface IBar {}");
JavaOptions options;
options.output_file_name_ = "preprocessed";
options.files_to_preprocess_.resize(2);
options.files_to_preprocess_[0] = "p/Outer.aidl";
options.files_to_preprocess_[1] = "one/IBar.aidl";
EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_));
string output;
EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output));
EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output);
}
TEST_F(AidlTest, RequireOuterClass) {
io_delegate_.SetFileContents("p/Outer.aidl",
"package p; parcelable Outer.Inner;");
import_paths_.push_back("");
auto parse_result = Parse(
"p/IFoo.aidl",
"package p; import p.Outer; interface IFoo { void f(in Inner c); }",
&java_types_);
EXPECT_EQ(nullptr, parse_result);
}
TEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) {
io_delegate_.SetFileContents("preprocessed",
"parcelable p.Outer.Inner;");
preprocessed_files_.push_back("preprocessed");
auto parse_result = Parse(
"p/IFoo.aidl",
"package p; interface IFoo { void f(in Inner c); }",
&java_types_);
// TODO(wiley): This should actually return nullptr because we require
// the outer class name. However, for legacy reasons,
// this behavior must be maintained. b/17415692
EXPECT_NE(nullptr, parse_result);
}
TEST_F(AidlTest, FailOnParcelable) {
JavaOptions options;
options.input_file_name_ = "p/IFoo.aidl";
io_delegate_.SetFileContents(options.input_file_name_,
"package p; parcelable IFoo;");
// By default, we shouldn't fail on parcelable.
EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
options.fail_on_parcelable_ = true;
EXPECT_NE(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
}
TEST_F(AidlTest, FailOnDuplicateConstantNames) {
AidlError reported_error;
EXPECT_EQ(nullptr,
Parse("p/IFoo.aidl",
R"(package p;
interface IFoo {
const String DUPLICATED = "d";
const int DUPLICATED = 1;
}
)",
&cpp_types_,
&reported_error));
EXPECT_EQ(AidlError::BAD_CONSTANTS, reported_error);
}
TEST_F(AidlTest, FailOnMalformedConstHexValue) {
AidlError reported_error;
EXPECT_EQ(nullptr,
Parse("p/IFoo.aidl",
R"(package p;
interface IFoo {
const int BAD_HEX_VALUE = 0xffffffffffffffffff;
}
)",
&cpp_types_,
&reported_error));
EXPECT_EQ(AidlError::BAD_CONSTANTS, reported_error);
}
TEST_F(AidlTest, ParsePositiveConstHexValue) {
AidlError reported_error;
auto cpp_parse_result =
Parse("p/IFoo.aidl",
R"(package p;
interface IFoo {
const int POSITIVE_HEX_VALUE = 0xf5;
}
)",
&cpp_types_,
&reported_error);
EXPECT_NE(nullptr, cpp_parse_result);
const auto& cpp_int_constants = cpp_parse_result->GetIntConstants();
EXPECT_EQ((size_t)1, cpp_int_constants.size());
EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_int_constants[0]->GetName());
EXPECT_EQ(245, cpp_int_constants[0]->GetValue());
}
TEST_F(AidlTest, ParseNegativeConstHexValue) {
AidlError reported_error;
auto cpp_parse_result =
Parse("p/IFoo.aidl",
R"(package p;
interface IFoo {
const int NEGATIVE_HEX_VALUE = 0xffffffff;
}
)",
&cpp_types_,
&reported_error);
EXPECT_NE(nullptr, cpp_parse_result);
const auto& cpp_int_constants = cpp_parse_result->GetIntConstants();
EXPECT_EQ((size_t)1, cpp_int_constants.size());
EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_int_constants[0]->GetName());
EXPECT_EQ(-1, cpp_int_constants[0]->GetValue());
}
TEST_F(AidlTest, UnderstandsNestedParcelables) {
io_delegate_.SetFileContents(
"p/Outer.aidl",
"package p; parcelable Outer.Inner cpp_header \"baz/header\";");
import_paths_.push_back("");
const string input_path = "p/IFoo.aidl";
const string input = "package p; import p.Outer; interface IFoo"
" { Outer.Inner get(); }";
auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
EXPECT_NE(nullptr, cpp_parse_result);
auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Outer.Inner");
ASSERT_NE(nullptr, cpp_type);
// C++ uses "::" instead of "." to refer to a inner class.
EXPECT_EQ("::p::Outer::Inner", cpp_type->CppType());
}
TEST_F(AidlTest, UnderstandsNativeParcelables) {
io_delegate_.SetFileContents(
"p/Bar.aidl",
"package p; parcelable Bar cpp_header \"baz/header\";");
import_paths_.push_back("");
const string input_path = "p/IFoo.aidl";
const string input = "package p; import p.Bar; interface IFoo { }";
// C++ understands C++ specific stuff
auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
EXPECT_NE(nullptr, cpp_parse_result);
auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Bar");
ASSERT_NE(nullptr, cpp_type);
EXPECT_EQ("::p::Bar", cpp_type->CppType());
set<string> headers;
cpp_type->GetHeaders(&headers);
EXPECT_EQ(1u, headers.size());
EXPECT_EQ(1u, headers.count("baz/header"));
// Java ignores C++ specific stuff
auto java_parse_result = Parse(input_path, input, &java_types_);
EXPECT_NE(nullptr, java_parse_result);
auto java_type = java_types_.FindTypeByCanonicalName("p.Bar");
ASSERT_NE(nullptr, java_type);
EXPECT_EQ("p.Bar", java_type->InstantiableName());
}
TEST_F(AidlTest, WritesCorrectDependencyFile) {
// While the in tree build system always gives us an output file name,
// other android tools take advantage of our ability to infer the intended
// file name. This test makes sure we handle this correctly.
JavaOptions options;
options.input_file_name_ = "p/IFoo.aidl";
options.output_base_folder_ = "place/for/output";
options.dep_file_name_ = "dep/file/path";
io_delegate_.SetFileContents(options.input_file_name_,
"package p; interface IFoo {}");
EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
string actual_dep_file_contents;
EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_,
&actual_dep_file_contents));
EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
}
TEST_F(AidlTest, WritesCorrectDependencyFileNinja) {
// While the in tree build system always gives us an output file name,
// other android tools take advantage of our ability to infer the intended
// file name. This test makes sure we handle this correctly.
JavaOptions options;
options.input_file_name_ = "p/IFoo.aidl";
options.output_base_folder_ = "place/for/output";
options.dep_file_name_ = "dep/file/path";
options.dep_file_ninja_ = true;
io_delegate_.SetFileContents(options.input_file_name_,
"package p; interface IFoo {}");
EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
string actual_dep_file_contents;
EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_,
&actual_dep_file_contents));
EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents);
}
TEST_F(AidlTest, WritesTrivialDependencyFileForParcelable) {
// The SDK uses aidl to decide whether a .aidl file is a parcelable. It does
// this by calling aidl with every .aidl file it finds, then parsing the
// generated dependency files. Those that reference .java output files are
// for interfaces and those that do not are parcelables. However, for both
// parcelables and interfaces, we *must* generate a non-empty dependency file.
JavaOptions options;
options.input_file_name_ = "p/Foo.aidl";
options.output_base_folder_ = "place/for/output";
options.dep_file_name_ = "dep/file/path";
io_delegate_.SetFileContents(options.input_file_name_,
"package p; parcelable Foo;");
EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
string actual_dep_file_contents;
EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_,
&actual_dep_file_contents));
EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDepFileContents);
}
} // namespace aidl
} // namespace android
|