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