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 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 17 | #include <memory> |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 18 | #include <set> |
| 19 | #include <string> |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 23 | #include <gmock/gmock.h> |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include "aidl.h" |
Steven Moreland | 4958524 | 2019-12-18 16:06:49 -0800 | [diff] [blame] | 27 | #include "aidl_checkapi.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 28 | #include "aidl_language.h" |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 29 | #include "aidl_to_cpp.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 30 | #include "aidl_to_java.h" |
Jiyong Park | 2a7c92b | 2020-07-22 19:12:36 +0900 | [diff] [blame] | 31 | #include "logging.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 32 | #include "options.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 33 | #include "tests/fake_io_delegate.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 34 | |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 35 | using android::aidl::internals::parse_preprocessed_file; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 36 | using android::aidl::test::FakeIoDelegate; |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 38 | using std::set; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 39 | using std::string; |
| 40 | using std::unique_ptr; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 41 | using std::vector; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 42 | using testing::TestParamInfo; |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 43 | using testing::internal::CaptureStderr; |
| 44 | using testing::internal::GetCapturedStderr; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | namespace aidl { |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | const char kExpectedDepFileContents[] = |
| 51 | R"(place/for/output/p/IFoo.java : \ |
| 52 | p/IFoo.aidl |
| 53 | |
| 54 | p/IFoo.aidl : |
| 55 | )"; |
| 56 | |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 57 | const char kExpectedNinjaDepFileContents[] = |
| 58 | R"(place/for/output/p/IFoo.java : \ |
| 59 | p/IFoo.aidl |
| 60 | )"; |
| 61 | |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 62 | const char kExpectedParcelableDeclarationDepFileContents[] = |
| 63 | R"( : \ |
| 64 | p/Foo.aidl |
| 65 | |
| 66 | p/Foo.aidl : |
| 67 | )"; |
| 68 | |
| 69 | const char kExpectedStructuredParcelableDepFileContents[] = |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 70 | R"(place/for/output/p/Foo.java : \ |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 71 | p/Foo.aidl |
| 72 | |
| 73 | p/Foo.aidl : |
| 74 | )"; |
| 75 | |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 76 | const char kExpectedJavaParcelableOutputContests[] = |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 77 | R"(/* |
| 78 | * This file is auto-generated. DO NOT MODIFY. |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 79 | */ |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 80 | @android.annotation.Hide |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 81 | public class Rect implements android.os.Parcelable |
| 82 | { |
| 83 | // Comment |
| 84 | |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 85 | @android.annotation.Hide |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 86 | public int x = 5; |
| 87 | |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 88 | @android.annotation.Hide |
| 89 | @android.compat.annotation.UnsupportedAppUsage(expectedSignature = "dummy", implicitMember = "dummy", maxTargetSdk = 28, publicAlternatives = "dummy", trackingBug = 42L, overrideSourcePosition="Rect.aidl:7:1:10:14") |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 90 | public int y = 0; |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 91 | |
| 92 | public android.os.ParcelFileDescriptor fd; |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 93 | public static final android.os.Parcelable.Creator<Rect> CREATOR = new android.os.Parcelable.Creator<Rect>() { |
| 94 | @Override |
| 95 | public Rect createFromParcel(android.os.Parcel _aidl_source) { |
| 96 | Rect _aidl_out = new Rect(); |
| 97 | _aidl_out.readFromParcel(_aidl_source); |
| 98 | return _aidl_out; |
| 99 | } |
| 100 | @Override |
| 101 | public Rect[] newArray(int _aidl_size) { |
| 102 | return new Rect[_aidl_size]; |
| 103 | } |
| 104 | }; |
| 105 | @Override public final void writeToParcel(android.os.Parcel _aidl_parcel, int _aidl_flag) |
| 106 | { |
| 107 | int _aidl_start_pos = _aidl_parcel.dataPosition(); |
| 108 | _aidl_parcel.writeInt(0); |
| 109 | _aidl_parcel.writeInt(x); |
| 110 | _aidl_parcel.writeInt(y); |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 111 | if ((fd!=null)) { |
| 112 | _aidl_parcel.writeInt(1); |
| 113 | fd.writeToParcel(_aidl_parcel, 0); |
| 114 | } |
| 115 | else { |
| 116 | _aidl_parcel.writeInt(0); |
| 117 | } |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 118 | int _aidl_end_pos = _aidl_parcel.dataPosition(); |
| 119 | _aidl_parcel.setDataPosition(_aidl_start_pos); |
| 120 | _aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos); |
| 121 | _aidl_parcel.setDataPosition(_aidl_end_pos); |
| 122 | } |
| 123 | public final void readFromParcel(android.os.Parcel _aidl_parcel) |
| 124 | { |
| 125 | int _aidl_start_pos = _aidl_parcel.dataPosition(); |
| 126 | int _aidl_parcelable_size = _aidl_parcel.readInt(); |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 127 | try { |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 128 | if (_aidl_parcelable_size < 0) return; |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 129 | x = _aidl_parcel.readInt(); |
| 130 | if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return; |
| 131 | y = _aidl_parcel.readInt(); |
| 132 | if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return; |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 133 | if ((0!=_aidl_parcel.readInt())) { |
| 134 | fd = android.os.ParcelFileDescriptor.CREATOR.createFromParcel(_aidl_parcel); |
| 135 | } |
| 136 | else { |
| 137 | fd = null; |
| 138 | } |
| 139 | if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return; |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 140 | } finally { |
Jeongik Cha | 8b32998 | 2020-09-01 20:59:36 +0900 | [diff] [blame] | 141 | if (_aidl_start_pos > (Integer.MAX_VALUE - _aidl_parcelable_size)) { |
| 142 | throw new android.os.BadParcelableException("Overflow in the size of parcelable"); |
| 143 | } |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 144 | _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size); |
| 145 | } |
| 146 | } |
| 147 | @Override public int describeContents() |
| 148 | { |
Jeongik Cha | 372a8c8 | 2018-12-12 16:31:11 +0900 | [diff] [blame] | 149 | return 0; |
| 150 | } |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 151 | } |
| 152 | )"; |
| 153 | |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 154 | // clang-format off |
| 155 | const char kExpectedCppHeaderOutput[] = |
| 156 | R"(#pragma once |
| 157 | |
| 158 | #include <binder/IBinder.h> |
| 159 | #include <binder/IInterface.h> |
| 160 | #include <binder/Status.h> |
| 161 | #include <optional> |
| 162 | #include <utils/String16.h> |
| 163 | #include <utils/StrongPointer.h> |
| 164 | #include <vector> |
| 165 | |
| 166 | class IFoo : public ::android::IInterface { |
| 167 | public: |
| 168 | DECLARE_META_INTERFACE(Foo) |
| 169 | virtual ::android::binder::Status foo(::std::optional<::std::vector<::std::optional<::android::String16>>>* _aidl_return) = 0; |
| 170 | }; // class IFoo |
| 171 | class IFooDefault : public IFoo { |
| 172 | public: |
| 173 | ::android::IBinder* onAsBinder() override { |
| 174 | return nullptr; |
| 175 | } |
| 176 | ::android::binder::Status foo(::std::optional<::std::vector<::std::optional<::android::String16>>>*) override { |
| 177 | return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION); |
| 178 | } |
| 179 | }; // class IFooDefault |
| 180 | )"; |
| 181 | // clang-format on |
| 182 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 183 | } // namespace |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 184 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 185 | class AidlTest : public ::testing::TestWithParam<Options::Language> { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 186 | protected: |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 187 | AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_, |
| 188 | Options::Language lang, AidlError* error = nullptr, |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 189 | const vector<string> additional_arguments = {}) { |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 190 | io_delegate_.SetFileContents(path, contents); |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 191 | vector<string> args; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 192 | args.emplace_back("aidl"); |
| 193 | args.emplace_back("--lang=" + Options::LanguageToString(lang)); |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 194 | for (const string& s : additional_arguments) { |
| 195 | args.emplace_back(s); |
| 196 | } |
| 197 | for (const string& f : preprocessed_files_) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 198 | args.emplace_back("--preprocessed=" + f); |
| 199 | } |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 200 | for (const string& i : import_paths_) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 201 | args.emplace_back("--include=" + i); |
| 202 | } |
| 203 | args.emplace_back(path); |
| 204 | Options options = Options::From(args); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 205 | vector<string> imported_files; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 206 | ImportResolver import_resolver{io_delegate_, path, import_paths_, {}}; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 207 | AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl( |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 208 | path, options, io_delegate_, &typenames_, &imported_files); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 209 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 210 | if (error != nullptr) { |
| 211 | *error = actual_error; |
| 212 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 213 | |
| 214 | if (actual_error != AidlError::OK) { |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 218 | const auto& defined_types = typenames_.MainDocument().DefinedTypes(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 219 | EXPECT_EQ(1ul, defined_types.size()); |
| 220 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 221 | return defined_types.front().get(); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 222 | } |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 223 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 224 | Options::Language GetLanguage() { return GetParam(); } |
| 225 | |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 226 | FakeIoDelegate io_delegate_; |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 227 | vector<string> preprocessed_files_; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 228 | set<string> import_paths_; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 229 | AidlTypenames typenames_; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 232 | // Instantiate the AidlTest parameterized suite, calling all of the TEST_P |
| 233 | // tests with each of the supported languages as a parameter. |
| 234 | INSTANTIATE_TEST_SUITE_P(AidlTestSuite, AidlTest, |
| 235 | testing::Values(Options::Language::CPP, Options::Language::JAVA, |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 236 | Options::Language::NDK, Options::Language::RUST), |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 237 | [](const testing::TestParamInfo<Options::Language>& info) { |
| 238 | return Options::LanguageToString(info.param); |
| 239 | }); |
| 240 | |
| 241 | TEST_P(AidlTest, AcceptMissingPackage) { |
| 242 | EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, GetLanguage())); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 245 | TEST_P(AidlTest, EndsInSingleLineComment) { |
| 246 | EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, GetLanguage())); |
Steven Moreland | 9c2988f | 2019-07-17 17:49:10 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 249 | TEST_P(AidlTest, InterfaceRequiresCorrectPath) { |
| 250 | const string expected_stderr = |
| 251 | "ERROR: a/Foo.aidl:1.11-21: IBar should be declared in a file called a/IBar.aidl\n"; |
| 252 | const std::string file_contents = "package a; interface IBar {}"; |
| 253 | CaptureStderr(); |
| 254 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 255 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 256 | } |
| 257 | |
| 258 | TEST_P(AidlTest, ParcelableRequiresCorrectPath) { |
| 259 | const string expected_stderr = |
| 260 | "ERROR: a/Foo.aidl:1.11-21: Bar should be declared in a file called a/Bar.aidl\n"; |
| 261 | const std::string file_contents = "package a; interface Bar {}"; |
| 262 | CaptureStderr(); |
| 263 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 264 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 265 | } |
| 266 | |
| 267 | TEST_P(AidlTest, UnstructuredParcelableRequiresCorrectPath) { |
| 268 | const string expected_stderr = |
| 269 | "ERROR: a/Foo.aidl:1.22-26: Bar should be declared in a file called a/Bar.aidl\n"; |
| 270 | const std::string file_contents = "package a; parcelable Bar cpp_header \"anything.h\";"; |
| 271 | CaptureStderr(); |
| 272 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 273 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 274 | } |
| 275 | |
| 276 | TEST_P(AidlTest, EnumRequiresCorrectPath) { |
| 277 | const string expected_stderr = |
| 278 | "ERROR: a/Foo.aidl:1.16-20: Bar should be declared in a file called a/Bar.aidl\n"; |
| 279 | const std::string file_contents = "package a; enum Bar { A, }"; |
| 280 | CaptureStderr(); |
| 281 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", file_contents, typenames_, GetLanguage())); |
| 282 | EXPECT_EQ(expected_stderr, GetCapturedStderr()) << file_contents; |
| 283 | } |
| 284 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 285 | TEST_P(AidlTest, RejectsArraysOfBinders) { |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 286 | import_paths_.emplace(""); |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 287 | io_delegate_.SetFileContents("bar/IBar.aidl", |
| 288 | "package bar; interface IBar {}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 289 | const string path = "foo/IFoo.aidl"; |
| 290 | const string contents = |
| 291 | "package foo;\n" |
| 292 | "import bar.IBar;\n" |
| 293 | "interface IFoo { void f(in IBar[] input); }"; |
| 294 | const string expected_stderr = "ERROR: foo/IFoo.aidl:3.27-32: Binder type cannot be an array\n"; |
| 295 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 296 | EXPECT_EQ(nullptr, Parse(path, contents, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 297 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 300 | TEST_P(AidlTest, SupportOnlyOutParameters) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 301 | 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] | 302 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, GetLanguage())); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 303 | } |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 304 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 305 | TEST_P(AidlTest, RejectOutParametersForIBinder) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 306 | const string interface_ibinder = "package a; interface IBaz { void f(out IBinder bar); }"; |
| 307 | const string expected_ibinder_stderr = |
| 308 | "ERROR: a/IBaz.aidl:1.47-51: 'out IBinder bar' can only be an in parameter.\n"; |
| 309 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 310 | EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 311 | EXPECT_EQ(expected_ibinder_stderr, GetCapturedStderr()); |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 312 | } |
| 313 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 314 | TEST_P(AidlTest, RejectsOutParametersInOnewayInterface) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 315 | const string oneway_interface = "package a; oneway interface IBar { void f(out int bar); }"; |
| 316 | const string expected_stderr = |
| 317 | "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] | 318 | CaptureStderr(); |
| 319 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage())); |
| 320 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 321 | } |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 322 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 323 | TEST_P(AidlTest, RejectsOutParametersInOnewayMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 324 | 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] | 325 | const string expected_stderr = |
| 326 | "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] | 327 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 328 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 329 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 332 | TEST_P(AidlTest, RejectsOnewayNonVoidReturn) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 333 | const string oneway_method = "package a; interface IFoo { oneway int f(); }"; |
| 334 | const string expected_stderr = |
| 335 | "ERROR: a/IFoo.aidl:1.39-41: oneway method 'f' cannot return a value\n"; |
| 336 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 337 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 338 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 341 | TEST_P(AidlTest, RejectsNullablePrimitive) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 342 | const string oneway_method = "package a; interface IFoo { @nullable int f(); }"; |
| 343 | const string expected_stderr = |
| 344 | "ERROR: a/IFoo.aidl:1.38-42: Primitive type cannot get nullable annotation\n"; |
| 345 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 346 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 347 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 350 | TEST_P(AidlTest, AcceptNullableList) { |
| 351 | const string oneway_method = "package a; interface IFoo { @nullable List<String> f(); }"; |
| 352 | const string expected_stderr = ""; |
| 353 | CaptureStderr(); |
| 354 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
| 355 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 356 | } |
| 357 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 358 | TEST_P(AidlTest, RejectsDuplicatedArgumentNames) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 359 | const string method = "package a; interface IFoo { void f(int a, int a); }"; |
| 360 | const string expected_stderr = |
| 361 | "ERROR: a/IFoo.aidl:1.33-35: method 'f' has duplicate argument name 'a'\n"; |
| 362 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 363 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 364 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | b3cd3c7 | 2018-10-11 12:37:45 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Jeongik Cha | 13066da | 2020-08-06 15:43:19 +0900 | [diff] [blame] | 367 | TEST_P(AidlTest, RejectsDuplicatedFieldNames) { |
| 368 | const string method = "package a; parcelable Foo { int a; String a; }"; |
| 369 | const string expected_stderr = |
| 370 | "ERROR: a/Foo.aidl:1.22-26: The parcelable 'Foo' has duplicate field name 'a'\n"; |
| 371 | CaptureStderr(); |
| 372 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage())); |
| 373 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 374 | } |
| 375 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 376 | TEST_P(AidlTest, RejectsDuplicatedAnnotationParams) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 377 | const string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }"; |
| 378 | const string expected_stderr = "ERROR: a/IFoo.aidl:1.56-62: Trying to redefine parameter foo.\n"; |
| 379 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 380 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 381 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 384 | TEST_P(AidlTest, RejectUnsupportedInterfaceAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 385 | AidlError error; |
| 386 | const string method = "package a; @nullable interface IFoo { int f(); }"; |
| 387 | const string expected_stderr = |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 388 | "ERROR: a/IFoo.aidl:1.21-31: 'nullable' is not a supported annotation for this node. " |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 389 | "It must be one of: Hide, UnsupportedAppUsage, VintfStability, JavaPassthrough, Descriptor\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 390 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 391 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 392 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 393 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 396 | TEST_P(AidlTest, RejectUnsupportedTypeAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 397 | AidlError error; |
| 398 | const string method = "package a; interface IFoo { @JavaOnlyStableParcelable int f(); }"; |
| 399 | const string expected_stderr = |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 400 | "ERROR: a/IFoo.aidl:1.54-58: 'JavaOnlyStableParcelable' is not a supported annotation " |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 401 | "for this node. It must be one of: Hide, UnsupportedAppUsage, nullable, utf8InCpp, JavaPassthrough\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 402 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 403 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 404 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 405 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 408 | TEST_P(AidlTest, RejectUnsupportedParcelableAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 409 | AidlError error; |
| 410 | const string method = "package a; @nullable parcelable IFoo cpp_header \"IFoo.h\";"; |
| 411 | const string expected_stderr = |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 412 | "ERROR: a/IFoo.aidl:1.32-37: 'nullable' is not a supported annotation for this node. " |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 413 | "It must be one of: Hide, JavaOnlyStableParcelable, UnsupportedAppUsage, VintfStability, " |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 414 | "JavaPassthrough, JavaOnlyImmutable\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 415 | CaptureStderr(); |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 416 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 417 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 418 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 421 | TEST_P(AidlTest, RejectUnsupportedParcelableDefineAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 422 | AidlError error; |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 423 | const string method = "package a; @nullable parcelable IFoo { String a; String b; }"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 424 | const string expected_stderr = |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 425 | "ERROR: a/IFoo.aidl:1.32-37: 'nullable' is not a supported annotation for this node. " |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 426 | "It must be one of: Hide, UnsupportedAppUsage, VintfStability, JavaPassthrough, JavaDebug, " |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 427 | "JavaOnlyImmutable, FixedSize, RustDerive\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 428 | CaptureStderr(); |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 429 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 430 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 431 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 434 | TEST_P(AidlTest, ParsesNonNullableAnnotation) { |
| 435 | auto parse_result = |
| 436 | Parse("a/IFoo.aidl", "package a; interface IFoo { String f(); }", typenames_, GetLanguage()); |
| 437 | ASSERT_NE(nullptr, parse_result); |
| 438 | const AidlInterface* interface = parse_result->AsInterface(); |
| 439 | ASSERT_NE(nullptr, interface); |
| 440 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 441 | EXPECT_FALSE(interface->GetMethods()[0]->GetType().IsNullable()); |
| 442 | } |
| 443 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 444 | TEST_P(AidlTest, ParsesNullableAnnotation) { |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 445 | auto parse_result = Parse("a/IFoo.aidl", "package a; interface IFoo { @nullable String f(); }", |
| 446 | typenames_, GetLanguage()); |
| 447 | ASSERT_NE(nullptr, parse_result); |
| 448 | const AidlInterface* interface = parse_result->AsInterface(); |
| 449 | ASSERT_NE(nullptr, interface); |
| 450 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 451 | EXPECT_TRUE(interface->GetMethods()[0]->GetType().IsNullable()); |
| 452 | } |
| 453 | |
| 454 | TEST_P(AidlTest, ParsesNonUtf8Annotations) { |
| 455 | auto parse_result = |
| 456 | Parse("a/IFoo.aidl", "package a; interface IFoo { String f(); }", typenames_, GetLanguage()); |
| 457 | ASSERT_NE(nullptr, parse_result); |
| 458 | const AidlInterface* interface = parse_result->AsInterface(); |
| 459 | ASSERT_NE(nullptr, interface); |
| 460 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 461 | EXPECT_FALSE(interface->GetMethods()[0]->GetType().IsUtf8InCpp()); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 464 | TEST_P(AidlTest, ParsesUtf8Annotations) { |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 465 | auto parse_result = Parse("a/IFoo.aidl", "package a; interface IFoo { @utf8InCpp String f(); }", |
| 466 | typenames_, GetLanguage()); |
| 467 | ASSERT_NE(nullptr, parse_result); |
| 468 | const AidlInterface* interface = parse_result->AsInterface(); |
| 469 | ASSERT_NE(nullptr, interface); |
| 470 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 471 | EXPECT_TRUE(interface->GetMethods()[0]->GetType().IsUtf8InCpp()); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 474 | TEST_P(AidlTest, VintfRequiresStructuredAndStability) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 475 | AidlError error; |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 476 | const string expected_stderr = |
| 477 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: " |
| 478 | "\"vintf\"'\n" |
| 479 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface " |
| 480 | "--structured\n"; |
| 481 | CaptureStderr(); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 482 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 483 | GetLanguage(), &error)); |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 484 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 485 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 488 | TEST_P(AidlTest, VintfRequiresStructured) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 489 | AidlError error; |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 490 | const string expected_stderr = |
| 491 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface " |
| 492 | "--structured\n"; |
| 493 | CaptureStderr(); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 494 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 495 | GetLanguage(), &error, {"--stability", "vintf"})); |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 496 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 497 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 500 | TEST_P(AidlTest, VintfRequiresSpecifiedStability) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 501 | AidlError error; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 502 | const string expected_stderr = |
| 503 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: " |
| 504 | "\"vintf\"'\n"; |
| 505 | CaptureStderr(); |
| 506 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 507 | GetLanguage(), &error, {"--structured"})); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 508 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 509 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 512 | TEST_P(AidlTest, ParsesStabilityAnnotations) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 513 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 514 | auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
| 515 | GetLanguage(), &error, {"--structured", "--stability", "vintf"}); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 516 | ASSERT_EQ(AidlError::OK, error); |
| 517 | ASSERT_NE(nullptr, parse_result); |
| 518 | const AidlInterface* interface = parse_result->AsInterface(); |
| 519 | ASSERT_NE(nullptr, interface); |
| 520 | ASSERT_TRUE(interface->IsVintfStability()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 523 | TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) { |
| 524 | Options java_options = Options::From("aidl -o out --structured a/Foo.aidl"); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 525 | Options cpp_options = Options::From("aidl --lang=cpp -o out -h out/include a/Foo.aidl"); |
| 526 | Options cpp_structured_options = |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 527 | 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] | 528 | 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] | 529 | io_delegate_.SetFileContents( |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 530 | "a/Foo.aidl", |
| 531 | StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo cpp_header \"Foo.h\" ;")); |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 532 | |
| 533 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 534 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 535 | const string expected_stderr = |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 536 | "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] | 537 | "Parcelable must be defined in AIDL directly.\n"; |
| 538 | CaptureStderr(); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 539 | EXPECT_NE(0, ::android::aidl::compile_aidl(cpp_structured_options, io_delegate_)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 540 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 541 | |
| 542 | CaptureStderr(); |
| 543 | EXPECT_NE(0, ::android::aidl::compile_aidl(rust_options, io_delegate_)); |
| 544 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 545 | } |
| 546 | |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 547 | TEST_F(AidlTest, ParsesJavaDebugAnnotation) { |
| 548 | io_delegate_.SetFileContents("a/IFoo.aidl", R"(package a; |
| 549 | @JavaDebug parcelable IFoo { int a; float b; })"); |
| 550 | Options java_options = Options::From("aidl --lang=java -o out a/IFoo.aidl"); |
| 551 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 552 | |
| 553 | string java_out; |
| 554 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/IFoo.java", &java_out)); |
| 555 | EXPECT_THAT(java_out, testing::HasSubstr("public String toString() {")); |
| 556 | |
| 557 | // Other backends shouldn't be bothered |
| 558 | Options cpp_options = Options::From("aidl --lang=cpp -o out -h out a/IFoo.aidl"); |
| 559 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
| 560 | |
| 561 | Options ndk_options = Options::From("aidl --lang=ndk -o out -h out a/IFoo.aidl"); |
| 562 | EXPECT_EQ(0, ::android::aidl::compile_aidl(ndk_options, io_delegate_)); |
| 563 | } |
| 564 | |
| 565 | TEST_F(AidlTest, RejectsJavaDebugAnnotation) { |
| 566 | { |
| 567 | io_delegate_.SetFileContents("a/IFoo.aidl", "package a; @JavaDebug interface IFoo{}"); |
| 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_)); |
| 571 | const std::string expected_stderr = |
| 572 | "ERROR: a/IFoo.aidl:1.22-32: 'JavaDebug' is not a supported annotation for this node. " |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 573 | "It must be one of: Hide, UnsupportedAppUsage, VintfStability, JavaPassthrough, " |
| 574 | "Descriptor\n"; |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 575 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 576 | } |
| 577 | |
| 578 | { |
| 579 | io_delegate_.SetFileContents("a/IFoo.aidl", "package a; @JavaDebug enum IFoo { A=1, }"); |
| 580 | Options java_options = Options::From("aidl --lang=java -o out a/IFoo.aidl"); |
| 581 | CaptureStderr(); |
| 582 | EXPECT_NE(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 583 | const std::string expected_stderr = |
| 584 | "ERROR: a/IFoo.aidl:1.27-32: 'JavaDebug' is not a supported annotation for this node. " |
| 585 | "It must be one of: Backing, Hide, VintfStability, JavaPassthrough\n"; |
| 586 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 587 | } |
| 588 | } |
| 589 | |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 590 | TEST_P(AidlTest, ParseDescriptorAnnotation) { |
| 591 | AidlError error; |
| 592 | auto parse_result = Parse("IFoo.aidl", R"(@Descriptor(value="IBar") interface IFoo{})", |
| 593 | typenames_, GetLanguage(), &error, {"--structured"}); |
| 594 | ASSERT_EQ(AidlError::OK, error); |
| 595 | ASSERT_NE(nullptr, parse_result); |
| 596 | const AidlInterface* interface = parse_result->AsInterface(); |
| 597 | ASSERT_NE(nullptr, interface); |
| 598 | ASSERT_EQ("IBar", interface->GetDescriptor()); |
| 599 | } |
| 600 | |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 601 | TEST_P(AidlTest, AcceptsOnewayMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 602 | 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] | 603 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | TEST_P(AidlTest, AcceptsOnewayInterface) { |
| 607 | 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] | 608 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage())); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 609 | } |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 610 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 611 | TEST_P(AidlTest, AcceptsAnnotatedOnewayMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 612 | const string oneway_method = |
| 613 | "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 614 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Steven Moreland | 65297cc | 2020-04-20 20:17:36 -0700 | [diff] [blame] | 617 | TEST_P(AidlTest, AnnotationsInMultiplePlaces) { |
| 618 | const string oneway_method = |
| 619 | "package a; interface IFoo { @UnsupportedAppUsage oneway @Hide void f(int a); }"; |
| 620 | const AidlDefinedType* defined = Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage()); |
| 621 | ASSERT_NE(nullptr, defined); |
| 622 | const AidlInterface* iface = defined->AsInterface(); |
| 623 | ASSERT_NE(nullptr, iface); |
| 624 | |
| 625 | const auto& methods = iface->GetMethods(); |
| 626 | ASSERT_EQ(1u, methods.size()); |
| 627 | const auto& method = methods[0]; |
| 628 | const AidlTypeSpecifier& ret_type = method->GetType(); |
| 629 | |
| 630 | // TODO(b/151102494): these annotations should be on the method |
| 631 | ASSERT_NE(nullptr, ret_type.UnsupportedAppUsage()); |
| 632 | ASSERT_TRUE(ret_type.IsHide()); |
| 633 | } |
| 634 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 635 | TEST_P(AidlTest, WritesComments) { |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 636 | string foo_interface = |
| 637 | "package a; /* foo */ interface IFoo {" |
| 638 | " /* i */ int i();" |
| 639 | " /* j */ @nullable String j();" |
| 640 | " /* k */ @UnsupportedAppUsage oneway void k(int a); }"; |
| 641 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 642 | auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, GetLanguage()); |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 643 | EXPECT_NE(nullptr, parse_result); |
| 644 | EXPECT_EQ("/* foo */", parse_result->GetComments()); |
| 645 | |
| 646 | const AidlInterface* interface = parse_result->AsInterface(); |
| 647 | EXPECT_EQ("/* i */", interface->GetMethods()[0]->GetComments()); |
| 648 | EXPECT_EQ("/* j */", interface->GetMethods()[1]->GetComments()); |
| 649 | EXPECT_EQ("/* k */", interface->GetMethods()[2]->GetComments()); |
| 650 | } |
| 651 | |
Jooyung Han | 1a551d3 | 2020-10-05 15:37:26 +0900 | [diff] [blame] | 652 | TEST_P(AidlTest, CppHeaderCanBeIdentifierAsWell) { |
| 653 | io_delegate_.SetFileContents("p/cpp_header.aidl", |
| 654 | R"(package p; |
| 655 | parcelable cpp_header cpp_header "bar/header";)"); |
| 656 | import_paths_.emplace(""); |
| 657 | const string input_path = "p/IFoo.aidl"; |
| 658 | const string input = R"(package p; |
| 659 | import p.cpp_header; |
| 660 | interface IFoo { |
| 661 | // get bar |
| 662 | cpp_header get(); |
| 663 | })"; |
| 664 | |
| 665 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 666 | EXPECT_NE(nullptr, parse_result); |
| 667 | const AidlInterface* interface = parse_result->AsInterface(); |
| 668 | EXPECT_EQ("// get bar\n", interface->GetMethods()[0]->GetComments()); |
| 669 | } |
| 670 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 671 | TEST_F(AidlTest, ParsesPreprocessedFile) { |
| 672 | string simple_content = "parcelable a.Foo;\ninterface b.IBar;"; |
| 673 | io_delegate_.SetFileContents("path", simple_content); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 674 | EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 675 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_)); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 676 | EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved); |
| 677 | EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) { |
| 681 | string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t"; |
| 682 | io_delegate_.SetFileContents("path", simple_content); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 683 | |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 684 | EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 685 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_)); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 686 | EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved); |
| 687 | EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 690 | TEST_P(AidlTest, PreferImportToPreprocessed) { |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 691 | io_delegate_.SetFileContents("preprocessed", "interface another.IBar;"); |
| 692 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; " |
| 693 | "interface IBar {}"); |
| 694 | preprocessed_files_.push_back("preprocessed"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 695 | import_paths_.emplace(""); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 696 | 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] | 697 | typenames_, GetLanguage()); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 698 | EXPECT_NE(nullptr, parse_result); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 699 | |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 700 | // We expect to know about both kinds of IBar |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 701 | EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved); |
| 702 | EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 703 | // But if we request just "IBar" we should get our imported one. |
Steven Moreland | 02e012e | 2018-08-02 14:58:10 -0700 | [diff] [blame] | 704 | AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, ""); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 705 | ambiguous_type.Resolve(typenames_); |
| 706 | EXPECT_EQ("one.IBar", ambiguous_type.GetName()); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 709 | // Special case of PreferImportToPreprocessed. Imported type should be preferred |
| 710 | // even when the preprocessed file already has the same type. |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 711 | TEST_P(AidlTest, B147918827) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 712 | io_delegate_.SetFileContents("preprocessed", "interface another.IBar;\ninterface one.IBar;"); |
| 713 | io_delegate_.SetFileContents("one/IBar.aidl", |
| 714 | "package one; " |
| 715 | "interface IBar {}"); |
| 716 | preprocessed_files_.push_back("preprocessed"); |
| 717 | import_paths_.emplace(""); |
| 718 | 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] | 719 | typenames_, GetLanguage()); |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 720 | EXPECT_NE(nullptr, parse_result); |
| 721 | |
| 722 | // We expect to know about both kinds of IBar |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 723 | EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved); |
| 724 | EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved); |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 725 | // But if we request just "IBar" we should get our imported one. |
| 726 | AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, ""); |
| 727 | ambiguous_type.Resolve(typenames_); |
| 728 | EXPECT_EQ("one.IBar", ambiguous_type.GetName()); |
| 729 | } |
| 730 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 731 | TEST_F(AidlTest, WritePreprocessedFile) { |
| 732 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 733 | "package p; parcelable Outer.Inner;"); |
| 734 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;" |
| 735 | "interface IBar {}"); |
| 736 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 737 | vector<string> args { |
| 738 | "aidl", |
| 739 | "--preprocess", |
| 740 | "preprocessed", |
| 741 | "p/Outer.aidl", |
| 742 | "one/IBar.aidl"}; |
| 743 | Options options = Options::From(args); |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 744 | EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_)); |
| 745 | |
| 746 | string output; |
| 747 | EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output)); |
| 748 | EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output); |
| 749 | } |
| 750 | |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 751 | TEST_F(AidlTest, JavaParcelableOutput) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 752 | io_delegate_.SetFileContents( |
| 753 | "Rect.aidl", |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 754 | "@Hide\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 755 | "parcelable Rect {\n" |
| 756 | " // Comment\n" |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 757 | " @Hide\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 758 | " int x=5;\n" |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 759 | " @Hide\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 760 | " @UnsupportedAppUsage(maxTargetSdk = 28, trackingBug = 42, implicitMember = \"dummy\", " |
Andrei Onea | 07fa3e0 | 2019-07-04 17:55:19 +0100 | [diff] [blame] | 761 | "expectedSignature = \"dummy\", publicAlternatives = \"d\" \n + \"u\" + \n \"m\" \n + \"m\" " |
| 762 | "+ \"y\")\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 763 | " int y;\n" |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 764 | " ParcelFileDescriptor fd;\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 765 | "}"); |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 766 | |
| 767 | vector<string> args{"aidl", "Rect.aidl"}; |
| 768 | Options options = Options::From(args); |
| 769 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 770 | |
| 771 | string output; |
| 772 | EXPECT_TRUE(io_delegate_.GetWrittenContents("Rect.java", &output)); |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 773 | EXPECT_EQ(kExpectedJavaParcelableOutputContests, output); |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 774 | } |
| 775 | |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 776 | TEST_F(AidlTest, CppHeaderIncludes) { |
| 777 | io_delegate_.SetFileContents("IFoo.aidl", |
| 778 | "interface IFoo {\n" |
| 779 | " @nullable List<String> foo();\n" |
| 780 | "}"); |
| 781 | |
| 782 | vector<string> args{"aidl", "--lang=cpp", "IFoo.aidl"}; |
| 783 | Options options = Options::From(args); |
| 784 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 785 | std::vector<std::string> empty; |
| 786 | EXPECT_EQ(empty, io_delegate_.ListFiles("")); |
| 787 | EXPECT_EQ(empty, io_delegate_.ListFiles("/")); |
| 788 | EXPECT_EQ(empty, io_delegate_.ListFiles("out")); |
| 789 | |
| 790 | // Make sure the optional and String16.h includes are added |
| 791 | string output; |
| 792 | EXPECT_TRUE(io_delegate_.GetWrittenContents("IFoo.h", &output)); |
| 793 | EXPECT_EQ(kExpectedCppHeaderOutput, output); |
| 794 | } |
| 795 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 796 | TEST_P(AidlTest, RequireOuterClass) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 797 | 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] | 798 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 799 | "package p; parcelable Outer.Inner;"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 800 | import_paths_.emplace(""); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 801 | CaptureStderr(); |
| 802 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 803 | "package p; import p.Outer; interface IFoo { void f(in Inner c); }", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 804 | typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 805 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 808 | TEST_P(AidlTest, ParseCompoundParcelableFromPreprocess) { |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 809 | io_delegate_.SetFileContents("preprocessed", |
| 810 | "parcelable p.Outer.Inner;"); |
| 811 | preprocessed_files_.push_back("preprocessed"); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 812 | 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] | 813 | typenames_, GetLanguage()); |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 814 | // TODO(wiley): This should actually return nullptr because we require |
| 815 | // the outer class name. However, for legacy reasons, |
| 816 | // this behavior must be maintained. b/17415692 |
| 817 | EXPECT_NE(nullptr, parse_result); |
| 818 | } |
| 819 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 820 | TEST_F(AidlTest, FailOnParcelable) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 821 | const string expected_foo_stderr = |
| 822 | "ERROR: p/IFoo.aidl:1.22-27: Refusing to generate code with unstructured parcelables. " |
| 823 | "Declared parcelables should be in their own file and/or cannot be used with --structured " |
| 824 | "interfaces.\n"; |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 825 | io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;"); |
| 826 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 827 | // By default, we shouldn't fail on parcelable. |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 828 | Options options1 = Options::From("aidl p/IFoo.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 829 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 830 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 831 | EXPECT_EQ("", GetCapturedStderr()); |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 832 | |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 833 | // -b considers this an error |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 834 | Options options2 = Options::From("aidl -b p/IFoo.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 835 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 836 | EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 837 | EXPECT_EQ(expected_foo_stderr, GetCapturedStderr()); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 838 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 839 | const string expected_bar_stderr = |
| 840 | "ERROR: p/IBar.aidl:1.22-26: Refusing to generate code with unstructured parcelables. " |
| 841 | "Declared parcelables should be in their own file and/or cannot be used with --structured " |
| 842 | "interfaces.\n"; |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 843 | io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}"); |
| 844 | |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 845 | // With '-b' option, a parcelable and an interface should fail. |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 846 | Options options3 = Options::From("aidl p/IBar.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 847 | CaptureStderr(); |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 848 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 849 | EXPECT_EQ("", GetCapturedStderr()); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 850 | Options options4 = Options::From("aidl -b p/IBar.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 851 | CaptureStderr(); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 852 | EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 853 | EXPECT_EQ(expected_bar_stderr, GetCapturedStderr()); |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 856 | TEST_P(AidlTest, StructuredFailOnUnstructuredParcelable) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 857 | const string expected_stderr = |
| 858 | "ERROR: ./o/WhoKnowsWhat.aidl:1.22-35: o.WhoKnowsWhat is not structured, but this is a " |
| 859 | "structured interface.\n"; |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 860 | io_delegate_.SetFileContents("o/WhoKnowsWhat.aidl", "package o; parcelable WhoKnowsWhat;"); |
| 861 | import_paths_.emplace(""); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 862 | AidlError error; |
| 863 | CaptureStderr(); |
| 864 | EXPECT_EQ( |
| 865 | nullptr, |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 866 | Parse("p/IFoo.aidl", |
| 867 | "package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 868 | typenames_, GetLanguage(), &error, {"--structured"})); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 869 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 870 | EXPECT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 873 | TEST_P(AidlTest, FailOnDuplicateConstantNames) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 874 | AidlError error; |
| 875 | const string expected_stderr = |
| 876 | "ERROR: p/IFoo.aidl:4.34-45: Found duplicate constant name 'DUPLICATED'\n"; |
| 877 | CaptureStderr(); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 878 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 879 | R"(package p; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 880 | interface IFoo { |
| 881 | const String DUPLICATED = "d"; |
| 882 | const int DUPLICATED = 1; |
| 883 | } |
| 884 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 885 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 886 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 887 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 890 | TEST_P(AidlTest, FailOnManyDefinedTypes) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 891 | AidlError error; |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 892 | const string expected_stderr = |
| 893 | "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] | 894 | CaptureStderr(); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 895 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 896 | R"(package p; |
| 897 | interface IFoo {} |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 898 | parcelable IBar {} |
| 899 | parcelable StructuredParcelable {} |
| 900 | interface IBaz {} |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 901 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 902 | typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 903 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 904 | // Parse success is important for clear error handling even if the cases aren't |
| 905 | // actually supported in code generation. |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 906 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 909 | TEST_P(AidlTest, FailOnNoDefinedTypes) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 910 | AidlError error; |
| 911 | const string expected_stderr = "ERROR: p/IFoo.aidl:1.11-11: syntax error, unexpected $end\n"; |
| 912 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 913 | 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] | 914 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 915 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Steven Moreland | f9e922f | 2020-07-08 21:15:27 +0000 | [diff] [blame] | 918 | TEST_P(AidlTest, FailOnEmptyListWithComma) { |
| 919 | AidlError error; |
| 920 | const string expected_stderr = |
| 921 | "ERROR: p/Foo.aidl:1.45-47: syntax error, unexpected ',', expecting '}'\n"; |
| 922 | CaptureStderr(); |
| 923 | EXPECT_EQ(nullptr, Parse("p/Foo.aidl", R"(package p; parcelable Foo { uint64_t[] a = { , }; })", |
| 924 | typenames_, GetLanguage(), &error)); |
| 925 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 926 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
| 927 | } |
| 928 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 929 | TEST_P(AidlTest, FailOnMalformedConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 930 | AidlError error; |
| 931 | const string expected_stderr = |
Devin Moore | 2a08890 | 2020-09-17 10:51:19 -0700 | [diff] [blame] | 932 | "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] | 933 | CaptureStderr(); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 934 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 935 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 936 | interface IFoo { |
| 937 | const int BAD_HEX_VALUE = 0xffffffffffffffffff; |
| 938 | } |
| 939 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 940 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 941 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 942 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 945 | TEST_P(AidlTest, FailOnMalformedQualifiedNameAsIdentifier) { |
| 946 | AidlError error; |
| 947 | const string expected_stderr = |
| 948 | "ERROR: p/IFoo.aidl:1.25-26: syntax error, unexpected ';', expecting identifier or " |
| 949 | "cpp_header (which can also be used as an identifier)\n"; |
| 950 | CaptureStderr(); |
| 951 | // Notice the trailing dot(.) in the name, which isn't a correct name |
| 952 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p; parcelable A.; )", typenames_, |
| 953 | GetLanguage(), &error)); |
| 954 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 955 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
| 956 | } |
| 957 | |
| 958 | TEST_P(AidlTest, FailOnMalformedQualifiedNameAsPackage) { |
| 959 | AidlError error; |
| 960 | const string expected_stderr = |
| 961 | "ERROR: p/IFoo.aidl:1.11-12: syntax error, unexpected ';', expecting identifier or " |
| 962 | "cpp_header (which can also be used as an identifier)\n"; |
| 963 | CaptureStderr(); |
| 964 | // Notice the trailing dot(.) in the package name |
| 965 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p.; parcelable A; )", typenames_, |
| 966 | GetLanguage(), &error)); |
| 967 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 968 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
| 969 | } |
| 970 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 971 | TEST_P(AidlTest, ParsePositiveConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 972 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 973 | auto parse_result = Parse("p/IFoo.aidl", |
| 974 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 975 | interface IFoo { |
| 976 | const int POSITIVE_HEX_VALUE = 0xf5; |
| 977 | } |
| 978 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 979 | typenames_, GetLanguage(), &error); |
| 980 | EXPECT_NE(nullptr, parse_result); |
| 981 | const AidlInterface* interface = parse_result->AsInterface(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 982 | ASSERT_NE(nullptr, interface); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 983 | const auto& cpp_constants = interface->GetConstantDeclarations(); |
| 984 | EXPECT_EQ((size_t)1, cpp_constants.size()); |
| 985 | EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_constants[0]->GetName()); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 986 | EXPECT_TRUE(cpp_constants[0]->CheckValid(typenames_)); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 987 | EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator)); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 990 | TEST_P(AidlTest, ParseNegativeConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 991 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 992 | auto parse_result = Parse("p/IFoo.aidl", |
| 993 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 994 | interface IFoo { |
| 995 | const int NEGATIVE_HEX_VALUE = 0xffffffff; |
| 996 | } |
| 997 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 998 | typenames_, GetLanguage(), &error); |
| 999 | ASSERT_NE(nullptr, parse_result); |
| 1000 | const AidlInterface* interface = parse_result->AsInterface(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1001 | ASSERT_NE(nullptr, interface); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1002 | const auto& cpp_constants = interface->GetConstantDeclarations(); |
| 1003 | EXPECT_EQ((size_t)1, cpp_constants.size()); |
| 1004 | EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_constants[0]->GetName()); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1005 | EXPECT_EQ(true, cpp_constants[0]->CheckValid(typenames_)); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1006 | EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator)); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1009 | TEST_P(AidlTest, UnderstandsNestedParcelables) { |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1010 | io_delegate_.SetFileContents( |
| 1011 | "p/Outer.aidl", |
| 1012 | "package p; parcelable Outer.Inner cpp_header \"baz/header\";"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 1013 | import_paths_.emplace(""); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1014 | const string input_path = "p/IFoo.aidl"; |
| 1015 | const string input = "package p; import p.Outer; interface IFoo" |
| 1016 | " { Outer.Inner get(); }"; |
| 1017 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1018 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 1019 | EXPECT_NE(nullptr, parse_result); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1020 | |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 1021 | EXPECT_TRUE(typenames_.ResolveTypename("p.Outer.Inner").is_resolved); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1022 | // C++ uses "::" instead of "." to refer to a inner class. |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1023 | AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, ""); |
| 1024 | EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_)); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1027 | TEST_P(AidlTest, UnderstandsNativeParcelables) { |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 1028 | io_delegate_.SetFileContents( |
| 1029 | "p/Bar.aidl", |
Casey Dahlin | cd63921 | 2015-12-15 12:51:04 -0800 | [diff] [blame] | 1030 | "package p; parcelable Bar cpp_header \"baz/header\";"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 1031 | import_paths_.emplace(""); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 1032 | const string input_path = "p/IFoo.aidl"; |
| 1033 | const string input = "package p; import p.Bar; interface IFoo { }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1034 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 1035 | EXPECT_NE(nullptr, parse_result); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 1036 | EXPECT_TRUE(typenames_.ResolveTypename("p.Bar").is_resolved); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1037 | AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, ""); |
| 1038 | native_type.Resolve(typenames_); |
| 1039 | |
| 1040 | EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type, typenames_)); |
| 1041 | // C++ understands C++ specific stuff |
| 1042 | EXPECT_EQ("::p::Bar", cpp::CppNameOf(native_type, typenames_)); |
| 1043 | set<string> headers; |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 1044 | cpp::AddHeaders(native_type, typenames_, &headers); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1045 | EXPECT_EQ(1u, headers.size()); |
| 1046 | EXPECT_EQ(1u, headers.count("baz/header")); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 1049 | TEST_F(AidlTest, WritesCorrectDependencyFile) { |
| 1050 | // While the in tree build system always gives us an output file name, |
| 1051 | // other android tools take advantage of our ability to infer the intended |
| 1052 | // file name. This test makes sure we handle this correctly. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1053 | vector<string> args = { |
| 1054 | "aidl", |
| 1055 | "-d dep/file/path", |
| 1056 | "-o place/for/output", |
| 1057 | "p/IFoo.aidl"}; |
| 1058 | Options options = Options::From(args); |
| 1059 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1060 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 1061 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1062 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 1063 | EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents); |
| 1064 | } |
| 1065 | |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 1066 | TEST_F(AidlTest, WritesCorrectDependencyFileNinja) { |
| 1067 | // While the in tree build system always gives us an output file name, |
| 1068 | // other android tools take advantage of our ability to infer the intended |
| 1069 | // file name. This test makes sure we handle this correctly. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1070 | vector<string> args = { |
| 1071 | "aidl", |
| 1072 | "-d dep/file/path", |
| 1073 | "--ninja", |
| 1074 | "-o place/for/output", |
| 1075 | "p/IFoo.aidl"}; |
| 1076 | Options options = Options::From(args); |
| 1077 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1078 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 1079 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1080 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 1081 | EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents); |
| 1082 | } |
| 1083 | |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 1084 | TEST_F(AidlTest, WritesTrivialDependencyFileForParcelableDeclaration) { |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 1085 | // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does |
| 1086 | // this by calling aidl with every .aidl file it finds, then parsing the |
| 1087 | // generated dependency files. Those that reference .java output files are |
| 1088 | // for interfaces and those that do not are parcelables. However, for both |
| 1089 | // parcelables and interfaces, we *must* generate a non-empty dependency file. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1090 | vector<string> args = { |
| 1091 | "aidl", |
| 1092 | "-o place/for/output", |
| 1093 | "-d dep/file/path", |
| 1094 | "p/Foo.aidl"}; |
| 1095 | Options options = Options::From(args); |
| 1096 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1097 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 1098 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1099 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 1100 | EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDeclarationDepFileContents); |
| 1101 | } |
| 1102 | |
| 1103 | TEST_F(AidlTest, WritesDependencyFileForStructuredParcelable) { |
| 1104 | vector<string> args = { |
| 1105 | "aidl", |
| 1106 | "--structured", |
| 1107 | "-o place/for/output", |
| 1108 | "-d dep/file/path", |
| 1109 | "p/Foo.aidl"}; |
| 1110 | Options options = Options::From(args); |
| 1111 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo {int a;}"); |
| 1112 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1113 | string actual_dep_file_contents; |
| 1114 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
| 1115 | EXPECT_EQ(actual_dep_file_contents, kExpectedStructuredParcelableDepFileContents); |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Jiyong Park | 9ca5c7e | 2019-10-17 15:01:14 +0900 | [diff] [blame] | 1118 | TEST_F(AidlTest, NoJavaOutputForParcelableDeclaration) { |
| 1119 | vector<string> args = { |
| 1120 | "aidl", |
| 1121 | "--lang=java", |
| 1122 | "-o place/for/output", |
| 1123 | "p/Foo.aidl"}; |
| 1124 | Options options = Options::From(args); |
| 1125 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;"); |
| 1126 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1127 | string output_file_contents; |
| 1128 | EXPECT_FALSE(io_delegate_.GetWrittenContents(options.OutputFile(), &output_file_contents)); |
| 1129 | } |
| 1130 | |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 1131 | // TODO(b/136048684) |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1132 | TEST_P(AidlTest, PrimitiveList) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1133 | const string primitive_interface = |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 1134 | "package a; interface IFoo {\n" |
| 1135 | " List<int> foo(); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1136 | string expected_stderr; |
| 1137 | switch (GetLanguage()) { |
| 1138 | case Options::Language::CPP: |
| 1139 | expected_stderr = |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1140 | "ERROR: a/IFoo.aidl:2.1-7: A generic type cannot have any primitive type parameters.\n" |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1141 | "ERROR: a/IFoo.aidl:2.1-7: List<int> is not supported. List in cpp supports only " |
| 1142 | "String and IBinder.\n"; |
| 1143 | break; |
| 1144 | case Options::Language::JAVA: |
| 1145 | expected_stderr = |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1146 | "ERROR: a/IFoo.aidl:2.1-7: A generic type cannot have any primitive type parameters.\n" |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1147 | "ERROR: a/IFoo.aidl:2.1-7: List<int> is not supported. List in Java supports only " |
| 1148 | "String, IBinder, and ParcelFileDescriptor.\n"; |
| 1149 | break; |
| 1150 | case Options::Language::NDK: |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 1151 | case Options::Language::RUST: |
| 1152 | expected_stderr = |
| 1153 | "ERROR: a/IFoo.aidl:2.1-7: A generic type cannot have any primitive type parameters.\n"; |
| 1154 | break; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1155 | default: |
| 1156 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 1157 | << "Unexpected Options::Language enumerator: " << static_cast<size_t>(GetLanguage()); |
| 1158 | } |
| 1159 | CaptureStderr(); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 1160 | AidlTypenames tn1; |
| 1161 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, tn1, GetLanguage())); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1162 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1163 | |
| 1164 | string primitive_parcelable = |
| 1165 | "package a; parcelable IFoo {\n" |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 1166 | " List<int> foo;}"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1167 | CaptureStderr(); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 1168 | AidlTypenames tn2; |
| 1169 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_parcelable, tn2, GetLanguage())); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1170 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 1171 | } |
| 1172 | |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 1173 | TEST_P(AidlTest, RejectsListArray) { |
Devin Moore | 6a01ca1 | 2020-08-28 10:24:19 -0700 | [diff] [blame] | 1174 | 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] | 1175 | const string list_array_parcelable = |
| 1176 | "package a; parcelable Foo {\n" |
| 1177 | " List[] lists; }"; |
| 1178 | CaptureStderr(); |
| 1179 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", list_array_parcelable, typenames_, GetLanguage())); |
| 1180 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1181 | } |
| 1182 | |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1183 | TEST_P(AidlTest, RejectsPrimitiveListInStableAidl) { |
| 1184 | AidlError error; |
| 1185 | string expected_stderr = |
| 1186 | "ERROR: a/IFoo.aidl:2.7-11: " |
| 1187 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 1188 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 1189 | "the receiving side. Consider switching to an array or a generic List/Map.\n"; |
| 1190 | if (GetLanguage() != Options::Language::JAVA) { |
| 1191 | expected_stderr = |
| 1192 | "ERROR: a/IFoo.aidl:2.1-7: " |
| 1193 | "Currently, only the Java backend supports non-generic List.\n"; |
| 1194 | } |
| 1195 | |
| 1196 | const string primitive_interface = |
| 1197 | "package a; interface IFoo {\n" |
| 1198 | " List foo(); }"; |
| 1199 | CaptureStderr(); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 1200 | AidlTypenames tn1; |
| 1201 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, tn1, GetLanguage(), &error, |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1202 | {"--structured"})); |
| 1203 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1204 | |
| 1205 | string primitive_parcelable = |
| 1206 | "package a; parcelable IFoo {\n" |
| 1207 | " List foo;}"; |
| 1208 | CaptureStderr(); |
Jiyong Park | 40782d0 | 2020-07-24 19:17:43 +0900 | [diff] [blame] | 1209 | AidlTypenames tn2; |
| 1210 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_parcelable, tn2, GetLanguage(), &error, |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1211 | {"--structured"})); |
| 1212 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1213 | } |
| 1214 | |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1215 | TEST_P(AidlTest, ExtensionTest) { |
| 1216 | CaptureStderr(); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1217 | string extendable_parcelable = |
| 1218 | "package a; parcelable Data {\n" |
| 1219 | " ParcelableHolder extension;\n" |
| 1220 | " ParcelableHolder extension2;\n" |
| 1221 | "}"; |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1222 | if (GetLanguage() == Options::Language::NDK || GetLanguage() == Options::Language::RUST) { |
| 1223 | EXPECT_EQ(nullptr, Parse("a/Data.aidl", extendable_parcelable, typenames_, GetLanguage())); |
| 1224 | EXPECT_EQ( |
| 1225 | "ERROR: a/Data.aidl:2.1-19: The NDK and Rust backend does not support ParcelableHolder " |
| 1226 | "yet.\n", |
| 1227 | GetCapturedStderr()); |
| 1228 | } else { |
| 1229 | EXPECT_NE(nullptr, Parse("a/Data.aidl", extendable_parcelable, typenames_, GetLanguage())); |
| 1230 | EXPECT_EQ("", GetCapturedStderr()); |
| 1231 | } |
| 1232 | } |
| 1233 | TEST_P(AidlTest, ParcelableHolderAsReturnType) { |
| 1234 | CaptureStderr(); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1235 | string parcelableholder_return_interface = |
| 1236 | "package a; interface IFoo {\n" |
| 1237 | " ParcelableHolder foo();\n" |
| 1238 | "}"; |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1239 | EXPECT_EQ(nullptr, |
| 1240 | Parse("a/IFoo.aidl", parcelableholder_return_interface, typenames_, GetLanguage())); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1241 | |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1242 | if (GetLanguage() == Options::Language::NDK || GetLanguage() == Options::Language::RUST) { |
| 1243 | EXPECT_EQ( |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1244 | "ERROR: a/IFoo.aidl:2.19-23: ParcelableHolder cannot be a return type\n" |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1245 | "ERROR: a/IFoo.aidl:2.1-19: The NDK and Rust backend does not support ParcelableHolder " |
| 1246 | "yet.\n", |
| 1247 | GetCapturedStderr()); |
| 1248 | return; |
| 1249 | } |
| 1250 | EXPECT_EQ("ERROR: a/IFoo.aidl:2.19-23: ParcelableHolder cannot be a return type\n", |
| 1251 | GetCapturedStderr()); |
| 1252 | } |
| 1253 | |
| 1254 | TEST_P(AidlTest, ParcelableHolderAsArgumentType) { |
| 1255 | CaptureStderr(); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1256 | string extendable_parcelable_arg_interface = |
| 1257 | "package a; interface IFoo {\n" |
| 1258 | " void foo(in ParcelableHolder ph);\n" |
| 1259 | "}"; |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1260 | EXPECT_EQ(nullptr, |
| 1261 | Parse("a/IFoo.aidl", extendable_parcelable_arg_interface, typenames_, GetLanguage())); |
| 1262 | |
| 1263 | if (GetLanguage() == Options::Language::NDK || GetLanguage() == Options::Language::RUST) { |
| 1264 | EXPECT_EQ( |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1265 | "ERROR: a/IFoo.aidl:2.31-34: ParcelableHolder cannot be an argument type\n" |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 1266 | "ERROR: a/IFoo.aidl:2.14-31: The NDK and Rust backend does not support ParcelableHolder " |
| 1267 | "yet.\n", |
| 1268 | GetCapturedStderr()); |
| 1269 | return; |
| 1270 | } |
| 1271 | EXPECT_EQ("ERROR: a/IFoo.aidl:2.31-34: ParcelableHolder cannot be an argument type\n", |
| 1272 | GetCapturedStderr()); |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1273 | } |
| 1274 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1275 | TEST_F(AidlTest, ApiDump) { |
| 1276 | io_delegate_.SetFileContents( |
| 1277 | "foo/bar/IFoo.aidl", |
| 1278 | "package foo.bar;\n" |
| 1279 | "import foo.bar.Data;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1280 | "// comment @hide\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1281 | "interface IFoo {\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1282 | " /* @hide */\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1283 | " int foo(out int[] a, String b, boolean c, inout List<String> d);\n" |
| 1284 | " int foo2(@utf8InCpp String x, inout List<String> y);\n" |
| 1285 | " IFoo foo3(IFoo foo);\n" |
| 1286 | " Data getData();\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1287 | " // @hide\n" |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1288 | " const int A = 1;\n" |
| 1289 | " const String STR = \"Hello\";\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1290 | "}\n"); |
| 1291 | io_delegate_.SetFileContents("foo/bar/Data.aidl", |
| 1292 | "package foo.bar;\n" |
| 1293 | "import foo.bar.IFoo;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1294 | "/* @hide*/\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1295 | "parcelable Data {\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1296 | " // @hide\n" |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 1297 | " int x = 10;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1298 | " // @hide\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1299 | " int y;\n" |
| 1300 | " IFoo foo;\n" |
| 1301 | " List<IFoo> a;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1302 | " /*@hide2*/\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1303 | " List<foo.bar.IFoo> b;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1304 | " // It should be @hide property\n" |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1305 | " @nullable String[] c;\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1306 | "}\n"); |
| 1307 | io_delegate_.SetFileContents("api.aidl", ""); |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1308 | vector<string> args = {"aidl", "--dumpapi", "--out=dump", "--include=.", |
| 1309 | "foo/bar/IFoo.aidl", "foo/bar/Data.aidl"}; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1310 | Options options = Options::From(args); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1311 | bool result = dump_api(options, io_delegate_); |
| 1312 | ASSERT_TRUE(result); |
| 1313 | string actual; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1314 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1315 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1316 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1317 | interface IFoo { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1318 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1319 | int foo(out int[] a, String b, boolean c, inout List<String> d); |
| 1320 | int foo2(@utf8InCpp String x, inout List<String> y); |
| 1321 | foo.bar.IFoo foo3(foo.bar.IFoo foo); |
| 1322 | foo.bar.Data getData(); |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1323 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1324 | const int A = 1; |
| 1325 | const String STR = "Hello"; |
| 1326 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1327 | )")); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1328 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1329 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1330 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1331 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1332 | parcelable Data { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1333 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1334 | int x = 10; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1335 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1336 | int y; |
| 1337 | foo.bar.IFoo foo; |
| 1338 | List<foo.bar.IFoo> a; |
| 1339 | List<foo.bar.IFoo> b; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1340 | /* @hide */ |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1341 | @nullable String[] c; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1342 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1343 | )")); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1344 | } |
| 1345 | |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1346 | TEST_F(AidlTest, ApiDumpWithManualIds) { |
| 1347 | io_delegate_.SetFileContents( |
| 1348 | "foo/bar/IFoo.aidl", |
| 1349 | "package foo.bar;\n" |
| 1350 | "interface IFoo {\n" |
| 1351 | " int foo() = 1;\n" |
| 1352 | " int bar() = 2;\n" |
| 1353 | " int baz() = 10;\n" |
| 1354 | "}\n"); |
| 1355 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1356 | vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"}; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1357 | Options options = Options::From(args); |
| 1358 | bool result = dump_api(options, io_delegate_); |
| 1359 | ASSERT_TRUE(result); |
| 1360 | string actual; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1361 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1362 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1363 | interface IFoo { |
| 1364 | int foo() = 1; |
| 1365 | int bar() = 2; |
| 1366 | int baz() = 10; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1367 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1368 | )")); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | TEST_F(AidlTest, ApiDumpWithManualIdsOnlyOnSomeMethods) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1372 | const string expected_stderr = |
| 1373 | "ERROR: foo/bar/IFoo.aidl:4.8-12: You must either assign id's to all methods or to none of " |
| 1374 | "them.\n"; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1375 | io_delegate_.SetFileContents( |
| 1376 | "foo/bar/IFoo.aidl", |
| 1377 | "package foo.bar;\n" |
| 1378 | "interface IFoo {\n" |
| 1379 | " int foo() = 1;\n" |
| 1380 | " int bar();\n" |
| 1381 | " int baz() = 10;\n" |
| 1382 | "}\n"); |
| 1383 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1384 | vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"}; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1385 | Options options = Options::From(args); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1386 | CaptureStderr(); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1387 | EXPECT_FALSE(dump_api(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1388 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1389 | } |
| 1390 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1391 | TEST_F(AidlTest, CheckNumGenericTypeSecifier) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1392 | const string expected_list_stderr = |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 1393 | "ERROR: p/IFoo.aidl:1.37-41: List can only have one type parameter, but got: " |
| 1394 | "'List<String,String>'\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1395 | const string expected_map_stderr = |
| 1396 | "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] | 1397 | Options options = Options::From("aidl p/IFoo.aidl IFoo.java"); |
| 1398 | io_delegate_.SetFileContents(options.InputFiles().front(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1399 | "package p; interface IFoo {" |
| 1400 | "void foo(List<String, String> a);}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1401 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1402 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1403 | EXPECT_EQ(expected_list_stderr, GetCapturedStderr()); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1404 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1405 | io_delegate_.SetFileContents(options.InputFiles().front(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1406 | "package p; interface IFoo {" |
| 1407 | "void foo(Map<String> a);}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1408 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1409 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1410 | EXPECT_EQ(expected_map_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1411 | } |
| 1412 | |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1413 | TEST_F(AidlTest, CheckTypeParameterInMapType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1414 | const string expected_stderr = |
| 1415 | "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] | 1416 | Options options = Options::From("aidl -I p p/IFoo.aidl"); |
| 1417 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar { String s; }"); |
| 1418 | |
| 1419 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1420 | "package p; interface IFoo {" |
| 1421 | "Map<String, Bar> foo();}"); |
| 1422 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1423 | |
| 1424 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1425 | "package p; interface IFoo {" |
| 1426 | "Map<Bar, Bar> foo();}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1427 | CaptureStderr(); |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1428 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1429 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1430 | |
| 1431 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1432 | "package p; interface IFoo {" |
| 1433 | "Map<String, String> foo();}"); |
| 1434 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1435 | |
| 1436 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1437 | "package p; interface IFoo {" |
| 1438 | "Map<String, ParcelFileDescriptor> foo();}"); |
| 1439 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1440 | } |
| 1441 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1442 | TEST_F(AidlTest, WrongGenericType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1443 | 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] | 1444 | Options options = Options::From("aidl p/IFoo.aidl IFoo.java"); |
| 1445 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1446 | "package p; interface IFoo {" |
| 1447 | "String<String> foo(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1448 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1449 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1450 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | TEST_F(AidlTest, UserDefinedUnstructuredGenericParcelableType) { |
| 1454 | Options optionsForParcelable = Options::From("aidl -I p p/Bar.aidl"); |
| 1455 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, T>;"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1456 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1457 | EXPECT_NE(0, ::android::aidl::compile_aidl(optionsForParcelable, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1458 | EXPECT_EQ("ERROR: p/Bar.aidl:1.22-26: Every type parameter should be unique.\n", |
| 1459 | GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1460 | |
| 1461 | Options options = Options::From("aidl -I p p/IFoo.aidl"); |
| 1462 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar;"); |
| 1463 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1464 | "package p; interface IFoo {" |
| 1465 | "Bar<String, String> foo();}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1466 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1467 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1468 | 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] | 1469 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T>;"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1470 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1471 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1472 | EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar must have 1 type parameters, but got 2\n", |
| 1473 | GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1474 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, V>;"); |
| 1475 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1476 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1477 | "package p; interface IFoo {" |
| 1478 | "Bar<String, ParcelFileDescriptor> foo();}"); |
| 1479 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jeongik Cha | e74c86d | 2019-12-12 16:54:03 +0900 | [diff] [blame] | 1480 | |
| 1481 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1482 | "package p; interface IFoo {" |
| 1483 | "Bar<int, long> foo();}"); |
| 1484 | |
| 1485 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1486 | "package p; interface IFoo {" |
| 1487 | "Bar<int[], long[]> foo();}"); |
| 1488 | |
| 1489 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1490 | } |
| 1491 | |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1492 | TEST_F(AidlTest, FailOnMultipleTypesInSingleFile) { |
| 1493 | 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] | 1494 | "aidl --lang=cpp -o out -h out/include foo/bar/Foo.aidl", |
| 1495 | "aidl --lang=rust -o out foo/bar/Foo.aidl"}; |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1496 | for (const auto& rawOption : rawOptions) { |
| 1497 | string expected_stderr = |
| 1498 | "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] | 1499 | Options options = Options::From(rawOption); |
| 1500 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1501 | "package foo.bar;\n" |
| 1502 | "interface IFoo1 { int foo(); }\n" |
| 1503 | "interface IFoo2 { int foo(); }\n" |
| 1504 | "parcelable Data1 { int a; int b;}\n" |
| 1505 | "parcelable Data2 { int a; int b;}\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1506 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1507 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1508 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1509 | |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1510 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1511 | "package foo.bar;\n" |
| 1512 | "interface IFoo1 { int foo(); }\n" |
| 1513 | "interface IFoo2 { int foo(); }\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1514 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1515 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1516 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1517 | |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1518 | 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] | 1519 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1520 | "package foo.bar;\n" |
| 1521 | "parcelable Data1 { int a; int b;}\n" |
| 1522 | "parcelable Data2 { int a; int b;}\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1523 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1524 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1525 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1526 | } |
| 1527 | } |
| 1528 | |
Devin Moore | bdba2a8 | 2020-03-31 15:19:02 -0700 | [diff] [blame] | 1529 | TEST_P(AidlTest, FailParseOnEmptyFile) { |
| 1530 | const string contents = ""; |
| 1531 | const string expected_stderr = "ERROR: a/IFoo.aidl:1.1-1: syntax error, unexpected $end\n"; |
| 1532 | CaptureStderr(); |
| 1533 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", contents, typenames_, GetLanguage())); |
| 1534 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1535 | } |
| 1536 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1537 | TEST_F(AidlTest, MultipleInputFiles) { |
| 1538 | Options options = Options::From( |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1539 | "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] | 1540 | |
| 1541 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1542 | "package foo.bar;\n" |
| 1543 | "import foo.bar.Data;\n" |
| 1544 | "interface IFoo { Data getData(); }\n"); |
| 1545 | |
| 1546 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1547 | "package foo.bar;\n" |
| 1548 | "import foo.bar.IFoo;\n" |
| 1549 | "parcelable Data { IFoo foo; }\n"); |
| 1550 | |
| 1551 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1552 | |
| 1553 | string content; |
| 1554 | for (const auto file : { |
| 1555 | "out/foo/bar/IFoo.java", "out/foo/bar/Data.java"}) { |
| 1556 | content.clear(); |
| 1557 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1558 | EXPECT_FALSE(content.empty()); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | TEST_F(AidlTest, MultipleInputFilesCpp) { |
| 1563 | Options options = Options::From("aidl --lang=cpp -o out -h out/include " |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1564 | "-I . foo/bar/IFoo.aidl foo/bar/Data.aidl"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1565 | |
| 1566 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1567 | "package foo.bar;\n" |
| 1568 | "import foo.bar.Data;\n" |
| 1569 | "interface IFoo { Data getData(); }\n"); |
| 1570 | |
| 1571 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1572 | "package foo.bar;\n" |
| 1573 | "import foo.bar.IFoo;\n" |
| 1574 | "parcelable Data { IFoo foo; }\n"); |
| 1575 | |
| 1576 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1577 | |
| 1578 | string content; |
| 1579 | for (const auto file : { |
Jiyong Park | b03551f | 2018-08-06 19:20:51 +0900 | [diff] [blame] | 1580 | "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp", |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1581 | "out/include/foo/bar/IFoo.h", "out/include/foo/bar/Data.h", |
| 1582 | "out/include/foo/bar/BpFoo.h", "out/include/foo/bar/BpData.h", |
| 1583 | "out/include/foo/bar/BnFoo.h", "out/include/foo/bar/BnData.h"}) { |
| 1584 | content.clear(); |
| 1585 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1586 | EXPECT_FALSE(content.empty()); |
| 1587 | } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1588 | } |
| 1589 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 1590 | TEST_F(AidlTest, MultipleInputFilesRust) { |
| 1591 | Options options = |
| 1592 | Options::From("aidl --lang=rust -o out -I . foo/bar/IFoo.aidl foo/bar/Data.aidl"); |
| 1593 | |
| 1594 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1595 | "package foo.bar;\n" |
| 1596 | "import foo.bar.Data;\n" |
| 1597 | "interface IFoo { Data getData(); }\n"); |
| 1598 | |
| 1599 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1600 | "package foo.bar;\n" |
| 1601 | "import foo.bar.IFoo;\n" |
| 1602 | "parcelable Data { IFoo foo; }\n"); |
| 1603 | |
| 1604 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1605 | |
| 1606 | string content; |
| 1607 | for (const auto file : {"out/foo/bar/IFoo.rs", "out/foo/bar/Data.rs"}) { |
| 1608 | content.clear(); |
| 1609 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1610 | EXPECT_FALSE(content.empty()); |
| 1611 | } |
| 1612 | } |
| 1613 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1614 | TEST_F(AidlTest, ConflictWithMetaTransactionGetVersion) { |
| 1615 | const string expected_stderr = |
| 1616 | "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] | 1617 | Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl"); |
| 1618 | // int getInterfaceVersion() is one of the meta transactions |
| 1619 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1620 | "package p; interface IFoo {" |
| 1621 | "int getInterfaceVersion(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1622 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1623 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1624 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1625 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1626 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1627 | TEST_F(AidlTest, ConflictWithSimilarMetaTransaction) { |
| 1628 | // boolean getInterfaceVersion() is not a meta transaction, but should be |
| 1629 | // prevented because return type is not part of a method signature |
| 1630 | const string expected_stderr = |
| 1631 | "ERROR: p/IFoo.aidl:1.35-55: method getInterfaceVersion() is reserved for internal use.\n"; |
| 1632 | 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] | 1633 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1634 | "package p; interface IFoo {" |
| 1635 | "boolean getInterfaceVersion(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1636 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1637 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1638 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1639 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1640 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1641 | TEST_F(AidlTest, ConflictWithMetaTransactionGetName) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1642 | // this is another reserved name |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1643 | const string expected_stderr = |
| 1644 | "ERROR: p/IFoo.aidl:1.34-53: method getTransactionName(int) is reserved for internal use.\n"; |
| 1645 | 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] | 1646 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1647 | "package p; interface IFoo {" |
| 1648 | "String getTransactionName(int code); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1649 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1650 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1651 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1652 | |
| 1653 | // this is not a meta interface method as it differs type arguments |
| 1654 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1655 | "package p; interface IFoo {" |
| 1656 | "String getTransactionName(); }"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1657 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1658 | } |
| 1659 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1660 | TEST_F(AidlTest, DifferentOrderAnnotationsInCheckAPI) { |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1661 | Options options = Options::From("aidl --checkapi old new"); |
| 1662 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1663 | "package p; interface IFoo{ @utf8InCpp @nullable String foo();}"); |
| 1664 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1665 | "package p; interface IFoo{ @nullable @utf8InCpp String foo();}"); |
| 1666 | |
| 1667 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1668 | } |
| 1669 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1670 | TEST_F(AidlTest, SuccessOnIdenticalApiDumps) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1671 | Options options = Options::From("aidl --checkapi old new"); |
| 1672 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo();}"); |
| 1673 | 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] | 1674 | |
| 1675 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1676 | } |
| 1677 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1678 | class AidlTestCompatibleChanges : public AidlTest { |
| 1679 | protected: |
| 1680 | Options options_ = Options::From("aidl --checkapi old new"); |
| 1681 | }; |
| 1682 | |
| 1683 | TEST_F(AidlTestCompatibleChanges, NewType) { |
| 1684 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1685 | "package p;" |
| 1686 | "interface IFoo {" |
| 1687 | " void foo(int a);" |
| 1688 | "}"); |
| 1689 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1690 | "package p;" |
| 1691 | "interface IFoo {" |
| 1692 | " void foo(int a);" |
| 1693 | "}"); |
| 1694 | io_delegate_.SetFileContents("new/p/IBar.aidl", |
| 1695 | "package p;" |
| 1696 | "interface IBar {" |
| 1697 | " void bar();" |
| 1698 | "}"); |
| 1699 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1700 | } |
| 1701 | |
| 1702 | TEST_F(AidlTestCompatibleChanges, NewMethod) { |
| 1703 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1704 | "package p;" |
| 1705 | "interface IFoo {" |
| 1706 | " void foo(int a);" |
| 1707 | "}"); |
| 1708 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1709 | "package p;" |
| 1710 | "interface IFoo {" |
| 1711 | " void foo(int a);" |
| 1712 | " void bar();" |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1713 | " void baz(in List<IFoo> arg);" |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1714 | "}"); |
| 1715 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1716 | } |
| 1717 | |
| 1718 | TEST_F(AidlTestCompatibleChanges, NewField) { |
| 1719 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1720 | "package p;" |
| 1721 | "parcelable Data {" |
| 1722 | " int foo;" |
| 1723 | "}"); |
| 1724 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1725 | "package p;" |
| 1726 | "parcelable Data {" |
| 1727 | " int foo;" |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 1728 | " int bar = 0;" |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1729 | " @nullable List<Data> list;" |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1730 | "}"); |
| 1731 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1732 | } |
| 1733 | |
| 1734 | TEST_F(AidlTestCompatibleChanges, NewEnumerator) { |
| 1735 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1736 | "package p;" |
| 1737 | "enum Enum {" |
| 1738 | " FOO = 1," |
| 1739 | "}"); |
| 1740 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1741 | "package p;" |
| 1742 | "enum Enum {" |
| 1743 | " FOO = 1," |
| 1744 | " BAR = 2," |
| 1745 | "}"); |
| 1746 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1747 | } |
| 1748 | |
| 1749 | TEST_F(AidlTestCompatibleChanges, ReorderedEnumerator) { |
| 1750 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1751 | "package p;" |
| 1752 | "enum Enum {" |
| 1753 | " FOO = 1," |
| 1754 | " BAR = 2," |
| 1755 | "}"); |
| 1756 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1757 | "package p;" |
| 1758 | "enum Enum {" |
| 1759 | " BAR = 2," |
| 1760 | " FOO = 1," |
| 1761 | "}"); |
| 1762 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1763 | } |
| 1764 | |
| 1765 | TEST_F(AidlTestCompatibleChanges, NewPackage) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1766 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1767 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1768 | "interface IFoo {" |
| 1769 | " void foo(int a);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1770 | "}"); |
| 1771 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1772 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1773 | "parcelable Data {" |
| 1774 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1775 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1776 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1777 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1778 | "interface IFoo {" |
| 1779 | " void foo(int a);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1780 | "}"); |
| 1781 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1782 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1783 | "parcelable Data {" |
| 1784 | " int foo;" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1785 | "}"); |
| 1786 | io_delegate_.SetFileContents("new/q/IFoo.aidl", |
| 1787 | "package q;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1788 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1789 | " void foo(int a);" |
| 1790 | "}"); |
| 1791 | io_delegate_.SetFileContents("new/q/Data.aidl", |
| 1792 | "package q;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1793 | "parcelable Data {" |
| 1794 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1795 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1796 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1797 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1798 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1799 | TEST_F(AidlTestCompatibleChanges, ArgNameChange) { |
| 1800 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1801 | "package p;" |
| 1802 | "interface IFoo {" |
| 1803 | " void foo(int a);" |
| 1804 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1805 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1806 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1807 | "interface IFoo {" |
| 1808 | " void foo(int b);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1809 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1810 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1811 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1812 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1813 | TEST_F(AidlTestCompatibleChanges, AddedConstValue) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1814 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 1815 | "package p; interface I {" |
| 1816 | "const int A = 1; }"); |
| 1817 | io_delegate_.SetFileContents("new/p/I.aidl", |
| 1818 | "package p ; interface I {" |
| 1819 | "const int A = 1; const int B = 2;}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1820 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1821 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1822 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1823 | TEST_F(AidlTestCompatibleChanges, ChangedConstValueOrder) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1824 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 1825 | "package p; interface I {" |
| 1826 | "const int A = 1; const int B = 2;}"); |
| 1827 | io_delegate_.SetFileContents("new/p/I.aidl", |
| 1828 | "package p ; interface I {" |
| 1829 | "const int B = 2; const int A = 1;}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1830 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1831 | } |
| 1832 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1833 | class AidlTestIncompatibleChanges : public AidlTest { |
| 1834 | protected: |
| 1835 | Options options_ = Options::From("aidl --checkapi old new"); |
| 1836 | }; |
| 1837 | |
| 1838 | TEST_F(AidlTestIncompatibleChanges, RemovedType) { |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 1839 | 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] | 1840 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1841 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1842 | "interface IFoo {" |
| 1843 | " void foo(in String[] str);" |
| 1844 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1845 | "}"); |
Devin Moore | d131d5e | 2020-03-31 10:39:10 -0700 | [diff] [blame] | 1846 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1847 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | d131d5e | 2020-03-31 10:39:10 -0700 | [diff] [blame] | 1848 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | TEST_F(AidlTestIncompatibleChanges, RemovedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1852 | const string expected_stderr = |
| 1853 | "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] | 1854 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1855 | "package p;" |
| 1856 | "interface IFoo {" |
| 1857 | " void foo(in String[] str);" |
| 1858 | " void bar(@utf8InCpp String str);" |
| 1859 | "}"); |
| 1860 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1861 | "package p;" |
| 1862 | "interface IFoo {" |
| 1863 | " void foo(in String[] str);" |
| 1864 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1865 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1866 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1867 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1870 | TEST_F(AidlTestIncompatibleChanges, UntypedListInInterface) { |
| 1871 | const string expected_stderr = |
| 1872 | "ERROR: new/p/IFoo.aidl:1.61-65: " |
| 1873 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 1874 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 1875 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 1876 | "ERROR: new/p/IFoo.aidl: Failed to read.\n"; |
| 1877 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1878 | "package p;" |
| 1879 | "interface IFoo {" |
| 1880 | " void foo(in String[] str);" |
| 1881 | "}"); |
| 1882 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1883 | "package p;" |
| 1884 | "interface IFoo {" |
| 1885 | " void foo(in String[] str);" |
| 1886 | " void bar(in List arg);" |
| 1887 | "}"); |
| 1888 | CaptureStderr(); |
| 1889 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 1890 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1891 | } |
| 1892 | |
| 1893 | TEST_F(AidlTestCompatibleChanges, UntypedListInParcelable) { |
| 1894 | const string expected_stderr = |
| 1895 | "ERROR: new/p/Data.aidl:1.54-59: " |
| 1896 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 1897 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 1898 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 1899 | "ERROR: new/p/Data.aidl: Failed to read.\n"; |
| 1900 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1901 | "package p;" |
| 1902 | "parcelable Data {" |
| 1903 | " int foo;" |
| 1904 | "}"); |
| 1905 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1906 | "package p;" |
| 1907 | "parcelable Data {" |
| 1908 | " int foo;" |
| 1909 | " @nullable List list;" |
| 1910 | "}"); |
| 1911 | CaptureStderr(); |
| 1912 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 1913 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1914 | } |
| 1915 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1916 | TEST_F(AidlTestIncompatibleChanges, RemovedField) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1917 | const string expected_stderr = |
| 1918 | "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] | 1919 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1920 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1921 | "parcelable Data {" |
| 1922 | " int foo;" |
| 1923 | " int bar;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1924 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1925 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1926 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1927 | "parcelable Data {" |
| 1928 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1929 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1930 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1931 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1932 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1933 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1934 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1935 | TEST_F(AidlTestIncompatibleChanges, RemovedEnumerator) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1936 | const string expected_stderr = |
| 1937 | "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] | 1938 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1939 | "package p;" |
| 1940 | "enum Enum {" |
| 1941 | " FOO = 1," |
| 1942 | " BAR = 2," |
| 1943 | "}"); |
| 1944 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1945 | "package p;" |
| 1946 | "enum Enum {" |
| 1947 | " BAR = 2," |
| 1948 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1949 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1950 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1951 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | TEST_F(AidlTestIncompatibleChanges, RenamedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1955 | const string expected_stderr = |
| 1956 | "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] | 1957 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1958 | "package p;" |
| 1959 | "interface IFoo {" |
| 1960 | " void foo(in String[] str);" |
| 1961 | " void bar(@utf8InCpp String str);" |
| 1962 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1963 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1964 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1965 | "interface IFoo {" |
| 1966 | " void foo(in String[] str);" |
| 1967 | " void bar2(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1968 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1969 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1970 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1971 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1974 | TEST_F(AidlTestIncompatibleChanges, RenamedType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1975 | 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] | 1976 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1977 | "package p;" |
| 1978 | "interface IFoo {" |
| 1979 | " void foo(in String[] str);" |
| 1980 | " void bar(@utf8InCpp String str);" |
| 1981 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1982 | io_delegate_.SetFileContents("new/p/IFoo2.aidl", |
| 1983 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1984 | "interface IFoo2 {" |
| 1985 | " void foo(in String[] str);" |
| 1986 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1987 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1988 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1989 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1990 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1991 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1992 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1993 | TEST_F(AidlTestIncompatibleChanges, ChangedEnumerator) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1994 | const string expected_stderr = |
| 1995 | "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] | 1996 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1997 | "package p;" |
| 1998 | "enum Enum {" |
| 1999 | " FOO = 1," |
| 2000 | " BAR = 2," |
| 2001 | "}"); |
| 2002 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 2003 | "package p;" |
| 2004 | "enum Enum {" |
| 2005 | " FOO = 3," |
| 2006 | " BAR = 2," |
| 2007 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2008 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2009 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2010 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | TEST_F(AidlTestIncompatibleChanges, ReorderedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2014 | const string expected_stderr = |
| 2015 | "ERROR: new/p/IFoo.aidl:1.67-71: Transaction ID changed: p.IFoo.foo(String[]) is changed " |
| 2016 | "from 0 to 1.\n" |
| 2017 | "ERROR: new/p/IFoo.aidl:1.33-37: Transaction ID changed: p.IFoo.bar(String) is changed from " |
| 2018 | "1 to 0.\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2019 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2020 | "package p;" |
| 2021 | "interface IFoo {" |
| 2022 | " void foo(in String[] str);" |
| 2023 | " void bar(@utf8InCpp String str);" |
| 2024 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2025 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2026 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2027 | "interface IFoo {" |
| 2028 | " void bar(@utf8InCpp String str);" |
| 2029 | " void foo(in String[] str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2030 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2031 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2032 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2033 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | TEST_F(AidlTestIncompatibleChanges, ReorderedField) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2037 | const string expected_stderr = |
Jiyong Park | b07d993 | 2020-05-15 12:56:54 +0900 | [diff] [blame] | 2038 | "ERROR: new/p/Data.aidl:1.33-37: Reordered bar from 1 to 0.\n" |
| 2039 | "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] | 2040 | io_delegate_.SetFileContents("old/p/Data.aidl", |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2041 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2042 | "parcelable Data {" |
| 2043 | " int foo;" |
| 2044 | " int bar;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2045 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2046 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 2047 | "package p;" |
| 2048 | "parcelable Data {" |
| 2049 | " int bar;" |
| 2050 | " int foo;" |
| 2051 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2052 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2053 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2054 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2055 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2056 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2057 | TEST_F(AidlTestIncompatibleChanges, ChangedDirectionSpecifier) { |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 2058 | 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] | 2059 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2060 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2061 | "interface IFoo {" |
| 2062 | " void foo(in String[] str);" |
| 2063 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2064 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2065 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2066 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2067 | "interface IFoo {" |
| 2068 | " void foo(out String[] str);" |
| 2069 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2070 | "}"); |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 2071 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2072 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 2073 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2074 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2075 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2076 | TEST_F(AidlTestIncompatibleChanges, AddedAnnotation) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2077 | const string expected_stderr = |
| 2078 | "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] | 2079 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2080 | "package p;" |
| 2081 | "interface IFoo {" |
| 2082 | " void foo(in String[] str);" |
| 2083 | " void bar(@utf8InCpp String str);" |
| 2084 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2085 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2086 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2087 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2088 | " void foo(in @utf8InCpp String[] str);" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2089 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2090 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2091 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2092 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2093 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2094 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2095 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2096 | TEST_F(AidlTestIncompatibleChanges, RemovedAnnotation) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2097 | const string expected_stderr = |
| 2098 | "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] | 2099 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 2100 | "package p;" |
| 2101 | "interface IFoo {" |
| 2102 | " void foo(in String[] str);" |
| 2103 | " void bar(@utf8InCpp String str);" |
| 2104 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2105 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 2106 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2107 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2108 | " void foo(in String[] str);" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2109 | " void bar(String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2110 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2111 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2112 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2113 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2114 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 2115 | |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 2116 | TEST_F(AidlTestIncompatibleChanges, AddedParcelableAnnotation) { |
| 2117 | const string expected_stderr = |
| 2118 | "ERROR: new/p/Foo.aidl:1.47-51: Changed annotations: (empty) to @JavaOnlyStableParcelable\n"; |
| 2119 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2120 | "package p;" |
| 2121 | "parcelable Foo {" |
| 2122 | " int A;" |
| 2123 | "}"); |
| 2124 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2125 | "package p;" |
| 2126 | "@JavaOnlyStableParcelable parcelable Foo {" |
| 2127 | " int A;" |
| 2128 | "}"); |
| 2129 | CaptureStderr(); |
| 2130 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2131 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2132 | } |
| 2133 | |
| 2134 | TEST_F(AidlTestIncompatibleChanges, RemovedParcelableAnnotation) { |
| 2135 | const string expected_stderr = |
| 2136 | "ERROR: new/p/Foo.aidl:1.21-25: Changed annotations: @JavaOnlyStableParcelable to (empty)\n"; |
| 2137 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2138 | "package p;" |
| 2139 | "@JavaOnlyStableParcelable parcelable Foo {" |
| 2140 | " int A;" |
| 2141 | "}"); |
| 2142 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2143 | "package p;" |
| 2144 | "parcelable Foo {" |
| 2145 | " int A;" |
| 2146 | "}"); |
| 2147 | CaptureStderr(); |
| 2148 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2149 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2150 | } |
| 2151 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2152 | TEST_F(AidlTestIncompatibleChanges, RemovedPackage) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2153 | 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] | 2154 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}"); |
| 2155 | io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2156 | io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2157 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2158 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2159 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2160 | } |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 2161 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2162 | TEST_F(AidlTestIncompatibleChanges, ChangedDefaultValue) { |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 2163 | 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] | 2164 | io_delegate_.SetFileContents("old/p/D.aidl", "package p; parcelable D { int a = 1; }"); |
| 2165 | 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] | 2166 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2167 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2168 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2169 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 2170 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2171 | TEST_F(AidlTestIncompatibleChanges, RemovedConstValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2172 | const string expected_stderr = |
| 2173 | "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] | 2174 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 2175 | "package p; interface I {" |
| 2176 | "const int A = 1; const int B = 2;}"); |
| 2177 | 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] | 2178 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2179 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2180 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2181 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 2182 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2183 | TEST_F(AidlTestIncompatibleChanges, ChangedConstValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2184 | const string expected_stderr = |
| 2185 | "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] | 2186 | io_delegate_.SetFileContents("old/p/I.aidl", "package p; interface I { const int A = 1; }"); |
| 2187 | 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] | 2188 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 2189 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2190 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 2191 | } |
| 2192 | |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 2193 | TEST_F(AidlTestIncompatibleChanges, FixedSizeAddedField) { |
| 2194 | const string expected_stderr = |
| 2195 | "ERROR: new/p/Foo.aidl:1.33-37: Number of fields in p.Foo is changed from 1 to 2. " |
| 2196 | "This is an incompatible change for FixedSize types.\n"; |
| 2197 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2198 | "package p; @FixedSize parcelable Foo { int A = 1; }"); |
| 2199 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2200 | "package p; @FixedSize parcelable Foo { int A = 1; int B = 2; }"); |
| 2201 | CaptureStderr(); |
| 2202 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2203 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2204 | } |
| 2205 | |
| 2206 | TEST_F(AidlTestIncompatibleChanges, FixedSizeRemovedField) { |
| 2207 | const string expected_stderr = |
| 2208 | "ERROR: new/p/Foo.aidl:1.33-37: Number of fields in p.Foo is reduced from 2 to 1.\n"; |
| 2209 | io_delegate_.SetFileContents("old/p/Foo.aidl", |
| 2210 | "package p; @FixedSize parcelable Foo { int A = 1; int B = 1; }"); |
| 2211 | io_delegate_.SetFileContents("new/p/Foo.aidl", |
| 2212 | "package p; @FixedSize parcelable Foo { int A = 1; }"); |
| 2213 | CaptureStderr(); |
| 2214 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 2215 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2216 | } |
| 2217 | |
| 2218 | TEST_P(AidlTest, RejectNonFixedSizeFromFixedSize) { |
| 2219 | const string expected_stderr = |
| 2220 | "ERROR: Foo.aidl:1.36-38: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2221 | "a.\n" |
| 2222 | "ERROR: Foo.aidl:1.44-46: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2223 | "b.\n" |
| 2224 | "ERROR: Foo.aidl:1.55-57: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2225 | "c.\n" |
| 2226 | "ERROR: Foo.aidl:1.80-82: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2227 | "d.\n" |
| 2228 | "ERROR: Foo.aidl:1.92-94: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2229 | "e.\n" |
| 2230 | "ERROR: Foo.aidl:1.109-111: The @FixedSize parcelable 'Foo' has a non-fixed size field named " |
| 2231 | "f.\n"; |
| 2232 | |
| 2233 | io_delegate_.SetFileContents("Foo.aidl", |
| 2234 | "@FixedSize parcelable Foo { " |
| 2235 | " int[] a;" |
| 2236 | " Bar b;" |
| 2237 | " String c;" |
| 2238 | " ParcelFileDescriptor d;" |
| 2239 | " IBinder e;" |
| 2240 | " List<String> f;" |
Steven Moreland | 265f3d4 | 2020-09-24 19:06:54 +0000 | [diff] [blame] | 2241 | " int isFixedSize;" |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 2242 | "}"); |
| 2243 | io_delegate_.SetFileContents("Bar.aidl", "parcelable Bar { int a; }"); |
| 2244 | Options options = |
| 2245 | Options::From("aidl Foo.aidl -I . --lang=" + Options::LanguageToString(GetLanguage())); |
| 2246 | |
| 2247 | CaptureStderr(); |
| 2248 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2249 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2250 | } |
| 2251 | |
| 2252 | TEST_P(AidlTest, AcceptFixedSizeFromFixedSize) { |
| 2253 | const string expected_stderr = ""; |
| 2254 | |
| 2255 | io_delegate_.SetFileContents("Foo.aidl", "@FixedSize parcelable Foo { int a; Bar b; }"); |
| 2256 | io_delegate_.SetFileContents("Bar.aidl", "@FixedSize parcelable Bar { Val c; }"); |
| 2257 | io_delegate_.SetFileContents("Val.aidl", "enum Val { A, B, }"); |
| 2258 | Options options = |
| 2259 | Options::From("aidl Foo.aidl -I . --lang=" + Options::LanguageToString(GetLanguage())); |
| 2260 | |
| 2261 | CaptureStderr(); |
| 2262 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2263 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2264 | } |
| 2265 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 2266 | TEST_F(AidlTest, RejectAmbiguousImports) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2267 | const string expected_stderr = |
| 2268 | "ERROR: p/IFoo.aidl: Duplicate files found for q.IBar from:\n" |
| 2269 | "dir1/q/IBar.aidl\n" |
| 2270 | "dir2/q/IBar.aidl\n" |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 2271 | "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] | 2272 | Options options = Options::From("aidl --lang=java -o out -I dir1 -I dir2 p/IFoo.aidl"); |
| 2273 | io_delegate_.SetFileContents("p/IFoo.aidl", "package p; import q.IBar; interface IFoo{}"); |
| 2274 | io_delegate_.SetFileContents("dir1/q/IBar.aidl", "package q; interface IBar{}"); |
| 2275 | io_delegate_.SetFileContents("dir2/q/IBar.aidl", "package q; interface IBar{}"); |
| 2276 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2277 | CaptureStderr(); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 2278 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2279 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 2280 | } |
| 2281 | |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2282 | TEST_F(AidlTest, HandleManualIdAssignments) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2283 | const string expected_stderr = |
| 2284 | "ERROR: new/p/IFoo.aidl:1.32-36: Transaction ID changed: p.IFoo.foo() is changed from 10 to " |
| 2285 | "11.\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2286 | Options options = Options::From("aidl --checkapi old new"); |
| 2287 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}"); |
| 2288 | 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] | 2289 | |
| 2290 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 2291 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 2292 | 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] | 2293 | CaptureStderr(); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2294 | EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2295 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2296 | } |
| 2297 | |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2298 | TEST_F(AidlTest, ParcelFileDescriptorIsBuiltinType) { |
| 2299 | Options javaOptions = Options::From("aidl --lang=java -o out p/IFoo.aidl"); |
| 2300 | 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] | 2301 | Options rustOptions = Options::From("aidl --lang=rust -o out p/IFoo.aidl"); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2302 | |
| 2303 | // use without import |
| 2304 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 2305 | "package p; interface IFoo{ void foo(in ParcelFileDescriptor fd);}"); |
| 2306 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 2307 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2308 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rustOptions, io_delegate_)); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2309 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2310 | // use without import but with full name |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2311 | io_delegate_.SetFileContents( |
| 2312 | "p/IFoo.aidl", |
| 2313 | "package p; interface IFoo{ void foo(in android.os.ParcelFileDescriptor fd);}"); |
| 2314 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 2315 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2316 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rustOptions, io_delegate_)); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2317 | |
| 2318 | // use with import (as before) |
| 2319 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 2320 | "package p;" |
| 2321 | "import android.os.ParcelFileDescriptor;" |
| 2322 | "interface IFoo{" |
| 2323 | " void foo(in ParcelFileDescriptor fd);" |
| 2324 | "}"); |
| 2325 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 2326 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2327 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rustOptions, io_delegate_)); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 2328 | } |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 2329 | |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2330 | TEST_F(AidlTest, ManualIds) { |
| 2331 | Options options = Options::From("aidl --lang=java -o out IFoo.aidl"); |
| 2332 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2333 | "interface IFoo {\n" |
| 2334 | " void foo() = 0;\n" |
| 2335 | " void bar() = 1;\n" |
| 2336 | "}"); |
| 2337 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2338 | } |
| 2339 | |
| 2340 | TEST_F(AidlTest, ManualIdsWithMetaTransactions) { |
| 2341 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2342 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2343 | "interface IFoo {\n" |
| 2344 | " void foo() = 0;\n" |
| 2345 | " void bar() = 1;\n" |
| 2346 | "}"); |
| 2347 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2348 | } |
| 2349 | |
| 2350 | TEST_F(AidlTest, FailOnDuplicatedIds) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2351 | const string expected_stderr = |
| 2352 | "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] | 2353 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2354 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2355 | "interface IFoo {\n" |
| 2356 | " void foo() = 3;\n" |
| 2357 | " void bar() = 3;\n" |
| 2358 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2359 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2360 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2361 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | TEST_F(AidlTest, FailOnOutOfRangeIds) { |
| 2365 | // 16777115 is kLastMetaMethodId + 1 |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2366 | const string expected_stderr = |
| 2367 | "ERROR: IFoo.aidl:3.7-11: Found out of bounds id (16777115) for method bar. " |
| 2368 | "Value for id must be between 0 and 16777114 inclusive.\n"; |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2369 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2370 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2371 | "interface IFoo {\n" |
| 2372 | " void foo() = 3;\n" |
| 2373 | " void bar() = 16777115;\n" |
| 2374 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2375 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2376 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2377 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | TEST_F(AidlTest, FailOnPartiallyAssignedIds) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2381 | const string expected_stderr = |
| 2382 | "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] | 2383 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 2384 | io_delegate_.SetFileContents("IFoo.aidl", |
| 2385 | "interface IFoo {\n" |
| 2386 | " void foo() = 3;\n" |
| 2387 | " void bar();\n" |
| 2388 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2389 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2390 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2391 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 2392 | } |
| 2393 | |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2394 | TEST_F(AidlTest, AllowDuplicatedImportPaths) { |
| 2395 | Options options = Options::From("aidl --lang=java -I dir -I dir IFoo.aidl"); |
| 2396 | io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}"); |
| 2397 | io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}"); |
| 2398 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2399 | } |
| 2400 | |
| 2401 | TEST_F(AidlTest, FailOnAmbiguousImports) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2402 | const string expected_stderr = |
| 2403 | "ERROR: IFoo.aidl: Duplicate files found for IBar from:\n" |
| 2404 | "dir/IBar.aidl\n" |
| 2405 | "dir2/IBar.aidl\n" |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 2406 | "ERROR: IFoo.aidl: Couldn't find import for class IBar\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2407 | |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2408 | Options options = Options::From("aidl --lang=java -I dir -I dir2 IFoo.aidl"); |
| 2409 | io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}"); |
| 2410 | io_delegate_.SetFileContents("dir2/IBar.aidl", "interface IBar{}"); |
| 2411 | io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2412 | CaptureStderr(); |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2413 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2414 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 2415 | } |
| 2416 | |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 2417 | TEST_F(AidlTest, UnusedImportDoesNotContributeInclude) { |
| 2418 | io_delegate_.SetFileContents("a/b/IFoo.aidl", |
| 2419 | "package a.b;\n" |
| 2420 | "import a.b.IBar;\n" |
| 2421 | "import a.b.IQux;\n" |
| 2422 | "interface IFoo { IQux foo(); }\n"); |
| 2423 | io_delegate_.SetFileContents("a/b/IBar.aidl", "package a.b; interface IBar { void foo(); }"); |
| 2424 | io_delegate_.SetFileContents("a/b/IQux.aidl", "package a.b; interface IQux { void foo(); }"); |
| 2425 | |
| 2426 | Options options = Options::From("aidl --lang=ndk a/b/IFoo.aidl -I . -o out -h out/include"); |
| 2427 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2428 | |
| 2429 | string output; |
| 2430 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/include/aidl/a/b/IFoo.h", &output)); |
| 2431 | // IBar was imported but wasn't used. include is not expected. |
| 2432 | EXPECT_THAT(output, Not(testing::HasSubstr("#include <aidl/a/b/IBar.h>"))); |
| 2433 | // IBar was imported and used. include is expected. |
| 2434 | EXPECT_THAT(output, (testing::HasSubstr("#include <aidl/a/b/IQux.h>"))); |
| 2435 | } |
| 2436 | |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2437 | TEST_F(AidlTest, ParseJavaPassthroughAnnotation) { |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2438 | io_delegate_.SetFileContents("a/IFoo.aidl", R"--(package a; |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2439 | import a.MyEnum; |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2440 | @JavaPassthrough(annotation="@com.android.Alice(arg=com.android.Alice.Value.A)") |
| 2441 | @JavaPassthrough(annotation="@com.android.AliceTwo") |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2442 | interface IFoo { |
| 2443 | @JavaPassthrough(annotation="@com.android.Bob") |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2444 | void foo(@JavaPassthrough(annotation="@com.android.Cat") int x, MyEnum y); |
Jiyong Park | 7c3b0e4 | 2020-08-04 16:08:32 +0900 | [diff] [blame] | 2445 | const @JavaPassthrough(annotation="@com.android.David") int A = 3; |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2446 | })--"); |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2447 | // JavaPassthrough should work with other types as well (e.g. enum) |
| 2448 | io_delegate_.SetFileContents("a/MyEnum.aidl", R"--(package a; |
| 2449 | @JavaPassthrough(annotation="@com.android.Alice(arg=com.android.Alice.Value.A)") |
| 2450 | @JavaPassthrough(annotation="@com.android.AliceTwo") |
| 2451 | @Backing(type="byte") |
| 2452 | enum MyEnum { |
| 2453 | a, b, c |
| 2454 | })--"); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2455 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2456 | 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] | 2457 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 2458 | |
| 2459 | string java_out; |
| 2460 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/IFoo.java", &java_out)); |
Jooyung Han | e6bc5e1 | 2020-10-13 10:57:10 +0900 | [diff] [blame] | 2461 | // type-decl-level annotations with newline at the end |
| 2462 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Alice(arg=com.android.Alice.Value.A)\n")); |
| 2463 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.AliceTwo\n")); |
| 2464 | // member-decl-level annotations with newline at the end |
| 2465 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Bob\n")); |
| 2466 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.David\n")); |
| 2467 | // inline annotations with space at the end |
| 2468 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Cat ")); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2469 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2470 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/MyEnum.java", &java_out)); |
| 2471 | // type-decl-level annotations with newline at the end |
| 2472 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.Alice(arg=com.android.Alice.Value.A)\n")); |
| 2473 | EXPECT_THAT(java_out, testing::HasSubstr("@com.android.AliceTwo\n")); |
| 2474 | |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2475 | // Other backends shouldn't be bothered |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2476 | Options cpp_options = |
| 2477 | 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] | 2478 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
| 2479 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2480 | Options ndk_options = |
| 2481 | 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] | 2482 | EXPECT_EQ(0, ::android::aidl::compile_aidl(ndk_options, io_delegate_)); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 2483 | |
Jooyung Han | 4cde01d | 2020-10-13 11:10:47 +0900 | [diff] [blame] | 2484 | 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] | 2485 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rust_options, io_delegate_)); |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 2486 | } |
| 2487 | |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 2488 | TEST_F(AidlTest, ParseRustDerive) { |
| 2489 | io_delegate_.SetFileContents("a/Foo.aidl", R"(package a; |
| 2490 | @RustDerive(Clone=true, Copy=false) |
| 2491 | parcelable Foo { |
| 2492 | int a; |
| 2493 | })"); |
| 2494 | |
| 2495 | Options rust_options = Options::From("aidl --lang=rust -o out a/Foo.aidl"); |
| 2496 | EXPECT_EQ(0, ::android::aidl::compile_aidl(rust_options, io_delegate_)); |
| 2497 | |
| 2498 | string rust_out; |
| 2499 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.rs", &rust_out)); |
| 2500 | EXPECT_THAT(rust_out, testing::HasSubstr("#[derive(Debug, Clone)]")); |
| 2501 | |
| 2502 | // Other backends shouldn't be bothered |
| 2503 | Options cpp_options = Options::From("aidl --lang=cpp -o out -h out a/Foo.aidl"); |
| 2504 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
| 2505 | |
| 2506 | Options ndk_options = Options::From("aidl --lang=ndk -o out -h out a/Foo.aidl"); |
| 2507 | EXPECT_EQ(0, ::android::aidl::compile_aidl(ndk_options, io_delegate_)); |
| 2508 | |
| 2509 | Options java_options = Options::From("aidl --lang=java -o out a/Foo.aidl"); |
| 2510 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
| 2511 | } |
| 2512 | |
Jiyong Park | 56f73d7 | 2019-06-11 12:20:28 +0900 | [diff] [blame] | 2513 | class AidlOutputPathTest : public AidlTest { |
| 2514 | protected: |
| 2515 | void SetUp() override { |
| 2516 | AidlTest::SetUp(); |
| 2517 | io_delegate_.SetFileContents("sub/dir/foo/bar/IFoo.aidl", "package foo.bar; interface IFoo {}"); |
| 2518 | } |
| 2519 | |
| 2520 | void Test(const Options& options, const std::string expected_output_path) { |
| 2521 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2522 | // check the existence |
| 2523 | EXPECT_TRUE(io_delegate_.GetWrittenContents(expected_output_path, nullptr)); |
| 2524 | } |
| 2525 | }; |
| 2526 | |
| 2527 | TEST_F(AidlOutputPathTest, OutDirWithNoOutputFile) { |
| 2528 | // <out_dir> / <package_name> / <type_name>.java |
| 2529 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl"), "out/foo/bar/IFoo.java"); |
| 2530 | } |
| 2531 | |
| 2532 | TEST_F(AidlOutputPathTest, OutDirWithOutputFile) { |
| 2533 | // when output file is explicitly set, it is always respected. -o option is |
| 2534 | // ignored. |
| 2535 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java"); |
| 2536 | } |
| 2537 | |
| 2538 | TEST_F(AidlOutputPathTest, NoOutDirWithOutputFile) { |
| 2539 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java"); |
| 2540 | } |
| 2541 | |
| 2542 | TEST_F(AidlOutputPathTest, NoOutDirWithNoOutputFile) { |
| 2543 | // output is the same as the input file except for the suffix |
| 2544 | Test(Options::From("aidl sub/dir/foo/bar/IFoo.aidl"), "sub/dir/foo/bar/IFoo.java"); |
| 2545 | } |
| 2546 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2547 | TEST_P(AidlTest, FailOnOutOfBoundsInt32MaxConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2548 | AidlError error; |
| 2549 | const string expected_stderr = |
| 2550 | "ERROR: p/IFoo.aidl:3.58-69: Invalid type specifier for an int64 literal: int\n"; |
| 2551 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2552 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2553 | R"(package p; |
| 2554 | interface IFoo { |
| 2555 | const int int32_max_oob = 2147483650; |
| 2556 | } |
| 2557 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2558 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2559 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2560 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2563 | TEST_P(AidlTest, FailOnOutOfBoundsInt32MinConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2564 | AidlError error; |
| 2565 | const string expected_stderr = |
| 2566 | "ERROR: p/IFoo.aidl:3.58-60: Invalid type specifier for an int64 literal: int\n"; |
| 2567 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2568 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2569 | R"(package p; |
| 2570 | interface IFoo { |
| 2571 | const int int32_min_oob = -2147483650; |
| 2572 | } |
| 2573 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2574 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2575 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2576 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2579 | TEST_P(AidlTest, FailOnOutOfBoundsInt64MaxConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2580 | AidlError error; |
| 2581 | const string expected_stderr = |
Devin Moore | 2a08890 | 2020-09-17 10:51:19 -0700 | [diff] [blame] | 2582 | "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] | 2583 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2584 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2585 | R"(package p; |
| 2586 | interface IFoo { |
| 2587 | const long int64_max_oob = 21474836509999999999999999; |
| 2588 | } |
| 2589 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2590 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2591 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2592 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2593 | } |
| 2594 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2595 | TEST_P(AidlTest, FailOnOutOfBoundsInt64MinConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2596 | AidlError error; |
| 2597 | const string expected_stderr = |
Devin Moore | 2a08890 | 2020-09-17 10:51:19 -0700 | [diff] [blame] | 2598 | "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] | 2599 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2600 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2601 | R"(package p; |
| 2602 | interface IFoo { |
| 2603 | const long int64_min_oob = -21474836509999999999999999; |
| 2604 | } |
| 2605 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2606 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2607 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2608 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2611 | TEST_P(AidlTest, FailOnOutOfBoundsAutofilledEnum) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2612 | AidlError error; |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 2613 | const string expected_stderr = |
| 2614 | "ERROR: p/TestEnum.aidl:3.35-44: Invalid type specifier for an int32 " |
| 2615 | "literal: byte\n" |
| 2616 | "ERROR: p/TestEnum.aidl:5.1-36: Enumerator type differs from enum backing type.\n"; |
| 2617 | CaptureStderr(); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 2618 | EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl", |
| 2619 | R"(package p; |
| 2620 | @Backing(type="byte") |
| 2621 | enum TestEnum { |
| 2622 | FOO = 127, |
| 2623 | BAR, |
| 2624 | } |
| 2625 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2626 | typenames_, GetLanguage(), &error)); |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 2627 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2628 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 2629 | } |
| 2630 | |
Devin Moore | decaf29 | 2020-04-30 09:16:40 -0700 | [diff] [blame] | 2631 | TEST_P(AidlTest, UnsupportedBackingAnnotationParam) { |
| 2632 | AidlError error; |
| 2633 | const string expected_stderr = |
| 2634 | "ERROR: p/TestEnum.aidl:2.1-51: Parameter foo not supported for annotation Backing. It must " |
| 2635 | "be one of: type\n" |
| 2636 | "ERROR: p/TestEnum.aidl:2.1-51: Parameter foo not supported for annotation Backing. It must " |
| 2637 | "be one of: type\n"; |
| 2638 | CaptureStderr(); |
| 2639 | EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl", |
| 2640 | R"(package p; |
| 2641 | @Backing(foo="byte") |
| 2642 | enum TestEnum { |
| 2643 | FOO = 1, |
| 2644 | BAR, |
| 2645 | } |
| 2646 | )", |
| 2647 | typenames_, GetLanguage(), &error)); |
| 2648 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2649 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
| 2650 | } |
| 2651 | |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 2652 | TEST_P(AidlTest, SupportJavaOnlyImmutableAnnotation) { |
| 2653 | io_delegate_.SetFileContents("Foo.aidl", |
| 2654 | "@JavaOnlyImmutable parcelable Foo { int a; Bar b; List<Bar> c; " |
| 2655 | "Map<String, Baz> d; Bar[] e; }"); |
| 2656 | io_delegate_.SetFileContents("Bar.aidl", "@JavaOnlyImmutable parcelable Bar { String a; }"); |
| 2657 | io_delegate_.SetFileContents("Baz.aidl", |
| 2658 | "@JavaOnlyImmutable @JavaOnlyStableParcelable parcelable Baz;"); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 2659 | Options options = Options::From("aidl --lang=java -I . Foo.aidl"); |
| 2660 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2661 | } |
| 2662 | |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 2663 | TEST_P(AidlTest, RejectMutableParcelableFromJavaOnlyImmutableParcelable) { |
| 2664 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { Bar bar; }"); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 2665 | io_delegate_.SetFileContents("Bar.aidl", "parcelable Bar { String a; }"); |
| 2666 | Options options = Options::From("aidl --lang=java Foo.aidl -I ."); |
| 2667 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2668 | } |
| 2669 | |
| 2670 | TEST_P(AidlTest, ImmtuableParcelableCannotBeInOut) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 2671 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { int a; }"); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 2672 | io_delegate_.SetFileContents("IBar.aidl", "interface IBar { void my(inout Foo); }"); |
| 2673 | Options options = Options::From("aidl --lang=java IBar.aidl -I ."); |
| 2674 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2675 | } |
| 2676 | |
| 2677 | TEST_P(AidlTest, ImmtuableParcelableCannotBeOut) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 2678 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { int a; }"); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 2679 | io_delegate_.SetFileContents("IBar.aidl", "interface IBar { void my(out Foo); }"); |
| 2680 | Options options = Options::From("aidl --lang=java IBar.aidl -I ."); |
| 2681 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2682 | } |
| 2683 | |
Jeongik Cha | 9118025 | 2020-07-31 15:43:11 +0900 | [diff] [blame] | 2684 | TEST_P(AidlTest, ImmtuableParcelableFieldNameRestriction) { |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 2685 | io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { int a; int A; }"); |
Jeongik Cha | 9118025 | 2020-07-31 15:43:11 +0900 | [diff] [blame] | 2686 | Options options = Options::From("aidl --lang=java Foo.aidl"); |
| 2687 | const string expected_stderr = |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 2688 | "ERROR: Foo.aidl:1.30-34: The parcelable 'Foo' has duplicate field name 'A' after " |
Jeongik Cha | 9118025 | 2020-07-31 15:43:11 +0900 | [diff] [blame] | 2689 | "capitalizing the first letter\n"; |
| 2690 | CaptureStderr(); |
| 2691 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2692 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2693 | } |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 2694 | |
| 2695 | TEST_P(AidlTest, GenericStructuredParcelable) { |
| 2696 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo<T, U> { int a; int A; }"); |
| 2697 | Options options = |
| 2698 | Options::From("aidl Foo.aidl --lang=" + Options::LanguageToString(GetLanguage())); |
| 2699 | const string expected_stderr = ""; |
| 2700 | CaptureStderr(); |
| 2701 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2702 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2703 | } |
| 2704 | |
| 2705 | TEST_P(AidlTest, RejectGenericStructuredParcelabelRepeatedParam) { |
| 2706 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo<T,T> { int a; int A; }"); |
| 2707 | Options options = |
| 2708 | Options::From("aidl Foo.aidl --lang=" + Options::LanguageToString(GetLanguage())); |
| 2709 | const string expected_stderr = |
| 2710 | "ERROR: Foo.aidl:1.11-15: Every type parameter should be unique.\n"; |
| 2711 | CaptureStderr(); |
| 2712 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2713 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2714 | } |
| 2715 | |
| 2716 | TEST_P(AidlTest, RejectGenericStructuredParcelableField) { |
| 2717 | io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo<T,T> { T a; int A; }"); |
| 2718 | Options options = |
| 2719 | Options::From("aidl Foo.aidl --lang=" + Options::LanguageToString(GetLanguage())); |
| 2720 | const string expected_stderr = "ERROR: Foo.aidl:1.22-24: Failed to resolve 'T'\n"; |
| 2721 | CaptureStderr(); |
| 2722 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 2723 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2724 | } |
| 2725 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 2726 | } // namespace aidl |
| 2727 | } // namespace android |