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