Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Jooyung Han | 1f14ab3 | 2020-10-14 13:48:29 +0900 | [diff] [blame] | 17 | #include "aidl.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 18 | |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 19 | #include <android-base/format.h> |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 21 | #include <gmock/gmock.h> |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | |
Jooyung Han | 1f14ab3 | 2020-10-14 13:48:29 +0900 | [diff] [blame] | 24 | #include <map> |
| 25 | #include <memory> |
| 26 | #include <set> |
| 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
Steven Moreland | 4958524 | 2019-12-18 16:06:49 -0800 | [diff] [blame] | 30 | #include "aidl_checkapi.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 31 | #include "aidl_language.h" |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 32 | #include "aidl_to_cpp.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 33 | #include "aidl_to_java.h" |
Jiyong Park | 2a7c92b | 2020-07-22 19:12:36 +0900 | [diff] [blame] | 34 | #include "logging.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 35 | #include "options.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 36 | #include "tests/fake_io_delegate.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 37 | |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 38 | using android::aidl::internals::parse_preprocessed_file; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 39 | using android::aidl::test::FakeIoDelegate; |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 40 | using android::base::StringPrintf; |
Jooyung Han | 1f14ab3 | 2020-10-14 13:48:29 +0900 | [diff] [blame] | 41 | using std::map; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 42 | using std::set; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 43 | using std::string; |
| 44 | using std::unique_ptr; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 45 | using std::vector; |
Jooyung Han | f8dbbcc | 2020-12-26 03:05:55 +0900 | [diff] [blame] | 46 | using testing::HasSubstr; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 47 | using testing::TestParamInfo; |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 48 | using testing::internal::CaptureStderr; |
| 49 | using testing::internal::GetCapturedStderr; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 50 | |
| 51 | namespace android { |
| 52 | namespace aidl { |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 53 | namespace { |
| 54 | |
| 55 | const char kExpectedDepFileContents[] = |
| 56 | R"(place/for/output/p/IFoo.java : \ |
| 57 | p/IFoo.aidl |
| 58 | |
| 59 | p/IFoo.aidl : |
| 60 | )"; |
| 61 | |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 62 | const char kExpectedNinjaDepFileContents[] = |
| 63 | R"(place/for/output/p/IFoo.java : \ |
| 64 | p/IFoo.aidl |
| 65 | )"; |
| 66 | |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 67 | const char kExpectedParcelableDeclarationDepFileContents[] = |
| 68 | R"( : \ |
| 69 | p/Foo.aidl |
| 70 | |
| 71 | p/Foo.aidl : |
| 72 | )"; |
| 73 | |
| 74 | const char kExpectedStructuredParcelableDepFileContents[] = |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 75 | R"(place/for/output/p/Foo.java : \ |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 76 | p/Foo.aidl |
| 77 | |
| 78 | p/Foo.aidl : |
| 79 | )"; |
| 80 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 81 | } // namespace |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 82 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 83 | class AidlTest : public ::testing::TestWithParam<Options::Language> { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 84 | protected: |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 85 | AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_, |
| 86 | Options::Language lang, AidlError* error = nullptr, |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 87 | const vector<string> additional_arguments = {}) { |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 88 | io_delegate_.SetFileContents(path, contents); |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 89 | vector<string> args; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 90 | args.emplace_back("aidl"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 91 | args.emplace_back("--lang=" + to_string(lang)); |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 92 | for (const string& s : additional_arguments) { |
| 93 | args.emplace_back(s); |
| 94 | } |
| 95 | for (const string& f : preprocessed_files_) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 96 | args.emplace_back("--preprocessed=" + f); |
| 97 | } |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 98 | for (const string& i : import_paths_) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 99 | args.emplace_back("--include=" + i); |
| 100 | } |
| 101 | args.emplace_back(path); |
| 102 | Options options = Options::From(args); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 103 | vector<string> imported_files; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 104 | ImportResolver import_resolver{io_delegate_, path, import_paths_, {}}; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 105 | AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl( |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 106 | path, options, io_delegate_, &typenames_, &imported_files); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 107 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 108 | if (error != nullptr) { |
| 109 | *error = actual_error; |
| 110 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 111 | |
| 112 | if (actual_error != AidlError::OK) { |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 116 | const auto& defined_types = typenames_.MainDocument().DefinedTypes(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 117 | EXPECT_EQ(1ul, defined_types.size()); |
| 118 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 119 | return defined_types.front().get(); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 120 | } |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 121 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 122 | Options::Language GetLanguage() { return GetParam(); } |
| 123 | |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 124 | FakeIoDelegate io_delegate_; |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 125 | vector<string> preprocessed_files_; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 126 | set<string> import_paths_; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 127 | AidlTypenames typenames_; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 130 | // Instantiate the AidlTest parameterized suite, calling all of the TEST_P |
| 131 | // tests with each of the supported languages as a parameter. |
| 132 | INSTANTIATE_TEST_SUITE_P(AidlTestSuite, AidlTest, |
| 133 | testing::Values(Options::Language::CPP, Options::Language::JAVA, |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 134 | Options::Language::NDK, Options::Language::RUST), |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 135 | [](const testing::TestParamInfo<Options::Language>& info) { |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 136 | return to_string(info.param); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 137 | }); |
| 138 | |
| 139 | TEST_P(AidlTest, AcceptMissingPackage) { |
| 140 | EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, GetLanguage())); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 143 | TEST_P(AidlTest, EndsInSingleLineComment) { |
| 144 | EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, GetLanguage())); |
Steven Moreland | 9c2988f | 2019-07-17 17:49:10 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 147 | TEST_P(AidlTest, InterfaceRequiresCorrectPath) { |
| 148 | const string expected_stderr = |
| 149 | "ERROR: a/Foo.aidl:1.11-21: IBar should be declared in a file called a/IBar.aidl\n"; |
| 150 | const std::string file_contents = "package a; interface IBar {}"; |
| 151 | CaptureStderr(); |
| 152 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 153 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 154 | } |
| 155 | |
| 156 | TEST_P(AidlTest, ParcelableRequiresCorrectPath) { |
| 157 | const string expected_stderr = |
| 158 | "ERROR: a/Foo.aidl:1.11-21: Bar should be declared in a file called a/Bar.aidl\n"; |
| 159 | const std::string file_contents = "package a; interface Bar {}"; |
| 160 | CaptureStderr(); |
| 161 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 162 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 163 | } |
| 164 | |
| 165 | TEST_P(AidlTest, UnstructuredParcelableRequiresCorrectPath) { |
| 166 | const string expected_stderr = |
| 167 | "ERROR: a/Foo.aidl:1.22-26: Bar should be declared in a file called a/Bar.aidl\n"; |
| 168 | const std::string file_contents = "package a; parcelable Bar cpp_header \"anything.h\";"; |
| 169 | CaptureStderr(); |
| 170 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 171 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 172 | } |
| 173 | |
| 174 | TEST_P(AidlTest, EnumRequiresCorrectPath) { |
| 175 | const string expected_stderr = |
| 176 | "ERROR: a/Foo.aidl:1.16-20: Bar should be declared in a file called a/Bar.aidl\n"; |
| 177 | const std::string file_contents = "package a; enum Bar { A, }"; |
| 178 | CaptureStderr(); |
| 179 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 180 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 181 | } |
| 182 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 183 | TEST_P(AidlTest, RejectsArraysOfBinders) { |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 184 | import_paths_.emplace(""); |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 185 | io_delegate_.SetFileContents("bar/IBar.aidl", |
| 186 | "package bar; interface IBar {}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 187 | const string path = "foo/IFoo.aidl"; |
| 188 | const string contents = |
| 189 | "package foo;\n" |
| 190 | "import bar.IBar;\n" |
| 191 | "interface IFoo { void f(in IBar[] input); }"; |
| 192 | const string expected_stderr = "ERROR: foo/IFoo.aidl:3.27-32: Binder type cannot be an array\n"; |
| 193 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 194 | EXPECT_EQ(nullptr, Parse(path, contents, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 195 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 198 | TEST_P(AidlTest, SupportOnlyOutParameters) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 199 | const string interface_list = "package a; interface IBar { void f(out List<String> bar); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 200 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, GetLanguage())); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 201 | } |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 202 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 203 | TEST_P(AidlTest, RejectOutParametersForIBinder) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 204 | const string interface_ibinder = "package a; interface IBaz { void f(out IBinder bar); }"; |
| 205 | const string expected_ibinder_stderr = |
Jooyung Han | 15fd6c6 | 2020-10-23 13:54:46 +0900 | [diff] [blame] | 206 | "ERROR: a/IBaz.aidl:1.47-51: 'bar' can't be an out parameter because IBinder can only be an " |
| 207 | "in parameter.\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 208 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 209 | EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 210 | EXPECT_EQ(expected_ibinder_stderr, GetCapturedStderr()); |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 211 | } |
| 212 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 213 | TEST_P(AidlTest, RejectsOutParametersInOnewayInterface) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 214 | const string oneway_interface = "package a; oneway interface IBar { void f(out int bar); }"; |
| 215 | const string expected_stderr = |
| 216 | "ERROR: a/IBar.aidl:1.40-42: oneway method 'f' cannot have out parameters\n"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 217 | CaptureStderr(); |
| 218 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage())); |
| 219 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 220 | } |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 221 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 222 | TEST_P(AidlTest, RejectsOutParametersInOnewayMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 223 | const string oneway_method = "package a; interface IBar { oneway void f(out int bar); }"; |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 224 | const string expected_stderr = |
| 225 | "ERROR: a/IBar.aidl:1.40-42: oneway method 'f' cannot have out parameters\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 226 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 227 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 228 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 231 | TEST_P(AidlTest, RejectsOnewayNonVoidReturn) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 232 | const string oneway_method = "package a; interface IFoo { oneway int f(); }"; |
| 233 | const string expected_stderr = |
| 234 | "ERROR: a/IFoo.aidl:1.39-41: oneway method 'f' cannot return a value\n"; |
| 235 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 236 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 237 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 240 | TEST_P(AidlTest, RejectsNullablePrimitive) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 241 | const string oneway_method = "package a; interface IFoo { @nullable int f(); }"; |
| 242 | const string expected_stderr = |
| 243 | "ERROR: a/IFoo.aidl:1.38-42: Primitive type cannot get nullable annotation\n"; |
| 244 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 245 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 246 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 249 | TEST_P(AidlTest, AcceptNullableList) { |
| 250 | const string oneway_method = "package a; interface IFoo { @nullable List<String> f(); }"; |
| 251 | const string expected_stderr = ""; |
| 252 | CaptureStderr(); |
| 253 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
| 254 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 255 | } |
| 256 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 257 | TEST_P(AidlTest, RejectsDuplicatedArgumentNames) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 258 | const string method = "package a; interface IFoo { void f(int a, int a); }"; |
| 259 | const string expected_stderr = |
| 260 | "ERROR: a/IFoo.aidl:1.33-35: method 'f' has duplicate argument name 'a'\n"; |
| 261 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 262 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 263 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | b3cd3c7 | 2018-10-11 12:37:45 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Jeongik Cha | 13066da | 2020-08-06 15:43:19 +0900 | [diff] [blame] | 266 | TEST_P(AidlTest, RejectsDuplicatedFieldNames) { |
| 267 | const string method = "package a; parcelable Foo { int a; String a; }"; |
Jooyung Han | 59af9cc | 2020-10-25 21:44:14 +0900 | [diff] [blame] | 268 | const string expected_stderr = "ERROR: a/Foo.aidl:1.42-44: 'Foo' has duplicate field name 'a'\n"; |
Jeongik Cha | 13066da | 2020-08-06 15:43:19 +0900 | [diff] [blame] | 269 | CaptureStderr(); |
| 270 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage())); |
| 271 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 272 | } |
| 273 | |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 274 | TEST_P(AidlTest, RejectsRepeatedAnnotations) { |
| 275 | const string method = R"(@Hide @Hide parcelable Foo {})"; |
| 276 | const string expected_stderr = |
| 277 | "ERROR: Foo.aidl:1.23-27: 'Hide' is repeated, but not allowed. Previous location: " |
| 278 | "Foo.aidl:1.1-6\n"; |
| 279 | CaptureStderr(); |
| 280 | EXPECT_EQ(nullptr, Parse("Foo.aidl", method, typenames_, GetLanguage())); |
| 281 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 282 | } |
| 283 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 284 | TEST_P(AidlTest, RejectsDuplicatedAnnotationParams) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 285 | const string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }"; |
| 286 | const string expected_stderr = "ERROR: a/IFoo.aidl:1.56-62: Trying to redefine parameter foo.\n"; |
| 287 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 288 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 289 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 292 | TEST_P(AidlTest, RejectUnsupportedInterfaceAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 293 | AidlError error; |
| 294 | const string method = "package a; @nullable interface IFoo { int f(); }"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 295 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 296 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 297 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@nullable is not available.")); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 298 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 301 | TEST_P(AidlTest, RejectUnsupportedTypeAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 302 | AidlError error; |
| 303 | const string method = "package a; interface IFoo { @JavaOnlyStableParcelable int f(); }"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 304 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 305 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 306 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@JavaOnlyStableParcelable is not available.")); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 307 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 310 | TEST_P(AidlTest, RejectUnsupportedParcelableAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 311 | AidlError error; |
| 312 | const string method = "package a; @nullable parcelable IFoo cpp_header \"IFoo.h\";"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 313 | CaptureStderr(); |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 314 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 315 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@nullable is not available.")); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 316 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 319 | TEST_P(AidlTest, RejectUnsupportedParcelableDefineAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 320 | AidlError error; |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 321 | const string method = "package a; @nullable parcelable IFoo { String a; String b; }"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 322 | CaptureStderr(); |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 323 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 324 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@nullable is not available.")); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 325 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 328 | TEST_P(AidlTest, ParsesNonNullableAnnotation) { |
| 329 | auto parse_result = |
| 330 | Parse("a/IFoo.aidl", "package a; interface IFoo { String f(); }", typenames_, GetLanguage()); |
| 331 | ASSERT_NE(nullptr, parse_result); |
| 332 | const AidlInterface* interface = parse_result->AsInterface(); |
| 333 | ASSERT_NE(nullptr, interface); |
| 334 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 335 | EXPECT_FALSE(interface->GetMethods()[0]->GetType().IsNullable()); |
| 336 | } |
| 337 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 338 | TEST_P(AidlTest, ParsesNullableAnnotation) { |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 339 | auto parse_result = Parse("a/IFoo.aidl", "package a; interface IFoo { @nullable String f(); }", |
| 340 | typenames_, GetLanguage()); |
| 341 | ASSERT_NE(nullptr, parse_result); |
| 342 | const AidlInterface* interface = parse_result->AsInterface(); |
| 343 | ASSERT_NE(nullptr, interface); |
| 344 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 345 | EXPECT_TRUE(interface->GetMethods()[0]->GetType().IsNullable()); |
| 346 | } |
| 347 | |
| 348 | TEST_P(AidlTest, ParsesNonUtf8Annotations) { |
| 349 | auto parse_result = |
| 350 | Parse("a/IFoo.aidl", "package a; interface IFoo { String f(); }", typenames_, GetLanguage()); |
| 351 | ASSERT_NE(nullptr, parse_result); |
| 352 | const AidlInterface* interface = parse_result->AsInterface(); |
| 353 | ASSERT_NE(nullptr, interface); |
| 354 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 355 | EXPECT_FALSE(interface->GetMethods()[0]->GetType().IsUtf8InCpp()); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 358 | TEST_P(AidlTest, ParsesUtf8Annotations) { |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 359 | auto parse_result = Parse("a/IFoo.aidl", "package a; interface IFoo { @utf8InCpp String f(); }", |
| 360 | typenames_, GetLanguage()); |
| 361 | ASSERT_NE(nullptr, parse_result); |
| 362 | const AidlInterface* interface = parse_result->AsInterface(); |
| 363 | ASSERT_NE(nullptr, interface); |
| 364 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 365 | EXPECT_TRUE(interface->GetMethods()[0]->GetType().IsUtf8InCpp()); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 368 | TEST_P(AidlTest, VintfRequiresStructuredAndStability) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 369 | AidlError error; |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 370 | const string expected_stderr = |
| 371 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: " |
| 372 | "\"vintf\"'\n" |
| 373 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface " |
| 374 | "--structured\n"; |
| 375 | CaptureStderr(); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 376 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 377 | GetLanguage(), &error)); |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 378 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 379 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 382 | TEST_P(AidlTest, VintfRequiresStructured) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 383 | AidlError error; |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 384 | const string expected_stderr = |
| 385 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface " |
| 386 | "--structured\n"; |
| 387 | CaptureStderr(); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 388 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 389 | GetLanguage(), &error, {"--stability", "vintf"})); |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 390 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 391 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 394 | TEST_P(AidlTest, VintfRequiresSpecifiedStability) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 395 | AidlError error; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 396 | const string expected_stderr = |
| 397 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: " |
| 398 | "\"vintf\"'\n"; |
| 399 | CaptureStderr(); |
| 400 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 401 | GetLanguage(), &error, {"--structured"})); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 402 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 403 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 406 | TEST_P(AidlTest, ParsesStabilityAnnotations) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 407 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 408 | auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
| 409 | GetLanguage(), &error, {"--structured", "--stability", "vintf"}); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 410 | ASSERT_EQ(AidlError::OK, error); |
| 411 | ASSERT_NE(nullptr, parse_result); |
| 412 | const AidlInterface* interface = parse_result->AsInterface(); |
| 413 | ASSERT_NE(nullptr, interface); |
| 414 | ASSERT_TRUE(interface->IsVintfStability()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 417 | TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) { |
| 418 | Options java_options = Options::From("aidl -o out --structured a/Foo.aidl"); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 419 | Options cpp_options = Options::From("aidl --lang=cpp -o out -h out/include a/Foo.aidl"); |
| 420 | Options cpp_structured_options = |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 421 | Options::From("aidl --lang=cpp --structured -o out -h out/include a/Foo.aidl"); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 422 | Options rust_options = Options::From("aidl --lang=rust -o out --structured a/Foo.aidl"); |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 423 | io_delegate_.SetFileContents( |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 424 | "a/Foo.aidl", |
| 425 | StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo cpp_header \"Foo.h\" ;")); |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 426 | |
| 427 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 428 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 429 | const string expected_stderr = |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 430 | "ERROR: a/Foo.aidl:1.48-52: Cannot declared parcelable in a --structured interface. " |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 431 | "Parcelable must be defined in AIDL directly.\n"; |
| 432 | CaptureStderr(); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 433 | EXPECT_NE(0, ::android::aidl::compile_aidl(cpp_structured_options, io_delegate_)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 434 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 435 | |
| 436 | CaptureStderr(); |
| 437 | EXPECT_NE(0, ::android::aidl::compile_aidl(rust_options, io_delegate_)); |
| 438 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 439 | } |
| 440 | |
Jooyung Han | 1cbc496 | 2020-10-25 10:07:15 +0900 | [diff] [blame] | 441 | TEST_F(AidlTest, ParcelableSupportJavaDeriveToString) { |
| 442 | io_delegate_.SetFileContents("a/Foo.aidl", R"(package a; |
| 443 | @JavaDerive(toString=true) parcelable Foo { int a; float b; })"); |
| 444 | Options java_options = Options::From("aidl --lang=java -o out a/Foo.aidl"); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 445 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 446 | |
| 447 | string java_out; |
Jooyung Han | 1cbc496 | 2020-10-25 10:07:15 +0900 | [diff] [blame] | 448 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.java", &java_out)); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 449 | EXPECT_THAT(java_out, testing::HasSubstr("public String toString() {")); |
| 450 | |
| 451 | // Other backends shouldn't be bothered |
Jooyung Han | 1cbc496 | 2020-10-25 10:07:15 +0900 | [diff] [blame] | 452 | Options cpp_options = Options::From("aidl --lang=cpp -o out -h out a/Foo.aidl"); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 453 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
| 454 | |
Jooyung Han | 1cbc496 | 2020-10-25 10:07:15 +0900 | [diff] [blame] | 455 | Options ndk_options = Options::From("aidl --lang=ndk -o out -h out a/Foo.aidl"); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 456 | EXPECT_EQ(0, ::android::aidl::compile_aidl(ndk_options, io_delegate_)); |
| 457 | } |
| 458 | |
Jooyung Han | 1cbc496 | 2020-10-25 10:07:15 +0900 | [diff] [blame] | 459 | TEST_F(AidlTest, UnionSupportJavaDeriveToString) { |
| 460 | io_delegate_.SetFileContents("a/Foo.aidl", R"(package a; |
| 461 | @JavaDerive(toString=true) union Foo { int a; int[] b; })"); |
| 462 | CaptureStderr(); |
| 463 | Options java_options = Options::From("aidl --lang=java -o out a/Foo.aidl"); |
| 464 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 465 | EXPECT_EQ("", GetCapturedStderr()); |
| 466 | |
| 467 | const string expected_to_string_method = R"--( |
| 468 | @Override |
| 469 | public String toString() { |
| 470 | switch (_tag) { |
| 471 | case a: return "a.Foo.a(" + (getA()) + ")"; |
| 472 | case b: return "a.Foo.b(" + (java.util.Arrays.toString(getB())) + ")"; |
| 473 | } |
| 474 | throw new IllegalStateException("unknown field: " + _tag); |
| 475 | } |
| 476 | )--"; |
| 477 | |
| 478 | string java_out; |
| 479 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.java", &java_out)); |
| 480 | EXPECT_THAT(java_out, testing::HasSubstr(expected_to_string_method)); |
| 481 | } |
| 482 | |
Jiyong Park | 9aa3d04 | 2020-12-04 23:30:02 +0900 | [diff] [blame] | 483 | TEST_F(AidlTest, ParcelableSupportJavaDeriveEquals) { |
| 484 | io_delegate_.SetFileContents("a/Foo.aidl", R"(package a; |
| 485 | @JavaDerive(equals=true) parcelable Foo { int a; float b; })"); |
| 486 | CaptureStderr(); |
| 487 | Options java_options = Options::From("aidl --lang=java -o out a/Foo.aidl"); |
| 488 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 489 | EXPECT_EQ("", GetCapturedStderr()); |
| 490 | |
| 491 | const std::string expected = R"--( |
| 492 | @Override |
| 493 | public boolean equals(Object other) { |
| 494 | if (this == other) return true; |
| 495 | if (other == null) return false; |
| 496 | if (!(other instanceof Foo)) return false; |
| 497 | Foo that = (Foo)other; |
| 498 | if (!java.util.Objects.deepEquals(a, that.a)) return false; |
| 499 | if (!java.util.Objects.deepEquals(b, that.b)) return false; |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public int hashCode() { |
| 505 | return java.util.Arrays.deepHashCode(java.util.Arrays.asList(a, b).toArray()); |
| 506 | } |
| 507 | )--"; |
| 508 | |
| 509 | string java_out; |
| 510 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.java", &java_out)); |
| 511 | EXPECT_THAT(java_out, testing::HasSubstr(expected)); |
| 512 | } |
| 513 | |
| 514 | TEST_F(AidlTest, UnionSupportJavaDeriveEquals) { |
| 515 | io_delegate_.SetFileContents("a/Foo.aidl", R"(package a; |
| 516 | @JavaDerive(equals=true) union Foo { int a; int[] b; })"); |
| 517 | CaptureStderr(); |
| 518 | Options java_options = Options::From("aidl --lang=java -o out a/Foo.aidl"); |
| 519 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 520 | EXPECT_EQ("", GetCapturedStderr()); |
| 521 | |
| 522 | const std::string expected = R"--( |
| 523 | @Override |
| 524 | public boolean equals(Object other) { |
| 525 | if (this == other) return true; |
| 526 | if (other == null) return false; |
| 527 | if (!(other instanceof Foo)) return false; |
| 528 | Foo that = (Foo)other; |
| 529 | if (_tag != that._tag) return false; |
| 530 | if (!java.util.Objects.deepEquals(_value, that._value)) return false; |
| 531 | return true; |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public int hashCode() { |
| 536 | return java.util.Arrays.deepHashCode(java.util.Arrays.asList(_tag, _value).toArray()); |
| 537 | } |
| 538 | )--"; |
| 539 | |
| 540 | string java_out; |
| 541 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.java", &java_out)); |
| 542 | EXPECT_THAT(java_out, testing::HasSubstr(expected)); |
| 543 | } |
| 544 | |
Jooyung Han | 9034500 | 2020-10-23 15:28:53 +0900 | [diff] [blame] | 545 | TEST_F(AidlTest, RejectsJavaDeriveAnnotation) { |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 546 | { |
Jooyung Han | 9034500 | 2020-10-23 15:28:53 +0900 | [diff] [blame] | 547 | io_delegate_.SetFileContents("a/Foo.aidl", |
| 548 | "package a; @JavaDerive(blah=true) parcelable Foo{}"); |
| 549 | Options java_options = Options::From("aidl --lang=java -o out a/Foo.aidl"); |
| 550 | CaptureStderr(); |
| 551 | EXPECT_NE(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 552 | const std::string expected_stderr = |
| 553 | "ERROR: a/Foo.aidl:1.11-34: Parameter blah not supported for annotation JavaDerive."; |
Jooyung Han | f8dbbcc | 2020-12-26 03:05:55 +0900 | [diff] [blame] | 554 | EXPECT_THAT(GetCapturedStderr(), |
| 555 | HasSubstr("Parameter blah not supported for annotation JavaDerive.")); |
Jooyung Han | 9034500 | 2020-10-23 15:28:53 +0900 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | { |
| 559 | io_delegate_.SetFileContents("a/IFoo.aidl", "package a; @JavaDerive interface IFoo{}"); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 560 | Options java_options = Options::From("aidl --lang=java -o out a/IFoo.aidl"); |
| 561 | CaptureStderr(); |
| 562 | EXPECT_NE(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 563 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@JavaDerive is not available.")); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | { |
Jooyung Han | 9034500 | 2020-10-23 15:28:53 +0900 | [diff] [blame] | 567 | io_delegate_.SetFileContents("a/IFoo.aidl", "package a; @JavaDerive enum IFoo { A=1, }"); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 568 | Options java_options = Options::From("aidl --lang=java -o out a/IFoo.aidl"); |
| 569 | CaptureStderr(); |
| 570 | EXPECT_NE(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 571 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@JavaDerive is not available.")); |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 575 | TEST_P(AidlTest, ParseDescriptorAnnotation) { |
| 576 | AidlError error; |
| 577 | auto parse_result = Parse("IFoo.aidl", R"(@Descriptor(value="IBar") interface IFoo{})", |
| 578 | typenames_, GetLanguage(), &error, {"--structured"}); |
| 579 | ASSERT_EQ(AidlError::OK, error); |
| 580 | ASSERT_NE(nullptr, parse_result); |
| 581 | const AidlInterface* interface = parse_result->AsInterface(); |
| 582 | ASSERT_NE(nullptr, interface); |
| 583 | ASSERT_EQ("IBar", interface->GetDescriptor()); |
| 584 | } |
| 585 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 586 | TEST_P(AidlTest, AcceptsOnewayMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 587 | const string oneway_method = "package a; interface IFoo { oneway void f(int a); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 588 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | TEST_P(AidlTest, AcceptsOnewayInterface) { |
| 592 | const string oneway_interface = "package a; oneway interface IBar { void f(int a); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 593 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage())); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 594 | } |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 595 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 596 | TEST_P(AidlTest, AcceptsAnnotatedOnewayMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 597 | const string oneway_method = |
| 598 | "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 599 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Steven Moreland | 65297cc | 2020-04-20 20:17:36 -0700 | [diff] [blame] | 602 | TEST_P(AidlTest, AnnotationsInMultiplePlaces) { |
| 603 | const string oneway_method = |
| 604 | "package a; interface IFoo { @UnsupportedAppUsage oneway @Hide void f(int a); }"; |
| 605 | const AidlDefinedType* defined = Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage()); |
| 606 | ASSERT_NE(nullptr, defined); |
| 607 | const AidlInterface* iface = defined->AsInterface(); |
| 608 | ASSERT_NE(nullptr, iface); |
| 609 | |
| 610 | const auto& methods = iface->GetMethods(); |
| 611 | ASSERT_EQ(1u, methods.size()); |
| 612 | const auto& method = methods[0]; |
| 613 | const AidlTypeSpecifier& ret_type = method->GetType(); |
| 614 | |
| 615 | // TODO(b/151102494): these annotations should be on the method |
| 616 | ASSERT_NE(nullptr, ret_type.UnsupportedAppUsage()); |
| 617 | ASSERT_TRUE(ret_type.IsHide()); |
| 618 | } |
| 619 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 620 | TEST_P(AidlTest, WritesComments) { |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 621 | string foo_interface = |
| 622 | "package a; /* foo */ interface IFoo {" |
| 623 | " /* i */ int i();" |
| 624 | " /* j */ @nullable String j();" |
| 625 | " /* k */ @UnsupportedAppUsage oneway void k(int a); }"; |
| 626 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 627 | auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, GetLanguage()); |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 628 | EXPECT_NE(nullptr, parse_result); |
| 629 | EXPECT_EQ("/* foo */", parse_result->GetComments()); |
| 630 | |
| 631 | const AidlInterface* interface = parse_result->AsInterface(); |
| 632 | EXPECT_EQ("/* i */", interface->GetMethods()[0]->GetComments()); |
| 633 | EXPECT_EQ("/* j */", interface->GetMethods()[1]->GetComments()); |
| 634 | EXPECT_EQ("/* k */", interface->GetMethods()[2]->GetComments()); |
| 635 | } |
| 636 | |
Jooyung Han | 1a551d3 | 2020-10-05 15:37:26 +0900 | [diff] [blame] | 637 | TEST_P(AidlTest, CppHeaderCanBeIdentifierAsWell) { |
| 638 | io_delegate_.SetFileContents("p/cpp_header.aidl", |
| 639 | R"(package p; |
| 640 | parcelable cpp_header cpp_header "bar/header";)"); |
| 641 | import_paths_.emplace(""); |
| 642 | const string input_path = "p/IFoo.aidl"; |
| 643 | const string input = R"(package p; |
| 644 | import p.cpp_header; |
| 645 | interface IFoo { |
| 646 | // get bar |
| 647 | cpp_header get(); |
| 648 | })"; |
| 649 | |
| 650 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 651 | EXPECT_NE(nullptr, parse_result); |
| 652 | const AidlInterface* interface = parse_result->AsInterface(); |
| 653 | EXPECT_EQ("// get bar\n", interface->GetMethods()[0]->GetComments()); |
| 654 | } |
| 655 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 656 | TEST_F(AidlTest, ParsesPreprocessedFile) { |
| 657 | string simple_content = "parcelable a.Foo;\ninterface b.IBar;"; |
| 658 | io_delegate_.SetFileContents("path", simple_content); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 659 | EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 660 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_)); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 661 | EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved); |
| 662 | EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) { |
| 666 | string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t"; |
| 667 | io_delegate_.SetFileContents("path", simple_content); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 668 | |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 669 | EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 670 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_)); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 671 | EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved); |
| 672 | EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 675 | TEST_P(AidlTest, PreferImportToPreprocessed) { |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 676 | io_delegate_.SetFileContents("preprocessed", "interface another.IBar;"); |
| 677 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; " |
| 678 | "interface IBar {}"); |
| 679 | preprocessed_files_.push_back("preprocessed"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 680 | import_paths_.emplace(""); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 681 | auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 682 | typenames_, GetLanguage()); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 683 | EXPECT_NE(nullptr, parse_result); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 684 | |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 685 | // We expect to know about both kinds of IBar |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 686 | EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved); |
| 687 | EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 688 | // But if we request just "IBar" we should get our imported one. |
Steven Moreland | 02e012e | 2018-08-02 14:58:10 -0700 | [diff] [blame] | 689 | AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, ""); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 690 | ambiguous_type.Resolve(typenames_); |
| 691 | EXPECT_EQ("one.IBar", ambiguous_type.GetName()); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 694 | // Special case of PreferImportToPreprocessed. Imported type should be preferred |
| 695 | // even when the preprocessed file already has the same type. |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 696 | TEST_P(AidlTest, B147918827) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 697 | io_delegate_.SetFileContents("preprocessed", "interface another.IBar;\ninterface one.IBar;"); |
| 698 | io_delegate_.SetFileContents("one/IBar.aidl", |
| 699 | "package one; " |
| 700 | "interface IBar {}"); |
| 701 | preprocessed_files_.push_back("preprocessed"); |
| 702 | import_paths_.emplace(""); |
| 703 | auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 704 | typenames_, GetLanguage()); |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 705 | EXPECT_NE(nullptr, parse_result); |
| 706 | |
| 707 | // We expect to know about both kinds of IBar |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 708 | EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved); |
| 709 | EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved); |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 710 | // But if we request just "IBar" we should get our imported one. |
| 711 | AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, ""); |
| 712 | ambiguous_type.Resolve(typenames_); |
| 713 | EXPECT_EQ("one.IBar", ambiguous_type.GetName()); |
| 714 | } |
| 715 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 716 | TEST_F(AidlTest, WritePreprocessedFile) { |
| 717 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 718 | "package p; parcelable Outer.Inner;"); |
| 719 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;" |
| 720 | "interface IBar {}"); |
| 721 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 722 | vector<string> args { |
| 723 | "aidl", |
| 724 | "--preprocess", |
| 725 | "preprocessed", |
| 726 | "p/Outer.aidl", |
| 727 | "one/IBar.aidl"}; |
| 728 | Options options = Options::From(args); |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 729 | EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_)); |
| 730 | |
| 731 | string output; |
| 732 | EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output)); |
| 733 | EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output); |
| 734 | } |
| 735 | |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 736 | TEST_P(AidlTest, SupportDeprecated) { |
| 737 | struct TestCase { |
| 738 | std::string output_file; |
| 739 | std::string annotation; |
| 740 | }; |
| 741 | |
| 742 | auto CheckDeprecated = [&](const std::string& filename, const std::string& contents, |
| 743 | std::map<Options::Language, TestCase> expectation) { |
| 744 | io_delegate_.SetFileContents(filename, contents); |
| 745 | |
| 746 | auto options = Options::From("aidl --lang=" + to_string(GetLanguage()) + " " + filename + |
| 747 | " --out=out --header_out=out"); |
| 748 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 749 | if (auto it = expectation.find(GetLanguage()); it != expectation.end()) { |
| 750 | const auto& test_case = it->second; |
| 751 | string output; |
| 752 | EXPECT_TRUE(io_delegate_.GetWrittenContents(test_case.output_file, &output)) |
| 753 | << base::Join(io_delegate_.ListOutputFiles(), ","); |
| 754 | EXPECT_THAT(output, HasSubstr(test_case.annotation)); |
| 755 | } |
| 756 | }; |
| 757 | |
| 758 | CheckDeprecated("IFoo.aidl", |
| 759 | "interface IFoo {\n" |
| 760 | " /** @deprecated use bar() */\n" |
| 761 | " List<String> foo();\n" |
| 762 | "}", |
| 763 | { |
| 764 | {Options::Language::JAVA, {"out/IFoo.java", "@Deprecated"}}, |
| 765 | {Options::Language::CPP, {"out/IFoo.h", "__attribute__((deprecated"}}, |
| 766 | {Options::Language::NDK, {"out/aidl/IFoo.h", "__attribute__((deprecated"}}, |
| 767 | {Options::Language::RUST, {"out/IFoo.rs", "#[deprecated"}}, |
| 768 | }); |
| 769 | |
| 770 | CheckDeprecated("Foo.aidl", |
| 771 | "parcelable Foo {\n" |
| 772 | " /** @deprecated use bar*/\n" |
| 773 | " int foo = 0;\n" |
| 774 | "}", |
| 775 | { |
| 776 | {Options::Language::JAVA, {"out/Foo.java", "@Deprecated"}}, |
| 777 | {Options::Language::CPP, {"out/Foo.h", "__attribute__((deprecated"}}, |
| 778 | {Options::Language::NDK, {"out/aidl/Foo.h", "__attribute__((deprecated"}}, |
| 779 | {Options::Language::RUST, {"out/Foo.rs", "#[deprecated"}}, |
| 780 | }); |
| 781 | |
| 782 | CheckDeprecated("IFoo.aidl", |
| 783 | "interface IFoo {\n" |
| 784 | " /** @deprecated use bar*/\n" |
| 785 | " const int FOO = 0;\n" |
| 786 | "}", |
| 787 | { |
| 788 | {Options::Language::JAVA, {"out/IFoo.java", "@Deprecated"}}, |
| 789 | {Options::Language::CPP, {"out/IFoo.h", "__attribute__((deprecated"}}, |
| 790 | {Options::Language::NDK, {"out/aidl/IFoo.h", "__attribute__((deprecated"}}, |
| 791 | {Options::Language::RUST, {"out/IFoo.rs", "#[deprecated"}}, |
| 792 | }); |
| 793 | |
| 794 | // union fields |
| 795 | CheckDeprecated("Foo.aidl", |
| 796 | "union Foo {\n" |
| 797 | " int bar = 0;\n" |
| 798 | " /** @deprecated use bar*/\n" |
| 799 | " int foo;\n" |
| 800 | "}", |
| 801 | { |
| 802 | {Options::Language::JAVA, {"out/Foo.java", "@Deprecated"}}, |
| 803 | {Options::Language::CPP, {"out/Foo.h", "__attribute__((deprecated"}}, |
| 804 | {Options::Language::NDK, {"out/aidl/Foo.h", "__attribute__((deprecated"}}, |
| 805 | {Options::Language::RUST, {"out/Foo.rs", "#[deprecated"}}, |
| 806 | }); |
| 807 | |
| 808 | CheckDeprecated("Foo.aidl", |
| 809 | "/** @deprecated use Bar */\n" |
| 810 | "parcelable Foo {}", |
| 811 | { |
| 812 | {Options::Language::JAVA, {"out/Foo.java", "@Deprecated"}}, |
| 813 | {Options::Language::CPP, {"out/Foo.h", "__attribute__((deprecated"}}, |
| 814 | {Options::Language::NDK, {"out/aidl/Foo.h", "__attribute__((deprecated"}}, |
| 815 | {Options::Language::RUST, {"out/Foo.rs", "#[deprecated"}}, |
| 816 | }); |
| 817 | |
| 818 | CheckDeprecated("Foo.aidl", |
| 819 | "/** @deprecated use Bar */\n" |
| 820 | "union Foo { int foo = 0; }", |
| 821 | { |
| 822 | {Options::Language::JAVA, {"out/Foo.java", "@Deprecated"}}, |
| 823 | {Options::Language::CPP, {"out/Foo.h", "__attribute__((deprecated"}}, |
| 824 | {Options::Language::NDK, {"out/aidl/Foo.h", "__attribute__((deprecated"}}, |
| 825 | {Options::Language::RUST, {"out/Foo.rs", "#[deprecated"}}, |
| 826 | }); |
| 827 | |
| 828 | CheckDeprecated("IFoo.aidl", |
| 829 | "/** @deprecated use IBar */\n" |
| 830 | "interface IFoo {}", |
| 831 | { |
| 832 | {Options::Language::JAVA, {"out/IFoo.java", "@Deprecated"}}, |
| 833 | {Options::Language::CPP, {"out/IFoo.h", "__attribute__((deprecated"}}, |
| 834 | {Options::Language::NDK, {"out/aidl/IFoo.h", "__attribute__((deprecated"}}, |
| 835 | {Options::Language::RUST, {"out/IFoo.rs", "#[deprecated"}}, |
| 836 | }); |
| 837 | |
| 838 | CheckDeprecated("Foo.aidl", |
| 839 | "/** @deprecated use IBar */\n" |
| 840 | "enum Foo { FOO }", |
| 841 | { |
| 842 | {Options::Language::JAVA, {"out/Foo.java", "@Deprecated"}}, |
| 843 | {Options::Language::CPP, {"out/Foo.h", "__attribute__((deprecated"}}, |
| 844 | {Options::Language::NDK, {"out/aidl/Foo.h", "__attribute__((deprecated"}}, |
| 845 | // TODO(b/174514415) support "deprecated" |
| 846 | // {Options::Language::RUST, {"out/Foo.rs", "#[deprecated"}}, |
| 847 | }); |
| 848 | } |
| 849 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 850 | TEST_P(AidlTest, RequireOuterClass) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 851 | const string expected_stderr = "ERROR: p/IFoo.aidl:1.54-60: Failed to resolve 'Inner'\n"; |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 852 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 853 | "package p; parcelable Outer.Inner;"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 854 | import_paths_.emplace(""); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 855 | CaptureStderr(); |
| 856 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 857 | "package p; import p.Outer; interface IFoo { void f(in Inner c); }", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 858 | typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 859 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 862 | TEST_P(AidlTest, ParseCompoundParcelableFromPreprocess) { |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 863 | io_delegate_.SetFileContents("preprocessed", |
| 864 | "parcelable p.Outer.Inner;"); |
| 865 | preprocessed_files_.push_back("preprocessed"); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 866 | auto parse_result = Parse("p/IFoo.aidl", "package p; interface IFoo { void f(in Inner c); }", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 867 | typenames_, GetLanguage()); |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 868 | // TODO(wiley): This should actually return nullptr because we require |
| 869 | // the outer class name. However, for legacy reasons, |
| 870 | // this behavior must be maintained. b/17415692 |
| 871 | EXPECT_NE(nullptr, parse_result); |
| 872 | } |
| 873 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 874 | TEST_F(AidlTest, FailOnParcelable) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 875 | const string expected_foo_stderr = |
| 876 | "ERROR: p/IFoo.aidl:1.22-27: Refusing to generate code with unstructured parcelables. " |
| 877 | "Declared parcelables should be in their own file and/or cannot be used with --structured " |
| 878 | "interfaces.\n"; |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 879 | io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;"); |
| 880 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 881 | // By default, we shouldn't fail on parcelable. |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 882 | Options options1 = Options::From("aidl p/IFoo.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 883 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 884 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 885 | EXPECT_EQ("", GetCapturedStderr()); |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 886 | |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 887 | // -b considers this an error |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 888 | Options options2 = Options::From("aidl -b p/IFoo.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 889 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 890 | EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 891 | EXPECT_EQ(expected_foo_stderr, GetCapturedStderr()); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 892 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 893 | const string expected_bar_stderr = |
| 894 | "ERROR: p/IBar.aidl:1.22-26: Refusing to generate code with unstructured parcelables. " |
| 895 | "Declared parcelables should be in their own file and/or cannot be used with --structured " |
| 896 | "interfaces.\n"; |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 897 | io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}"); |
| 898 | |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 899 | // With '-b' option, a parcelable and an interface should fail. |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 900 | Options options3 = Options::From("aidl p/IBar.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 901 | CaptureStderr(); |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 902 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 903 | EXPECT_EQ("", GetCapturedStderr()); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 904 | Options options4 = Options::From("aidl -b p/IBar.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 905 | CaptureStderr(); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 906 | EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 907 | EXPECT_EQ(expected_bar_stderr, GetCapturedStderr()); |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 908 | } |
| 909 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 910 | TEST_P(AidlTest, StructuredFailOnUnstructuredParcelable) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 911 | const string expected_stderr = |
| 912 | "ERROR: ./o/WhoKnowsWhat.aidl:1.22-35: o.WhoKnowsWhat is not structured, but this is a " |
| 913 | "structured interface.\n"; |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 914 | io_delegate_.SetFileContents("o/WhoKnowsWhat.aidl", "package o; parcelable WhoKnowsWhat;"); |
| 915 | import_paths_.emplace(""); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 916 | AidlError error; |
| 917 | CaptureStderr(); |
| 918 | EXPECT_EQ( |
| 919 | nullptr, |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 920 | Parse("p/IFoo.aidl", |
| 921 | "package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 922 | typenames_, GetLanguage(), &error, {"--structured"})); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 923 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 924 | EXPECT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 927 | TEST_P(AidlTest, FailOnDuplicateConstantNames) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 928 | AidlError error; |
| 929 | const string expected_stderr = |
| 930 | "ERROR: p/IFoo.aidl:4.34-45: Found duplicate constant name 'DUPLICATED'\n"; |
| 931 | CaptureStderr(); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 932 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 933 | R"(package p; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 934 | interface IFoo { |
| 935 | const String DUPLICATED = "d"; |
| 936 | const int DUPLICATED = 1; |
| 937 | } |
| 938 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 939 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 940 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 941 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Steven Moreland | e689da2 | 2020-11-10 02:06:30 +0000 | [diff] [blame] | 944 | TEST_P(AidlTest, FailOnTooBigConstant) { |
| 945 | AidlError error; |
| 946 | const string expected_stderr = |
| 947 | "ERROR: p/IFoo.aidl:3.48-52: Invalid type specifier for an int32 literal: byte\n"; |
| 948 | CaptureStderr(); |
| 949 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 950 | R"(package p; |
| 951 | interface IFoo { |
| 952 | const byte type2small = 256; |
| 953 | } |
| 954 | )", |
| 955 | typenames_, GetLanguage(), &error)); |
| 956 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 957 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
| 958 | } |
| 959 | |
Jooyung Han | 30f64ad | 2020-12-15 08:16:31 +0900 | [diff] [blame] | 960 | TEST_F(AidlTest, BoolConstantsEvaluatesToIntegers) { |
| 961 | io_delegate_.SetFileContents("a/Foo.aidl", "package a; parcelable Foo { const int y = true; }"); |
| 962 | CaptureStderr(); |
| 963 | auto options = Options::From("aidl --lang java -o out a/Foo.aidl"); |
| 964 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 965 | EXPECT_EQ("", GetCapturedStderr()); |
| 966 | string code; |
| 967 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.java", &code)); |
| 968 | EXPECT_THAT(code, testing::HasSubstr("public static final int y = 1;")); |
| 969 | } |
| 970 | |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 971 | TEST_F(AidlTest, AidlConstantValue_EvaluatedValue) { |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 972 | using Ptr = unique_ptr<AidlConstantValue>; |
| 973 | const AidlLocation& loc = AIDL_LOCATION_HERE; |
| 974 | |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 975 | EXPECT_EQ('c', Ptr(AidlConstantValue::Character(loc, 'c'))->EvaluatedValue<char>()); |
| 976 | EXPECT_EQ("abc", Ptr(AidlConstantValue::String(loc, "\"abc\""))->EvaluatedValue<string>()); |
| 977 | EXPECT_FLOAT_EQ(1.0f, Ptr(AidlConstantValue::Floating(loc, "1.0f"))->EvaluatedValue<float>()); |
| 978 | EXPECT_EQ(true, Ptr(AidlConstantValue::Boolean(loc, true))->EvaluatedValue<bool>()); |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 979 | |
| 980 | AidlBinaryConstExpression one_plus_one(loc, Ptr(AidlConstantValue::Integral(loc, "1")), "+", |
| 981 | Ptr(AidlConstantValue::Integral(loc, "1"))); |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 982 | EXPECT_EQ(2, one_plus_one.EvaluatedValue<int32_t>()); |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 983 | |
| 984 | auto values = unique_ptr<vector<Ptr>>{new vector<Ptr>}; |
| 985 | values->emplace_back(AidlConstantValue::String(loc, "\"hello\"")); |
| 986 | values->emplace_back(AidlConstantValue::String(loc, "\"world\"")); |
| 987 | vector<string> expected{"hello", "world"}; |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 988 | EXPECT_EQ( |
| 989 | expected, |
| 990 | Ptr(AidlConstantValue::Array(loc, std::move(values)))->EvaluatedValue<vector<string>>()); |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 991 | } |
| 992 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 993 | TEST_P(AidlTest, FailOnManyDefinedTypes) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 994 | AidlError error; |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 995 | const string expected_stderr = |
| 996 | "ERROR: p/IFoo.aidl:3.33-38: You must declare only one type per file.\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 997 | CaptureStderr(); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 998 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 999 | R"(package p; |
| 1000 | interface IFoo {} |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 1001 | parcelable IBar {} |
| 1002 | parcelable StructuredParcelable {} |
| 1003 | interface IBaz {} |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 1004 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1005 | typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 1006 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 1007 | // Parse success is important for clear error handling even if the cases aren't |
| 1008 | // actually supported in code generation. |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1009 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1012 | TEST_P(AidlTest, FailOnNoDefinedTypes) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1013 | AidlError error; |
| 1014 | const string expected_stderr = "ERROR: p/IFoo.aidl:1.11-11: syntax error, unexpected $end\n"; |
| 1015 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1016 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1017 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1018 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Steven Moreland | f9e922f | 2020-07-08 21:15:27 +0000 | [diff] [blame] | 1021 | TEST_P(AidlTest, FailOnEmptyListWithComma) { |
| 1022 | AidlError error; |
| 1023 | const string expected_stderr = |
| 1024 | "ERROR: p/Foo.aidl:1.45-47: syntax error, unexpected ',', expecting '}'\n"; |
| 1025 | CaptureStderr(); |
| 1026 | EXPECT_EQ(nullptr, Parse("p/Foo.aidl", R"(package p; parcelable Foo { uint64_t[] a = { , }; })", |
| 1027 | typenames_, GetLanguage(), &error)); |
| 1028 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1029 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
| 1030 | } |
| 1031 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1032 | TEST_P(AidlTest, FailOnMalformedConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1033 | AidlError error; |
| 1034 | const string expected_stderr = |
Devin Moore | 2a08890 | 2020-09-17 10:51:19 -0700 | [diff] [blame] | 1035 | "ERROR: p/IFoo.aidl:3.50-71: Could not parse hexvalue: 0xffffffffffffffffff\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1036 | CaptureStderr(); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1037 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 1038 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 1039 | interface IFoo { |
| 1040 | const int BAD_HEX_VALUE = 0xffffffffffffffffff; |
| 1041 | } |
| 1042 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1043 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1044 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1045 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 1048 | TEST_P(AidlTest, FailOnMalformedQualifiedNameAsIdentifier) { |
| 1049 | AidlError error; |
| 1050 | const string expected_stderr = |
| 1051 | "ERROR: p/IFoo.aidl:1.25-26: syntax error, unexpected ';', expecting identifier or " |
| 1052 | "cpp_header (which can also be used as an identifier)\n"; |
| 1053 | CaptureStderr(); |
| 1054 | // Notice the trailing dot(.) in the name, which isn't a correct name |
| 1055 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p; parcelable A.; )", typenames_, |
| 1056 | GetLanguage(), &error)); |
| 1057 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1058 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
| 1059 | } |
| 1060 | |
| 1061 | TEST_P(AidlTest, FailOnMalformedQualifiedNameAsPackage) { |
| 1062 | AidlError error; |
| 1063 | const string expected_stderr = |
| 1064 | "ERROR: p/IFoo.aidl:1.11-12: syntax error, unexpected ';', expecting identifier or " |
| 1065 | "cpp_header (which can also be used as an identifier)\n"; |
| 1066 | CaptureStderr(); |
| 1067 | // Notice the trailing dot(.) in the package name |
| 1068 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p.; parcelable A; )", typenames_, |
| 1069 | GetLanguage(), &error)); |
| 1070 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1071 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
| 1072 | } |
| 1073 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1074 | TEST_P(AidlTest, ParsePositiveConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1075 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1076 | auto parse_result = Parse("p/IFoo.aidl", |
| 1077 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 1078 | interface IFoo { |
| 1079 | const int POSITIVE_HEX_VALUE = 0xf5; |
| 1080 | } |
| 1081 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1082 | typenames_, GetLanguage(), &error); |
| 1083 | EXPECT_NE(nullptr, parse_result); |
| 1084 | const AidlInterface* interface = parse_result->AsInterface(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1085 | ASSERT_NE(nullptr, interface); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1086 | const auto& cpp_constants = interface->GetConstantDeclarations(); |
| 1087 | EXPECT_EQ((size_t)1, cpp_constants.size()); |
| 1088 | EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_constants[0]->GetName()); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1089 | EXPECT_TRUE(cpp_constants[0]->CheckValid(typenames_)); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1090 | EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator)); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1093 | TEST_P(AidlTest, ParseNegativeConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1094 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1095 | auto parse_result = Parse("p/IFoo.aidl", |
| 1096 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 1097 | interface IFoo { |
| 1098 | const int NEGATIVE_HEX_VALUE = 0xffffffff; |
| 1099 | } |
| 1100 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1101 | typenames_, GetLanguage(), &error); |
| 1102 | ASSERT_NE(nullptr, parse_result); |
| 1103 | const AidlInterface* interface = parse_result->AsInterface(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1104 | ASSERT_NE(nullptr, interface); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1105 | const auto& cpp_constants = interface->GetConstantDeclarations(); |
| 1106 | EXPECT_EQ((size_t)1, cpp_constants.size()); |
| 1107 | EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_constants[0]->GetName()); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1108 | EXPECT_EQ(true, cpp_constants[0]->CheckValid(typenames_)); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1109 | EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator)); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1112 | TEST_P(AidlTest, UnderstandsNestedParcelables) { |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1113 | io_delegate_.SetFileContents( |
| 1114 | "p/Outer.aidl", |
| 1115 | "package p; parcelable Outer.Inner cpp_header \"baz/header\";"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 1116 | import_paths_.emplace(""); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1117 | const string input_path = "p/IFoo.aidl"; |
| 1118 | const string input = "package p; import p.Outer; interface IFoo" |
| 1119 | " { Outer.Inner get(); }"; |
| 1120 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1121 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 1122 | EXPECT_NE(nullptr, parse_result); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1123 | |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 1124 | EXPECT_TRUE(typenames_.ResolveTypename("p.Outer.Inner").is_resolved); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1125 | // C++ uses "::" instead of "." to refer to a inner class. |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1126 | AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, ""); |
| 1127 | EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_)); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1130 | TEST_P(AidlTest, UnderstandsNativeParcelables) { |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 1131 | io_delegate_.SetFileContents( |
| 1132 | "p/Bar.aidl", |
Casey Dahlin | cd63921 | 2015-12-15 12:51:04 -0800 | [diff] [blame] | 1133 | "package p; parcelable Bar cpp_header \"baz/header\";"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 1134 | import_paths_.emplace(""); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 1135 | const string input_path = "p/IFoo.aidl"; |
| 1136 | const string input = "package p; import p.Bar; interface IFoo { }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1137 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 1138 | EXPECT_NE(nullptr, parse_result); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 1139 | EXPECT_TRUE(typenames_.ResolveTypename("p.Bar").is_resolved); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1140 | AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, ""); |
| 1141 | native_type.Resolve(typenames_); |
| 1142 | |
| 1143 | EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type, typenames_)); |
| 1144 | // C++ understands C++ specific stuff |
| 1145 | EXPECT_EQ("::p::Bar", cpp::CppNameOf(native_type, typenames_)); |
| 1146 | set<string> headers; |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 1147 | cpp::AddHeaders(native_type, typenames_, &headers); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1148 | EXPECT_EQ(1u, headers.size()); |
| 1149 | EXPECT_EQ(1u, headers.count("baz/header")); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 1152 | TEST_F(AidlTest, WritesCorrectDependencyFile) { |
| 1153 | // While the in tree build system always gives us an output file name, |
| 1154 | // other android tools take advantage of our ability to infer the intended |
| 1155 | // file name. This test makes sure we handle this correctly. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1156 | vector<string> args = { |
| 1157 | "aidl", |
| 1158 | "-d dep/file/path", |
| 1159 | "-o place/for/output", |
| 1160 | "p/IFoo.aidl"}; |
| 1161 | Options options = Options::From(args); |
| 1162 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1163 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 1164 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1165 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 1166 | EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents); |
| 1167 | } |
| 1168 | |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 1169 | TEST_F(AidlTest, WritesCorrectDependencyFileNinja) { |
| 1170 | // While the in tree build system always gives us an output file name, |
| 1171 | // other android tools take advantage of our ability to infer the intended |
| 1172 | // file name. This test makes sure we handle this correctly. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1173 | vector<string> args = { |
| 1174 | "aidl", |
| 1175 | "-d dep/file/path", |
| 1176 | "--ninja", |
| 1177 | "-o place/for/output", |
| 1178 | "p/IFoo.aidl"}; |
| 1179 | Options options = Options::From(args); |
| 1180 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1181 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 1182 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1183 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 1184 | EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents); |
| 1185 | } |
| 1186 | |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 1187 | TEST_F(AidlTest, WritesTrivialDependencyFileForParcelableDeclaration) { |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 1188 | // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does |
| 1189 | // this by calling aidl with every .aidl file it finds, then parsing the |
| 1190 | // generated dependency files. Those that reference .java output files are |
| 1191 | // for interfaces and those that do not are parcelables. However, for both |
| 1192 | // parcelables and interfaces, we *must* generate a non-empty dependency file. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1193 | vector<string> args = { |
| 1194 | "aidl", |
| 1195 | "-o place/for/output", |
| 1196 | "-d dep/file/path", |
| 1197 | "p/Foo.aidl"}; |
| 1198 | Options options = Options::From(args); |
| 1199 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1200 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 1201 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1202 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 1203 | EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDeclarationDepFileContents); |
| 1204 | } |
| 1205 | |
| 1206 | TEST_F(AidlTest, WritesDependencyFileForStructuredParcelable) { |
| 1207 | vector<string> args = { |
| 1208 | "aidl", |
| 1209 | "--structured", |
| 1210 | "-o place/for/output", |
| 1211 | "-d dep/file/path", |
| 1212 | "p/Foo.aidl"}; |
| 1213 | Options options = Options::From(args); |
| 1214 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo {int a;}"); |
| 1215 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1216 | string actual_dep_file_contents; |
| 1217 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
| 1218 | EXPECT_EQ(actual_dep_file_contents, kExpectedStructuredParcelableDepFileContents); |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Jiyong Park | 9ca5c7e | 2019-10-17 15:01:14 +0900 | [diff] [blame] | 1221 | TEST_F(AidlTest, NoJavaOutputForParcelableDeclaration) { |
| 1222 | vector<string> args = { |
| 1223 | "aidl", |
| 1224 | "--lang=java", |
| 1225 | "-o place/for/output", |
| 1226 | "p/Foo.aidl"}; |
| 1227 | Options options = Options::From(args); |
| 1228 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;"); |
| 1229 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1230 | string output_file_contents; |
| 1231 | EXPECT_FALSE(io_delegate_.GetWrittenContents(options.OutputFile(), &output_file_contents)); |
| 1232 | } |
| 1233 | |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 1234 | TEST_P(AidlTest, RejectsListArray) { |
Devin Moore | 6a01ca1 | 2020-08-28 10:24:19 -0700 | [diff] [blame] | 1235 | const string expected_stderr = "ERROR: a/Foo.aidl:2.1-7: List[] is not supported.\n"; |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 1236 | const string list_array_parcelable = |
| 1237 | "package a; parcelable Foo {\n" |
| 1238 | " List[] lists; }"; |
| 1239 | CaptureStderr(); |
| 1240 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", list_array_parcelable, typenames_, GetLanguage())); |
| 1241 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1242 | } |
| 1243 | |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1244 | TEST_P(AidlTest, RejectsPrimitiveListInStableAidl) { |
| 1245 | AidlError error; |
| 1246 | string expected_stderr = |
| 1247 | "ERROR: a/IFoo.aidl:2.7-11: " |
| 1248 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 1249 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 1250 | "the receiving side. Consider switching to an array or a generic List/Map.\n"; |
| 1251 | if (GetLanguage() != Options::Language::JAVA) { |
| 1252 | expected_stderr = |
| 1253 | "ERROR: a/IFoo.aidl:2.1-7: " |
| 1254 | "Currently, only the Java backend supports non-generic List.\n"; |
| 1255 | } |
| 1256 | |
| 1257 | const string primitive_interface = |
| 1258 | "package a; interface IFoo {\n" |
| 1259 | " List foo(); }"; |
| 1260 | CaptureStderr(); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 1261 | AidlTypenames tn1; |
| 1262 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, tn1, GetLanguage(), &error, |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1263 | {"--structured"})); |
| 1264 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1265 | |
| 1266 | string primitive_parcelable = |
| 1267 | "package a; parcelable IFoo {\n" |
| 1268 | " List foo;}"; |
| 1269 | CaptureStderr(); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 1270 | AidlTypenames tn2; |
| 1271 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_parcelable, tn2, GetLanguage(), &error, |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1272 | {"--structured"})); |
| 1273 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1274 | } |
| 1275 | |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1276 | TEST_P(AidlTest, ExtensionTest) { |
| 1277 | CaptureStderr(); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1278 | string extendable_parcelable = |
| 1279 | "package a; parcelable Data {\n" |
| 1280 | " ParcelableHolder extension;\n" |
| 1281 | " ParcelableHolder extension2;\n" |
| 1282 | "}"; |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 1283 | if (GetLanguage() == Options::Language::RUST) { |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1284 | EXPECT_EQ(nullptr, Parse("a/Data.aidl", extendable_parcelable, typenames_, GetLanguage())); |
| 1285 | EXPECT_EQ( |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 1286 | "ERROR: a/Data.aidl:2.1-19: The Rust backend does not support ParcelableHolder " |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1287 | "yet.\n", |
| 1288 | GetCapturedStderr()); |
| 1289 | } else { |
| 1290 | EXPECT_NE(nullptr, Parse("a/Data.aidl", extendable_parcelable, typenames_, GetLanguage())); |
| 1291 | EXPECT_EQ("", GetCapturedStderr()); |
| 1292 | } |
| 1293 | } |
| 1294 | TEST_P(AidlTest, ParcelableHolderAsReturnType) { |
| 1295 | CaptureStderr(); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1296 | string parcelableholder_return_interface = |
| 1297 | "package a; interface IFoo {\n" |
| 1298 | " ParcelableHolder foo();\n" |
| 1299 | "}"; |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1300 | EXPECT_EQ(nullptr, |
| 1301 | Parse("a/IFoo.aidl", parcelableholder_return_interface, typenames_, GetLanguage())); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1302 | |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 1303 | if (GetLanguage() == Options::Language::RUST) { |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1304 | EXPECT_EQ( |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1305 | "ERROR: a/IFoo.aidl:2.19-23: ParcelableHolder cannot be a return type\n" |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 1306 | "ERROR: a/IFoo.aidl:2.1-19: The Rust backend does not support ParcelableHolder " |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1307 | "yet.\n", |
| 1308 | GetCapturedStderr()); |
| 1309 | return; |
| 1310 | } |
| 1311 | EXPECT_EQ("ERROR: a/IFoo.aidl:2.19-23: ParcelableHolder cannot be a return type\n", |
| 1312 | GetCapturedStderr()); |
| 1313 | } |
| 1314 | |
| 1315 | TEST_P(AidlTest, ParcelableHolderAsArgumentType) { |
| 1316 | CaptureStderr(); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1317 | string extendable_parcelable_arg_interface = |
| 1318 | "package a; interface IFoo {\n" |
| 1319 | " void foo(in ParcelableHolder ph);\n" |
| 1320 | "}"; |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1321 | EXPECT_EQ(nullptr, |
| 1322 | Parse("a/IFoo.aidl", extendable_parcelable_arg_interface, typenames_, GetLanguage())); |
| 1323 | |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 1324 | if (GetLanguage() == Options::Language::RUST) { |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1325 | EXPECT_EQ( |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1326 | "ERROR: a/IFoo.aidl:2.31-34: ParcelableHolder cannot be an argument type\n" |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 1327 | "ERROR: a/IFoo.aidl:2.14-31: The Rust backend does not support ParcelableHolder " |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1328 | "yet.\n", |
| 1329 | GetCapturedStderr()); |
| 1330 | return; |
| 1331 | } |
| 1332 | EXPECT_EQ("ERROR: a/IFoo.aidl:2.31-34: ParcelableHolder cannot be an argument type\n", |
| 1333 | GetCapturedStderr()); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1334 | } |
| 1335 | |
Jeongik Cha | f6ec898 | 2020-10-15 00:10:30 +0900 | [diff] [blame] | 1336 | TEST_P(AidlTest, RejectNullableParcelableHolderField) { |
| 1337 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo { @nullable ParcelableHolder ext; }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 1338 | Options options = Options::From("aidl Foo.aidl --lang=" + to_string(GetLanguage())); |
Jeongik Cha | f6ec898 | 2020-10-15 00:10:30 +0900 | [diff] [blame] | 1339 | const string expected_stderr = "ERROR: Foo.aidl:1.27-44: ParcelableHolder cannot be nullable.\n"; |
| 1340 | CaptureStderr(); |
| 1341 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1342 | if (GetLanguage() == Options::Language::RUST) { |
| 1343 | EXPECT_EQ( |
| 1344 | "ERROR: Foo.aidl:1.27-44: ParcelableHolder cannot be nullable.\n" |
| 1345 | "ERROR: Foo.aidl:1.27-44: The Rust backend does not support ParcelableHolder " |
| 1346 | "yet.\n", |
| 1347 | GetCapturedStderr()); |
| 1348 | return; |
| 1349 | } |
| 1350 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1351 | } |
| 1352 | |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 1353 | TEST_P(AidlTest, ParcelablesWithConstants) { |
| 1354 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo { const int BIT = 0x1 << 3; }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 1355 | Options options = Options::From("aidl Foo.aidl --lang=" + to_string(GetLanguage())); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 1356 | CaptureStderr(); |
| 1357 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1358 | EXPECT_EQ("", GetCapturedStderr()); |
| 1359 | } |
| 1360 | |
| 1361 | TEST_P(AidlTest, UnionWithConstants) { |
| 1362 | io_delegate_.SetFileContents("Foo.aidl", "union Foo { const int BIT = 0x1 << 3; int n; }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 1363 | Options options = Options::From("aidl Foo.aidl --lang=" + to_string(GetLanguage())); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 1364 | CaptureStderr(); |
| 1365 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1366 | EXPECT_EQ("", GetCapturedStderr()); |
| 1367 | } |
| 1368 | |
Jooyung Han | 633eab6 | 2021-01-07 14:12:40 +0900 | [diff] [blame] | 1369 | TEST_F(AidlTest, ConstantsWithAnnotations) { |
| 1370 | io_delegate_.SetFileContents("IFoo.aidl", |
| 1371 | "interface IFoo {\n" |
| 1372 | " @JavaPassthrough(annotation=\"@Foo\")\n" |
| 1373 | " const @JavaPassthrough(annotation=\"@Bar\") int FOO = 0;\n" |
| 1374 | "}"); |
| 1375 | Options options = Options::From("aidl IFoo.aidl --lang=java -o out"); |
| 1376 | CaptureStderr(); |
| 1377 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1378 | EXPECT_EQ("", GetCapturedStderr()); |
| 1379 | string code; |
| 1380 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/IFoo.java", &code)); |
| 1381 | EXPECT_THAT(code, HasSubstr("@Foo\n")); |
| 1382 | EXPECT_THAT(code, HasSubstr("@Bar\n")); |
| 1383 | } |
| 1384 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1385 | TEST_F(AidlTest, ApiDump) { |
| 1386 | io_delegate_.SetFileContents( |
| 1387 | "foo/bar/IFoo.aidl", |
| 1388 | "package foo.bar;\n" |
| 1389 | "import foo.bar.Data;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1390 | "// comment @hide\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1391 | "interface IFoo {\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1392 | " /* @hide */\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1393 | " int foo(out int[] a, String b, boolean c, inout List<String> d);\n" |
| 1394 | " int foo2(@utf8InCpp String x, inout List<String> y);\n" |
| 1395 | " IFoo foo3(IFoo foo);\n" |
| 1396 | " Data getData();\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1397 | " // @hide\n" |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1398 | " const int A = 1;\n" |
| 1399 | " const String STR = \"Hello\";\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1400 | "}\n"); |
| 1401 | io_delegate_.SetFileContents("foo/bar/Data.aidl", |
| 1402 | "package foo.bar;\n" |
| 1403 | "import foo.bar.IFoo;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1404 | "/* @hide*/\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1405 | "parcelable Data {\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1406 | " // @hide\n" |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 1407 | " int x = 10;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1408 | " // @hide\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1409 | " int y;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1410 | " /*@hide2*/\n" |
Jooyung Han | 55f96ad | 2020-12-13 10:08:33 +0900 | [diff] [blame] | 1411 | " IFoo foo;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1412 | " // It should be @hide property\n" |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1413 | " @nullable String[] c;\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1414 | "}\n"); |
| 1415 | io_delegate_.SetFileContents("api.aidl", ""); |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1416 | vector<string> args = {"aidl", "--dumpapi", "--out=dump", "--include=.", |
| 1417 | "foo/bar/IFoo.aidl", "foo/bar/Data.aidl"}; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1418 | Options options = Options::From(args); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1419 | bool result = dump_api(options, io_delegate_); |
| 1420 | ASSERT_TRUE(result); |
| 1421 | string actual; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1422 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1423 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1424 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1425 | interface IFoo { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1426 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1427 | int foo(out int[] a, String b, boolean c, inout List<String> d); |
| 1428 | int foo2(@utf8InCpp String x, inout List<String> y); |
| 1429 | foo.bar.IFoo foo3(foo.bar.IFoo foo); |
| 1430 | foo.bar.Data getData(); |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1431 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1432 | const int A = 1; |
| 1433 | const String STR = "Hello"; |
| 1434 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1435 | )")); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1436 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1437 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1438 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1439 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1440 | parcelable Data { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1441 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1442 | int x = 10; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1443 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1444 | int y; |
| 1445 | foo.bar.IFoo foo; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1446 | /* @hide */ |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1447 | @nullable String[] c; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1448 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1449 | )")); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1450 | } |
| 1451 | |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1452 | TEST_F(AidlTest, ApiDumpWithManualIds) { |
| 1453 | io_delegate_.SetFileContents( |
| 1454 | "foo/bar/IFoo.aidl", |
| 1455 | "package foo.bar;\n" |
| 1456 | "interface IFoo {\n" |
| 1457 | " int foo() = 1;\n" |
| 1458 | " int bar() = 2;\n" |
| 1459 | " int baz() = 10;\n" |
| 1460 | "}\n"); |
| 1461 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1462 | vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"}; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1463 | Options options = Options::From(args); |
| 1464 | bool result = dump_api(options, io_delegate_); |
| 1465 | ASSERT_TRUE(result); |
| 1466 | string actual; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1467 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1468 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1469 | interface IFoo { |
| 1470 | int foo() = 1; |
| 1471 | int bar() = 2; |
| 1472 | int baz() = 10; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1473 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1474 | )")); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | TEST_F(AidlTest, ApiDumpWithManualIdsOnlyOnSomeMethods) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1478 | const string expected_stderr = |
| 1479 | "ERROR: foo/bar/IFoo.aidl:4.8-12: You must either assign id's to all methods or to none of " |
| 1480 | "them.\n"; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1481 | io_delegate_.SetFileContents( |
| 1482 | "foo/bar/IFoo.aidl", |
| 1483 | "package foo.bar;\n" |
| 1484 | "interface IFoo {\n" |
| 1485 | " int foo() = 1;\n" |
| 1486 | " int bar();\n" |
| 1487 | " int baz() = 10;\n" |
| 1488 | "}\n"); |
| 1489 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1490 | vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"}; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1491 | Options options = Options::From(args); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1492 | CaptureStderr(); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1493 | EXPECT_FALSE(dump_api(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1494 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1495 | } |
| 1496 | |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 1497 | TEST_F(AidlTest, ApiDumpConstWithAnnotation) { |
| 1498 | io_delegate_.SetFileContents("foo/bar/IFoo.aidl", |
| 1499 | "package foo.bar;\n" |
| 1500 | "interface IFoo {\n" |
| 1501 | " @utf8InCpp String foo();\n" |
| 1502 | " const @utf8InCpp String bar = \"bar\";\n" |
| 1503 | "}\n"); |
| 1504 | |
| 1505 | vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"}; |
| 1506 | Options options = Options::From(args); |
| 1507 | CaptureStderr(); |
| 1508 | EXPECT_TRUE(dump_api(options, io_delegate_)); |
| 1509 | EXPECT_EQ("", GetCapturedStderr()); |
| 1510 | string actual; |
| 1511 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual)); |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 1512 | EXPECT_EQ(string(kPreamble).append(R"(package foo.bar; |
| 1513 | interface IFoo { |
| 1514 | @utf8InCpp String foo(); |
Jooyung Han | b3ca630 | 2020-11-27 14:13:27 +0900 | [diff] [blame] | 1515 | const @utf8InCpp String bar = "bar"; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 1516 | } |
| 1517 | )"), |
| 1518 | actual); |
| 1519 | } |
| 1520 | |
Jooyung Han | fdaae1d | 2020-12-14 13:16:15 +0900 | [diff] [blame] | 1521 | TEST_F(AidlTest, ApiDumpWithEnums) { |
| 1522 | io_delegate_.SetFileContents("foo/bar/Enum.aidl", |
| 1523 | "package foo.bar;\n" |
| 1524 | "enum Enum {\n" |
| 1525 | " FOO,\n" |
| 1526 | " BAR = FOO + 1,\n" |
| 1527 | "}\n"); |
| 1528 | |
| 1529 | vector<string> args = {"aidl", "--dumpapi", "-I . ", "-o dump", "foo/bar/Enum.aidl"}; |
| 1530 | Options options = Options::From(args); |
| 1531 | CaptureStderr(); |
| 1532 | EXPECT_TRUE(dump_api(options, io_delegate_)); |
| 1533 | EXPECT_EQ("", GetCapturedStderr()); |
| 1534 | string actual; |
| 1535 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Enum.aidl", &actual)); |
| 1536 | EXPECT_EQ(string(kPreamble).append("package foo.bar;\n" |
| 1537 | "enum Enum {\n" |
| 1538 | " FOO = 0,\n" |
| 1539 | " BAR = 1,\n" |
| 1540 | "}\n"), |
| 1541 | actual); |
| 1542 | } |
| 1543 | |
| 1544 | TEST_F(AidlTest, ApiDumpWithEnumDefaultValues) { |
| 1545 | io_delegate_.SetFileContents("foo/bar/Enum.aidl", |
| 1546 | "package foo.bar;\n" |
| 1547 | "enum Enum {\n" |
| 1548 | " FOO,\n" |
| 1549 | "}\n"); |
| 1550 | io_delegate_.SetFileContents("foo/bar/Foo.aidl", |
| 1551 | "package foo.bar;\n" |
| 1552 | "import foo.bar.Enum;\n" |
| 1553 | "parcelable Foo {\n" |
| 1554 | " Enum e = Enum.FOO;\n" |
| 1555 | "}\n"); |
| 1556 | |
| 1557 | vector<string> args = {"aidl", "--dumpapi", "-I . ", "-o dump", "foo/bar/Foo.aidl"}; |
| 1558 | Options options = Options::From(args); |
| 1559 | CaptureStderr(); |
| 1560 | EXPECT_TRUE(dump_api(options, io_delegate_)); |
| 1561 | EXPECT_EQ("", GetCapturedStderr()); |
| 1562 | string actual; |
| 1563 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Foo.aidl", &actual)); |
| 1564 | EXPECT_EQ(string(kPreamble).append("package foo.bar;\n" |
| 1565 | "parcelable Foo {\n" |
| 1566 | " foo.bar.Enum e = foo.bar.Enum.FOO;\n" |
| 1567 | "}\n"), |
| 1568 | actual); |
| 1569 | } |
| 1570 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1571 | TEST_F(AidlTest, CheckNumGenericTypeSecifier) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1572 | const string expected_list_stderr = |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1573 | "ERROR: p/IFoo.aidl:1.37-41: List can only have one type parameter, but got: " |
| 1574 | "'List<String,String>'\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1575 | const string expected_map_stderr = |
| 1576 | "ERROR: p/IFoo.aidl:1.37-40: Map must have 0 or 2 type parameters, but got 'Map<String>'\n"; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1577 | Options options = Options::From("aidl p/IFoo.aidl IFoo.java"); |
| 1578 | io_delegate_.SetFileContents(options.InputFiles().front(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1579 | "package p; interface IFoo {" |
| 1580 | "void foo(List<String, String> a);}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1581 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1582 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1583 | EXPECT_EQ(expected_list_stderr, GetCapturedStderr()); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1584 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1585 | io_delegate_.SetFileContents(options.InputFiles().front(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1586 | "package p; interface IFoo {" |
| 1587 | "void foo(Map<String> a);}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1588 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1589 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1590 | EXPECT_EQ(expected_map_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1591 | } |
| 1592 | |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1593 | TEST_F(AidlTest, CheckTypeParameterInMapType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1594 | const string expected_stderr = |
| 1595 | "ERROR: p/IFoo.aidl:1.28-31: The type of key in map must be String, but it is 'p.Bar'\n"; |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1596 | Options options = Options::From("aidl -I p p/IFoo.aidl"); |
| 1597 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar { String s; }"); |
| 1598 | |
| 1599 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1600 | "package p; interface IFoo {" |
| 1601 | "Map<String, Bar> foo();}"); |
| 1602 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1603 | |
| 1604 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1605 | "package p; interface IFoo {" |
| 1606 | "Map<Bar, Bar> foo();}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1607 | CaptureStderr(); |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1608 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1609 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1610 | |
| 1611 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1612 | "package p; interface IFoo {" |
| 1613 | "Map<String, String> foo();}"); |
| 1614 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1615 | |
| 1616 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1617 | "package p; interface IFoo {" |
| 1618 | "Map<String, ParcelFileDescriptor> foo();}"); |
| 1619 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1620 | } |
| 1621 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1622 | TEST_F(AidlTest, WrongGenericType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1623 | const string expected_stderr = "ERROR: p/IFoo.aidl:1.28-34: String is not a generic type.\n"; |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1624 | Options options = Options::From("aidl p/IFoo.aidl IFoo.java"); |
| 1625 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1626 | "package p; interface IFoo {" |
| 1627 | "String<String> foo(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1628 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1629 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1630 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | TEST_F(AidlTest, UserDefinedUnstructuredGenericParcelableType) { |
| 1634 | Options optionsForParcelable = Options::From("aidl -I p p/Bar.aidl"); |
| 1635 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, T>;"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1636 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1637 | EXPECT_NE(0, ::android::aidl::compile_aidl(optionsForParcelable, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1638 | EXPECT_EQ("ERROR: p/Bar.aidl:1.22-26: Every type parameter should be unique.\n", |
| 1639 | GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1640 | |
| 1641 | Options options = Options::From("aidl -I p p/IFoo.aidl"); |
| 1642 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar;"); |
| 1643 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1644 | "package p; interface IFoo {" |
| 1645 | "Bar<String, String> foo();}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1646 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1647 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1648 | EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar is not a generic type.\n", GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1649 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T>;"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1650 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1651 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1652 | EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar must have 1 type parameters, but got 2\n", |
| 1653 | GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1654 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, V>;"); |
| 1655 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1656 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1657 | "package p; interface IFoo {" |
| 1658 | "Bar<String, ParcelFileDescriptor> foo();}"); |
| 1659 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jeongik Cha | e74c86d | 2019-12-12 16:54:03 +0900 | [diff] [blame] | 1660 | |
| 1661 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1662 | "package p; interface IFoo {" |
| 1663 | "Bar<int, long> foo();}"); |
| 1664 | |
| 1665 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1666 | "package p; interface IFoo {" |
| 1667 | "Bar<int[], long[]> foo();}"); |
| 1668 | |
| 1669 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1670 | } |
| 1671 | |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1672 | TEST_F(AidlTest, FailOnMultipleTypesInSingleFile) { |
| 1673 | std::vector<std::string> rawOptions{"aidl --lang=java -o out foo/bar/Foo.aidl", |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 1674 | "aidl --lang=cpp -o out -h out/include foo/bar/Foo.aidl", |
| 1675 | "aidl --lang=rust -o out foo/bar/Foo.aidl"}; |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1676 | for (const auto& rawOption : rawOptions) { |
| 1677 | string expected_stderr = |
| 1678 | "ERROR: foo/bar/Foo.aidl:3.1-10: You must declare only one type per file.\n"; |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1679 | Options options = Options::From(rawOption); |
| 1680 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1681 | "package foo.bar;\n" |
| 1682 | "interface IFoo1 { int foo(); }\n" |
| 1683 | "interface IFoo2 { int foo(); }\n" |
| 1684 | "parcelable Data1 { int a; int b;}\n" |
| 1685 | "parcelable Data2 { int a; int b;}\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1686 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1687 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1688 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1689 | |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1690 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1691 | "package foo.bar;\n" |
| 1692 | "interface IFoo1 { int foo(); }\n" |
| 1693 | "interface IFoo2 { int foo(); }\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1694 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1695 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1696 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1697 | |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1698 | expected_stderr = "ERROR: foo/bar/Foo.aidl:3.11-17: You must declare only one type per file.\n"; |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1699 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1700 | "package foo.bar;\n" |
| 1701 | "parcelable Data1 { int a; int b;}\n" |
| 1702 | "parcelable Data2 { int a; int b;}\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1703 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1704 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1705 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | |
Devin Moore | bdba2a8 | 2020-03-31 15:19:02 -0700 | [diff] [blame] | 1709 | TEST_P(AidlTest, FailParseOnEmptyFile) { |
| 1710 | const string contents = ""; |
| 1711 | const string expected_stderr = "ERROR: a/IFoo.aidl:1.1-1: syntax error, unexpected $end\n"; |
| 1712 | CaptureStderr(); |
| 1713 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", contents, typenames_, GetLanguage())); |
| 1714 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1715 | } |
| 1716 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1717 | TEST_F(AidlTest, MultipleInputFiles) { |
| 1718 | Options options = Options::From( |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1719 | "aidl --lang=java -o out -I . foo/bar/IFoo.aidl foo/bar/Data.aidl"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1720 | |
| 1721 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1722 | "package foo.bar;\n" |
| 1723 | "import foo.bar.Data;\n" |
| 1724 | "interface IFoo { Data getData(); }\n"); |
| 1725 | |
| 1726 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1727 | "package foo.bar;\n" |
| 1728 | "import foo.bar.IFoo;\n" |
| 1729 | "parcelable Data { IFoo foo; }\n"); |
| 1730 | |
| 1731 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1732 | |
| 1733 | string content; |
| 1734 | for (const auto file : { |
| 1735 | "out/foo/bar/IFoo.java", "out/foo/bar/Data.java"}) { |
| 1736 | content.clear(); |
| 1737 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1738 | EXPECT_FALSE(content.empty()); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | TEST_F(AidlTest, MultipleInputFilesCpp) { |
| 1743 | Options options = Options::From("aidl --lang=cpp -o out -h out/include " |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1744 | "-I . foo/bar/IFoo.aidl foo/bar/Data.aidl"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1745 | |
| 1746 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1747 | "package foo.bar;\n" |
| 1748 | "import foo.bar.Data;\n" |
| 1749 | "interface IFoo { Data getData(); }\n"); |
| 1750 | |
| 1751 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1752 | "package foo.bar;\n" |
| 1753 | "import foo.bar.IFoo;\n" |
| 1754 | "parcelable Data { IFoo foo; }\n"); |
| 1755 | |
| 1756 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1757 | |
| 1758 | string content; |
| 1759 | for (const auto file : { |
Jiyong Park | b03551f | 2018-08-06 19:20:51 +0900 | [diff] [blame] | 1760 | "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp", |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1761 | "out/include/foo/bar/IFoo.h", "out/include/foo/bar/Data.h", |
| 1762 | "out/include/foo/bar/BpFoo.h", "out/include/foo/bar/BpData.h", |
| 1763 | "out/include/foo/bar/BnFoo.h", "out/include/foo/bar/BnData.h"}) { |
| 1764 | content.clear(); |
| 1765 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1766 | EXPECT_FALSE(content.empty()); |
| 1767 | } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1768 | } |
| 1769 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 1770 | TEST_F(AidlTest, MultipleInputFilesRust) { |
| 1771 | Options options = |
| 1772 | Options::From("aidl --lang=rust -o out -I . foo/bar/IFoo.aidl foo/bar/Data.aidl"); |
| 1773 | |
| 1774 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1775 | "package foo.bar;\n" |
| 1776 | "import foo.bar.Data;\n" |
| 1777 | "interface IFoo { Data getData(); }\n"); |
| 1778 | |
| 1779 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1780 | "package foo.bar;\n" |
| 1781 | "import foo.bar.IFoo;\n" |
| 1782 | "parcelable Data { IFoo foo; }\n"); |
| 1783 | |
| 1784 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1785 | |
| 1786 | string content; |
| 1787 | for (const auto file : {"out/foo/bar/IFoo.rs", "out/foo/bar/Data.rs"}) { |
| 1788 | content.clear(); |
| 1789 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1790 | EXPECT_FALSE(content.empty()); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1794 | TEST_F(AidlTest, ConflictWithMetaTransactionGetVersion) { |
| 1795 | const string expected_stderr = |
| 1796 | "ERROR: p/IFoo.aidl:1.31-51: method getInterfaceVersion() is reserved for internal use.\n"; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1797 | Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl"); |
| 1798 | // int getInterfaceVersion() is one of the meta transactions |
| 1799 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1800 | "package p; interface IFoo {" |
| 1801 | "int getInterfaceVersion(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1802 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1803 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1804 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1805 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1806 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1807 | TEST_F(AidlTest, ConflictWithSimilarMetaTransaction) { |
| 1808 | // boolean getInterfaceVersion() is not a meta transaction, but should be |
| 1809 | // prevented because return type is not part of a method signature |
| 1810 | const string expected_stderr = |
| 1811 | "ERROR: p/IFoo.aidl:1.35-55: method getInterfaceVersion() is reserved for internal use.\n"; |
| 1812 | Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl"); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1813 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1814 | "package p; interface IFoo {" |
| 1815 | "boolean getInterfaceVersion(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1816 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1817 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1818 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1819 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1820 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1821 | TEST_F(AidlTest, ConflictWithMetaTransactionGetName) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1822 | // this is another reserved name |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1823 | const string expected_stderr = |
| 1824 | "ERROR: p/IFoo.aidl:1.34-53: method getTransactionName(int) is reserved for internal use.\n"; |
| 1825 | Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl"); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1826 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1827 | "package p; interface IFoo {" |
| 1828 | "String getTransactionName(int code); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1829 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1830 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1831 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1832 | |
| 1833 | // this is not a meta interface method as it differs type arguments |
| 1834 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1835 | "package p; interface IFoo {" |
| 1836 | "String getTransactionName(); }"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1837 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1838 | } |
| 1839 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1840 | TEST_F(AidlTest, DifferentOrderAnnotationsInCheckAPI) { |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1841 | Options options = Options::From("aidl --checkapi old new"); |
| 1842 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1843 | "package p; interface IFoo{ @utf8InCpp @nullable String foo();}"); |
| 1844 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1845 | "package p; interface IFoo{ @nullable @utf8InCpp String foo();}"); |
| 1846 | |
| 1847 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1848 | } |
| 1849 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1850 | TEST_F(AidlTest, SuccessOnIdenticalApiDumps) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1851 | Options options = Options::From("aidl --checkapi old new"); |
| 1852 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo();}"); |
| 1853 | io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo();}"); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1854 | |
| 1855 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1856 | } |
| 1857 | |
Jooyung Han | fdaae1d | 2020-12-14 13:16:15 +0900 | [diff] [blame] | 1858 | TEST_F(AidlTest, CheckApi_EnumFieldsWithDefaultValues) { |
| 1859 | Options options = Options::From("aidl --checkapi old new"); |
| 1860 | const string foo_definition = "package p; parcelable Foo{ p.Enum e = p.Enum.FOO; }"; |
| 1861 | const string enum_definition = "package p; enum Enum { FOO }"; |
| 1862 | io_delegate_.SetFileContents("old/p/Foo.aidl", foo_definition); |
| 1863 | io_delegate_.SetFileContents("old/p/Enum.aidl", enum_definition); |
| 1864 | io_delegate_.SetFileContents("new/p/Foo.aidl", foo_definition); |
| 1865 | io_delegate_.SetFileContents("new/p/Enum.aidl", enum_definition); |
| 1866 | |
| 1867 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1868 | } |
| 1869 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1870 | class AidlTestCompatibleChanges : public AidlTest { |
| 1871 | protected: |
| 1872 | Options options_ = Options::From("aidl --checkapi old new"); |
| 1873 | }; |
| 1874 | |
| 1875 | TEST_F(AidlTestCompatibleChanges, NewType) { |
| 1876 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1877 | "package p;" |
| 1878 | "interface IFoo {" |
| 1879 | " void foo(int a);" |
| 1880 | "}"); |
| 1881 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1882 | "package p;" |
| 1883 | "interface IFoo {" |
| 1884 | " void foo(int a);" |
| 1885 | "}"); |
| 1886 | io_delegate_.SetFileContents("new/p/IBar.aidl", |
| 1887 | "package p;" |
| 1888 | "interface IBar {" |
| 1889 | " void bar();" |
| 1890 | "}"); |
| 1891 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1892 | } |
| 1893 | |
| 1894 | TEST_F(AidlTestCompatibleChanges, NewMethod) { |
| 1895 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1896 | "package p;" |
| 1897 | "interface IFoo {" |
| 1898 | " void foo(int a);" |
| 1899 | "}"); |
| 1900 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1901 | "package p;" |
| 1902 | "interface IFoo {" |
| 1903 | " void foo(int a);" |
| 1904 | " void bar();" |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1905 | " void baz(in List<IFoo> arg);" |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1906 | "}"); |
| 1907 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1908 | } |
| 1909 | |
| 1910 | TEST_F(AidlTestCompatibleChanges, NewField) { |
| 1911 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1912 | "package p;" |
| 1913 | "parcelable Data {" |
| 1914 | " int foo;" |
| 1915 | "}"); |
| 1916 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1917 | "package p;" |
| 1918 | "parcelable Data {" |
| 1919 | " int foo;" |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 1920 | " int bar = 0;" |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1921 | " @nullable List<Data> list;" |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1922 | "}"); |
| 1923 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1924 | } |
| 1925 | |
| 1926 | TEST_F(AidlTestCompatibleChanges, NewEnumerator) { |
| 1927 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1928 | "package p;" |
| 1929 | "enum Enum {" |
| 1930 | " FOO = 1," |
| 1931 | "}"); |
| 1932 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1933 | "package p;" |
| 1934 | "enum Enum {" |
| 1935 | " FOO = 1," |
| 1936 | " BAR = 2," |
| 1937 | "}"); |
| 1938 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1939 | } |
| 1940 | |
| 1941 | TEST_F(AidlTestCompatibleChanges, ReorderedEnumerator) { |
| 1942 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1943 | "package p;" |
| 1944 | "enum Enum {" |
| 1945 | " FOO = 1," |
| 1946 | " BAR = 2," |
| 1947 | "}"); |
| 1948 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1949 | "package p;" |
| 1950 | "enum Enum {" |
| 1951 | " BAR = 2," |
| 1952 | " FOO = 1," |
| 1953 | "}"); |
| 1954 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1955 | } |
| 1956 | |
Jooyung Han | b5c55cd | 2020-10-22 07:59:01 +0900 | [diff] [blame] | 1957 | TEST_F(AidlTestCompatibleChanges, NewUnionField) { |
| 1958 | io_delegate_.SetFileContents("old/p/Union.aidl", |
| 1959 | "package p;" |
| 1960 | "union Union {" |
| 1961 | " String foo;" |
| 1962 | "}"); |
| 1963 | io_delegate_.SetFileContents("new/p/Union.aidl", |
| 1964 | "package p;" |
| 1965 | "union Union {" |
| 1966 | " String foo;" |
| 1967 | " int num;" |
| 1968 | "}"); |
| 1969 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1970 | } |
| 1971 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1972 | TEST_F(AidlTestCompatibleChanges, NewPackage) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1973 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1974 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1975 | "interface IFoo {" |
| 1976 | " void foo(int a);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1977 | "}"); |
| 1978 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1979 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1980 | "parcelable Data {" |
| 1981 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1982 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1983 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1984 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1985 | "interface IFoo {" |
| 1986 | " void foo(int a);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1987 | "}"); |
| 1988 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1989 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1990 | "parcelable Data {" |
| 1991 | " int foo;" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1992 | "}"); |
| 1993 | io_delegate_.SetFileContents("new/q/IFoo.aidl", |
| 1994 | "package q;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1995 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1996 | " void foo(int a);" |
| 1997 | "}"); |
| 1998 | io_delegate_.SetFileContents("new/q/Data.aidl", |
| 1999 | "package q;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2000 | "parcelable Data {" |
| 2001 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2002 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2003 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 2004 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2005 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2006 | TEST_F(AidlTestCompatibleChanges, ArgNameChange) { |
| 2007 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2008 | "package p;" |
| 2009 | "interface IFoo {" |
| 2010 | " void foo(int a);" |
| 2011 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2012 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2013 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2014 | "interface IFoo {" |
| 2015 | " void foo(int b);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2016 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2017 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 2018 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2019 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2020 | TEST_F(AidlTestCompatibleChanges, AddedConstValue) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2021 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 2022 | "package p; interface I {" |
| 2023 | "const int A = 1; }"); |
| 2024 | io_delegate_.SetFileContents("new/p/I.aidl", |
| 2025 | "package p ; interface I {" |
| 2026 | "const int A = 1; const int B = 2;}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2027 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 2028 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 2029 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2030 | TEST_F(AidlTestCompatibleChanges, ChangedConstValueOrder) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2031 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 2032 | "package p; interface I {" |
| 2033 | "const int A = 1; const int B = 2;}"); |
| 2034 | io_delegate_.SetFileContents("new/p/I.aidl", |
| 2035 | "package p ; interface I {" |
| 2036 | "const int B = 2; const int A = 1;}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2037 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2038 | } |
| 2039 | |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 2040 | TEST_F(AidlTestCompatibleChanges, ReorderedAnnatations) { |
| 2041 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2042 | "package p;" |
| 2043 | "@JavaPassthrough(annotation=\"Alice\")" |
| 2044 | "@JavaPassthrough(annotation=\"Bob\")" |
| 2045 | "parcelable Foo {}"); |
| 2046 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2047 | "package p;" |
| 2048 | "@JavaPassthrough(annotation=\"Bob\")" |
| 2049 | "@JavaPassthrough(annotation=\"Alice\")" |
| 2050 | "parcelable Foo {}"); |
| 2051 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 2052 | } |
| 2053 | |
Jooyung Han | 0007327 | 2020-11-27 14:20:20 +0900 | [diff] [blame] | 2054 | TEST_F(AidlTestCompatibleChanges, OkayToDeprecate) { |
| 2055 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2056 | "package p;" |
| 2057 | "parcelable Foo {}"); |
| 2058 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2059 | "package p;" |
| 2060 | "@JavaPassthrough(annotation=\"@Deprecated\")" |
| 2061 | "parcelable Foo {}"); |
| 2062 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 2063 | } |
| 2064 | |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 2065 | TEST_F(AidlTestCompatibleChanges, NewFieldOfNewType) { |
| 2066 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 2067 | "package p;" |
| 2068 | "parcelable Data {" |
| 2069 | " int num;" |
| 2070 | "}"); |
| 2071 | io_delegate_.SetFileContents( |
| 2072 | "new/p/Data.aidl", |
| 2073 | "package p;" |
| 2074 | "parcelable Data {" |
| 2075 | " int num;" |
| 2076 | " p.Enum e;" // this is considered as valid since 0(enum default) is valid for "Enum" type |
| 2077 | "}"); |
| 2078 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 2079 | "package p;" |
| 2080 | "enum Enum {" |
| 2081 | " FOO = 0," |
| 2082 | " BAR = 1," |
| 2083 | "}"); |
| 2084 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 2085 | } |
| 2086 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2087 | class AidlTestIncompatibleChanges : public AidlTest { |
| 2088 | protected: |
| 2089 | Options options_ = Options::From("aidl --checkapi old new"); |
| 2090 | }; |
| 2091 | |
| 2092 | TEST_F(AidlTestIncompatibleChanges, RemovedType) { |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 2093 | const string expected_stderr = "ERROR: old/p/IFoo.aidl:1.11-20: Removed type: p.IFoo\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2094 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2095 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2096 | "interface IFoo {" |
| 2097 | " void foo(in String[] str);" |
| 2098 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2099 | "}"); |
Devin Moore | d131d5e | 2020-03-31 10:39:10 -0700 | [diff] [blame] | 2100 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2101 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | d131d5e | 2020-03-31 10:39:10 -0700 | [diff] [blame] | 2102 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | TEST_F(AidlTestIncompatibleChanges, RemovedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2106 | const string expected_stderr = |
| 2107 | "ERROR: old/p/IFoo.aidl:1.61-65: Removed or changed method: p.IFoo.bar(String)\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2108 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2109 | "package p;" |
| 2110 | "interface IFoo {" |
| 2111 | " void foo(in String[] str);" |
| 2112 | " void bar(@utf8InCpp String str);" |
| 2113 | "}"); |
| 2114 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2115 | "package p;" |
| 2116 | "interface IFoo {" |
| 2117 | " void foo(in String[] str);" |
| 2118 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2119 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2120 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2121 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 2124 | TEST_F(AidlTestIncompatibleChanges, UntypedListInInterface) { |
| 2125 | const string expected_stderr = |
| 2126 | "ERROR: new/p/IFoo.aidl:1.61-65: " |
| 2127 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 2128 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 2129 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 2130 | "ERROR: new/p/IFoo.aidl: Failed to read.\n"; |
| 2131 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2132 | "package p;" |
| 2133 | "interface IFoo {" |
| 2134 | " void foo(in String[] str);" |
| 2135 | "}"); |
| 2136 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2137 | "package p;" |
| 2138 | "interface IFoo {" |
| 2139 | " void foo(in String[] str);" |
| 2140 | " void bar(in List arg);" |
| 2141 | "}"); |
| 2142 | CaptureStderr(); |
| 2143 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2144 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2145 | } |
| 2146 | |
| 2147 | TEST_F(AidlTestCompatibleChanges, UntypedListInParcelable) { |
| 2148 | const string expected_stderr = |
| 2149 | "ERROR: new/p/Data.aidl:1.54-59: " |
| 2150 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 2151 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 2152 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 2153 | "ERROR: new/p/Data.aidl: Failed to read.\n"; |
| 2154 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 2155 | "package p;" |
| 2156 | "parcelable Data {" |
| 2157 | " int foo;" |
| 2158 | "}"); |
| 2159 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 2160 | "package p;" |
| 2161 | "parcelable Data {" |
| 2162 | " int foo;" |
| 2163 | " @nullable List list;" |
| 2164 | "}"); |
| 2165 | CaptureStderr(); |
| 2166 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2167 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2168 | } |
| 2169 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2170 | TEST_F(AidlTestIncompatibleChanges, RemovedField) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2171 | const string expected_stderr = |
| 2172 | "ERROR: new/p/Data.aidl:1.21-26: Number of fields in p.Data is reduced from 2 to 1.\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2173 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 2174 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2175 | "parcelable Data {" |
| 2176 | " int foo;" |
| 2177 | " int bar;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2178 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2179 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 2180 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2181 | "parcelable Data {" |
| 2182 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2183 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2184 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2185 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2186 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2187 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2188 | |
Jooyung Han | b5c55cd | 2020-10-22 07:59:01 +0900 | [diff] [blame] | 2189 | TEST_F(AidlTestIncompatibleChanges, NewFieldWithNoDefault) { |
| 2190 | const string expected_stderr = |
| 2191 | "ERROR: new/p/Data.aidl:1.46-50: Field 'str' does not have a useful default in some " |
| 2192 | "backends. Please either provide a default value for this field or mark the field as " |
| 2193 | "@nullable. This value or a null value will be used automatically when an old version of " |
| 2194 | "this parcelable is sent to a process which understands a new version of this parcelable. In " |
| 2195 | "order to make sure your code continues to be backwards compatible, make sure the default or " |
| 2196 | "null value does not cause a semantic change to this parcelable.\n"; |
| 2197 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 2198 | "package p;" |
| 2199 | "parcelable Data {" |
| 2200 | " int num;" |
| 2201 | "}"); |
| 2202 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 2203 | "package p;" |
| 2204 | "parcelable Data {" |
| 2205 | " int num;" |
| 2206 | " String str;" |
| 2207 | "}"); |
| 2208 | CaptureStderr(); |
| 2209 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2210 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2211 | } |
| 2212 | |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 2213 | TEST_F(AidlTestIncompatibleChanges, NewFieldWithNonZeroEnum) { |
| 2214 | const string expected_stderr = |
| 2215 | "ERROR: new/p/Data.aidl:1.46-48: Field 'e' of enum 'Enum' can't be initialized as '0'. " |
| 2216 | "Please make sure 'Enum' has '0' as a valid value.\n"; |
| 2217 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 2218 | "package p;" |
| 2219 | "parcelable Data {" |
| 2220 | " int num;" |
| 2221 | "}"); |
| 2222 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 2223 | "package p;" |
| 2224 | "parcelable Data {" |
| 2225 | " int num;" |
| 2226 | " p.Enum e;" |
| 2227 | "}"); |
| 2228 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 2229 | "package p;" |
| 2230 | "enum Enum {" |
| 2231 | " FOO = 1," |
| 2232 | " BAR = 2," |
| 2233 | "}"); |
| 2234 | CaptureStderr(); |
| 2235 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2236 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2237 | } |
| 2238 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2239 | TEST_F(AidlTestIncompatibleChanges, RemovedEnumerator) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2240 | const string expected_stderr = |
| 2241 | "ERROR: new/p/Enum.aidl:1.15-20: Removed enumerator from p.Enum: FOO\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2242 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 2243 | "package p;" |
| 2244 | "enum Enum {" |
| 2245 | " FOO = 1," |
| 2246 | " BAR = 2," |
| 2247 | "}"); |
| 2248 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 2249 | "package p;" |
| 2250 | "enum Enum {" |
| 2251 | " BAR = 2," |
| 2252 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2253 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2254 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2255 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Jooyung Han | b5c55cd | 2020-10-22 07:59:01 +0900 | [diff] [blame] | 2258 | TEST_F(AidlTestIncompatibleChanges, RemovedUnionField) { |
| 2259 | const string expected_stderr = |
| 2260 | "ERROR: new/p/Union.aidl:1.16-22: Number of fields in p.Union is reduced from 2 to 1.\n"; |
| 2261 | io_delegate_.SetFileContents("old/p/Union.aidl", |
| 2262 | "package p;" |
| 2263 | "union Union {" |
| 2264 | " String str;" |
| 2265 | " int num;" |
| 2266 | "}"); |
| 2267 | io_delegate_.SetFileContents("new/p/Union.aidl", |
| 2268 | "package p;" |
| 2269 | "union Union {" |
| 2270 | " String str;" |
| 2271 | "}"); |
| 2272 | CaptureStderr(); |
| 2273 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2274 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2275 | } |
| 2276 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2277 | TEST_F(AidlTestIncompatibleChanges, RenamedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2278 | const string expected_stderr = |
| 2279 | "ERROR: old/p/IFoo.aidl:1.61-65: Removed or changed method: p.IFoo.bar(String)\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2280 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2281 | "package p;" |
| 2282 | "interface IFoo {" |
| 2283 | " void foo(in String[] str);" |
| 2284 | " void bar(@utf8InCpp String str);" |
| 2285 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2286 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2287 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2288 | "interface IFoo {" |
| 2289 | " void foo(in String[] str);" |
| 2290 | " void bar2(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2291 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2292 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2293 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2294 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2297 | TEST_F(AidlTestIncompatibleChanges, RenamedType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2298 | const string expected_stderr = "ERROR: old/p/IFoo.aidl:1.11-20: Removed type: p.IFoo\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2299 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2300 | "package p;" |
| 2301 | "interface IFoo {" |
| 2302 | " void foo(in String[] str);" |
| 2303 | " void bar(@utf8InCpp String str);" |
| 2304 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2305 | io_delegate_.SetFileContents("new/p/IFoo2.aidl", |
| 2306 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2307 | "interface IFoo2 {" |
| 2308 | " void foo(in String[] str);" |
| 2309 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2310 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2311 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2312 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2313 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2314 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2315 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2316 | TEST_F(AidlTestIncompatibleChanges, ChangedEnumerator) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2317 | const string expected_stderr = |
| 2318 | "ERROR: new/p/Enum.aidl:1.15-20: Changed enumerator value: p.Enum::FOO from 1 to 3.\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2319 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 2320 | "package p;" |
| 2321 | "enum Enum {" |
| 2322 | " FOO = 1," |
| 2323 | " BAR = 2," |
| 2324 | "}"); |
| 2325 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 2326 | "package p;" |
| 2327 | "enum Enum {" |
| 2328 | " FOO = 3," |
| 2329 | " BAR = 2," |
| 2330 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2331 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2332 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2333 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | TEST_F(AidlTestIncompatibleChanges, ReorderedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2337 | const string expected_stderr = |
| 2338 | "ERROR: new/p/IFoo.aidl:1.67-71: Transaction ID changed: p.IFoo.foo(String[]) is changed " |
| 2339 | "from 0 to 1.\n" |
| 2340 | "ERROR: new/p/IFoo.aidl:1.33-37: Transaction ID changed: p.IFoo.bar(String) is changed from " |
| 2341 | "1 to 0.\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2342 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2343 | "package p;" |
| 2344 | "interface IFoo {" |
| 2345 | " void foo(in String[] str);" |
| 2346 | " void bar(@utf8InCpp String str);" |
| 2347 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2348 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2349 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2350 | "interface IFoo {" |
| 2351 | " void bar(@utf8InCpp String str);" |
| 2352 | " void foo(in String[] str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2353 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2354 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2355 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2356 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | TEST_F(AidlTestIncompatibleChanges, ReorderedField) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2360 | const string expected_stderr = |
Jiyong Park | b07d993 | 2020-05-15 12:56:54 +0900 | [diff] [blame] | 2361 | "ERROR: new/p/Data.aidl:1.33-37: Reordered bar from 1 to 0.\n" |
| 2362 | "ERROR: new/p/Data.aidl:1.43-47: Reordered foo from 0 to 1.\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2363 | io_delegate_.SetFileContents("old/p/Data.aidl", |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2364 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2365 | "parcelable Data {" |
| 2366 | " int foo;" |
| 2367 | " int bar;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2368 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2369 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 2370 | "package p;" |
| 2371 | "parcelable Data {" |
| 2372 | " int bar;" |
| 2373 | " int foo;" |
| 2374 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2375 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2376 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2377 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2378 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2379 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2380 | TEST_F(AidlTestIncompatibleChanges, ChangedDirectionSpecifier) { |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 2381 | const string expected_stderr = "ERROR: new/p/IFoo.aidl:1.33-37: Direction changed: in to out.\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2382 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2383 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2384 | "interface IFoo {" |
| 2385 | " void foo(in String[] str);" |
| 2386 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2387 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2388 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2389 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2390 | "interface IFoo {" |
| 2391 | " void foo(out String[] str);" |
| 2392 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2393 | "}"); |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 2394 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2395 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 2396 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2397 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2398 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2399 | TEST_F(AidlTestIncompatibleChanges, AddedAnnotation) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2400 | const string expected_stderr = |
| 2401 | "ERROR: new/p/IFoo.aidl:1.51-58: Changed annotations: (empty) to @utf8InCpp\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2402 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2403 | "package p;" |
| 2404 | "interface IFoo {" |
| 2405 | " void foo(in String[] str);" |
| 2406 | " void bar(@utf8InCpp String str);" |
| 2407 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2408 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2409 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2410 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2411 | " void foo(in @utf8InCpp String[] str);" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2412 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2413 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2414 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2415 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2416 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2417 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2418 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2419 | TEST_F(AidlTestIncompatibleChanges, RemovedAnnotation) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2420 | const string expected_stderr = |
| 2421 | "ERROR: new/p/IFoo.aidl:1.66-72: Changed annotations: @utf8InCpp to (empty)\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2422 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2423 | "package p;" |
| 2424 | "interface IFoo {" |
| 2425 | " void foo(in String[] str);" |
| 2426 | " void bar(@utf8InCpp String str);" |
| 2427 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2428 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2429 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2430 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2431 | " void foo(in String[] str);" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2432 | " void bar(String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2433 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2434 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2435 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2436 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2437 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2438 | |
Jooyung Han | 60b2fde | 2020-10-29 15:12:40 +0900 | [diff] [blame] | 2439 | TEST_F(AidlTestIncompatibleChanges, ChangedBackingTypeOfEnum) { |
| 2440 | const string expected_stderr = |
| 2441 | "ERROR: new/p/Foo.aidl:1.11-32: Type changed: byte to long.\n" |
| 2442 | "ERROR: new/p/Foo.aidl:1.36-40: Changed backing types.\n"; |
| 2443 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2444 | "package p;" |
| 2445 | "@Backing(type=\"byte\")" |
| 2446 | "enum Foo {" |
| 2447 | " FOO, BAR," |
| 2448 | "}"); |
| 2449 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2450 | "package p;" |
| 2451 | "@Backing(type=\"long\")" |
| 2452 | "enum Foo {" |
| 2453 | " FOO, BAR," |
| 2454 | "}"); |
| 2455 | CaptureStderr(); |
| 2456 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2457 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2458 | } |
| 2459 | |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 2460 | TEST_F(AidlTestIncompatibleChanges, ChangedAnnatationParams) { |
| 2461 | const string expected_stderr = |
| 2462 | "ERROR: new/p/Foo.aidl:1.55-59: Changed annotations: @JavaPassthrough(annotation=\"Alice\") " |
| 2463 | "to @JavaPassthrough(annotation=\"Bob\")\n"; |
| 2464 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2465 | "package p;" |
| 2466 | "@JavaPassthrough(annotation=\"Alice\")" |
| 2467 | "parcelable Foo {}"); |
| 2468 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2469 | "package p;" |
| 2470 | "@JavaPassthrough(annotation=\"Bob\")" |
| 2471 | "parcelable Foo {}"); |
| 2472 | |
| 2473 | CaptureStderr(); |
| 2474 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2475 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2476 | } |
| 2477 | |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 2478 | TEST_F(AidlTestIncompatibleChanges, AddedParcelableAnnotation) { |
| 2479 | const string expected_stderr = |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 2480 | "ERROR: new/p/Foo.aidl:1.32-36: Changed annotations: (empty) to @FixedSize\n"; |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 2481 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2482 | "package p;" |
| 2483 | "parcelable Foo {" |
| 2484 | " int A;" |
| 2485 | "}"); |
| 2486 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2487 | "package p;" |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 2488 | "@FixedSize parcelable Foo {" |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 2489 | " int A;" |
| 2490 | "}"); |
| 2491 | CaptureStderr(); |
| 2492 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2493 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2494 | } |
| 2495 | |
| 2496 | TEST_F(AidlTestIncompatibleChanges, RemovedParcelableAnnotation) { |
| 2497 | const string expected_stderr = |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 2498 | "ERROR: new/p/Foo.aidl:1.21-25: Changed annotations: @FixedSize to (empty)\n"; |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 2499 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2500 | "package p;" |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 2501 | "@FixedSize parcelable Foo {" |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 2502 | " int A;" |
| 2503 | "}"); |
| 2504 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2505 | "package p;" |
| 2506 | "parcelable Foo {" |
| 2507 | " int A;" |
| 2508 | "}"); |
| 2509 | CaptureStderr(); |
| 2510 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2511 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2512 | } |
| 2513 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2514 | TEST_F(AidlTestIncompatibleChanges, RemovedPackage) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2515 | const string expected_stderr = "ERROR: old/q/IFoo.aidl:1.11-21: Removed type: q.IFoo\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2516 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}"); |
| 2517 | io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2518 | io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2519 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2520 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2521 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2522 | } |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 2523 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2524 | TEST_F(AidlTestIncompatibleChanges, ChangedDefaultValue) { |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 2525 | const string expected_stderr = "ERROR: new/p/D.aidl:1.30-32: Changed default value: 1 to 2.\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2526 | io_delegate_.SetFileContents("old/p/D.aidl", "package p; parcelable D { int a = 1; }"); |
| 2527 | io_delegate_.SetFileContents("new/p/D.aidl", "package p; parcelable D { int a = 2; }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2528 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2529 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2530 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2531 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 2532 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2533 | TEST_F(AidlTestIncompatibleChanges, RemovedConstValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2534 | const string expected_stderr = |
| 2535 | "ERROR: old/p/I.aidl:1.51-53: Removed constant declaration: p.I.B\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2536 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 2537 | "package p; interface I {" |
| 2538 | "const int A = 1; const int B = 2;}"); |
| 2539 | io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 1; }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2540 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2541 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2542 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2543 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 2544 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2545 | TEST_F(AidlTestIncompatibleChanges, ChangedConstValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2546 | const string expected_stderr = |
| 2547 | "ERROR: new/p/I.aidl:1.11-21: Changed constant value: p.I.A from 1 to 2.\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2548 | io_delegate_.SetFileContents("old/p/I.aidl", "package p; interface I { const int A = 1; }"); |
| 2549 | io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 2; }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2550 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2551 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2552 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 2553 | } |
| 2554 | |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 2555 | TEST_F(AidlTestIncompatibleChanges, FixedSizeAddedField) { |
| 2556 | const string expected_stderr = |
| 2557 | "ERROR: new/p/Foo.aidl:1.33-37: Number of fields in p.Foo is changed from 1 to 2. " |
| 2558 | "This is an incompatible change for FixedSize types.\n"; |
| 2559 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2560 | "package p; @FixedSize parcelable Foo { int A = 1; }"); |
| 2561 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2562 | "package p; @FixedSize parcelable Foo { int A = 1; int B = 2; }"); |
| 2563 | CaptureStderr(); |
| 2564 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2565 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2566 | } |
| 2567 | |
| 2568 | TEST_F(AidlTestIncompatibleChanges, FixedSizeRemovedField) { |
| 2569 | const string expected_stderr = |
| 2570 | "ERROR: new/p/Foo.aidl:1.33-37: Number of fields in p.Foo is reduced from 2 to 1.\n"; |
| 2571 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2572 | "package p; @FixedSize parcelable Foo { int A = 1; int B = 1; }"); |
| 2573 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2574 | "package p; @FixedSize parcelable Foo { int A = 1; }"); |
| 2575 | CaptureStderr(); |
| 2576 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2577 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2578 | } |
| 2579 | |
| 2580 | TEST_P(AidlTest, RejectNonFixedSizeFromFixedSize) { |
| 2581 | const string expected_stderr = |
| 2582 | "ERROR: Foo.aidl:1.36-38: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2583 | "a.\n" |
| 2584 | "ERROR: Foo.aidl:1.44-46: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2585 | "b.\n" |
| 2586 | "ERROR: Foo.aidl:1.55-57: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2587 | "c.\n" |
| 2588 | "ERROR: Foo.aidl:1.80-82: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2589 | "d.\n" |
| 2590 | "ERROR: Foo.aidl:1.92-94: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2591 | "e.\n" |
| 2592 | "ERROR: Foo.aidl:1.109-111: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2593 | "f.\n"; |
| 2594 | |
| 2595 | io_delegate_.SetFileContents("Foo.aidl", |
| 2596 | "@FixedSize parcelable Foo { " |
| 2597 | " int[] a;" |
| 2598 | " Bar b;" |
| 2599 | " String c;" |
| 2600 | " ParcelFileDescriptor d;" |
| 2601 | " IBinder e;" |
| 2602 | " List<String> f;" |
Steven Moreland | 265f3d4 | 2020-09-24 19:06:54 +0000 | [diff] [blame] | 2603 | " int isFixedSize;" |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 2604 | "}"); |
| 2605 | io_delegate_.SetFileContents("Bar.aidl", "parcelable Bar { int a; }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 2606 | Options options = Options::From("aidl Foo.aidl -I . --lang=" + to_string(GetLanguage())); |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 2607 | |
| 2608 | CaptureStderr(); |
| 2609 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2610 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2611 | } |
| 2612 | |
| 2613 | TEST_P(AidlTest, AcceptFixedSizeFromFixedSize) { |
| 2614 | const string expected_stderr = ""; |
| 2615 | |
| 2616 | io_delegate_.SetFileContents("Foo.aidl", "@FixedSize parcelable Foo { int a; Bar b; }"); |
| 2617 | io_delegate_.SetFileContents("Bar.aidl", "@FixedSize parcelable Bar { Val c; }"); |
| 2618 | io_delegate_.SetFileContents("Val.aidl", "enum Val { A, B, }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 2619 | Options options = Options::From("aidl Foo.aidl -I . --lang=" + to_string(GetLanguage())); |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 2620 | |
| 2621 | CaptureStderr(); |
| 2622 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2623 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2624 | } |
| 2625 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 2626 | TEST_F(AidlTest, RejectAmbiguousImports) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2627 | const string expected_stderr = |
| 2628 | "ERROR: p/IFoo.aidl: Duplicate files found for q.IBar from:\n" |
| 2629 | "dir1/q/IBar.aidl\n" |
| 2630 | "dir2/q/IBar.aidl\n" |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 2631 | "ERROR: p/IFoo.aidl: Couldn't find import for class q.IBar\n"; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 2632 | Options options = Options::From("aidl --lang=java -o out -I dir1 -I dir2 p/IFoo.aidl"); |
| 2633 | io_delegate_.SetFileContents("p/IFoo.aidl", "package p; import q.IBar; interface IFoo{}"); |
| 2634 | io_delegate_.SetFileContents("dir1/q/IBar.aidl", "package q; interface IBar{}"); |
| 2635 | io_delegate_.SetFileContents("dir2/q/IBar.aidl", "package q; interface IBar{}"); |
| 2636 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2637 | CaptureStderr(); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 2638 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2639 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 2640 | } |
| 2641 | |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2642 | TEST_F(AidlTest, HandleManualIdAssignments) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2643 | const string expected_stderr = |
| 2644 | "ERROR: new/p/IFoo.aidl:1.32-36: Transaction ID changed: p.IFoo.foo() is changed from 10 to " |
| 2645 | "11.\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2646 | Options options = Options::From("aidl --checkapi old new"); |
| 2647 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}"); |
| 2648 | io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}"); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2649 | |
| 2650 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 2651 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2652 | io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 11;}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2653 | CaptureStderr(); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2654 | EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2655 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2656 | } |
| 2657 | |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2658 | TEST_F(AidlTest, ParcelFileDescriptorIsBuiltinType) { |
| 2659 | Options javaOptions = Options::From("aidl --lang=java -o out p/IFoo.aidl"); |
| 2660 | Options cppOptions = Options::From("aidl --lang=cpp -h out -o out p/IFoo.aidl"); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2661 | Options rustOptions = Options::From("aidl --lang=rust -o out p/IFoo.aidl"); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2662 | |
| 2663 | // use without import |
| 2664 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 2665 | "package p; interface IFoo{ void foo(in ParcelFileDescriptor fd);}"); |
| 2666 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 2667 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2668 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rustOptions, io_delegate_)); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2669 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2670 | // use without import but with full name |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2671 | io_delegate_.SetFileContents( |
| 2672 | "p/IFoo.aidl", |
| 2673 | "package p; interface IFoo{ void foo(in android.os.ParcelFileDescriptor fd);}"); |
| 2674 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 2675 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2676 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rustOptions, io_delegate_)); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2677 | |
| 2678 | // use with import (as before) |
| 2679 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 2680 | "package p;" |
| 2681 | "import android.os.ParcelFileDescriptor;" |
| 2682 | "interface IFoo{" |
| 2683 | " void foo(in ParcelFileDescriptor fd);" |
| 2684 | "}"); |
| 2685 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 2686 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2687 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rustOptions, io_delegate_)); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2688 | } |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2689 | |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2690 | TEST_F(AidlTest, ManualIds) { |
| 2691 | Options options = Options::From("aidl --lang=java -o out IFoo.aidl"); |
| 2692 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2693 | "interface IFoo {\n" |
| 2694 | " void foo() = 0;\n" |
| 2695 | " void bar() = 1;\n" |
| 2696 | "}"); |
| 2697 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2698 | } |
| 2699 | |
| 2700 | TEST_F(AidlTest, ManualIdsWithMetaTransactions) { |
| 2701 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2702 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2703 | "interface IFoo {\n" |
| 2704 | " void foo() = 0;\n" |
| 2705 | " void bar() = 1;\n" |
| 2706 | "}"); |
| 2707 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2708 | } |
| 2709 | |
| 2710 | TEST_F(AidlTest, FailOnDuplicatedIds) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2711 | const string expected_stderr = |
| 2712 | "ERROR: IFoo.aidl:3.7-11: Found duplicate method id (3) for method bar\n"; |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2713 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2714 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2715 | "interface IFoo {\n" |
| 2716 | " void foo() = 3;\n" |
| 2717 | " void bar() = 3;\n" |
| 2718 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2719 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2720 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2721 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | TEST_F(AidlTest, FailOnOutOfRangeIds) { |
| 2725 | // 16777115 is kLastMetaMethodId + 1 |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2726 | const string expected_stderr = |
| 2727 | "ERROR: IFoo.aidl:3.7-11: Found out of bounds id (16777115) for method bar. " |
| 2728 | "Value for id must be between 0 and 16777114 inclusive.\n"; |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2729 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2730 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2731 | "interface IFoo {\n" |
| 2732 | " void foo() = 3;\n" |
| 2733 | " void bar() = 16777115;\n" |
| 2734 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2735 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2736 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2737 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | TEST_F(AidlTest, FailOnPartiallyAssignedIds) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2741 | const string expected_stderr = |
| 2742 | "ERROR: IFoo.aidl:3.7-11: You must either assign id's to all methods or to none of them.\n"; |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2743 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2744 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2745 | "interface IFoo {\n" |
| 2746 | " void foo() = 3;\n" |
| 2747 | " void bar();\n" |
| 2748 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2749 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2750 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2751 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2752 | } |
| 2753 | |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2754 | TEST_F(AidlTest, AllowDuplicatedImportPaths) { |
| 2755 | Options options = Options::From("aidl --lang=java -I dir -I dir IFoo.aidl"); |
| 2756 | io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}"); |
| 2757 | io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}"); |
| 2758 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2759 | } |
| 2760 | |
| 2761 | TEST_F(AidlTest, FailOnAmbiguousImports) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2762 | const string expected_stderr = |
| 2763 | "ERROR: IFoo.aidl: Duplicate files found for IBar from:\n" |
| 2764 | "dir/IBar.aidl\n" |
| 2765 | "dir2/IBar.aidl\n" |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 2766 | "ERROR: IFoo.aidl: Couldn't find import for class IBar\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2767 | |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2768 | Options options = Options::From("aidl --lang=java -I dir -I dir2 IFoo.aidl"); |
| 2769 | io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}"); |
| 2770 | io_delegate_.SetFileContents("dir2/IBar.aidl", "interface IBar{}"); |
| 2771 | io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2772 | CaptureStderr(); |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2773 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2774 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2775 | } |
| 2776 | |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 2777 | TEST_F(AidlTest, UnusedImportDoesNotContributeInclude) { |
| 2778 | io_delegate_.SetFileContents("a/b/IFoo.aidl", |
| 2779 | "package a.b;\n" |
| 2780 | "import a.b.IBar;\n" |
| 2781 | "import a.b.IQux;\n" |
| 2782 | "interface IFoo { IQux foo(); }\n"); |
| 2783 | io_delegate_.SetFileContents("a/b/IBar.aidl", "package a.b; interface IBar { void foo(); }"); |
| 2784 | io_delegate_.SetFileContents("a/b/IQux.aidl", "package a.b; interface IQux { void foo(); }"); |
| 2785 | |
| 2786 | Options options = Options::From("aidl --lang=ndk a/b/IFoo.aidl -I . -o out -h out/include"); |
| 2787 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2788 | |
| 2789 | string output; |
| 2790 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/include/aidl/a/b/IFoo.h", &output)); |
| 2791 | // IBar was imported but wasn't used. include is not expected. |
| 2792 | EXPECT_THAT(output, Not(testing::HasSubstr("#include <aidl/a/b/IBar.h>"))); |
| 2793 | // IBar was imported and used. include is expected. |
| 2794 | EXPECT_THAT(output, (testing::HasSubstr("#include <aidl/a/b/IQux.h>"))); |
| 2795 | } |
| 2796 | |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2797 | TEST_F(AidlTest, ParseJavaPassthroughAnnotation) { |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2798 | io_delegate_.SetFileContents("a/IFoo.aidl", R"--(package a; |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2799 | import a.MyEnum; |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2800 | @JavaPassthrough(annotation="@com.android.Alice(arg=com.android.Alice.Value.A)") |
| 2801 | @JavaPassthrough(annotation="@com.android.AliceTwo") |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2802 | interface IFoo { |
| 2803 | @JavaPassthrough(annotation="@com.android.Bob") |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2804 | void foo(@JavaPassthrough(annotation="@com.android.Cat") int x, MyEnum y); |
Jiyong Park | 7c3b0e4 | 2020-08-04 16:08:32 +0900 | [diff] [blame] | 2805 | const @JavaPassthrough(annotation="@com.android.David") int A = 3; |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2806 | })--"); |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2807 | // JavaPassthrough should work with other types as well (e.g. enum) |
| 2808 | io_delegate_.SetFileContents("a/MyEnum.aidl", R"--(package a; |
| 2809 | @JavaPassthrough(annotation="@com.android.Alice(arg=com.android.Alice.Value.A)") |
| 2810 | @JavaPassthrough(annotation="@com.android.AliceTwo") |
| 2811 | @Backing(type="byte") |
| 2812 | enum MyEnum { |
| 2813 | a, b, c |
| 2814 | })--"); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2815 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2816 | Options java_options = Options::From("aidl -I . --lang=java -o out a/IFoo.aidl a/MyEnum.aidl"); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2817 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 2818 | |
| 2819 | string java_out; |
| 2820 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/IFoo.java", &java_out)); |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2821 | // type-decl-level annotations with newline at the end |
| 2822 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Alice(arg=com.android.Alice.Value.A)\n")); |
| 2823 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.AliceTwo\n")); |
| 2824 | // member-decl-level annotations with newline at the end |
| 2825 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Bob\n")); |
| 2826 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.David\n")); |
| 2827 | // inline annotations with space at the end |
| 2828 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Cat ")); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2829 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2830 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/MyEnum.java", &java_out)); |
| 2831 | // type-decl-level annotations with newline at the end |
| 2832 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Alice(arg=com.android.Alice.Value.A)\n")); |
| 2833 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.AliceTwo\n")); |
| 2834 | |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2835 | // Other backends shouldn't be bothered |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2836 | Options cpp_options = |
| 2837 | Options::From("aidl -I . --lang=cpp -o out -h out a/IFoo.aidl a/MyEnum.aidl"); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2838 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
| 2839 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2840 | Options ndk_options = |
| 2841 | Options::From("aidl -I . --lang=ndk -o out -h out a/IFoo.aidl a/MyEnum.aidl"); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2842 | EXPECT_EQ(0, ::android::aidl::compile_aidl(ndk_options, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2843 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2844 | Options rust_options = Options::From("aidl -I . --lang=rust -o out a/IFoo.aidl a/MyEnum.aidl"); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2845 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rust_options, io_delegate_)); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2846 | } |
| 2847 | |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 2848 | TEST_F(AidlTest, ParseRustDerive) { |
| 2849 | io_delegate_.SetFileContents("a/Foo.aidl", R"(package a; |
| 2850 | @RustDerive(Clone=true, Copy=false) |
| 2851 | parcelable Foo { |
| 2852 | int a; |
| 2853 | })"); |
| 2854 | |
| 2855 | Options rust_options = Options::From("aidl --lang=rust -o out a/Foo.aidl"); |
| 2856 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rust_options, io_delegate_)); |
| 2857 | |
| 2858 | string rust_out; |
| 2859 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.rs", &rust_out)); |
| 2860 | EXPECT_THAT(rust_out, testing::HasSubstr("#[derive(Debug, Clone)]")); |
| 2861 | |
| 2862 | // Other backends shouldn't be bothered |
| 2863 | Options cpp_options = Options::From("aidl --lang=cpp -o out -h out a/Foo.aidl"); |
| 2864 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
| 2865 | |
| 2866 | Options ndk_options = Options::From("aidl --lang=ndk -o out -h out a/Foo.aidl"); |
| 2867 | EXPECT_EQ(0, ::android::aidl::compile_aidl(ndk_options, io_delegate_)); |
| 2868 | |
| 2869 | Options java_options = Options::From("aidl --lang=java -o out a/Foo.aidl"); |
| 2870 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 2871 | } |
| 2872 | |
Jiyong Park | 56f73d7 | 2019-06-11 12:20:28 +0900 | [diff] [blame] | 2873 | class AidlOutputPathTest : public AidlTest { |
| 2874 | protected: |
| 2875 | void SetUp() override { |
| 2876 | AidlTest::SetUp(); |
| 2877 | io_delegate_.SetFileContents("sub/dir/foo/bar/IFoo.aidl", "package foo.bar; interface IFoo {}"); |
| 2878 | } |
| 2879 | |
| 2880 | void Test(const Options& options, const std::string expected_output_path) { |
| 2881 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2882 | // check the existence |
| 2883 | EXPECT_TRUE(io_delegate_.GetWrittenContents(expected_output_path, nullptr)); |
| 2884 | } |
| 2885 | }; |
| 2886 | |
| 2887 | TEST_F(AidlOutputPathTest, OutDirWithNoOutputFile) { |
| 2888 | // <out_dir> / <package_name> / <type_name>.java |
| 2889 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl"), "out/foo/bar/IFoo.java"); |
| 2890 | } |
| 2891 | |
| 2892 | TEST_F(AidlOutputPathTest, OutDirWithOutputFile) { |
| 2893 | // when output file is explicitly set, it is always respected. -o option is |
| 2894 | // ignored. |
| 2895 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java"); |
| 2896 | } |
| 2897 | |
| 2898 | TEST_F(AidlOutputPathTest, NoOutDirWithOutputFile) { |
| 2899 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java"); |
| 2900 | } |
| 2901 | |
| 2902 | TEST_F(AidlOutputPathTest, NoOutDirWithNoOutputFile) { |
| 2903 | // output is the same as the input file except for the suffix |
| 2904 | Test(Options::From("aidl sub/dir/foo/bar/IFoo.aidl"), "sub/dir/foo/bar/IFoo.java"); |
| 2905 | } |
| 2906 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2907 | TEST_P(AidlTest, FailOnOutOfBoundsInt32MaxConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2908 | AidlError error; |
| 2909 | const string expected_stderr = |
| 2910 | "ERROR: p/IFoo.aidl:3.58-69: Invalid type specifier for an int64 literal: int\n"; |
| 2911 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2912 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2913 | R"(package p; |
| 2914 | interface IFoo { |
| 2915 | const int int32_max_oob = 2147483650; |
| 2916 | } |
| 2917 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2918 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2919 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2920 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2921 | } |
| 2922 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2923 | TEST_P(AidlTest, FailOnOutOfBoundsInt32MinConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2924 | AidlError error; |
| 2925 | const string expected_stderr = |
| 2926 | "ERROR: p/IFoo.aidl:3.58-60: Invalid type specifier for an int64 literal: int\n"; |
| 2927 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2928 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2929 | R"(package p; |
| 2930 | interface IFoo { |
| 2931 | const int int32_min_oob = -2147483650; |
| 2932 | } |
| 2933 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2934 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2935 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2936 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2939 | TEST_P(AidlTest, FailOnOutOfBoundsInt64MaxConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2940 | AidlError error; |
| 2941 | const string expected_stderr = |
Devin Moore | 2a08890 | 2020-09-17 10:51:19 -0700 | [diff] [blame] | 2942 | "ERROR: p/IFoo.aidl:3.59-86: Could not parse integer: 21474836509999999999999999\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2943 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2944 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2945 | R"(package p; |
| 2946 | interface IFoo { |
| 2947 | const long int64_max_oob = 21474836509999999999999999; |
| 2948 | } |
| 2949 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2950 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2951 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2952 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2953 | } |
| 2954 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2955 | TEST_P(AidlTest, FailOnOutOfBoundsInt64MinConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2956 | AidlError error; |
| 2957 | const string expected_stderr = |
Devin Moore | 2a08890 | 2020-09-17 10:51:19 -0700 | [diff] [blame] | 2958 | "ERROR: p/IFoo.aidl:3.61-87: Could not parse integer: 21474836509999999999999999\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2959 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2960 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2961 | R"(package p; |
| 2962 | interface IFoo { |
| 2963 | const long int64_min_oob = -21474836509999999999999999; |
| 2964 | } |
| 2965 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2966 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2967 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2968 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2969 | } |
| 2970 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2971 | TEST_P(AidlTest, FailOnOutOfBoundsAutofilledEnum) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2972 | AidlError error; |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 2973 | const string expected_stderr = |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 2974 | "ERROR: p/TestEnum.aidl:5.1-36: Invalid type specifier for an int32 literal: byte\n" |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 2975 | "ERROR: p/TestEnum.aidl:5.1-36: Enumerator type differs from enum backing type.\n"; |
| 2976 | CaptureStderr(); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 2977 | EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl", |
| 2978 | R"(package p; |
| 2979 | @Backing(type="byte") |
| 2980 | enum TestEnum { |
| 2981 | FOO = 127, |
| 2982 | BAR, |
| 2983 | } |
| 2984 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2985 | typenames_, GetLanguage(), &error)); |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 2986 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2987 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Devin Moore | decaf29 | 2020-04-30 09:16:40 -0700 | [diff] [blame] | 2990 | TEST_P(AidlTest, UnsupportedBackingAnnotationParam) { |
| 2991 | AidlError error; |
| 2992 | const string expected_stderr = |
| 2993 | "ERROR: p/TestEnum.aidl:2.1-51: Parameter foo not supported for annotation Backing. It must " |
Devin Moore | decaf29 | 2020-04-30 09:16:40 -0700 | [diff] [blame] | 2994 | "be one of: type\n"; |
| 2995 | CaptureStderr(); |
| 2996 | EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl", |
| 2997 | R"(package p; |
| 2998 | @Backing(foo="byte") |
| 2999 | enum TestEnum { |
| 3000 | FOO = 1, |
| 3001 | BAR, |
| 3002 | } |
| 3003 | )", |
| 3004 | typenames_, GetLanguage(), &error)); |
| 3005 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3006 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
| 3007 | } |
| 3008 | |
Jooyung Han | 5721a23 | 2020-12-24 04:34:55 +0900 | [diff] [blame] | 3009 | TEST_P(AidlTest, BackingAnnotationRequireTypeParameter) { |
| 3010 | const string expected_stderr = "ERROR: Enum.aidl:1.1-9: Missing 'type' on @Backing.\n"; |
| 3011 | CaptureStderr(); |
| 3012 | EXPECT_EQ(nullptr, Parse("Enum.aidl", "@Backing enum Enum { FOO }", typenames_, GetLanguage())); |
| 3013 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3014 | } |
| 3015 | |
Jooyung Han | 6681393 | 2020-10-23 13:24:44 +0900 | [diff] [blame] | 3016 | TEST_F(AidlTest, SupportJavaOnlyImmutableAnnotation) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 3017 | io_delegate_.SetFileContents("Foo.aidl", |
| 3018 | "@JavaOnlyImmutable parcelable Foo { int a; Bar b; List<Bar> c; " |
| 3019 | "Map<String, Baz> d; Bar[] e; }"); |
| 3020 | io_delegate_.SetFileContents("Bar.aidl", "@JavaOnlyImmutable parcelable Bar { String a; }"); |
| 3021 | io_delegate_.SetFileContents("Baz.aidl", |
| 3022 | "@JavaOnlyImmutable @JavaOnlyStableParcelable parcelable Baz;"); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3023 | Options options = Options::From("aidl --lang=java -I . Foo.aidl"); |
| 3024 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3025 | } |
| 3026 | |
Jooyung Han | 6681393 | 2020-10-23 13:24:44 +0900 | [diff] [blame] | 3027 | TEST_F(AidlTest, RejectMutableParcelableFromJavaOnlyImmutableParcelable) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 3028 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { Bar bar; }"); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3029 | io_delegate_.SetFileContents("Bar.aidl", "parcelable Bar { String a; }"); |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame] | 3030 | string expected_error = |
Jooyung Han | 59af9cc | 2020-10-25 21:44:14 +0900 | [diff] [blame] | 3031 | "ERROR: Foo.aidl:1.40-44: The @JavaOnlyImmutable 'Foo' has a non-immutable field " |
| 3032 | "named 'bar'.\n"; |
| 3033 | CaptureStderr(); |
| 3034 | Options options = Options::From("aidl --lang=java Foo.aidl -I ."); |
| 3035 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3036 | EXPECT_EQ(expected_error, GetCapturedStderr()); |
| 3037 | } |
| 3038 | |
| 3039 | TEST_F(AidlTest, RejectMutableParcelableFromJavaOnlyImmutableUnion) { |
| 3040 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable union Foo { Bar bar; }"); |
| 3041 | io_delegate_.SetFileContents("Bar.aidl", "parcelable Bar { String a; }"); |
| 3042 | string expected_error = |
| 3043 | "ERROR: Foo.aidl:1.35-39: The @JavaOnlyImmutable 'Foo' has a non-immutable field " |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame] | 3044 | "named 'bar'.\n"; |
| 3045 | CaptureStderr(); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3046 | Options options = Options::From("aidl --lang=java Foo.aidl -I ."); |
| 3047 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame] | 3048 | EXPECT_EQ(expected_error, GetCapturedStderr()); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3049 | } |
| 3050 | |
Jooyung Han | 6681393 | 2020-10-23 13:24:44 +0900 | [diff] [blame] | 3051 | TEST_F(AidlTest, ImmutableParcelableCannotBeInOut) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 3052 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { int a; }"); |
Jooyung Han | 15fd6c6 | 2020-10-23 13:54:46 +0900 | [diff] [blame] | 3053 | io_delegate_.SetFileContents("IBar.aidl", "interface IBar { void my(inout Foo foo); }"); |
| 3054 | string expected_error = |
| 3055 | "ERROR: IBar.aidl:1.35-39: 'foo' can't be an inout parameter because @JavaOnlyImmutable can " |
| 3056 | "only be an in parameter.\n"; |
| 3057 | CaptureStderr(); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3058 | Options options = Options::From("aidl --lang=java IBar.aidl -I ."); |
| 3059 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jooyung Han | 15fd6c6 | 2020-10-23 13:54:46 +0900 | [diff] [blame] | 3060 | EXPECT_EQ(expected_error, GetCapturedStderr()); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3061 | } |
| 3062 | |
Jooyung Han | 6681393 | 2020-10-23 13:24:44 +0900 | [diff] [blame] | 3063 | TEST_F(AidlTest, ImmutableParcelableCannotBeOut) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 3064 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { int a; }"); |
Jooyung Han | 15fd6c6 | 2020-10-23 13:54:46 +0900 | [diff] [blame] | 3065 | io_delegate_.SetFileContents("IBar.aidl", "interface IBar { void my(out Foo foo); }"); |
| 3066 | string expected_error = |
| 3067 | "ERROR: IBar.aidl:1.33-37: 'foo' can't be an out parameter because @JavaOnlyImmutable can " |
| 3068 | "only be an in parameter.\n"; |
| 3069 | CaptureStderr(); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3070 | Options options = Options::From("aidl --lang=java IBar.aidl -I ."); |
| 3071 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jooyung Han | 15fd6c6 | 2020-10-23 13:54:46 +0900 | [diff] [blame] | 3072 | EXPECT_EQ(expected_error, GetCapturedStderr()); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 3073 | } |
| 3074 | |
Jooyung Han | 6681393 | 2020-10-23 13:24:44 +0900 | [diff] [blame] | 3075 | TEST_F(AidlTest, ImmutableParcelableFieldNameRestriction) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 3076 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { int a; int A; }"); |
Jeongik Cha | 9118025 | 2020-07-31 15:43:11 +0900 | [diff] [blame] | 3077 | Options options = Options::From("aidl --lang=java Foo.aidl"); |
| 3078 | const string expected_stderr = |
Jooyung Han | 59af9cc | 2020-10-25 21:44:14 +0900 | [diff] [blame] | 3079 | "ERROR: Foo.aidl:1.47-49: 'Foo' has duplicate field name 'A' after capitalizing the first " |
| 3080 | "letter\n"; |
Jeongik Cha | 9118025 | 2020-07-31 15:43:11 +0900 | [diff] [blame] | 3081 | CaptureStderr(); |
| 3082 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3083 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3084 | } |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 3085 | |
Jooyung Han | fe89f12 | 2020-10-14 03:49:18 +0900 | [diff] [blame] | 3086 | TEST_P(AidlTest, UnionRejectsEmptyDecl) { |
| 3087 | const string method = "package a; union Foo {}"; |
| 3088 | const string expected_stderr = "ERROR: a/Foo.aidl:1.17-21: The union 'Foo' has no fields.\n"; |
| 3089 | CaptureStderr(); |
| 3090 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage())); |
| 3091 | EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_stderr)); |
| 3092 | } |
| 3093 | |
| 3094 | TEST_P(AidlTest, UnionRejectsParcelableHolder) { |
| 3095 | const string method = "package a; union Foo { ParcelableHolder x; }"; |
| 3096 | const string expected_stderr = |
| 3097 | "ERROR: a/Foo.aidl:1.40-42: A union can't have a member of ParcelableHolder 'x'\n"; |
| 3098 | CaptureStderr(); |
| 3099 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage())); |
| 3100 | EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_stderr)); |
| 3101 | } |
| 3102 | |
Jooyung Han | 53fb424 | 2020-12-17 16:03:49 +0900 | [diff] [blame] | 3103 | TEST_P(AidlTest, UnionRejectsFirstEnumWithNoDefaults) { |
| 3104 | import_paths_.insert("."); |
| 3105 | io_delegate_.SetFileContents("a/Enum.aidl", "package a; enum Enum { FOO, BAR }"); |
| 3106 | const string expected_err = "The union's first member should have a useful default value."; |
| 3107 | CaptureStderr(); |
| 3108 | EXPECT_EQ(nullptr, |
| 3109 | Parse("a/Foo.aidl", "package a; union Foo { a.Enum e; }", typenames_, GetLanguage())); |
| 3110 | EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_err)); |
| 3111 | } |
| 3112 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 3113 | TEST_P(AidlTest, GenericStructuredParcelable) { |
| 3114 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo<T, U> { int a; int A; }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3115 | Options options = Options::From("aidl Foo.aidl --lang=" + to_string(GetLanguage())); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 3116 | const string expected_stderr = ""; |
| 3117 | CaptureStderr(); |
| 3118 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3119 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3120 | } |
| 3121 | |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 3122 | TEST_F(AidlTest, GenericStructuredParcelableWithStringConstants_Cpp) { |
| 3123 | io_delegate_.SetFileContents("Foo.aidl", |
| 3124 | "parcelable Foo<T, U> { int a; const String s = \"\"; }"); |
| 3125 | Options options = |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3126 | Options::From("aidl Foo.aidl --lang=" + to_string(Options::Language::CPP) + " -o out -h out"); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 3127 | const string expected_stderr = ""; |
| 3128 | CaptureStderr(); |
| 3129 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3130 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3131 | |
| 3132 | string code; |
| 3133 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/Foo.h", &code)); |
| 3134 | EXPECT_THAT(code, testing::HasSubstr(R"--(template <typename T, typename U> |
| 3135 | const ::android::String16& Foo<T,U>::s() { |
| 3136 | static const ::android::String16 value(::android::String16("")); |
| 3137 | return value; |
| 3138 | })--")); |
| 3139 | } |
| 3140 | |
| 3141 | TEST_F(AidlTest, GenericStructuredParcelableWithStringConstants_Ndk) { |
| 3142 | io_delegate_.SetFileContents("Foo.aidl", |
| 3143 | "parcelable Foo<T, U> { int a; const String s = \"\"; }"); |
| 3144 | Options options = |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3145 | Options::From("aidl Foo.aidl --lang=" + to_string(Options::Language::NDK) + " -o out -h out"); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 3146 | const string expected_stderr = ""; |
| 3147 | CaptureStderr(); |
| 3148 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3149 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3150 | |
| 3151 | string code; |
| 3152 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/aidl/Foo.h", &code)); |
| 3153 | EXPECT_THAT(code, testing::HasSubstr(R"--(template <typename T, typename U> |
| 3154 | const char* Foo<T, U>::s = ""; |
| 3155 | )--")); |
| 3156 | } |
| 3157 | |
Jooyung Han | 8c9a4e1 | 2020-10-26 15:39:54 +0900 | [diff] [blame] | 3158 | TEST_F(AidlTest, NestedTypeArgs) { |
| 3159 | io_delegate_.SetFileContents("a/Bar.aidl", "package a; parcelable Bar<A> { }"); |
Steven Moreland | 6c07b83 | 2020-10-29 23:39:53 +0000 | [diff] [blame] | 3160 | io_delegate_.SetFileContents("a/Baz.aidl", "package a; parcelable Baz<A, B> { }"); |
| 3161 | |
| 3162 | io_delegate_.SetFileContents("a/Foo.aidl", |
| 3163 | "package a; import a.Bar; import a.Baz; parcelable Foo { " |
| 3164 | "Baz<Bar<Bar<String[]>>[], Bar<String>> barss; }"); |
Jooyung Han | 8c9a4e1 | 2020-10-26 15:39:54 +0900 | [diff] [blame] | 3165 | Options options = Options::From("aidl a/Foo.aidl -I . -o out --lang=java"); |
| 3166 | const string expected_stderr = ""; |
| 3167 | CaptureStderr(); |
| 3168 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3169 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3170 | |
| 3171 | string code; |
| 3172 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.java", &code)); |
Steven Moreland | 6c07b83 | 2020-10-29 23:39:53 +0000 | [diff] [blame] | 3173 | EXPECT_THAT(code, |
| 3174 | testing::HasSubstr( |
| 3175 | "a.Baz<a.Bar<a.Bar<java.lang.String[]>>[],a.Bar<java.lang.String>> barss;")); |
| 3176 | } |
| 3177 | |
| 3178 | TEST_F(AidlTest, DoubleArrayError) { |
| 3179 | io_delegate_.SetFileContents("a/Bar.aidl", "package a; parcelable Bar { String[][] a; }"); |
| 3180 | |
| 3181 | Options options = Options::From("aidl a/Bar.aidl -I . -o out --lang=java"); |
| 3182 | const string expected_stderr = |
| 3183 | "ERROR: a/Bar.aidl:1.28-37: Can only have one dimensional arrays.\n"; |
| 3184 | CaptureStderr(); |
| 3185 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3186 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3187 | } |
| 3188 | |
| 3189 | TEST_F(AidlTest, DoubleGenericError) { |
| 3190 | io_delegate_.SetFileContents("a/Bar.aidl", |
| 3191 | "package a; parcelable Bar { List<String><String> a; }"); |
| 3192 | |
| 3193 | Options options = Options::From("aidl a/Bar.aidl -I . -o out --lang=java"); |
| 3194 | const string expected_stderr = |
| 3195 | "ERROR: a/Bar.aidl:1.28-33: Can only specify one set of type parameters.\n"; |
| 3196 | CaptureStderr(); |
| 3197 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3198 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3199 | } |
| 3200 | |
| 3201 | TEST_F(AidlTest, ArrayBeforeGenericError) { |
| 3202 | io_delegate_.SetFileContents("a/Bar.aidl", "package a; parcelable Bar { List[]<String> a; }"); |
| 3203 | |
| 3204 | Options options = Options::From("aidl a/Bar.aidl -I . -o out --lang=java"); |
| 3205 | const string expected_stderr = |
| 3206 | "ERROR: a/Bar.aidl:1.28-33: Must specify type parameters (<>) before array ([]).\n"; |
| 3207 | CaptureStderr(); |
| 3208 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3209 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jooyung Han | 8c9a4e1 | 2020-10-26 15:39:54 +0900 | [diff] [blame] | 3210 | } |
| 3211 | |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 3212 | struct GenericAidlTest : ::testing::Test { |
| 3213 | FakeIoDelegate io_delegate_; |
| 3214 | void Compile(string cmd) { |
Steven Moreland | 6c07b83 | 2020-10-29 23:39:53 +0000 | [diff] [blame] | 3215 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo { Bar<Baz<Qux>> x; }"); |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 3216 | io_delegate_.SetFileContents("Bar.aidl", "parcelable Bar<T> { }"); |
| 3217 | io_delegate_.SetFileContents("Baz.aidl", "parcelable Baz<T> { }"); |
| 3218 | io_delegate_.SetFileContents("Qux.aidl", "parcelable Qux { }"); |
| 3219 | |
| 3220 | Options options = Options::From(cmd); |
| 3221 | CaptureStderr(); |
| 3222 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3223 | EXPECT_EQ("", GetCapturedStderr()); |
| 3224 | } |
| 3225 | }; |
| 3226 | |
| 3227 | TEST_F(GenericAidlTest, ImportGenericParameterTypesCPP) { |
| 3228 | Compile("aidl Foo.aidl --lang=cpp -I . -o out -h out"); |
| 3229 | string code; |
| 3230 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/Foo.h", &code)); |
| 3231 | EXPECT_THAT(code, testing::HasSubstr("#include <Bar.h>")); |
| 3232 | EXPECT_THAT(code, testing::HasSubstr("#include <Baz.h>")); |
| 3233 | EXPECT_THAT(code, testing::HasSubstr("#include <Qux.h>")); |
| 3234 | } |
| 3235 | |
| 3236 | TEST_F(GenericAidlTest, ImportGenericParameterTypesNDK) { |
| 3237 | Compile("aidl Foo.aidl --lang=ndk -I . -o out -h out"); |
| 3238 | string code; |
| 3239 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/aidl/Foo.h", &code)); |
| 3240 | EXPECT_THAT(code, testing::HasSubstr("#include <aidl/Bar.h>")); |
| 3241 | EXPECT_THAT(code, testing::HasSubstr("#include <aidl/Baz.h>")); |
| 3242 | EXPECT_THAT(code, testing::HasSubstr("#include <aidl/Qux.h>")); |
| 3243 | } |
| 3244 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 3245 | TEST_P(AidlTest, RejectGenericStructuredParcelabelRepeatedParam) { |
| 3246 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo<T,T> { int a; int A; }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3247 | Options options = Options::From("aidl Foo.aidl --lang=" + to_string(GetLanguage())); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 3248 | const string expected_stderr = |
| 3249 | "ERROR: Foo.aidl:1.11-15: Every type parameter should be unique.\n"; |
| 3250 | CaptureStderr(); |
| 3251 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3252 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3253 | } |
| 3254 | |
| 3255 | TEST_P(AidlTest, RejectGenericStructuredParcelableField) { |
| 3256 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo<T,T> { T a; int A; }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3257 | Options options = Options::From("aidl Foo.aidl --lang=" + to_string(GetLanguage())); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 3258 | const string expected_stderr = "ERROR: Foo.aidl:1.22-24: Failed to resolve 'T'\n"; |
| 3259 | CaptureStderr(); |
| 3260 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3261 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 3262 | } |
| 3263 | |
Jooyung Han | 7a30884 | 2020-10-27 19:06:49 +0900 | [diff] [blame] | 3264 | TEST_P(AidlTest, LongCommentWithinConstExpression) { |
| 3265 | io_delegate_.SetFileContents("Foo.aidl", "enum Foo { FOO = (1 << 1) /* comment */ | 0x0 }"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3266 | Options options = Options::From("aidl Foo.aidl --lang=" + to_string(GetLanguage())); |
Jooyung Han | 7a30884 | 2020-10-27 19:06:49 +0900 | [diff] [blame] | 3267 | CaptureStderr(); |
| 3268 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3269 | EXPECT_EQ("", GetCapturedStderr()); |
| 3270 | } |
| 3271 | |
Jeongik Cha | ef44e62 | 2020-10-23 16:00:52 +0900 | [diff] [blame] | 3272 | TEST_F(AidlTest, RejectUntypdeListAndMapInUnion) { |
| 3273 | io_delegate_.SetFileContents("a/Foo.aidl", "package a; union Foo { List l; Map m; }"); |
| 3274 | Options options = Options::From("aidl a/Foo.aidl --lang=java -o out"); |
| 3275 | std::string expectedErr = |
| 3276 | "ERROR: a/Foo.aidl:1.28-30: " |
| 3277 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 3278 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 3279 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 3280 | "ERROR: a/Foo.aidl:1.35-37: " |
| 3281 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 3282 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 3283 | "the receiving side. Consider switching to an array or a generic List/Map.\n"; |
| 3284 | CaptureStderr(); |
| 3285 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3286 | EXPECT_EQ(expectedErr, GetCapturedStderr()); |
| 3287 | } |
| 3288 | |
| 3289 | TEST_F(AidlTest, RejectUntypdeListAndMapInUnstructuredParcelable) { |
Jooyung Han | ea2147c | 2020-10-21 18:16:56 +0900 | [diff] [blame] | 3290 | io_delegate_.SetFileContents("a/Foo.aidl", "package a; parcelable Foo { List l; Map m; }"); |
| 3291 | Options options = Options::From("aidl a/Foo.aidl --lang=java -o out"); |
Jeongik Cha | ef44e62 | 2020-10-23 16:00:52 +0900 | [diff] [blame] | 3292 | std::string expectedErr = |
| 3293 | "ERROR: a/Foo.aidl:1.33-35: " |
| 3294 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 3295 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 3296 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 3297 | "ERROR: a/Foo.aidl:1.40-42: " |
| 3298 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 3299 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 3300 | "the receiving side. Consider switching to an array or a generic List/Map.\n"; |
Jooyung Han | ea2147c | 2020-10-21 18:16:56 +0900 | [diff] [blame] | 3301 | CaptureStderr(); |
Jeongik Cha | ef44e62 | 2020-10-23 16:00:52 +0900 | [diff] [blame] | 3302 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3303 | EXPECT_EQ(expectedErr, GetCapturedStderr()); |
| 3304 | } |
Jooyung Han | ea2147c | 2020-10-21 18:16:56 +0900 | [diff] [blame] | 3305 | |
Jeongik Cha | ef44e62 | 2020-10-23 16:00:52 +0900 | [diff] [blame] | 3306 | TEST_F(AidlTest, RejectNestedUntypedListAndMap) { |
| 3307 | io_delegate_.SetFileContents("a/Bar.aidl", "package a; parcelable Bar<T>;"); |
| 3308 | io_delegate_.SetFileContents( |
| 3309 | "a/Foo.aidl", "package a; import a.Bar; parcelable Foo { Bar<List> a; Bar<Map> b; }"); |
| 3310 | Options options = Options::From("aidl a/Foo.aidl -I . --lang=java -o out"); |
| 3311 | std::string expectedErr = |
| 3312 | "ERROR: a/Foo.aidl:1.52-54: " |
| 3313 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 3314 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 3315 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 3316 | "ERROR: a/Foo.aidl:1.64-66: " |
| 3317 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 3318 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 3319 | "the receiving side. Consider switching to an array or a generic List/Map.\n"; |
| 3320 | CaptureStderr(); |
| 3321 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 3322 | EXPECT_EQ(expectedErr, GetCapturedStderr()); |
Jooyung Han | ea2147c | 2020-10-21 18:16:56 +0900 | [diff] [blame] | 3323 | } |
| 3324 | |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 3325 | TEST_F(AidlTest, EnumWithDefaults_Java) { |
| 3326 | io_delegate_.SetFileContents("a/p/Enum.aidl", "package p; enum Enum { FOO, BAR }"); |
| 3327 | io_delegate_.SetFileContents("a/p/Foo.aidl", R"( |
| 3328 | package p; |
| 3329 | import p.Enum; |
| 3330 | parcelable Foo { |
| 3331 | Enum e = Enum.BAR; |
| 3332 | })"); |
| 3333 | CaptureStderr(); |
| 3334 | auto options = Options::From("aidl -I a --lang java -o out a/p/Foo.aidl"); |
| 3335 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 3336 | auto err = GetCapturedStderr(); |
| 3337 | EXPECT_EQ("", err); |
| 3338 | |
| 3339 | string code; |
| 3340 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/p/Foo.java", &code)); |
Jooyung Han | 2d83361 | 2020-12-17 15:59:27 +0900 | [diff] [blame] | 3341 | EXPECT_THAT(code, testing::HasSubstr("byte e = p.Enum.BAR")); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | TEST_F(AidlTest, EnumWithDefaults_Cpp) { |
| 3345 | io_delegate_.SetFileContents("a/p/Enum.aidl", "package p; enum Enum { FOO, BAR }"); |
| 3346 | io_delegate_.SetFileContents("a/p/Foo.aidl", R"( |
| 3347 | package p; |
| 3348 | import p.Enum; |
| 3349 | parcelable Foo { |
| 3350 | Enum e = Enum.BAR; |
| 3351 | })"); |
| 3352 | CaptureStderr(); |
| 3353 | auto options = Options::From("aidl -I a --lang cpp -o out -h out a/p/Foo.aidl"); |
| 3354 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 3355 | auto err = GetCapturedStderr(); |
| 3356 | EXPECT_EQ("", err); |
| 3357 | |
| 3358 | string code; |
| 3359 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/p/Foo.h", &code)); |
| 3360 | EXPECT_THAT(code, testing::HasSubstr("::p::Enum e = ::p::Enum(::p::Enum::BAR);")); |
| 3361 | } |
| 3362 | |
| 3363 | TEST_F(AidlTest, EnumWithDefaults_Ndk) { |
| 3364 | io_delegate_.SetFileContents("a/p/Enum.aidl", "package p; enum Enum { FOO, BAR }"); |
| 3365 | io_delegate_.SetFileContents("a/p/Foo.aidl", R"( |
| 3366 | package p; |
| 3367 | import p.Enum; |
| 3368 | parcelable Foo { |
| 3369 | Enum e = Enum.BAR; |
| 3370 | })"); |
| 3371 | CaptureStderr(); |
| 3372 | auto options = Options::From("aidl -I a --lang ndk -o out -h out a/p/Foo.aidl"); |
| 3373 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 3374 | auto err = GetCapturedStderr(); |
| 3375 | EXPECT_EQ("", err); |
| 3376 | |
| 3377 | string code; |
| 3378 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/aidl/p/Foo.h", &code)); |
| 3379 | EXPECT_THAT(code, testing::HasSubstr("::aidl::p::Enum e = ::aidl::p::Enum::BAR;")); |
| 3380 | } |
| 3381 | |
| 3382 | TEST_F(AidlTest, EnumWithDefaults_Rust) { |
| 3383 | io_delegate_.SetFileContents("a/p/Enum.aidl", "package p; enum Enum { FOO, BAR }"); |
| 3384 | io_delegate_.SetFileContents("a/p/Foo.aidl", R"( |
| 3385 | package p; |
| 3386 | import p.Enum; |
| 3387 | parcelable Foo { |
| 3388 | int n = 42; |
| 3389 | Enum e = Enum.BAR; |
| 3390 | })"); |
| 3391 | CaptureStderr(); |
| 3392 | auto options = Options::From("aidl -I a --lang rust -o out -h out a/p/Foo.aidl"); |
| 3393 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 3394 | auto err = GetCapturedStderr(); |
| 3395 | EXPECT_EQ("", err); |
| 3396 | |
| 3397 | string code; |
| 3398 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/p/Foo.rs", &code)); |
| 3399 | EXPECT_THAT(code, testing::HasSubstr(R"( |
| 3400 | fn default() -> Self { |
| 3401 | Self { |
| 3402 | n: 42, |
| 3403 | e: crate::mangled::_1_p_4_Enum::BAR, |
| 3404 | } |
| 3405 | })")); |
| 3406 | } |
| 3407 | |
| 3408 | TEST_P(AidlTest, EnumeratorIsConstantValue_DefaultValue) { |
| 3409 | import_paths_.insert("a"); |
| 3410 | io_delegate_.SetFileContents("a/p/Enum.aidl", "package p; enum Enum { FOO = 1, BAR = 2}"); |
| 3411 | CaptureStderr(); |
| 3412 | const AidlDefinedType* type = Parse("a/p/Foo.aidl", R"( |
| 3413 | package p; |
| 3414 | import p.Enum; |
| 3415 | parcelable Foo { |
| 3416 | int e = Enum.FOO | Enum.BAR; |
| 3417 | })", |
| 3418 | typenames_, GetLanguage()); |
| 3419 | auto err = GetCapturedStderr(); |
| 3420 | EXPECT_EQ("", err); |
| 3421 | EXPECT_TRUE(type); |
| 3422 | const auto& fields = type->AsStructuredParcelable()->GetFields(); |
| 3423 | EXPECT_EQ("int e = 3", fields[0]->ToString()); |
| 3424 | } |
| 3425 | |
| 3426 | TEST_P(AidlTest, EnumeratorIsConstantValue_CanDefineOtherEnumerator) { |
| 3427 | CaptureStderr(); |
| 3428 | const AidlDefinedType* type = Parse("a/p/Foo.aidl", R"( |
| 3429 | @Backing(type="int") |
| 3430 | enum Foo { |
| 3431 | STANDARD_SHIFT = 16, |
| 3432 | STANDARD_BT709 = 1 << STANDARD_SHIFT, |
| 3433 | STANDARD_BT601_625 = 2 << STANDARD_SHIFT, |
| 3434 | } |
| 3435 | )", |
| 3436 | typenames_, GetLanguage()); |
| 3437 | auto err = GetCapturedStderr(); |
| 3438 | EXPECT_EQ("", err); |
| 3439 | EXPECT_TRUE(type); |
| 3440 | const auto& enum_type = type->AsEnumDeclaration(); |
| 3441 | string code; |
| 3442 | enum_type->Dump(CodeWriter::ForString(&code).get()); |
| 3443 | EXPECT_EQ(R"--(@Backing(type="int") |
| 3444 | enum Foo { |
| 3445 | STANDARD_SHIFT = 16, |
| 3446 | STANDARD_BT709 = 65536, |
| 3447 | STANDARD_BT601_625 = 131072, |
| 3448 | } |
| 3449 | )--", |
| 3450 | code); |
| 3451 | } |
| 3452 | |
| 3453 | TEST_F(AidlTest, EnumDefaultShouldBeEnumerators) { |
| 3454 | io_delegate_.SetFileContents("a/p/Enum.aidl", "package p; enum Enum { FOO = 1, BAR = 2}"); |
| 3455 | io_delegate_.SetFileContents("a/p/Foo.aidl", R"( |
| 3456 | package p; |
| 3457 | import p.Enum; |
| 3458 | parcelable Foo { |
| 3459 | Enum e = Enum.FOO | Enum.BAR; |
| 3460 | })"); |
| 3461 | CaptureStderr(); |
| 3462 | auto options = Options::From("aidl -I a --lang java -o out -h out a/p/Foo.aidl"); |
| 3463 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
| 3464 | auto err = GetCapturedStderr(); |
| 3465 | EXPECT_EQ("ERROR: a/p/Foo.aidl:5.11-20: Invalid value (Enum.FOO|Enum.BAR) for enum p.Enum\n", |
| 3466 | err); |
| 3467 | } |
| 3468 | |
| 3469 | TEST_P(AidlTest, DefaultWithEmptyArray) { |
| 3470 | io_delegate_.SetFileContents("a/p/Foo.aidl", "package p; parcelable Foo { p.Bar[] bars = {}; }"); |
| 3471 | io_delegate_.SetFileContents("a/p/Bar.aidl", "package p; parcelable Bar { }"); |
| 3472 | CaptureStderr(); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3473 | auto options = |
| 3474 | Options::From("aidl -I a --lang " + to_string(GetLanguage()) + " -o out -h out a/p/Foo.aidl"); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 3475 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 3476 | auto err = GetCapturedStderr(); |
| 3477 | EXPECT_EQ("", err); |
| 3478 | } |
| 3479 | |
| 3480 | TEST_P(AidlTest, RejectRefsInAnnotation) { |
| 3481 | io_delegate_.SetFileContents("a/p/IFoo.aidl", |
| 3482 | "package p; interface IFoo {\n" |
| 3483 | " const String ANNOTATION = \"@Annotation\";\n" |
| 3484 | " @JavaPassthrough(annotation=ANNOTATION) void foo();\n" |
| 3485 | "}"); |
| 3486 | CaptureStderr(); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3487 | auto options = |
| 3488 | Options::From("aidl --lang " + to_string(GetLanguage()) + " -o out -h out a/p/IFoo.aidl"); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 3489 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
| 3490 | auto err = GetCapturedStderr(); |
| 3491 | EXPECT_EQ( |
| 3492 | "ERROR: a/p/IFoo.aidl:3.31-41: Value must be a constant expression but contains reference to " |
| 3493 | "ANNOTATION.\n", |
| 3494 | err); |
| 3495 | } |
| 3496 | |
| 3497 | TEST_F(AidlTest, DefaultWithEnumValues) { |
| 3498 | io_delegate_.SetFileContents( |
| 3499 | "a/p/Foo.aidl", |
| 3500 | "package p; import p.Bar; parcelable Foo { Bar[] bars = { Bar.FOO, Bar.FOO }; }"); |
| 3501 | io_delegate_.SetFileContents("a/p/Bar.aidl", "package p; enum Bar { FOO, BAR }"); |
| 3502 | CaptureStderr(); |
| 3503 | auto options = Options::From("aidl -I a --lang ndk -o out -h out a/p/Foo.aidl"); |
| 3504 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 3505 | auto err = GetCapturedStderr(); |
| 3506 | EXPECT_EQ("", err); |
| 3507 | string code; |
| 3508 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/aidl/p/Foo.h", &code)); |
| 3509 | EXPECT_THAT( |
| 3510 | code, testing::HasSubstr( |
| 3511 | "std::vector<::aidl::p::Bar> bars = {::aidl::p::Bar::FOO, ::aidl::p::Bar::FOO};")); |
| 3512 | } |
| 3513 | |
Jooyung Han | 1425939 | 2020-12-07 10:00:08 +0900 | [diff] [blame] | 3514 | TEST_F(AidlTest, RejectsCircularReferencingEnumerators) { |
| 3515 | io_delegate_.SetFileContents("a/p/Foo.aidl", "package p; enum Foo { A = B, B }"); |
| 3516 | CaptureStderr(); |
| 3517 | auto options = Options::From("aidl -I a --lang ndk -o out -h out a/p/Foo.aidl"); |
| 3518 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
| 3519 | auto err = GetCapturedStderr(); |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 3520 | EXPECT_EQ( |
| 3521 | "ERROR: a/p/Foo.aidl:1.26-28: Found a circular reference: B -> A -> B\n" |
| 3522 | "ERROR: a/p/Foo.aidl:1.29-31: Found a circular reference: A -> B -> A\n", |
| 3523 | err); |
Jooyung Han | 1425939 | 2020-12-07 10:00:08 +0900 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | TEST_F(AidlTest, RejectsCircularReferencingConsts) { |
| 3527 | io_delegate_.SetFileContents("a/p/Foo.aidl", |
| 3528 | "package p; parcelable Foo { const int A = A + 1; }"); |
| 3529 | CaptureStderr(); |
| 3530 | auto options = Options::From("aidl -I a --lang ndk -o out -h out a/p/Foo.aidl"); |
| 3531 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
| 3532 | auto err = GetCapturedStderr(); |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 3533 | EXPECT_EQ("ERROR: a/p/Foo.aidl:1.42-44: Found a circular reference: A -> A\n", err); |
| 3534 | } |
| 3535 | |
| 3536 | TEST_F(AidlTest, RecursiveReferences) { |
| 3537 | io_delegate_.SetFileContents("a/p/Foo.aidl", |
| 3538 | "package p; parcelable Foo { const int A = p.Bar.A + 1; }"); |
| 3539 | io_delegate_.SetFileContents("a/p/Bar.aidl", |
| 3540 | "package p; parcelable Bar { const int A = p.Baz.A + 1; }"); |
| 3541 | io_delegate_.SetFileContents("a/p/Baz.aidl", "package p; parcelable Baz { const int A = 1; }"); |
| 3542 | CaptureStderr(); |
| 3543 | auto options = Options::From("aidl -I a --lang ndk -o out -h out a/p/Foo.aidl"); |
| 3544 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
| 3545 | EXPECT_EQ("", GetCapturedStderr()); |
| 3546 | } |
| 3547 | |
Jooyung Han | e9f5b27 | 2021-01-07 00:18:11 +0900 | [diff] [blame] | 3548 | TEST_P(AidlTest, UnknownConstReference) { |
| 3549 | io_delegate_.SetFileContents("Foo.aidl", " parcelable Foo { UnknownType field = UNKNOWN_REF; }"); |
| 3550 | auto options = |
| 3551 | Options::From("aidl --lang " + to_string(GetLanguage()) + " -o out -h out Foo.aidl"); |
| 3552 | const string err = |
| 3553 | "ERROR: Foo.aidl:1.18-30: Failed to resolve 'UnknownType'\n" |
| 3554 | "ERROR: Foo.aidl:1.38-50: Can't find UNKNOWN_REF in Foo\n" |
| 3555 | "ERROR: Foo.aidl:1.38-50: Unknown reference 'UNKNOWN_REF'\n"; |
| 3556 | CaptureStderr(); |
| 3557 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
| 3558 | EXPECT_EQ(err, GetCapturedStderr()); |
| 3559 | } |
| 3560 | |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 3561 | TEST_P(AidlTest, JavaCompatibleBuiltinTypes) { |
| 3562 | string contents = R"( |
| 3563 | import android.os.IBinder; |
| 3564 | import android.os.IInterface; |
| 3565 | interface IFoo {} |
| 3566 | )"; |
| 3567 | EXPECT_NE(nullptr, Parse("IFoo.aidl", contents, typenames_, GetLanguage())); |
Jooyung Han | 1425939 | 2020-12-07 10:00:08 +0900 | [diff] [blame] | 3568 | } |
| 3569 | |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 3570 | TEST_P(AidlTest, WarningInterfaceName) { |
| 3571 | io_delegate_.SetFileContents("p/Foo.aidl", "interface Foo {}"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3572 | auto options = Options::From("aidl --lang " + to_string(GetLanguage()) + |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 3573 | " -Weverything -o out -h out p/Foo.aidl"); |
| 3574 | CaptureStderr(); |
| 3575 | EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_)); |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 3576 | EXPECT_EQ("WARNING: p/Foo.aidl:1.1-10: Interface names should start with I. [-Winterface-name]\n", |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 3577 | GetCapturedStderr()); |
| 3578 | } |
| 3579 | |
| 3580 | TEST_P(AidlTest, ErrorInterfaceName) { |
| 3581 | io_delegate_.SetFileContents("p/Foo.aidl", "interface Foo {}"); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3582 | auto options = Options::From("aidl --lang " + to_string(GetLanguage()) + |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 3583 | " -Weverything -Werror -o out -h out p/Foo.aidl"); |
| 3584 | CaptureStderr(); |
| 3585 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 3586 | EXPECT_EQ("ERROR: p/Foo.aidl:1.1-10: Interface names should start with I. [-Winterface-name]\n", |
Jooyung Han | 3b99018 | 2020-12-22 17:44:31 +0900 | [diff] [blame] | 3587 | GetCapturedStderr()); |
| 3588 | } |
| 3589 | |
Jooyung Han | 2d6b5c4 | 2021-01-09 01:01:06 +0900 | [diff] [blame^] | 3590 | TEST_F(AidlTest, HideIsNotForArgs) { |
| 3591 | io_delegate_.SetFileContents("IFoo.aidl", |
| 3592 | "interface IFoo {\n" |
| 3593 | " void foo(in @Hide int x);\n" |
| 3594 | "}"); |
| 3595 | auto options = Options::From("aidl --lang=java IFoo.aidl"); |
| 3596 | CaptureStderr(); |
| 3597 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
| 3598 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@Hide is not available")); |
| 3599 | } |
| 3600 | |
| 3601 | TEST_F(AidlTest, SuppressWarningsIsNotForArgs) { |
| 3602 | io_delegate_.SetFileContents( |
| 3603 | "IFoo.aidl", |
| 3604 | "interface IFoo {\n" |
| 3605 | " void foo(in @SuppressWarnings(value=\"inout-parameter\") int x);\n" |
| 3606 | "}"); |
| 3607 | auto options = Options::From("aidl --lang=java IFoo.aidl"); |
| 3608 | CaptureStderr(); |
| 3609 | EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_)); |
| 3610 | EXPECT_THAT(GetCapturedStderr(), HasSubstr("@SuppressWarnings is not available")); |
| 3611 | } |
| 3612 | |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3613 | struct TypeParam { |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3614 | string kind; |
| 3615 | string literal; |
| 3616 | }; |
| 3617 | |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3618 | const TypeParam kTypeParams[] = { |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3619 | {"primitive", "int"}, {"String", "String"}, |
| 3620 | {"IBinder", "IBinder"}, {"ParcelFileDescriptor", "ParcelFileDescriptor"}, |
| 3621 | {"parcelable", "Foo"}, {"enum", "a.Enum"}, |
| 3622 | {"union", "a.Union"}, {"interface", "a.IBar"}, |
| 3623 | }; |
| 3624 | |
| 3625 | const std::map<std::string, std::string> kListSupportExpectations = { |
| 3626 | {"cpp_primitive", "A generic type cannot have any primitive type parameters."}, |
| 3627 | {"java_primitive", "A generic type cannot have any primitive type parameters."}, |
| 3628 | {"ndk_primitive", "A generic type cannot have any primitive type parameters."}, |
| 3629 | {"rust_primitive", "A generic type cannot have any primitive type parameters."}, |
| 3630 | {"cpp_String", ""}, |
| 3631 | {"java_String", ""}, |
| 3632 | {"ndk_String", ""}, |
| 3633 | {"rust_String", ""}, |
| 3634 | {"cpp_IBinder", ""}, |
| 3635 | {"java_IBinder", ""}, |
| 3636 | {"ndk_IBinder", "List<IBinder> is not supported. List in NDK doesn't support IBinder."}, |
| 3637 | {"rust_IBinder", ""}, |
Jooyung Han | 55f96ad | 2020-12-13 10:08:33 +0900 | [diff] [blame] | 3638 | {"cpp_ParcelFileDescriptor", ""}, |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3639 | {"java_ParcelFileDescriptor", ""}, |
| 3640 | {"ndk_ParcelFileDescriptor", ""}, |
| 3641 | {"rust_ParcelFileDescriptor", ""}, |
Jooyung Han | 55f96ad | 2020-12-13 10:08:33 +0900 | [diff] [blame] | 3642 | {"cpp_interface", "List<a.IBar> is not supported."}, |
| 3643 | {"java_interface", "List<a.IBar> is not supported."}, |
| 3644 | {"ndk_interface", "List<a.IBar> is not supported."}, |
| 3645 | {"rust_interface", "List<a.IBar> is not supported."}, |
| 3646 | {"cpp_parcelable", ""}, |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3647 | {"java_parcelable", ""}, |
| 3648 | {"ndk_parcelable", ""}, |
| 3649 | {"rust_parcelable", ""}, |
| 3650 | {"cpp_enum", "A generic type cannot have any primitive type parameters."}, |
| 3651 | {"java_enum", "A generic type cannot have any primitive type parameters."}, |
| 3652 | {"ndk_enum", "A generic type cannot have any primitive type parameters."}, |
| 3653 | {"rust_enum", "A generic type cannot have any primitive type parameters."}, |
Jooyung Han | 55f96ad | 2020-12-13 10:08:33 +0900 | [diff] [blame] | 3654 | {"cpp_union", ""}, |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3655 | {"java_union", ""}, |
| 3656 | {"ndk_union", ""}, |
| 3657 | {"rust_union", ""}, |
| 3658 | }; |
| 3659 | |
Jooyung Han | d236e55 | 2020-12-13 09:21:27 +0900 | [diff] [blame] | 3660 | const std::map<std::string, std::string> kArraySupportExpectations = { |
| 3661 | {"cpp_primitive", ""}, |
| 3662 | {"java_primitive", ""}, |
| 3663 | {"ndk_primitive", ""}, |
| 3664 | {"rust_primitive", ""}, |
| 3665 | {"cpp_String", ""}, |
| 3666 | {"java_String", ""}, |
| 3667 | {"ndk_String", ""}, |
| 3668 | {"rust_String", ""}, |
| 3669 | {"cpp_IBinder", ""}, |
| 3670 | {"java_IBinder", ""}, |
| 3671 | {"ndk_IBinder", "The ndk backend does not support array of IBinder"}, |
| 3672 | {"rust_IBinder", "The rust backend does not support array of IBinder"}, |
| 3673 | {"cpp_ParcelFileDescriptor", ""}, |
| 3674 | {"java_ParcelFileDescriptor", ""}, |
| 3675 | {"ndk_ParcelFileDescriptor", ""}, |
| 3676 | {"rust_ParcelFileDescriptor", ""}, |
| 3677 | {"cpp_interface", "Binder type cannot be an array"}, |
| 3678 | {"java_interface", "Binder type cannot be an array"}, |
| 3679 | {"ndk_interface", "Binder type cannot be an array"}, |
| 3680 | {"rust_interface", "Binder type cannot be an array"}, |
| 3681 | {"cpp_parcelable", ""}, |
| 3682 | {"java_parcelable", ""}, |
| 3683 | {"ndk_parcelable", ""}, |
| 3684 | {"rust_parcelable", ""}, |
| 3685 | {"cpp_enum", ""}, |
| 3686 | {"java_enum", ""}, |
| 3687 | {"ndk_enum", ""}, |
| 3688 | {"rust_enum", ""}, |
| 3689 | {"cpp_union", ""}, |
| 3690 | {"java_union", ""}, |
| 3691 | {"ndk_union", ""}, |
| 3692 | {"rust_union", ""}, |
| 3693 | }; |
| 3694 | |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3695 | class AidlTypeParamTest : public testing::TestWithParam<std::tuple<Options::Language, TypeParam>> { |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3696 | public: |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3697 | void Run(const std::string& generic_type_decl, |
| 3698 | const std::map<std::string, std::string>& expectations) { |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3699 | const auto& param = GetParam(); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3700 | const auto& lang = to_string(std::get<0>(param)); |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3701 | const auto& kind = std::get<1>(param).kind; |
| 3702 | |
| 3703 | FakeIoDelegate io; |
| 3704 | io.SetFileContents("a/IBar.aidl", "package a; interface IBar { }"); |
| 3705 | io.SetFileContents("a/Enum.aidl", "package a; enum Enum { A }"); |
| 3706 | io.SetFileContents("a/Union.aidl", "package a; union Union { int a; }"); |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3707 | std::string decl = fmt::format(generic_type_decl, std::get<1>(param).literal); |
| 3708 | io.SetFileContents("a/Foo.aidl", "package a; parcelable Foo { " + decl + " f; }"); |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3709 | |
| 3710 | const auto options = |
| 3711 | Options::From(fmt::format("aidl -I . --lang={} a/Foo.aidl -o out -h out", lang)); |
| 3712 | CaptureStderr(); |
| 3713 | compile_aidl(options, io); |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3714 | auto it = expectations.find(lang + "_" + kind); |
| 3715 | EXPECT_TRUE(it != expectations.end()); |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3716 | const string err = GetCapturedStderr(); |
| 3717 | if (it->second.empty()) { |
| 3718 | EXPECT_EQ("", err); |
| 3719 | } else { |
| 3720 | EXPECT_THAT(err, testing::HasSubstr(it->second)); |
| 3721 | } |
| 3722 | } |
| 3723 | }; |
| 3724 | |
| 3725 | INSTANTIATE_TEST_SUITE_P( |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3726 | AidlTestSuite, AidlTypeParamTest, |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3727 | testing::Combine(testing::Values(Options::Language::CPP, Options::Language::JAVA, |
| 3728 | Options::Language::NDK, Options::Language::RUST), |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3729 | testing::ValuesIn(kTypeParams)), |
| 3730 | [](const testing::TestParamInfo<std::tuple<Options::Language, TypeParam>>& info) { |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 3731 | return to_string(std::get<0>(info.param)) + "_" + std::get<1>(info.param).kind; |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3732 | }); |
| 3733 | |
Jooyung Han | fd3be32 | 2020-12-13 09:11:06 +0900 | [diff] [blame] | 3734 | TEST_P(AidlTypeParamTest, ListSupportedTypes) { |
| 3735 | Run("List<{}>", kListSupportExpectations); |
| 3736 | } |
Jooyung Han | e87cdd0 | 2020-12-11 16:47:35 +0900 | [diff] [blame] | 3737 | |
Jooyung Han | d236e55 | 2020-12-13 09:21:27 +0900 | [diff] [blame] | 3738 | TEST_P(AidlTypeParamTest, ArraySupportedTypes) { |
| 3739 | Run("{}[]", kArraySupportExpectations); |
| 3740 | } |
| 3741 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 3742 | } // namespace aidl |
| 3743 | } // namespace android |