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