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