Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 17 | #include <memory> |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 18 | #include <set> |
| 19 | #include <string> |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 23 | #include <gmock/gmock.h> |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include "aidl.h" |
Steven Moreland | 4958524 | 2019-12-18 16:06:49 -0800 | [diff] [blame] | 27 | #include "aidl_checkapi.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 28 | #include "aidl_language.h" |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 29 | #include "aidl_to_cpp.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 30 | #include "aidl_to_java.h" |
| 31 | #include "options.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 32 | #include "tests/fake_io_delegate.h" |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 33 | |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 34 | using android::aidl::internals::parse_preprocessed_file; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 35 | using android::aidl::test::FakeIoDelegate; |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 36 | using android::base::StringPrintf; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 37 | using std::set; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 38 | using std::string; |
| 39 | using std::unique_ptr; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 40 | using std::vector; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 41 | using testing::TestParamInfo; |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 42 | using testing::internal::CaptureStderr; |
| 43 | using testing::internal::GetCapturedStderr; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 44 | |
| 45 | namespace android { |
| 46 | namespace aidl { |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 47 | namespace { |
| 48 | |
| 49 | const char kExpectedDepFileContents[] = |
| 50 | R"(place/for/output/p/IFoo.java : \ |
| 51 | p/IFoo.aidl |
| 52 | |
| 53 | p/IFoo.aidl : |
| 54 | )"; |
| 55 | |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 56 | const char kExpectedNinjaDepFileContents[] = |
| 57 | R"(place/for/output/p/IFoo.java : \ |
| 58 | p/IFoo.aidl |
| 59 | )"; |
| 60 | |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 61 | const char kExpectedParcelableDeclarationDepFileContents[] = |
| 62 | R"( : \ |
| 63 | p/Foo.aidl |
| 64 | |
| 65 | p/Foo.aidl : |
| 66 | )"; |
| 67 | |
| 68 | const char kExpectedStructuredParcelableDepFileContents[] = |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 69 | R"(place/for/output/p/Foo.java : \ |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 70 | p/Foo.aidl |
| 71 | |
| 72 | p/Foo.aidl : |
| 73 | )"; |
| 74 | |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 75 | const char kExpectedJavaParcelableOutputContests[] = |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 76 | R"(/* |
| 77 | * This file is auto-generated. DO NOT MODIFY. |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 78 | */ |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 79 | @android.annotation.Hide |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 80 | public class Rect implements android.os.Parcelable |
| 81 | { |
| 82 | // Comment |
| 83 | |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 84 | @android.annotation.Hide |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 85 | public int x = 5; |
| 86 | |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 87 | @android.annotation.Hide |
| 88 | @android.compat.annotation.UnsupportedAppUsage(expectedSignature = "dummy", implicitMember = "dummy", maxTargetSdk = 28, publicAlternatives = "dummy", trackingBug = 42L, overrideSourcePosition="Rect.aidl:7:1:10:14") |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 89 | public int y; |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 90 | |
| 91 | public android.os.ParcelFileDescriptor fd; |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 92 | public static final android.os.Parcelable.Creator<Rect> CREATOR = new android.os.Parcelable.Creator<Rect>() { |
| 93 | @Override |
| 94 | public Rect createFromParcel(android.os.Parcel _aidl_source) { |
| 95 | Rect _aidl_out = new Rect(); |
| 96 | _aidl_out.readFromParcel(_aidl_source); |
| 97 | return _aidl_out; |
| 98 | } |
| 99 | @Override |
| 100 | public Rect[] newArray(int _aidl_size) { |
| 101 | return new Rect[_aidl_size]; |
| 102 | } |
| 103 | }; |
| 104 | @Override public final void writeToParcel(android.os.Parcel _aidl_parcel, int _aidl_flag) |
| 105 | { |
| 106 | int _aidl_start_pos = _aidl_parcel.dataPosition(); |
| 107 | _aidl_parcel.writeInt(0); |
| 108 | _aidl_parcel.writeInt(x); |
| 109 | _aidl_parcel.writeInt(y); |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 110 | if ((fd!=null)) { |
| 111 | _aidl_parcel.writeInt(1); |
| 112 | fd.writeToParcel(_aidl_parcel, 0); |
| 113 | } |
| 114 | else { |
| 115 | _aidl_parcel.writeInt(0); |
| 116 | } |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 117 | int _aidl_end_pos = _aidl_parcel.dataPosition(); |
| 118 | _aidl_parcel.setDataPosition(_aidl_start_pos); |
| 119 | _aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos); |
| 120 | _aidl_parcel.setDataPosition(_aidl_end_pos); |
| 121 | } |
| 122 | public final void readFromParcel(android.os.Parcel _aidl_parcel) |
| 123 | { |
| 124 | int _aidl_start_pos = _aidl_parcel.dataPosition(); |
| 125 | int _aidl_parcelable_size = _aidl_parcel.readInt(); |
| 126 | if (_aidl_parcelable_size < 0) return; |
| 127 | try { |
| 128 | x = _aidl_parcel.readInt(); |
| 129 | if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return; |
| 130 | y = _aidl_parcel.readInt(); |
| 131 | if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return; |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 132 | if ((0!=_aidl_parcel.readInt())) { |
| 133 | fd = android.os.ParcelFileDescriptor.CREATOR.createFromParcel(_aidl_parcel); |
| 134 | } |
| 135 | else { |
| 136 | fd = null; |
| 137 | } |
| 138 | if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return; |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 139 | } finally { |
| 140 | _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size); |
| 141 | } |
| 142 | } |
| 143 | @Override public int describeContents() |
| 144 | { |
Jeongik Cha | 372a8c8 | 2018-12-12 16:31:11 +0900 | [diff] [blame] | 145 | return 0; |
| 146 | } |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 147 | } |
| 148 | )"; |
| 149 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 150 | } // namespace |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 151 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 152 | class AidlTest : public ::testing::TestWithParam<Options::Language> { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 153 | protected: |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 154 | AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_, |
| 155 | Options::Language lang, AidlError* error = nullptr, |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 156 | const vector<string> additional_arguments = {}) { |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 157 | io_delegate_.SetFileContents(path, contents); |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 158 | vector<string> args; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 159 | args.emplace_back("aidl"); |
| 160 | args.emplace_back("--lang=" + Options::LanguageToString(lang)); |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 161 | for (const string& s : additional_arguments) { |
| 162 | args.emplace_back(s); |
| 163 | } |
| 164 | for (const string& f : preprocessed_files_) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 165 | args.emplace_back("--preprocessed=" + f); |
| 166 | } |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 167 | for (const string& i : import_paths_) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 168 | args.emplace_back("--include=" + i); |
| 169 | } |
| 170 | args.emplace_back(path); |
| 171 | Options options = Options::From(args); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 172 | vector<AidlDefinedType*> defined_types; |
| 173 | vector<string> imported_files; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 174 | ImportResolver import_resolver{io_delegate_, path, import_paths_, {}}; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 175 | AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl( |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 176 | path, options, io_delegate_, &typenames_, &defined_types, &imported_files); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 177 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 178 | if (error != nullptr) { |
| 179 | *error = actual_error; |
| 180 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 181 | |
| 182 | if (actual_error != AidlError::OK) { |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
| 186 | EXPECT_EQ(1ul, defined_types.size()); |
| 187 | |
| 188 | return defined_types.front(); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 189 | } |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 190 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 191 | Options::Language GetLanguage() { return GetParam(); } |
| 192 | |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 193 | FakeIoDelegate io_delegate_; |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 194 | vector<string> preprocessed_files_; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 195 | set<string> import_paths_; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 196 | AidlTypenames typenames_; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 199 | // Instantiate the AidlTest parameterized suite, calling all of the TEST_P |
| 200 | // tests with each of the supported languages as a parameter. |
| 201 | INSTANTIATE_TEST_SUITE_P(AidlTestSuite, AidlTest, |
| 202 | testing::Values(Options::Language::CPP, Options::Language::JAVA, |
| 203 | Options::Language::NDK), |
| 204 | [](const testing::TestParamInfo<Options::Language>& info) { |
| 205 | return Options::LanguageToString(info.param); |
| 206 | }); |
| 207 | |
| 208 | TEST_P(AidlTest, AcceptMissingPackage) { |
| 209 | EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, GetLanguage())); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 212 | TEST_P(AidlTest, EndsInSingleLineComment) { |
| 213 | EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, GetLanguage())); |
Steven Moreland | 9c2988f | 2019-07-17 17:49:10 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 216 | TEST_P(AidlTest, RejectsArraysOfBinders) { |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 217 | import_paths_.emplace(""); |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 218 | io_delegate_.SetFileContents("bar/IBar.aidl", |
| 219 | "package bar; interface IBar {}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 220 | const string path = "foo/IFoo.aidl"; |
| 221 | const string contents = |
| 222 | "package foo;\n" |
| 223 | "import bar.IBar;\n" |
| 224 | "interface IFoo { void f(in IBar[] input); }"; |
| 225 | const string expected_stderr = "ERROR: foo/IFoo.aidl:3.27-32: Binder type cannot be an array\n"; |
| 226 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 227 | EXPECT_EQ(nullptr, Parse(path, contents, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 228 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 231 | TEST_P(AidlTest, SupportOnlyOutParameters) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 232 | 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] | 233 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, GetLanguage())); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 234 | typenames_.Reset(); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 235 | |
| 236 | const string interface_ibinder = "package a; interface IBaz { void f(out IBinder bar); }"; |
| 237 | const string expected_ibinder_stderr = |
| 238 | "ERROR: a/IBaz.aidl:1.47-51: 'out IBinder bar' can only be an in parameter.\n"; |
| 239 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 240 | EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 241 | EXPECT_EQ(expected_ibinder_stderr, GetCapturedStderr()); |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 242 | } |
| 243 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 244 | TEST_P(AidlTest, RejectsOnewayOutParameters) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 245 | const string oneway_interface = "package a; oneway interface IBar { void f(out int bar); }"; |
| 246 | const string expected_stderr = |
| 247 | "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] | 248 | CaptureStderr(); |
| 249 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage())); |
| 250 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 251 | typenames_.Reset(); |
| 252 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 253 | const string oneway_method = "package a; interface IBar { oneway void f(out int bar); }"; |
| 254 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 255 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 256 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 259 | TEST_P(AidlTest, RejectsOnewayNonVoidReturn) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 260 | const string oneway_method = "package a; interface IFoo { oneway int f(); }"; |
| 261 | const string expected_stderr = |
| 262 | "ERROR: a/IFoo.aidl:1.39-41: oneway method 'f' cannot return a value\n"; |
| 263 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 264 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 265 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 268 | TEST_P(AidlTest, RejectsNullablePrimitive) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 269 | const string oneway_method = "package a; interface IFoo { @nullable int f(); }"; |
| 270 | const string expected_stderr = |
| 271 | "ERROR: a/IFoo.aidl:1.38-42: Primitive type cannot get nullable annotation\n"; |
| 272 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 273 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 274 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 277 | TEST_P(AidlTest, RejectsDuplicatedArgumentNames) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 278 | const string method = "package a; interface IFoo { void f(int a, int a); }"; |
| 279 | const string expected_stderr = |
| 280 | "ERROR: a/IFoo.aidl:1.33-35: method 'f' has duplicate argument name 'a'\n"; |
| 281 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 282 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 283 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | b3cd3c7 | 2018-10-11 12:37:45 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 286 | TEST_P(AidlTest, RejectsDuplicatedAnnotationParams) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 287 | const string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }"; |
| 288 | const string expected_stderr = "ERROR: a/IFoo.aidl:1.56-62: Trying to redefine parameter foo.\n"; |
| 289 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 290 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 291 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 294 | TEST_P(AidlTest, RejectUnsupportedInterfaceAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 295 | AidlError error; |
| 296 | const string method = "package a; @nullable interface IFoo { int f(); }"; |
| 297 | const string expected_stderr = |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 298 | "ERROR: a/IFoo.aidl:1.21-31: 'nullable' is not a supported annotation for this node. " |
| 299 | "It must be one of: Hide, UnsupportedAppUsage, VintfStability\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 300 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 301 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 302 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 303 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 306 | TEST_P(AidlTest, RejectUnsupportedTypeAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 307 | AidlError error; |
| 308 | const string method = "package a; interface IFoo { @JavaOnlyStableParcelable int f(); }"; |
| 309 | const string expected_stderr = |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 310 | "ERROR: a/IFoo.aidl:1.54-58: 'JavaOnlyStableParcelable' is not a supported annotation " |
| 311 | "for this node. It must be one of: Hide, UnsupportedAppUsage, nullable, utf8InCpp\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 312 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 313 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 314 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 315 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 318 | TEST_P(AidlTest, RejectUnsupportedParcelableAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 319 | AidlError error; |
| 320 | const string method = "package a; @nullable parcelable IFoo cpp_header \"IFoo.h\";"; |
| 321 | const string expected_stderr = |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 322 | "ERROR: a/Foo.aidl:1.32-37: 'nullable' is not a supported annotation for this node. " |
| 323 | "It must be one of: Hide, JavaOnlyStableParcelable, UnsupportedAppUsage, VintfStability\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 324 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 325 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 326 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 327 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 330 | TEST_P(AidlTest, RejectUnsupportedParcelableDefineAnnotations) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 331 | AidlError error; |
| 332 | const string method = "package a; @nullable parcelable Foo { String a; String b; }"; |
| 333 | const string expected_stderr = |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 334 | "ERROR: a/Foo.aidl:1.32-36: 'nullable' is not a supported annotation for this node. " |
| 335 | "It must be one of: Hide, UnsupportedAppUsage, VintfStability\n"; |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 336 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 337 | EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 338 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 339 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 342 | TEST_P(AidlTest, ParsesNullableAnnotation) { |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 343 | for (auto is_nullable: {true, false}) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 344 | auto parse_result = Parse("a/IFoo.aidl", |
| 345 | StringPrintf("package a; interface IFoo {%s String f(); }", |
| 346 | (is_nullable) ? "@nullable" : ""), |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 347 | typenames_, GetLanguage()); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 348 | ASSERT_NE(nullptr, parse_result); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 349 | const AidlInterface* interface = parse_result->AsInterface(); |
| 350 | ASSERT_NE(nullptr, interface); |
| 351 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 352 | EXPECT_EQ(interface->GetMethods()[0]->GetType().IsNullable(), is_nullable); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 353 | typenames_.Reset(); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 357 | TEST_P(AidlTest, ParsesUtf8Annotations) { |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 358 | for (auto is_utf8: {true, false}) { |
| 359 | auto parse_result = Parse( |
| 360 | "a/IFoo.aidl", |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 361 | StringPrintf("package a; interface IFoo {%s String f(); }", (is_utf8) ? "@utf8InCpp" : ""), |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 362 | typenames_, GetLanguage()); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 363 | ASSERT_NE(nullptr, parse_result); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 364 | const AidlInterface* interface = parse_result->AsInterface(); |
| 365 | ASSERT_NE(nullptr, interface); |
| 366 | ASSERT_FALSE(interface->GetMethods().empty()); |
| 367 | EXPECT_EQ(interface->GetMethods()[0]->GetType().IsUtf8InCpp(), is_utf8); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 368 | typenames_.Reset(); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 372 | TEST_P(AidlTest, VintfRequiresStructuredAndStability) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 373 | AidlError error; |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 374 | const string expected_stderr = |
| 375 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: " |
| 376 | "\"vintf\"'\n" |
| 377 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface " |
| 378 | "--structured\n"; |
| 379 | CaptureStderr(); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 380 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 381 | GetLanguage(), &error)); |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 382 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 383 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 386 | TEST_P(AidlTest, VintfRequiresStructured) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 387 | AidlError error; |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 388 | const string expected_stderr = |
| 389 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface " |
| 390 | "--structured\n"; |
| 391 | CaptureStderr(); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 392 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 393 | GetLanguage(), &error, {"--stability", "vintf"})); |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 394 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 395 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 398 | TEST_P(AidlTest, VintfRequiresSpecifiedStability) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 399 | AidlError error; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 400 | const string expected_stderr = |
| 401 | "ERROR: IFoo.aidl:1.16-26: Must compile @VintfStability type w/ aidl_interface 'stability: " |
| 402 | "\"vintf\"'\n"; |
| 403 | CaptureStderr(); |
| 404 | ASSERT_EQ(nullptr, Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 405 | GetLanguage(), &error, {"--structured"})); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 406 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 407 | ASSERT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 410 | TEST_P(AidlTest, ParsesStabilityAnnotations) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 411 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 412 | auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, |
| 413 | GetLanguage(), &error, {"--structured", "--stability", "vintf"}); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 414 | ASSERT_EQ(AidlError::OK, error); |
| 415 | ASSERT_NE(nullptr, parse_result); |
| 416 | const AidlInterface* interface = parse_result->AsInterface(); |
| 417 | ASSERT_NE(nullptr, interface); |
| 418 | ASSERT_TRUE(interface->IsVintfStability()); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 419 | typenames_.Reset(); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 422 | TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) { |
| 423 | Options java_options = Options::From("aidl -o out --structured a/Foo.aidl"); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 424 | Options cpp_options = Options::From("aidl --lang=cpp -o out -h out/include a/Foo.aidl"); |
| 425 | Options cpp_structured_options = |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 426 | Options::From("aidl --lang=cpp --structured -o out -h out/include a/Foo.aidl"); |
| 427 | io_delegate_.SetFileContents( |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 428 | "a/Foo.aidl", |
| 429 | StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo cpp_header \"Foo.h\" ;")); |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 430 | |
| 431 | EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_)); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 432 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 433 | const string expected_stderr = |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 434 | "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] | 435 | "Parcelable must be defined in AIDL directly.\n"; |
| 436 | CaptureStderr(); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 437 | EXPECT_NE(0, ::android::aidl::compile_aidl(cpp_structured_options, io_delegate_)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 438 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | 64783ed | 2019-06-07 18:30:54 +0900 | [diff] [blame] | 439 | } |
| 440 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 441 | TEST_P(AidlTest, AcceptsOneway) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 442 | const string oneway_method = "package a; interface IFoo { oneway void f(int a); }"; |
| 443 | 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] | 444 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 445 | typenames_.Reset(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 446 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, GetLanguage())); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 447 | } |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 448 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 449 | TEST_P(AidlTest, AcceptsAnnotatedOnewayMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 450 | const string oneway_method = |
| 451 | "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 452 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage())); |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Steven Moreland | 65297cc | 2020-04-20 20:17:36 -0700 | [diff] [blame] | 455 | TEST_P(AidlTest, AnnotationsInMultiplePlaces) { |
| 456 | const string oneway_method = |
| 457 | "package a; interface IFoo { @UnsupportedAppUsage oneway @Hide void f(int a); }"; |
| 458 | const AidlDefinedType* defined = Parse("a/IFoo.aidl", oneway_method, typenames_, GetLanguage()); |
| 459 | ASSERT_NE(nullptr, defined); |
| 460 | const AidlInterface* iface = defined->AsInterface(); |
| 461 | ASSERT_NE(nullptr, iface); |
| 462 | |
| 463 | const auto& methods = iface->GetMethods(); |
| 464 | ASSERT_EQ(1u, methods.size()); |
| 465 | const auto& method = methods[0]; |
| 466 | const AidlTypeSpecifier& ret_type = method->GetType(); |
| 467 | |
| 468 | // TODO(b/151102494): these annotations should be on the method |
| 469 | ASSERT_NE(nullptr, ret_type.UnsupportedAppUsage()); |
| 470 | ASSERT_TRUE(ret_type.IsHide()); |
| 471 | } |
| 472 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 473 | TEST_P(AidlTest, WritesComments) { |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 474 | string foo_interface = |
| 475 | "package a; /* foo */ interface IFoo {" |
| 476 | " /* i */ int i();" |
| 477 | " /* j */ @nullable String j();" |
| 478 | " /* k */ @UnsupportedAppUsage oneway void k(int a); }"; |
| 479 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 480 | auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, GetLanguage()); |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 481 | EXPECT_NE(nullptr, parse_result); |
| 482 | EXPECT_EQ("/* foo */", parse_result->GetComments()); |
| 483 | |
| 484 | const AidlInterface* interface = parse_result->AsInterface(); |
| 485 | EXPECT_EQ("/* i */", interface->GetMethods()[0]->GetComments()); |
| 486 | EXPECT_EQ("/* j */", interface->GetMethods()[1]->GetComments()); |
| 487 | EXPECT_EQ("/* k */", interface->GetMethods()[2]->GetComments()); |
| 488 | } |
| 489 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 490 | TEST_F(AidlTest, ParsesPreprocessedFile) { |
| 491 | string simple_content = "parcelable a.Foo;\ninterface b.IBar;"; |
| 492 | io_delegate_.SetFileContents("path", simple_content); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 493 | EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 494 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_)); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 495 | EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved); |
| 496 | EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) { |
| 500 | string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t"; |
| 501 | io_delegate_.SetFileContents("path", simple_content); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 502 | |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 503 | EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").is_resolved); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 504 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_)); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 505 | EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").is_resolved); |
| 506 | EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").is_resolved); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 509 | TEST_P(AidlTest, PreferImportToPreprocessed) { |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 510 | io_delegate_.SetFileContents("preprocessed", "interface another.IBar;"); |
| 511 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; " |
| 512 | "interface IBar {}"); |
| 513 | preprocessed_files_.push_back("preprocessed"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 514 | import_paths_.emplace(""); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 515 | 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] | 516 | typenames_, GetLanguage()); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 517 | EXPECT_NE(nullptr, parse_result); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 518 | |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 519 | // We expect to know about both kinds of IBar |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 520 | EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved); |
| 521 | EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 522 | // But if we request just "IBar" we should get our imported one. |
Steven Moreland | 02e012e | 2018-08-02 14:58:10 -0700 | [diff] [blame] | 523 | AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, ""); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 524 | ambiguous_type.Resolve(typenames_); |
| 525 | EXPECT_EQ("one.IBar", ambiguous_type.GetName()); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 528 | // Special case of PreferImportToPreprocessed. Imported type should be preferred |
| 529 | // even when the preprocessed file already has the same type. |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 530 | TEST_P(AidlTest, B147918827) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 531 | io_delegate_.SetFileContents("preprocessed", "interface another.IBar;\ninterface one.IBar;"); |
| 532 | io_delegate_.SetFileContents("one/IBar.aidl", |
| 533 | "package one; " |
| 534 | "interface IBar {}"); |
| 535 | preprocessed_files_.push_back("preprocessed"); |
| 536 | import_paths_.emplace(""); |
| 537 | 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] | 538 | typenames_, GetLanguage()); |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 539 | EXPECT_NE(nullptr, parse_result); |
| 540 | |
| 541 | // We expect to know about both kinds of IBar |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 542 | EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").is_resolved); |
| 543 | EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved); |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 544 | // But if we request just "IBar" we should get our imported one. |
| 545 | AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, ""); |
| 546 | ambiguous_type.Resolve(typenames_); |
| 547 | EXPECT_EQ("one.IBar", ambiguous_type.GetName()); |
| 548 | } |
| 549 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 550 | TEST_F(AidlTest, WritePreprocessedFile) { |
| 551 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 552 | "package p; parcelable Outer.Inner;"); |
| 553 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;" |
| 554 | "interface IBar {}"); |
| 555 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 556 | vector<string> args { |
| 557 | "aidl", |
| 558 | "--preprocess", |
| 559 | "preprocessed", |
| 560 | "p/Outer.aidl", |
| 561 | "one/IBar.aidl"}; |
| 562 | Options options = Options::From(args); |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 563 | EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_)); |
| 564 | |
| 565 | string output; |
| 566 | EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output)); |
| 567 | EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output); |
| 568 | } |
| 569 | |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 570 | TEST_F(AidlTest, JavaParcelableOutput) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 571 | io_delegate_.SetFileContents( |
| 572 | "Rect.aidl", |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 573 | "@Hide\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 574 | "parcelable Rect {\n" |
| 575 | " // Comment\n" |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 576 | " @Hide\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 577 | " int x=5;\n" |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 578 | " @Hide\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 579 | " @UnsupportedAppUsage(maxTargetSdk = 28, trackingBug = 42, implicitMember = \"dummy\", " |
Andrei Onea | 07fa3e0 | 2019-07-04 17:55:19 +0100 | [diff] [blame] | 580 | "expectedSignature = \"dummy\", publicAlternatives = \"d\" \n + \"u\" + \n \"m\" \n + \"m\" " |
| 581 | "+ \"y\")\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 582 | " int y;\n" |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 583 | " ParcelFileDescriptor fd;\n" |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 584 | "}"); |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 585 | |
| 586 | vector<string> args{"aidl", "Rect.aidl"}; |
| 587 | Options options = Options::From(args); |
| 588 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 589 | |
| 590 | string output; |
| 591 | EXPECT_TRUE(io_delegate_.GetWrittenContents("Rect.java", &output)); |
Jiyong Park | d4e73c8 | 2019-07-24 21:39:15 +0900 | [diff] [blame] | 592 | EXPECT_EQ(kExpectedJavaParcelableOutputContests, output); |
Jeongik Cha | a755c2a | 2018-12-12 16:28:23 +0900 | [diff] [blame] | 593 | } |
| 594 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 595 | TEST_P(AidlTest, RequireOuterClass) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 596 | 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] | 597 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 598 | "package p; parcelable Outer.Inner;"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 599 | import_paths_.emplace(""); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 600 | CaptureStderr(); |
| 601 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 602 | "package p; import p.Outer; interface IFoo { void f(in Inner c); }", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 603 | typenames_, GetLanguage())); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 604 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 607 | TEST_P(AidlTest, ParseCompoundParcelableFromPreprocess) { |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 608 | io_delegate_.SetFileContents("preprocessed", |
| 609 | "parcelable p.Outer.Inner;"); |
| 610 | preprocessed_files_.push_back("preprocessed"); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 611 | 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] | 612 | typenames_, GetLanguage()); |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 613 | // TODO(wiley): This should actually return nullptr because we require |
| 614 | // the outer class name. However, for legacy reasons, |
| 615 | // this behavior must be maintained. b/17415692 |
| 616 | EXPECT_NE(nullptr, parse_result); |
| 617 | } |
| 618 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 619 | TEST_F(AidlTest, FailOnParcelable) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 620 | const string expected_foo_stderr = |
| 621 | "ERROR: p/IFoo.aidl:1.22-27: Refusing to generate code with unstructured parcelables. " |
| 622 | "Declared parcelables should be in their own file and/or cannot be used with --structured " |
| 623 | "interfaces.\n"; |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 624 | io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;"); |
| 625 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 626 | // By default, we shouldn't fail on parcelable. |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 627 | Options options1 = Options::From("aidl p/IFoo.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 628 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 629 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 630 | EXPECT_EQ("", GetCapturedStderr()); |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 631 | |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 632 | // -b considers this an error |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 633 | Options options2 = Options::From("aidl -b p/IFoo.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 634 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 635 | EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 636 | EXPECT_EQ(expected_foo_stderr, GetCapturedStderr()); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 637 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 638 | const string expected_bar_stderr = |
| 639 | "ERROR: p/IBar.aidl:1.22-26: Refusing to generate code with unstructured parcelables. " |
| 640 | "Declared parcelables should be in their own file and/or cannot be used with --structured " |
| 641 | "interfaces.\n"; |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 642 | io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}"); |
| 643 | |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 644 | // With '-b' option, a parcelable and an interface should fail. |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 645 | Options options3 = Options::From("aidl p/IBar.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 646 | CaptureStderr(); |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 647 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 648 | EXPECT_EQ("", GetCapturedStderr()); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 649 | Options options4 = Options::From("aidl -b p/IBar.aidl"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 650 | CaptureStderr(); |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 651 | EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 652 | EXPECT_EQ(expected_bar_stderr, GetCapturedStderr()); |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 655 | TEST_P(AidlTest, StructuredFailOnUnstructuredParcelable) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 656 | const string expected_stderr = |
| 657 | "ERROR: ./o/WhoKnowsWhat.aidl:1.22-35: o.WhoKnowsWhat is not structured, but this is a " |
| 658 | "structured interface.\n"; |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 659 | io_delegate_.SetFileContents("o/WhoKnowsWhat.aidl", "package o; parcelable WhoKnowsWhat;"); |
| 660 | import_paths_.emplace(""); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 661 | AidlError error; |
| 662 | CaptureStderr(); |
| 663 | EXPECT_EQ( |
| 664 | nullptr, |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 665 | Parse("p/IFoo.aidl", |
| 666 | "package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 667 | typenames_, GetLanguage(), &error, {"--structured"})); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 668 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 669 | EXPECT_EQ(AidlError::NOT_STRUCTURED, error); |
Steven Moreland | 1eac5fa | 2018-08-27 19:35:05 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 672 | TEST_P(AidlTest, FailOnDuplicateConstantNames) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 673 | AidlError error; |
| 674 | const string expected_stderr = |
| 675 | "ERROR: p/IFoo.aidl:4.34-45: Found duplicate constant name 'DUPLICATED'\n"; |
| 676 | CaptureStderr(); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 677 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 678 | R"(package p; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 679 | interface IFoo { |
| 680 | const String DUPLICATED = "d"; |
| 681 | const int DUPLICATED = 1; |
| 682 | } |
| 683 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 684 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 685 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 686 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 689 | TEST_P(AidlTest, FailOnManyDefinedTypes) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 690 | AidlError error; |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 691 | const string expected_stderr = |
| 692 | "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] | 693 | CaptureStderr(); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 694 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 695 | R"(package p; |
| 696 | interface IFoo {} |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 697 | parcelable IBar {} |
| 698 | parcelable StructuredParcelable {} |
| 699 | interface IBaz {} |
Jeongik Cha | 2a5b4d8 | 2019-08-06 19:37:59 +0900 | [diff] [blame] | 700 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 701 | typenames_, GetLanguage(), &error)); |
Devin Moore | c054bf8 | 2020-03-10 16:31:48 -0700 | [diff] [blame] | 702 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 703 | // Parse success is important for clear error handling even if the cases aren't |
| 704 | // actually supported in code generation. |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 705 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 708 | TEST_P(AidlTest, FailOnNoDefinedTypes) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 709 | AidlError error; |
| 710 | const string expected_stderr = "ERROR: p/IFoo.aidl:1.11-11: syntax error, unexpected $end\n"; |
| 711 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 712 | 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] | 713 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 714 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 717 | TEST_P(AidlTest, FailOnMalformedConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 718 | AidlError error; |
| 719 | const string expected_stderr = |
| 720 | "ERROR: Could not parse hexvalue: 0xffffffffffffffffff at p/IFoo.aidl:3.50-70.\n"; |
| 721 | CaptureStderr(); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 722 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 723 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 724 | interface IFoo { |
| 725 | const int BAD_HEX_VALUE = 0xffffffffffffffffff; |
| 726 | } |
| 727 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 728 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 729 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 730 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 733 | TEST_P(AidlTest, ParsePositiveConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 734 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 735 | auto parse_result = Parse("p/IFoo.aidl", |
| 736 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 737 | interface IFoo { |
| 738 | const int POSITIVE_HEX_VALUE = 0xf5; |
| 739 | } |
| 740 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 741 | typenames_, GetLanguage(), &error); |
| 742 | EXPECT_NE(nullptr, parse_result); |
| 743 | const AidlInterface* interface = parse_result->AsInterface(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 744 | ASSERT_NE(nullptr, interface); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 745 | const auto& cpp_constants = interface->GetConstantDeclarations(); |
| 746 | EXPECT_EQ((size_t)1, cpp_constants.size()); |
| 747 | EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_constants[0]->GetName()); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 748 | EXPECT_TRUE(cpp_constants[0]->CheckValid(typenames_)); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 749 | EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator)); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 752 | TEST_P(AidlTest, ParseNegativeConstHexValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 753 | AidlError error; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 754 | auto parse_result = Parse("p/IFoo.aidl", |
| 755 | R"(package p; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 756 | interface IFoo { |
| 757 | const int NEGATIVE_HEX_VALUE = 0xffffffff; |
| 758 | } |
| 759 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 760 | typenames_, GetLanguage(), &error); |
| 761 | ASSERT_NE(nullptr, parse_result); |
| 762 | const AidlInterface* interface = parse_result->AsInterface(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 763 | ASSERT_NE(nullptr, interface); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 764 | const auto& cpp_constants = interface->GetConstantDeclarations(); |
| 765 | EXPECT_EQ((size_t)1, cpp_constants.size()); |
| 766 | EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_constants[0]->GetName()); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 767 | EXPECT_EQ(true, cpp_constants[0]->CheckValid(typenames_)); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 768 | EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator)); |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 771 | TEST_P(AidlTest, UnderstandsNestedParcelables) { |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 772 | io_delegate_.SetFileContents( |
| 773 | "p/Outer.aidl", |
| 774 | "package p; parcelable Outer.Inner cpp_header \"baz/header\";"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 775 | import_paths_.emplace(""); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 776 | const string input_path = "p/IFoo.aidl"; |
| 777 | const string input = "package p; import p.Outer; interface IFoo" |
| 778 | " { Outer.Inner get(); }"; |
| 779 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 780 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 781 | EXPECT_NE(nullptr, parse_result); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 782 | |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 783 | EXPECT_TRUE(typenames_.ResolveTypename("p.Outer.Inner").is_resolved); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 784 | // C++ uses "::" instead of "." to refer to a inner class. |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 785 | AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, ""); |
| 786 | EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_)); |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 789 | TEST_P(AidlTest, UnderstandsNativeParcelables) { |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 790 | io_delegate_.SetFileContents( |
| 791 | "p/Bar.aidl", |
Casey Dahlin | cd63921 | 2015-12-15 12:51:04 -0800 | [diff] [blame] | 792 | "package p; parcelable Bar cpp_header \"baz/header\";"); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 793 | import_paths_.emplace(""); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 794 | const string input_path = "p/IFoo.aidl"; |
| 795 | const string input = "package p; import p.Bar; interface IFoo { }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 796 | auto parse_result = Parse(input_path, input, typenames_, GetLanguage()); |
| 797 | EXPECT_NE(nullptr, parse_result); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 798 | EXPECT_TRUE(typenames_.ResolveTypename("p.Bar").is_resolved); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 799 | AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, ""); |
| 800 | native_type.Resolve(typenames_); |
| 801 | |
| 802 | EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type, typenames_)); |
| 803 | // C++ understands C++ specific stuff |
| 804 | EXPECT_EQ("::p::Bar", cpp::CppNameOf(native_type, typenames_)); |
| 805 | set<string> headers; |
| 806 | cpp::AddHeaders(native_type, typenames_, headers); |
| 807 | EXPECT_EQ(1u, headers.size()); |
| 808 | EXPECT_EQ(1u, headers.count("baz/header")); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 811 | TEST_F(AidlTest, WritesCorrectDependencyFile) { |
| 812 | // While the in tree build system always gives us an output file name, |
| 813 | // other android tools take advantage of our ability to infer the intended |
| 814 | // file name. This test makes sure we handle this correctly. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 815 | vector<string> args = { |
| 816 | "aidl", |
| 817 | "-d dep/file/path", |
| 818 | "-o place/for/output", |
| 819 | "p/IFoo.aidl"}; |
| 820 | Options options = Options::From(args); |
| 821 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 822 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 823 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 824 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 825 | EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents); |
| 826 | } |
| 827 | |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 828 | TEST_F(AidlTest, WritesCorrectDependencyFileNinja) { |
| 829 | // While the in tree build system always gives us an output file name, |
| 830 | // other android tools take advantage of our ability to infer the intended |
| 831 | // file name. This test makes sure we handle this correctly. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 832 | vector<string> args = { |
| 833 | "aidl", |
| 834 | "-d dep/file/path", |
| 835 | "--ninja", |
| 836 | "-o place/for/output", |
| 837 | "p/IFoo.aidl"}; |
| 838 | Options options = Options::From(args); |
| 839 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 840 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 841 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 842 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 843 | EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents); |
| 844 | } |
| 845 | |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 846 | TEST_F(AidlTest, WritesTrivialDependencyFileForParcelableDeclaration) { |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 847 | // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does |
| 848 | // this by calling aidl with every .aidl file it finds, then parsing the |
| 849 | // generated dependency files. Those that reference .java output files are |
| 850 | // for interfaces and those that do not are parcelables. However, for both |
| 851 | // parcelables and interfaces, we *must* generate a non-empty dependency file. |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 852 | vector<string> args = { |
| 853 | "aidl", |
| 854 | "-o place/for/output", |
| 855 | "-d dep/file/path", |
| 856 | "p/Foo.aidl"}; |
| 857 | Options options = Options::From(args); |
| 858 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 859 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 860 | string actual_dep_file_contents; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 861 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 862 | EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDeclarationDepFileContents); |
| 863 | } |
| 864 | |
| 865 | TEST_F(AidlTest, WritesDependencyFileForStructuredParcelable) { |
| 866 | vector<string> args = { |
| 867 | "aidl", |
| 868 | "--structured", |
| 869 | "-o place/for/output", |
| 870 | "-d dep/file/path", |
| 871 | "p/Foo.aidl"}; |
| 872 | Options options = Options::From(args); |
| 873 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo {int a;}"); |
| 874 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 875 | string actual_dep_file_contents; |
| 876 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents)); |
| 877 | EXPECT_EQ(actual_dep_file_contents, kExpectedStructuredParcelableDepFileContents); |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Jiyong Park | 9ca5c7e | 2019-10-17 15:01:14 +0900 | [diff] [blame] | 880 | TEST_F(AidlTest, NoJavaOutputForParcelableDeclaration) { |
| 881 | vector<string> args = { |
| 882 | "aidl", |
| 883 | "--lang=java", |
| 884 | "-o place/for/output", |
| 885 | "p/Foo.aidl"}; |
| 886 | Options options = Options::From(args); |
| 887 | io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;"); |
| 888 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 889 | string output_file_contents; |
| 890 | EXPECT_FALSE(io_delegate_.GetWrittenContents(options.OutputFile(), &output_file_contents)); |
| 891 | } |
| 892 | |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 893 | // TODO(b/136048684) |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 894 | TEST_P(AidlTest, PrimitiveList) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 895 | const string primitive_interface = |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 896 | "package a; interface IFoo {\n" |
| 897 | " List<int> foo(); }"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 898 | string expected_stderr; |
| 899 | switch (GetLanguage()) { |
| 900 | case Options::Language::CPP: |
| 901 | expected_stderr = |
| 902 | "ERROR: a/IFoo.aidl:2.1-7: List<int> is not supported. List in cpp supports only " |
| 903 | "String and IBinder.\n"; |
| 904 | break; |
| 905 | case Options::Language::JAVA: |
| 906 | expected_stderr = |
| 907 | "ERROR: a/IFoo.aidl:2.1-7: List<int> is not supported. List in Java supports only " |
| 908 | "String, IBinder, and ParcelFileDescriptor.\n"; |
| 909 | break; |
| 910 | case Options::Language::NDK: |
| 911 | expected_stderr = |
| 912 | "ERROR: a/IFoo.aidl:2.1-7: A generic type cannot have any primitive type parameters.\n"; |
| 913 | break; |
| 914 | default: |
| 915 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 916 | << "Unexpected Options::Language enumerator: " << static_cast<size_t>(GetLanguage()); |
| 917 | } |
| 918 | CaptureStderr(); |
| 919 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, typenames_, GetLanguage())); |
| 920 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 921 | typenames_.Reset(); |
| 922 | |
| 923 | string primitive_parcelable = |
| 924 | "package a; parcelable IFoo {\n" |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 925 | " List<int> foo;}"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 926 | CaptureStderr(); |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 927 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_parcelable, typenames_, GetLanguage())); |
| 928 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 929 | } |
| 930 | |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 931 | TEST_P(AidlTest, RejectsPrimitiveListInStableAidl) { |
| 932 | AidlError error; |
| 933 | string expected_stderr = |
| 934 | "ERROR: a/IFoo.aidl:2.7-11: " |
| 935 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 936 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 937 | "the receiving side. Consider switching to an array or a generic List/Map.\n"; |
| 938 | if (GetLanguage() != Options::Language::JAVA) { |
| 939 | expected_stderr = |
| 940 | "ERROR: a/IFoo.aidl:2.1-7: " |
| 941 | "Currently, only the Java backend supports non-generic List.\n"; |
| 942 | } |
| 943 | |
| 944 | const string primitive_interface = |
| 945 | "package a; interface IFoo {\n" |
| 946 | " List foo(); }"; |
| 947 | CaptureStderr(); |
| 948 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_interface, typenames_, GetLanguage(), &error, |
| 949 | {"--structured"})); |
| 950 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 951 | typenames_.Reset(); |
| 952 | |
| 953 | string primitive_parcelable = |
| 954 | "package a; parcelable IFoo {\n" |
| 955 | " List foo;}"; |
| 956 | CaptureStderr(); |
| 957 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", primitive_parcelable, typenames_, GetLanguage(), &error, |
| 958 | {"--structured"})); |
| 959 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 960 | } |
| 961 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 962 | TEST_F(AidlTest, ApiDump) { |
| 963 | io_delegate_.SetFileContents( |
| 964 | "foo/bar/IFoo.aidl", |
| 965 | "package foo.bar;\n" |
| 966 | "import foo.bar.Data;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 967 | "// comment @hide\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 968 | "interface IFoo {\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 969 | " /* @hide */\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 970 | " int foo(out int[] a, String b, boolean c, inout List<String> d);\n" |
| 971 | " int foo2(@utf8InCpp String x, inout List<String> y);\n" |
| 972 | " IFoo foo3(IFoo foo);\n" |
| 973 | " Data getData();\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 974 | " // @hide\n" |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 975 | " const int A = 1;\n" |
| 976 | " const String STR = \"Hello\";\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 977 | "}\n"); |
| 978 | io_delegate_.SetFileContents("foo/bar/Data.aidl", |
| 979 | "package foo.bar;\n" |
| 980 | "import foo.bar.IFoo;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 981 | "/* @hide*/\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 982 | "parcelable Data {\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 983 | " // @hide\n" |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 984 | " int x = 10;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 985 | " // @hide\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 986 | " int y;\n" |
| 987 | " IFoo foo;\n" |
| 988 | " List<IFoo> a;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 989 | " /*@hide2*/\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 990 | " List<foo.bar.IFoo> b;\n" |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 991 | " // It should be @hide property\n" |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 992 | " @nullable String[] c;\n" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 993 | "}\n"); |
| 994 | io_delegate_.SetFileContents("api.aidl", ""); |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 995 | vector<string> args = {"aidl", "--dumpapi", "--out=dump", "--include=.", |
| 996 | "foo/bar/IFoo.aidl", "foo/bar/Data.aidl"}; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 997 | Options options = Options::From(args); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 998 | bool result = dump_api(options, io_delegate_); |
| 999 | ASSERT_TRUE(result); |
| 1000 | string actual; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1001 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1002 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1003 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1004 | interface IFoo { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1005 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1006 | int foo(out int[] a, String b, boolean c, inout List<String> d); |
| 1007 | int foo2(@utf8InCpp String x, inout List<String> y); |
| 1008 | foo.bar.IFoo foo3(foo.bar.IFoo foo); |
| 1009 | foo.bar.Data getData(); |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1010 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1011 | const int A = 1; |
| 1012 | const String STR = "Hello"; |
| 1013 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1014 | )")); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1015 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1016 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1017 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1018 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1019 | parcelable Data { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1020 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1021 | int x = 10; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1022 | /* @hide */ |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1023 | int y; |
| 1024 | foo.bar.IFoo foo; |
| 1025 | List<foo.bar.IFoo> a; |
| 1026 | List<foo.bar.IFoo> b; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1027 | /* @hide */ |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1028 | @nullable String[] c; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1029 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1030 | )")); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1031 | } |
| 1032 | |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1033 | TEST_F(AidlTest, ApiDumpWithManualIds) { |
| 1034 | io_delegate_.SetFileContents( |
| 1035 | "foo/bar/IFoo.aidl", |
| 1036 | "package foo.bar;\n" |
| 1037 | "interface IFoo {\n" |
| 1038 | " int foo() = 1;\n" |
| 1039 | " int bar() = 2;\n" |
| 1040 | " int baz() = 10;\n" |
| 1041 | "}\n"); |
| 1042 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1043 | vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"}; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1044 | Options options = Options::From(args); |
| 1045 | bool result = dump_api(options, io_delegate_); |
| 1046 | ASSERT_TRUE(result); |
| 1047 | string actual; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1048 | EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual)); |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1049 | EXPECT_EQ(actual, string(kPreamble).append(R"(package foo.bar; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1050 | interface IFoo { |
| 1051 | int foo() = 1; |
| 1052 | int bar() = 2; |
| 1053 | int baz() = 10; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1054 | } |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 1055 | )")); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | TEST_F(AidlTest, ApiDumpWithManualIdsOnlyOnSomeMethods) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1059 | const string expected_stderr = |
| 1060 | "ERROR: foo/bar/IFoo.aidl:4.8-12: You must either assign id's to all methods or to none of " |
| 1061 | "them.\n"; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1062 | io_delegate_.SetFileContents( |
| 1063 | "foo/bar/IFoo.aidl", |
| 1064 | "package foo.bar;\n" |
| 1065 | "interface IFoo {\n" |
| 1066 | " int foo() = 1;\n" |
| 1067 | " int bar();\n" |
| 1068 | " int baz() = 10;\n" |
| 1069 | "}\n"); |
| 1070 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1071 | vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"}; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1072 | Options options = Options::From(args); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1073 | CaptureStderr(); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1074 | EXPECT_FALSE(dump_api(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1075 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1076 | } |
| 1077 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1078 | TEST_F(AidlTest, CheckNumGenericTypeSecifier) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1079 | const string expected_list_stderr = |
| 1080 | "ERROR: p/IFoo.aidl:1.37-41: List must have only one type parameter.\n"; |
| 1081 | const string expected_map_stderr = |
| 1082 | "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] | 1083 | Options options = Options::From("aidl p/IFoo.aidl IFoo.java"); |
| 1084 | io_delegate_.SetFileContents(options.InputFiles().front(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1085 | "package p; interface IFoo {" |
| 1086 | "void foo(List<String, String> a);}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1087 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1088 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1089 | EXPECT_EQ(expected_list_stderr, GetCapturedStderr()); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1090 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1091 | io_delegate_.SetFileContents(options.InputFiles().front(), |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1092 | "package p; interface IFoo {" |
| 1093 | "void foo(Map<String> a);}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1094 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1095 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1096 | EXPECT_EQ(expected_map_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1097 | } |
| 1098 | |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1099 | TEST_F(AidlTest, CheckTypeParameterInMapType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1100 | const string expected_stderr = |
| 1101 | "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] | 1102 | Options options = Options::From("aidl -I p p/IFoo.aidl"); |
| 1103 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar { String s; }"); |
| 1104 | |
| 1105 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1106 | "package p; interface IFoo {" |
| 1107 | "Map<String, Bar> foo();}"); |
| 1108 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1109 | |
| 1110 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1111 | "package p; interface IFoo {" |
| 1112 | "Map<Bar, Bar> foo();}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1113 | CaptureStderr(); |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1114 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1115 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 1116 | |
| 1117 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1118 | "package p; interface IFoo {" |
| 1119 | "Map<String, String> foo();}"); |
| 1120 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1121 | |
| 1122 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1123 | "package p; interface IFoo {" |
| 1124 | "Map<String, ParcelFileDescriptor> foo();}"); |
| 1125 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1126 | } |
| 1127 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1128 | TEST_F(AidlTest, WrongGenericType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1129 | 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] | 1130 | Options options = Options::From("aidl p/IFoo.aidl IFoo.java"); |
| 1131 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1132 | "package p; interface IFoo {" |
| 1133 | "String<String> foo(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1134 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1135 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1136 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | TEST_F(AidlTest, UserDefinedUnstructuredGenericParcelableType) { |
| 1140 | Options optionsForParcelable = Options::From("aidl -I p p/Bar.aidl"); |
| 1141 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, T>;"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1142 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1143 | EXPECT_NE(0, ::android::aidl::compile_aidl(optionsForParcelable, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1144 | EXPECT_EQ("ERROR: p/Bar.aidl:1.22-26: Every type parameter should be unique.\n", |
| 1145 | GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1146 | |
| 1147 | Options options = Options::From("aidl -I p p/IFoo.aidl"); |
| 1148 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar;"); |
| 1149 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1150 | "package p; interface IFoo {" |
| 1151 | "Bar<String, String> foo();}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1152 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1153 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1154 | 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] | 1155 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T>;"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1156 | CaptureStderr(); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1157 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1158 | EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar must have 1 type parameters, but got 2\n", |
| 1159 | GetCapturedStderr()); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1160 | io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, V>;"); |
| 1161 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1162 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1163 | "package p; interface IFoo {" |
| 1164 | "Bar<String, ParcelFileDescriptor> foo();}"); |
| 1165 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jeongik Cha | e74c86d | 2019-12-12 16:54:03 +0900 | [diff] [blame] | 1166 | |
| 1167 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1168 | "package p; interface IFoo {" |
| 1169 | "Bar<int, long> foo();}"); |
| 1170 | |
| 1171 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1172 | "package p; interface IFoo {" |
| 1173 | "Bar<int[], long[]> foo();}"); |
| 1174 | |
| 1175 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 1176 | } |
| 1177 | |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1178 | TEST_F(AidlTest, FailOnMultipleTypesInSingleFile) { |
| 1179 | std::vector<std::string> rawOptions{"aidl --lang=java -o out foo/bar/Foo.aidl", |
| 1180 | "aidl --lang=cpp -o out -h out/include foo/bar/Foo.aidl"}; |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1181 | for (const auto& rawOption : rawOptions) { |
| 1182 | string expected_stderr = |
| 1183 | "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] | 1184 | Options options = Options::From(rawOption); |
| 1185 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1186 | "package foo.bar;\n" |
| 1187 | "interface IFoo1 { int foo(); }\n" |
| 1188 | "interface IFoo2 { int foo(); }\n" |
| 1189 | "parcelable Data1 { int a; int b;}\n" |
| 1190 | "parcelable Data2 { int a; int b;}\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1191 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1192 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1193 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1194 | |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1195 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1196 | "package foo.bar;\n" |
| 1197 | "interface IFoo1 { int foo(); }\n" |
| 1198 | "interface IFoo2 { int foo(); }\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1199 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1200 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1201 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1202 | |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1203 | 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] | 1204 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1205 | "package foo.bar;\n" |
| 1206 | "parcelable Data1 { int a; int b;}\n" |
| 1207 | "parcelable Data2 { int a; int b;}\n"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1208 | CaptureStderr(); |
Jeongik Cha | 0e42601 | 2019-07-29 15:57:02 +0900 | [diff] [blame] | 1209 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1210 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
Devin Moore | bdba2a8 | 2020-03-31 15:19:02 -0700 | [diff] [blame] | 1214 | TEST_P(AidlTest, FailParseOnEmptyFile) { |
| 1215 | const string contents = ""; |
| 1216 | const string expected_stderr = "ERROR: a/IFoo.aidl:1.1-1: syntax error, unexpected $end\n"; |
| 1217 | CaptureStderr(); |
| 1218 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", contents, typenames_, GetLanguage())); |
| 1219 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1220 | } |
| 1221 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1222 | TEST_F(AidlTest, MultipleInputFiles) { |
| 1223 | Options options = Options::From( |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1224 | "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] | 1225 | |
| 1226 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1227 | "package foo.bar;\n" |
| 1228 | "import foo.bar.Data;\n" |
| 1229 | "interface IFoo { Data getData(); }\n"); |
| 1230 | |
| 1231 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1232 | "package foo.bar;\n" |
| 1233 | "import foo.bar.IFoo;\n" |
| 1234 | "parcelable Data { IFoo foo; }\n"); |
| 1235 | |
| 1236 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1237 | |
| 1238 | string content; |
| 1239 | for (const auto file : { |
| 1240 | "out/foo/bar/IFoo.java", "out/foo/bar/Data.java"}) { |
| 1241 | content.clear(); |
| 1242 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1243 | EXPECT_FALSE(content.empty()); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | TEST_F(AidlTest, MultipleInputFilesCpp) { |
| 1248 | Options options = Options::From("aidl --lang=cpp -o out -h out/include " |
Jiyong Park | 633246c | 2019-11-25 10:50:05 +0900 | [diff] [blame] | 1249 | "-I . foo/bar/IFoo.aidl foo/bar/Data.aidl"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1250 | |
| 1251 | io_delegate_.SetFileContents(options.InputFiles().at(0), |
| 1252 | "package foo.bar;\n" |
| 1253 | "import foo.bar.Data;\n" |
| 1254 | "interface IFoo { Data getData(); }\n"); |
| 1255 | |
| 1256 | io_delegate_.SetFileContents(options.InputFiles().at(1), |
| 1257 | "package foo.bar;\n" |
| 1258 | "import foo.bar.IFoo;\n" |
| 1259 | "parcelable Data { IFoo foo; }\n"); |
| 1260 | |
| 1261 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1262 | |
| 1263 | string content; |
| 1264 | for (const auto file : { |
Jiyong Park | b03551f | 2018-08-06 19:20:51 +0900 | [diff] [blame] | 1265 | "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp", |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1266 | "out/include/foo/bar/IFoo.h", "out/include/foo/bar/Data.h", |
| 1267 | "out/include/foo/bar/BpFoo.h", "out/include/foo/bar/BpData.h", |
| 1268 | "out/include/foo/bar/BnFoo.h", "out/include/foo/bar/BnData.h"}) { |
| 1269 | content.clear(); |
| 1270 | EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content)); |
| 1271 | EXPECT_FALSE(content.empty()); |
| 1272 | } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1273 | } |
| 1274 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1275 | TEST_F(AidlTest, ConflictWithMetaTransactionGetVersion) { |
| 1276 | const string expected_stderr = |
| 1277 | "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] | 1278 | Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl"); |
| 1279 | // int getInterfaceVersion() is one of the meta transactions |
| 1280 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1281 | "package p; interface IFoo {" |
| 1282 | "int getInterfaceVersion(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1283 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1284 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1285 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1286 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1287 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1288 | TEST_F(AidlTest, ConflictWithSimilarMetaTransaction) { |
| 1289 | // boolean getInterfaceVersion() is not a meta transaction, but should be |
| 1290 | // prevented because return type is not part of a method signature |
| 1291 | const string expected_stderr = |
| 1292 | "ERROR: p/IFoo.aidl:1.35-55: method getInterfaceVersion() is reserved for internal use.\n"; |
| 1293 | 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] | 1294 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1295 | "package p; interface IFoo {" |
| 1296 | "boolean getInterfaceVersion(); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1297 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1298 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1299 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1300 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1301 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1302 | TEST_F(AidlTest, ConflictWithMetaTransactionGetName) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1303 | // this is another reserved name |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1304 | const string expected_stderr = |
| 1305 | "ERROR: p/IFoo.aidl:1.34-53: method getTransactionName(int) is reserved for internal use.\n"; |
| 1306 | 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] | 1307 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1308 | "package p; interface IFoo {" |
| 1309 | "String getTransactionName(int code); }"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1310 | CaptureStderr(); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1311 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1312 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1313 | |
| 1314 | // this is not a meta interface method as it differs type arguments |
| 1315 | io_delegate_.SetFileContents(options.InputFiles().front(), |
| 1316 | "package p; interface IFoo {" |
| 1317 | "String getTransactionName(); }"); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 1318 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1319 | } |
| 1320 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1321 | TEST_F(AidlTest, DifferentOrderAnnotationsInCheckAPI) { |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 1322 | Options options = Options::From("aidl --checkapi old new"); |
| 1323 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1324 | "package p; interface IFoo{ @utf8InCpp @nullable String foo();}"); |
| 1325 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1326 | "package p; interface IFoo{ @nullable @utf8InCpp String foo();}"); |
| 1327 | |
| 1328 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1329 | } |
| 1330 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1331 | TEST_F(AidlTest, SuccessOnIdenticalApiDumps) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1332 | Options options = Options::From("aidl --checkapi old new"); |
| 1333 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo();}"); |
| 1334 | 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] | 1335 | |
| 1336 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1337 | } |
| 1338 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1339 | class AidlTestCompatibleChanges : public AidlTest { |
| 1340 | protected: |
| 1341 | Options options_ = Options::From("aidl --checkapi old new"); |
| 1342 | }; |
| 1343 | |
| 1344 | TEST_F(AidlTestCompatibleChanges, NewType) { |
| 1345 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1346 | "package p;" |
| 1347 | "interface IFoo {" |
| 1348 | " void foo(int a);" |
| 1349 | "}"); |
| 1350 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1351 | "package p;" |
| 1352 | "interface IFoo {" |
| 1353 | " void foo(int a);" |
| 1354 | "}"); |
| 1355 | io_delegate_.SetFileContents("new/p/IBar.aidl", |
| 1356 | "package p;" |
| 1357 | "interface IBar {" |
| 1358 | " void bar();" |
| 1359 | "}"); |
| 1360 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1361 | } |
| 1362 | |
| 1363 | TEST_F(AidlTestCompatibleChanges, NewMethod) { |
| 1364 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1365 | "package p;" |
| 1366 | "interface IFoo {" |
| 1367 | " void foo(int a);" |
| 1368 | "}"); |
| 1369 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1370 | "package p;" |
| 1371 | "interface IFoo {" |
| 1372 | " void foo(int a);" |
| 1373 | " void bar();" |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1374 | " void baz(in List<IFoo> arg);" |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1375 | "}"); |
| 1376 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1377 | } |
| 1378 | |
| 1379 | TEST_F(AidlTestCompatibleChanges, NewField) { |
| 1380 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1381 | "package p;" |
| 1382 | "parcelable Data {" |
| 1383 | " int foo;" |
| 1384 | "}"); |
| 1385 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1386 | "package p;" |
| 1387 | "parcelable Data {" |
| 1388 | " int foo;" |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 1389 | " int bar = 0;" |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1390 | " @nullable List<Data> list;" |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1391 | "}"); |
| 1392 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1393 | } |
| 1394 | |
| 1395 | TEST_F(AidlTestCompatibleChanges, NewEnumerator) { |
| 1396 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1397 | "package p;" |
| 1398 | "enum Enum {" |
| 1399 | " FOO = 1," |
| 1400 | "}"); |
| 1401 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1402 | "package p;" |
| 1403 | "enum Enum {" |
| 1404 | " FOO = 1," |
| 1405 | " BAR = 2," |
| 1406 | "}"); |
| 1407 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1408 | } |
| 1409 | |
| 1410 | TEST_F(AidlTestCompatibleChanges, ReorderedEnumerator) { |
| 1411 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1412 | "package p;" |
| 1413 | "enum Enum {" |
| 1414 | " FOO = 1," |
| 1415 | " BAR = 2," |
| 1416 | "}"); |
| 1417 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1418 | "package p;" |
| 1419 | "enum Enum {" |
| 1420 | " BAR = 2," |
| 1421 | " FOO = 1," |
| 1422 | "}"); |
| 1423 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1424 | } |
| 1425 | |
| 1426 | TEST_F(AidlTestCompatibleChanges, NewPackage) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1427 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1428 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1429 | "interface IFoo {" |
| 1430 | " void foo(int a);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1431 | "}"); |
| 1432 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1433 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1434 | "parcelable Data {" |
| 1435 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1436 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1437 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1438 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1439 | "interface IFoo {" |
| 1440 | " void foo(int a);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1441 | "}"); |
| 1442 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1443 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1444 | "parcelable Data {" |
| 1445 | " int foo;" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1446 | "}"); |
| 1447 | io_delegate_.SetFileContents("new/q/IFoo.aidl", |
| 1448 | "package q;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1449 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1450 | " void foo(int a);" |
| 1451 | "}"); |
| 1452 | io_delegate_.SetFileContents("new/q/Data.aidl", |
| 1453 | "package q;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1454 | "parcelable Data {" |
| 1455 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1456 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1457 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1458 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1459 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1460 | TEST_F(AidlTestCompatibleChanges, ArgNameChange) { |
| 1461 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1462 | "package p;" |
| 1463 | "interface IFoo {" |
| 1464 | " void foo(int a);" |
| 1465 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1466 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1467 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1468 | "interface IFoo {" |
| 1469 | " void foo(int b);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1470 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1471 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1472 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1473 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1474 | TEST_F(AidlTestCompatibleChanges, AddedConstValue) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1475 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 1476 | "package p; interface I {" |
| 1477 | "const int A = 1; }"); |
| 1478 | io_delegate_.SetFileContents("new/p/I.aidl", |
| 1479 | "package p ; interface I {" |
| 1480 | "const int A = 1; const int B = 2;}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1481 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
| 1482 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1483 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1484 | TEST_F(AidlTestCompatibleChanges, ChangedConstValueOrder) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1485 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 1486 | "package p; interface I {" |
| 1487 | "const int A = 1; const int B = 2;}"); |
| 1488 | io_delegate_.SetFileContents("new/p/I.aidl", |
| 1489 | "package p ; interface I {" |
| 1490 | "const int B = 2; const int A = 1;}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1491 | EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_)); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1492 | } |
| 1493 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1494 | class AidlTestIncompatibleChanges : public AidlTest { |
| 1495 | protected: |
| 1496 | Options options_ = Options::From("aidl --checkapi old new"); |
| 1497 | }; |
| 1498 | |
| 1499 | TEST_F(AidlTestIncompatibleChanges, RemovedType) { |
Devin Moore | d131d5e | 2020-03-31 10:39:10 -0700 | [diff] [blame] | 1500 | const string expected_stderr = "ERROR: new: API files have been removed: old/p/IFoo.aidl\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1501 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1502 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1503 | "interface IFoo {" |
| 1504 | " void foo(in String[] str);" |
| 1505 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1506 | "}"); |
Devin Moore | d131d5e | 2020-03-31 10:39:10 -0700 | [diff] [blame] | 1507 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1508 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | d131d5e | 2020-03-31 10:39:10 -0700 | [diff] [blame] | 1509 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | TEST_F(AidlTestIncompatibleChanges, RemovedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1513 | const string expected_stderr = |
| 1514 | "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] | 1515 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1516 | "package p;" |
| 1517 | "interface IFoo {" |
| 1518 | " void foo(in String[] str);" |
| 1519 | " void bar(@utf8InCpp String str);" |
| 1520 | "}"); |
| 1521 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1522 | "package p;" |
| 1523 | "interface IFoo {" |
| 1524 | " void foo(in String[] str);" |
| 1525 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1526 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1527 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1528 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 1531 | TEST_F(AidlTestIncompatibleChanges, UntypedListInInterface) { |
| 1532 | const string expected_stderr = |
| 1533 | "ERROR: new/p/IFoo.aidl:1.61-65: " |
| 1534 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 1535 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 1536 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 1537 | "ERROR: new/p/IFoo.aidl: Failed to read.\n"; |
| 1538 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1539 | "package p;" |
| 1540 | "interface IFoo {" |
| 1541 | " void foo(in String[] str);" |
| 1542 | "}"); |
| 1543 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1544 | "package p;" |
| 1545 | "interface IFoo {" |
| 1546 | " void foo(in String[] str);" |
| 1547 | " void bar(in List arg);" |
| 1548 | "}"); |
| 1549 | CaptureStderr(); |
| 1550 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 1551 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1552 | } |
| 1553 | |
| 1554 | TEST_F(AidlTestCompatibleChanges, UntypedListInParcelable) { |
| 1555 | const string expected_stderr = |
| 1556 | "ERROR: new/p/Data.aidl:1.54-59: " |
| 1557 | "Encountered an untyped List or Map. The use of untyped List/Map is " |
| 1558 | "prohibited because it is not guaranteed that the objects in the list are recognizable in " |
| 1559 | "the receiving side. Consider switching to an array or a generic List/Map.\n" |
| 1560 | "ERROR: new/p/Data.aidl: Failed to read.\n"; |
| 1561 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1562 | "package p;" |
| 1563 | "parcelable Data {" |
| 1564 | " int foo;" |
| 1565 | "}"); |
| 1566 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1567 | "package p;" |
| 1568 | "parcelable Data {" |
| 1569 | " int foo;" |
| 1570 | " @nullable List list;" |
| 1571 | "}"); |
| 1572 | CaptureStderr(); |
| 1573 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
| 1574 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 1575 | } |
| 1576 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1577 | TEST_F(AidlTestIncompatibleChanges, RemovedField) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1578 | const string expected_stderr = |
| 1579 | "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] | 1580 | io_delegate_.SetFileContents("old/p/Data.aidl", |
| 1581 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1582 | "parcelable Data {" |
| 1583 | " int foo;" |
| 1584 | " int bar;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1585 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1586 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1587 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1588 | "parcelable Data {" |
| 1589 | " int foo;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1590 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1591 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1592 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1593 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1594 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1595 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1596 | TEST_F(AidlTestIncompatibleChanges, RemovedEnumerator) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1597 | const string expected_stderr = |
| 1598 | "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] | 1599 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1600 | "package p;" |
| 1601 | "enum Enum {" |
| 1602 | " FOO = 1," |
| 1603 | " BAR = 2," |
| 1604 | "}"); |
| 1605 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1606 | "package p;" |
| 1607 | "enum Enum {" |
| 1608 | " BAR = 2," |
| 1609 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1610 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1611 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1612 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | TEST_F(AidlTestIncompatibleChanges, RenamedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1616 | const string expected_stderr = |
| 1617 | "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] | 1618 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1619 | "package p;" |
| 1620 | "interface IFoo {" |
| 1621 | " void foo(in String[] str);" |
| 1622 | " void bar(@utf8InCpp String str);" |
| 1623 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1624 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1625 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1626 | "interface IFoo {" |
| 1627 | " void foo(in String[] str);" |
| 1628 | " void bar2(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1629 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1630 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1631 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1632 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1635 | TEST_F(AidlTestIncompatibleChanges, RenamedType) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1636 | 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] | 1637 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1638 | "package p;" |
| 1639 | "interface IFoo {" |
| 1640 | " void foo(in String[] str);" |
| 1641 | " void bar(@utf8InCpp String str);" |
| 1642 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1643 | io_delegate_.SetFileContents("new/p/IFoo2.aidl", |
| 1644 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1645 | "interface IFoo2 {" |
| 1646 | " void foo(in String[] str);" |
| 1647 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1648 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1649 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1650 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1651 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1652 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1653 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1654 | TEST_F(AidlTestIncompatibleChanges, ChangedEnumerator) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1655 | const string expected_stderr = |
| 1656 | "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] | 1657 | io_delegate_.SetFileContents("old/p/Enum.aidl", |
| 1658 | "package p;" |
| 1659 | "enum Enum {" |
| 1660 | " FOO = 1," |
| 1661 | " BAR = 2," |
| 1662 | "}"); |
| 1663 | io_delegate_.SetFileContents("new/p/Enum.aidl", |
| 1664 | "package p;" |
| 1665 | "enum Enum {" |
| 1666 | " FOO = 3," |
| 1667 | " BAR = 2," |
| 1668 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1669 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1670 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1671 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | TEST_F(AidlTestIncompatibleChanges, ReorderedMethod) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1675 | const string expected_stderr = |
| 1676 | "ERROR: new/p/IFoo.aidl:1.67-71: Transaction ID changed: p.IFoo.foo(String[]) is changed " |
| 1677 | "from 0 to 1.\n" |
| 1678 | "ERROR: new/p/IFoo.aidl:1.33-37: Transaction ID changed: p.IFoo.bar(String) is changed from " |
| 1679 | "1 to 0.\n"; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1680 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1681 | "package p;" |
| 1682 | "interface IFoo {" |
| 1683 | " void foo(in String[] str);" |
| 1684 | " void bar(@utf8InCpp String str);" |
| 1685 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1686 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1687 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1688 | "interface IFoo {" |
| 1689 | " void bar(@utf8InCpp String str);" |
| 1690 | " void foo(in String[] str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1691 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1692 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1693 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1694 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | TEST_F(AidlTestIncompatibleChanges, ReorderedField) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1698 | const string expected_stderr = |
Jiyong Park | b07d993 | 2020-05-15 12:56:54 +0900 | [diff] [blame] | 1699 | "ERROR: new/p/Data.aidl:1.33-37: Reordered bar from 1 to 0.\n" |
| 1700 | "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] | 1701 | io_delegate_.SetFileContents("old/p/Data.aidl", |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1702 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1703 | "parcelable Data {" |
| 1704 | " int foo;" |
| 1705 | " int bar;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1706 | "}"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1707 | io_delegate_.SetFileContents("new/p/Data.aidl", |
| 1708 | "package p;" |
| 1709 | "parcelable Data {" |
| 1710 | " int bar;" |
| 1711 | " int foo;" |
| 1712 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1713 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1714 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1715 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1716 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1717 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1718 | TEST_F(AidlTestIncompatibleChanges, ChangedDirectionSpecifier) { |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 1719 | 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] | 1720 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1721 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1722 | "interface IFoo {" |
| 1723 | " void foo(in String[] str);" |
| 1724 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1725 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1726 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1727 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1728 | "interface IFoo {" |
| 1729 | " void foo(out String[] str);" |
| 1730 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1731 | "}"); |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 1732 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1733 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 1734 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1735 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1736 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1737 | TEST_F(AidlTestIncompatibleChanges, AddedAnnotation) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1738 | const string expected_stderr = |
| 1739 | "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] | 1740 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1741 | "package p;" |
| 1742 | "interface IFoo {" |
| 1743 | " void foo(in String[] str);" |
| 1744 | " void bar(@utf8InCpp String str);" |
| 1745 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1746 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1747 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1748 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1749 | " void foo(in @utf8InCpp String[] str);" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1750 | " void bar(@utf8InCpp String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1751 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1752 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1753 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1754 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1755 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1756 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1757 | TEST_F(AidlTestIncompatibleChanges, RemovedAnnotation) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1758 | const string expected_stderr = |
| 1759 | "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] | 1760 | io_delegate_.SetFileContents("old/p/IFoo.aidl", |
| 1761 | "package p;" |
| 1762 | "interface IFoo {" |
| 1763 | " void foo(in String[] str);" |
| 1764 | " void bar(@utf8InCpp String str);" |
| 1765 | "}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1766 | io_delegate_.SetFileContents("new/p/IFoo.aidl", |
| 1767 | "package p;" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1768 | "interface IFoo {" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1769 | " void foo(in String[] str);" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1770 | " void bar(String str);" |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1771 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1772 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1773 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1774 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1775 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1776 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1777 | TEST_F(AidlTestIncompatibleChanges, RemovedPackage) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1778 | 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] | 1779 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}"); |
| 1780 | io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}"); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1781 | io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1782 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1783 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1784 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1785 | } |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 1786 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1787 | TEST_F(AidlTestIncompatibleChanges, ChangedDefaultValue) { |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 1788 | 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] | 1789 | io_delegate_.SetFileContents("old/p/D.aidl", "package p; parcelable D { int a = 1; }"); |
| 1790 | 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] | 1791 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1792 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1793 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1794 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1795 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1796 | TEST_F(AidlTestIncompatibleChanges, RemovedConstValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1797 | const string expected_stderr = |
| 1798 | "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] | 1799 | io_delegate_.SetFileContents("old/p/I.aidl", |
| 1800 | "package p; interface I {" |
| 1801 | "const int A = 1; const int B = 2;}"); |
| 1802 | 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] | 1803 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1804 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1805 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1806 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1807 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1808 | TEST_F(AidlTestIncompatibleChanges, ChangedConstValue) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1809 | const string expected_stderr = |
| 1810 | "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] | 1811 | io_delegate_.SetFileContents("old/p/I.aidl", "package p; interface I { const int A = 1; }"); |
| 1812 | 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] | 1813 | CaptureStderr(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1814 | EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1815 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 1816 | } |
| 1817 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 1818 | TEST_F(AidlTest, RejectAmbiguousImports) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1819 | const string expected_stderr = |
| 1820 | "ERROR: p/IFoo.aidl: Duplicate files found for q.IBar from:\n" |
| 1821 | "dir1/q/IBar.aidl\n" |
| 1822 | "dir2/q/IBar.aidl\n" |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1823 | "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] | 1824 | Options options = Options::From("aidl --lang=java -o out -I dir1 -I dir2 p/IFoo.aidl"); |
| 1825 | io_delegate_.SetFileContents("p/IFoo.aidl", "package p; import q.IBar; interface IFoo{}"); |
| 1826 | io_delegate_.SetFileContents("dir1/q/IBar.aidl", "package q; interface IBar{}"); |
| 1827 | io_delegate_.SetFileContents("dir2/q/IBar.aidl", "package q; interface IBar{}"); |
| 1828 | |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1829 | CaptureStderr(); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 1830 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1831 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 1832 | } |
| 1833 | |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1834 | TEST_F(AidlTest, HandleManualIdAssignments) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1835 | const string expected_stderr = |
| 1836 | "ERROR: new/p/IFoo.aidl:1.32-36: Transaction ID changed: p.IFoo.foo() is changed from 10 to " |
| 1837 | "11.\n"; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1838 | Options options = Options::From("aidl --checkapi old new"); |
| 1839 | io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}"); |
| 1840 | 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] | 1841 | |
| 1842 | EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_)); |
| 1843 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 1844 | 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] | 1845 | CaptureStderr(); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1846 | EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1847 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1848 | } |
| 1849 | |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 1850 | TEST_F(AidlTest, ParcelFileDescriptorIsBuiltinType) { |
| 1851 | Options javaOptions = Options::From("aidl --lang=java -o out p/IFoo.aidl"); |
| 1852 | Options cppOptions = Options::From("aidl --lang=cpp -h out -o out p/IFoo.aidl"); |
| 1853 | |
| 1854 | // use without import |
| 1855 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1856 | "package p; interface IFoo{ void foo(in ParcelFileDescriptor fd);}"); |
| 1857 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 1858 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
| 1859 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 1860 | // use without import but with full name |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 1861 | io_delegate_.SetFileContents( |
| 1862 | "p/IFoo.aidl", |
| 1863 | "package p; interface IFoo{ void foo(in android.os.ParcelFileDescriptor fd);}"); |
| 1864 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 1865 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
| 1866 | |
| 1867 | // use with import (as before) |
| 1868 | io_delegate_.SetFileContents("p/IFoo.aidl", |
| 1869 | "package p;" |
| 1870 | "import android.os.ParcelFileDescriptor;" |
| 1871 | "interface IFoo{" |
| 1872 | " void foo(in ParcelFileDescriptor fd);" |
| 1873 | "}"); |
| 1874 | EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_)); |
| 1875 | EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_)); |
| 1876 | } |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 1877 | |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1878 | TEST_F(AidlTest, ManualIds) { |
| 1879 | Options options = Options::From("aidl --lang=java -o out IFoo.aidl"); |
| 1880 | io_delegate_.SetFileContents("IFoo.aidl", |
| 1881 | "interface IFoo {\n" |
| 1882 | " void foo() = 0;\n" |
| 1883 | " void bar() = 1;\n" |
| 1884 | "}"); |
| 1885 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1886 | } |
| 1887 | |
| 1888 | TEST_F(AidlTest, ManualIdsWithMetaTransactions) { |
| 1889 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 1890 | io_delegate_.SetFileContents("IFoo.aidl", |
| 1891 | "interface IFoo {\n" |
| 1892 | " void foo() = 0;\n" |
| 1893 | " void bar() = 1;\n" |
| 1894 | "}"); |
| 1895 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1896 | } |
| 1897 | |
| 1898 | TEST_F(AidlTest, FailOnDuplicatedIds) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1899 | const string expected_stderr = |
| 1900 | "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] | 1901 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 1902 | io_delegate_.SetFileContents("IFoo.aidl", |
| 1903 | "interface IFoo {\n" |
| 1904 | " void foo() = 3;\n" |
| 1905 | " void bar() = 3;\n" |
| 1906 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1907 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1908 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1909 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | TEST_F(AidlTest, FailOnOutOfRangeIds) { |
| 1913 | // 16777115 is kLastMetaMethodId + 1 |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1914 | const string expected_stderr = |
| 1915 | "ERROR: IFoo.aidl:3.7-11: Found out of bounds id (16777115) for method bar. " |
| 1916 | "Value for id must be between 0 and 16777114 inclusive.\n"; |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1917 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 1918 | io_delegate_.SetFileContents("IFoo.aidl", |
| 1919 | "interface IFoo {\n" |
| 1920 | " void foo() = 3;\n" |
| 1921 | " void bar() = 16777115;\n" |
| 1922 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1923 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1924 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1925 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | TEST_F(AidlTest, FailOnPartiallyAssignedIds) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1929 | const string expected_stderr = |
| 1930 | "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] | 1931 | Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl"); |
| 1932 | io_delegate_.SetFileContents("IFoo.aidl", |
| 1933 | "interface IFoo {\n" |
| 1934 | " void foo() = 3;\n" |
| 1935 | " void bar();\n" |
| 1936 | "}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1937 | CaptureStderr(); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1938 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1939 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 1940 | } |
| 1941 | |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 1942 | TEST_F(AidlTest, AllowDuplicatedImportPaths) { |
| 1943 | Options options = Options::From("aidl --lang=java -I dir -I dir IFoo.aidl"); |
| 1944 | io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}"); |
| 1945 | io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}"); |
| 1946 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1947 | } |
| 1948 | |
| 1949 | TEST_F(AidlTest, FailOnAmbiguousImports) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1950 | const string expected_stderr = |
| 1951 | "ERROR: IFoo.aidl: Duplicate files found for IBar from:\n" |
| 1952 | "dir/IBar.aidl\n" |
| 1953 | "dir2/IBar.aidl\n" |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 1954 | "ERROR: IFoo.aidl: Couldn't find import for class IBar\n"; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1955 | |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 1956 | Options options = Options::From("aidl --lang=java -I dir -I dir2 IFoo.aidl"); |
| 1957 | io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}"); |
| 1958 | io_delegate_.SetFileContents("dir2/IBar.aidl", "interface IBar{}"); |
| 1959 | io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}"); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1960 | CaptureStderr(); |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 1961 | EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1962 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Jiyong Park | c681625 | 2019-07-08 08:12:28 +0900 | [diff] [blame] | 1963 | } |
| 1964 | |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 1965 | TEST_F(AidlTest, UnusedImportDoesNotContributeInclude) { |
| 1966 | io_delegate_.SetFileContents("a/b/IFoo.aidl", |
| 1967 | "package a.b;\n" |
| 1968 | "import a.b.IBar;\n" |
| 1969 | "import a.b.IQux;\n" |
| 1970 | "interface IFoo { IQux foo(); }\n"); |
| 1971 | io_delegate_.SetFileContents("a/b/IBar.aidl", "package a.b; interface IBar { void foo(); }"); |
| 1972 | io_delegate_.SetFileContents("a/b/IQux.aidl", "package a.b; interface IQux { void foo(); }"); |
| 1973 | |
| 1974 | Options options = Options::From("aidl --lang=ndk a/b/IFoo.aidl -I . -o out -h out/include"); |
| 1975 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1976 | |
| 1977 | string output; |
| 1978 | EXPECT_TRUE(io_delegate_.GetWrittenContents("out/include/aidl/a/b/IFoo.h", &output)); |
| 1979 | // IBar was imported but wasn't used. include is not expected. |
| 1980 | EXPECT_THAT(output, Not(testing::HasSubstr("#include <aidl/a/b/IBar.h>"))); |
| 1981 | // IBar was imported and used. include is expected. |
| 1982 | EXPECT_THAT(output, (testing::HasSubstr("#include <aidl/a/b/IQux.h>"))); |
| 1983 | } |
| 1984 | |
Jiyong Park | 56f73d7 | 2019-06-11 12:20:28 +0900 | [diff] [blame] | 1985 | class AidlOutputPathTest : public AidlTest { |
| 1986 | protected: |
| 1987 | void SetUp() override { |
| 1988 | AidlTest::SetUp(); |
| 1989 | io_delegate_.SetFileContents("sub/dir/foo/bar/IFoo.aidl", "package foo.bar; interface IFoo {}"); |
| 1990 | } |
| 1991 | |
| 1992 | void Test(const Options& options, const std::string expected_output_path) { |
| 1993 | EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); |
| 1994 | // check the existence |
| 1995 | EXPECT_TRUE(io_delegate_.GetWrittenContents(expected_output_path, nullptr)); |
| 1996 | } |
| 1997 | }; |
| 1998 | |
| 1999 | TEST_F(AidlOutputPathTest, OutDirWithNoOutputFile) { |
| 2000 | // <out_dir> / <package_name> / <type_name>.java |
| 2001 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl"), "out/foo/bar/IFoo.java"); |
| 2002 | } |
| 2003 | |
| 2004 | TEST_F(AidlOutputPathTest, OutDirWithOutputFile) { |
| 2005 | // when output file is explicitly set, it is always respected. -o option is |
| 2006 | // ignored. |
| 2007 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java"); |
| 2008 | } |
| 2009 | |
| 2010 | TEST_F(AidlOutputPathTest, NoOutDirWithOutputFile) { |
| 2011 | Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java"); |
| 2012 | } |
| 2013 | |
| 2014 | TEST_F(AidlOutputPathTest, NoOutDirWithNoOutputFile) { |
| 2015 | // output is the same as the input file except for the suffix |
| 2016 | Test(Options::From("aidl sub/dir/foo/bar/IFoo.aidl"), "sub/dir/foo/bar/IFoo.java"); |
| 2017 | } |
| 2018 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2019 | TEST_P(AidlTest, FailOnOutOfBoundsInt32MaxConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2020 | AidlError error; |
| 2021 | const string expected_stderr = |
| 2022 | "ERROR: p/IFoo.aidl:3.58-69: Invalid type specifier for an int64 literal: int\n"; |
| 2023 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2024 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2025 | R"(package p; |
| 2026 | interface IFoo { |
| 2027 | const int int32_max_oob = 2147483650; |
| 2028 | } |
| 2029 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2030 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2031 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2032 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2035 | TEST_P(AidlTest, FailOnOutOfBoundsInt32MinConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2036 | AidlError error; |
| 2037 | const string expected_stderr = |
| 2038 | "ERROR: p/IFoo.aidl:3.58-60: Invalid type specifier for an int64 literal: int\n"; |
| 2039 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2040 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2041 | R"(package p; |
| 2042 | interface IFoo { |
| 2043 | const int int32_min_oob = -2147483650; |
| 2044 | } |
| 2045 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2046 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2047 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2048 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2051 | TEST_P(AidlTest, FailOnOutOfBoundsInt64MaxConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2052 | AidlError error; |
| 2053 | const string expected_stderr = |
| 2054 | "ERROR: Could not parse integer: 21474836509999999999999999 at p/IFoo.aidl:3.59-85.\n"; |
| 2055 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2056 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2057 | R"(package p; |
| 2058 | interface IFoo { |
| 2059 | const long int64_max_oob = 21474836509999999999999999; |
| 2060 | } |
| 2061 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2062 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2063 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2064 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2065 | } |
| 2066 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2067 | TEST_P(AidlTest, FailOnOutOfBoundsInt64MinConstInt) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2068 | AidlError error; |
| 2069 | const string expected_stderr = |
| 2070 | "ERROR: Could not parse integer: 21474836509999999999999999 at p/IFoo.aidl:3.61-86.\n"; |
| 2071 | CaptureStderr(); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2072 | EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", |
| 2073 | R"(package p; |
| 2074 | interface IFoo { |
| 2075 | const long int64_min_oob = -21474836509999999999999999; |
| 2076 | } |
| 2077 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2078 | typenames_, GetLanguage(), &error)); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2079 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2080 | EXPECT_EQ(AidlError::PARSE_ERROR, error); |
Will McVicker | a4e5b13 | 2019-10-03 13:52:21 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2083 | TEST_P(AidlTest, FailOnOutOfBoundsAutofilledEnum) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2084 | AidlError error; |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 2085 | const string expected_stderr = |
| 2086 | "ERROR: p/TestEnum.aidl:3.35-44: Invalid type specifier for an int32 " |
| 2087 | "literal: byte\n" |
| 2088 | "ERROR: p/TestEnum.aidl:5.1-36: Enumerator type differs from enum backing type.\n"; |
| 2089 | CaptureStderr(); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 2090 | EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl", |
| 2091 | R"(package p; |
| 2092 | @Backing(type="byte") |
| 2093 | enum TestEnum { |
| 2094 | FOO = 127, |
| 2095 | BAR, |
| 2096 | } |
| 2097 | )", |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 2098 | typenames_, GetLanguage(), &error)); |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 2099 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 2100 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
Devin Moore | decaf29 | 2020-04-30 09:16:40 -0700 | [diff] [blame] | 2103 | TEST_P(AidlTest, UnsupportedBackingAnnotationParam) { |
| 2104 | AidlError error; |
| 2105 | const string expected_stderr = |
| 2106 | "ERROR: p/TestEnum.aidl:2.1-51: Parameter foo not supported for annotation Backing. It must " |
| 2107 | "be one of: type\n" |
| 2108 | "ERROR: p/TestEnum.aidl:2.1-51: Parameter foo not supported for annotation Backing. It must " |
| 2109 | "be one of: type\n"; |
| 2110 | CaptureStderr(); |
| 2111 | EXPECT_EQ(nullptr, Parse("p/TestEnum.aidl", |
| 2112 | R"(package p; |
| 2113 | @Backing(foo="byte") |
| 2114 | enum TestEnum { |
| 2115 | FOO = 1, |
| 2116 | BAR, |
| 2117 | } |
| 2118 | )", |
| 2119 | typenames_, GetLanguage(), &error)); |
| 2120 | EXPECT_EQ(expected_stderr, GetCapturedStderr()); |
| 2121 | EXPECT_EQ(AidlError::BAD_TYPE, error); |
| 2122 | } |
| 2123 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 2124 | } // namespace aidl |
| 2125 | } // namespace android |