blob: cb72ec1920e4ac6f62bde2a4b44d00d26577c63b [file] [log] [blame]
Casey Dahlina834dd42015-09-23 11:52:15 -07001/*
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
17#include <string>
18
Elliott Hughes0a620672015-12-04 13:53:18 -080019#include <android-base/stringprintf.h>
Casey Dahlina834dd42015-09-23 11:52:15 -070020#include <gtest/gtest.h>
21
Casey Dahlin2cc93162015-10-02 16:14:17 -070022#include "aidl.h"
Casey Dahlina834dd42015-09-23 11:52:15 -070023#include "aidl_language.h"
24#include "ast_cpp.h"
25#include "code_writer.h"
Christopher Wileyad339272015-10-05 19:11:58 -070026#include "generate_cpp.h"
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080027#include "os.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070028#include "tests/fake_io_delegate.h"
Casey Dahlin80ada3d2015-10-20 20:33:56 -070029#include "tests/test_util.h"
Christopher Wileye3550c62015-09-29 13:26:10 -070030#include "type_cpp.h"
Casey Dahlina834dd42015-09-23 11:52:15 -070031
Casey Dahlinb8d9e882015-11-24 10:57:23 -080032using ::android::aidl::test::FakeIoDelegate;
33using ::android::base::StringPrintf;
Casey Dahlina834dd42015-09-23 11:52:15 -070034using std::string;
35using std::unique_ptr;
36
37namespace android {
38namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070039namespace cpp {
Casey Dahlina834dd42015-09-23 11:52:15 -070040namespace {
41
Casey Dahlinb0966612015-10-19 16:35:26 -070042const string kComplexTypeInterfaceAIDL =
43R"(package android.os;
Casey Dahlin389781f2015-10-22 13:13:21 -070044import foo.IFooType;
Casey Dahlinb0966612015-10-19 16:35:26 -070045interface IComplexTypeInterface {
Casey Dahlind40e2fe2015-11-24 14:06:52 -080046 const int MY_CONSTANT = 3;
Casey Dahlincb5317d2015-12-03 15:43:33 -080047 int[] Send(in @nullable int[] goes_in, inout double[] goes_in_and_out, out boolean[] goes_out);
Casey Dahlin0dd08af2015-10-20 18:45:50 -070048 oneway void Piff(int times);
Casey Dahlin389781f2015-10-22 13:13:21 -070049 IFooType TakesABinder(IFooType f);
Christopher Wiley56c9bf32015-10-30 10:41:12 -070050 List<String> StringListMethod(in java.util.List<String> input, out List<String> output);
Casey Dahlin7ecd69f2015-11-03 13:52:38 -080051 List<IBinder> BinderListMethod(in java.util.List<IBinder> input, out List<IBinder> output);
Casey Dahlina4ba4b62015-11-02 15:43:30 -080052 FileDescriptor TakesAFileDescriptor(in FileDescriptor f);
53 FileDescriptor[] TakesAFileDescriptorArray(in FileDescriptor[] f);
Casey Dahlinb0966612015-10-19 16:35:26 -070054})";
55
56const char kExpectedComplexTypeClientHeaderOutput[] =
57R"(#ifndef AIDL_GENERATED_ANDROID_OS_BP_COMPLEX_TYPE_INTERFACE_H_
58#define AIDL_GENERATED_ANDROID_OS_BP_COMPLEX_TYPE_INTERFACE_H_
59
60#include <binder/IBinder.h>
61#include <binder/IInterface.h>
62#include <utils/Errors.h>
63#include <android/os/IComplexTypeInterface.h>
64
65namespace android {
66
67namespace os {
68
Casey Dahlinb8d9e882015-11-24 10:57:23 -080069class BpComplexTypeInterface : public ::android::BpInterface<IComplexTypeInterface> {
Casey Dahlinb0966612015-10-19 16:35:26 -070070public:
Casey Dahlinb8d9e882015-11-24 10:57:23 -080071explicit BpComplexTypeInterface(const ::android::sp<::android::IBinder>& _aidl_impl);
Casey Dahlinb0966612015-10-19 16:35:26 -070072virtual ~BpComplexTypeInterface() = default;
Casey Dahlin57dbe242015-12-04 11:44:02 -080073::android::binder::Status Send(const ::std::unique_ptr<::std::vector<int32_t>>& goes_in, ::std::vector<double>* goes_in_and_out, ::std::vector<bool>* goes_out, ::std::vector<int32_t>* _aidl_return) override;
Casey Dahlinb8d9e882015-11-24 10:57:23 -080074::android::binder::Status Piff(int32_t times) override;
75::android::binder::Status TakesABinder(const ::android::sp<::foo::IFooType>& f, ::android::sp<::foo::IFooType>* _aidl_return) override;
76::android::binder::Status StringListMethod(const ::std::vector<::android::String16>& input, ::std::vector<::android::String16>* output, ::std::vector<::android::String16>* _aidl_return) override;
77::android::binder::Status BinderListMethod(const ::std::vector<::android::sp<::android::IBinder>>& input, ::std::vector<::android::sp<::android::IBinder>>* output, ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) override;
78::android::binder::Status TakesAFileDescriptor(const ::ScopedFd& f, ::ScopedFd* _aidl_return) override;
79::android::binder::Status TakesAFileDescriptorArray(const ::std::vector<::ScopedFd>& f, ::std::vector<::ScopedFd>* _aidl_return) override;
Casey Dahlinb0966612015-10-19 16:35:26 -070080}; // class BpComplexTypeInterface
81
82} // namespace os
83
84} // namespace android
85
86#endif // AIDL_GENERATED_ANDROID_OS_BP_COMPLEX_TYPE_INTERFACE_H_)";
87
88const char kExpectedComplexTypeClientSourceOutput[] =
89R"(#include <android/os/BpComplexTypeInterface.h>
90#include <binder/Parcel.h>
91
92namespace android {
93
94namespace os {
95
Casey Dahlinb8d9e882015-11-24 10:57:23 -080096BpComplexTypeInterface::BpComplexTypeInterface(const ::android::sp<::android::IBinder>& _aidl_impl)
97 : BpInterface<IComplexTypeInterface>(_aidl_impl){
Casey Dahlinb0966612015-10-19 16:35:26 -070098}
99
Casey Dahlin57dbe242015-12-04 11:44:02 -0800100::android::binder::Status BpComplexTypeInterface::Send(const ::std::unique_ptr<::std::vector<int32_t>>& goes_in, ::std::vector<double>* goes_in_and_out, ::std::vector<bool>* goes_out, ::std::vector<int32_t>* _aidl_return) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800101::android::Parcel _aidl_data;
102::android::Parcel _aidl_reply;
Christopher Wiley10957122015-12-04 14:35:38 -0800103::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800104::android::binder::Status _aidl_status;
105_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
106if (((_aidl_ret_status) != (::android::OK))) {
107goto _aidl_error;
Christopher Wiley8993cb52015-10-21 09:53:24 -0700108}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800109_aidl_ret_status = _aidl_data.writeInt32Vector(goes_in);
110if (((_aidl_ret_status) != (::android::OK))) {
111goto _aidl_error;
Christopher Wileybb967822015-10-27 14:04:53 -0700112}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800113_aidl_ret_status = _aidl_data.writeDoubleVector(*goes_in_and_out);
114if (((_aidl_ret_status) != (::android::OK))) {
115goto _aidl_error;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700116}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800117_aidl_ret_status = remote()->transact(IComplexTypeInterface::SEND, _aidl_data, &_aidl_reply);
118if (((_aidl_ret_status) != (::android::OK))) {
119goto _aidl_error;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700120}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800121_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
122if (((_aidl_ret_status) != (::android::OK))) {
123goto _aidl_error;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800124}
125if (!_aidl_status.isOk()) {
126return _aidl_status;
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700127}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800128_aidl_ret_status = _aidl_reply.readInt32Vector(_aidl_return);
129if (((_aidl_ret_status) != (::android::OK))) {
130goto _aidl_error;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700131}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800132_aidl_ret_status = _aidl_reply.readDoubleVector(goes_in_and_out);
133if (((_aidl_ret_status) != (::android::OK))) {
134goto _aidl_error;
Christopher Wileybb967822015-10-27 14:04:53 -0700135}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800136_aidl_ret_status = _aidl_reply.readBoolVector(goes_out);
137if (((_aidl_ret_status) != (::android::OK))) {
138goto _aidl_error;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700139}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800140_aidl_error:
141_aidl_status.setFromStatusT(_aidl_ret_status);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800142return _aidl_status;
Casey Dahlinb0966612015-10-19 16:35:26 -0700143}
144
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800145::android::binder::Status BpComplexTypeInterface::Piff(int32_t times) {
146::android::Parcel _aidl_data;
147::android::Parcel _aidl_reply;
Christopher Wiley10957122015-12-04 14:35:38 -0800148::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800149::android::binder::Status _aidl_status;
150_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
151if (((_aidl_ret_status) != (::android::OK))) {
152goto _aidl_error;
Christopher Wiley8993cb52015-10-21 09:53:24 -0700153}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800154_aidl_ret_status = _aidl_data.writeInt32(times);
155if (((_aidl_ret_status) != (::android::OK))) {
156goto _aidl_error;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700157}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800158_aidl_ret_status = remote()->transact(IComplexTypeInterface::PIFF, _aidl_data, &_aidl_reply, ::android::IBinder::FLAG_ONEWAY);
159if (((_aidl_ret_status) != (::android::OK))) {
160goto _aidl_error;
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700161}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800162_aidl_error:
163_aidl_status.setFromStatusT(_aidl_ret_status);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800164return _aidl_status;
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700165}
166
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800167::android::binder::Status BpComplexTypeInterface::TakesABinder(const ::android::sp<::foo::IFooType>& f, ::android::sp<::foo::IFooType>* _aidl_return) {
168::android::Parcel _aidl_data;
169::android::Parcel _aidl_reply;
Christopher Wiley10957122015-12-04 14:35:38 -0800170::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800171::android::binder::Status _aidl_status;
172_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
173if (((_aidl_ret_status) != (::android::OK))) {
174goto _aidl_error;
Casey Dahlin389781f2015-10-22 13:13:21 -0700175}
Casey Dahlin2dbab062016-01-05 17:41:08 -0800176_aidl_ret_status = _aidl_data.writeStrongBinder(::foo::IFooType::asBinder(f));
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800177if (((_aidl_ret_status) != (::android::OK))) {
178goto _aidl_error;
Casey Dahlin389781f2015-10-22 13:13:21 -0700179}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800180_aidl_ret_status = remote()->transact(IComplexTypeInterface::TAKESABINDER, _aidl_data, &_aidl_reply);
181if (((_aidl_ret_status) != (::android::OK))) {
182goto _aidl_error;
Casey Dahlin389781f2015-10-22 13:13:21 -0700183}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800184_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
185if (((_aidl_ret_status) != (::android::OK))) {
186goto _aidl_error;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800187}
188if (!_aidl_status.isOk()) {
189return _aidl_status;
Casey Dahlin389781f2015-10-22 13:13:21 -0700190}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800191_aidl_ret_status = _aidl_reply.readStrongBinder(_aidl_return);
192if (((_aidl_ret_status) != (::android::OK))) {
193goto _aidl_error;
Casey Dahlin389781f2015-10-22 13:13:21 -0700194}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800195_aidl_error:
196_aidl_status.setFromStatusT(_aidl_ret_status);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800197return _aidl_status;
Casey Dahlin389781f2015-10-22 13:13:21 -0700198}
199
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800200::android::binder::Status BpComplexTypeInterface::StringListMethod(const ::std::vector<::android::String16>& input, ::std::vector<::android::String16>* output, ::std::vector<::android::String16>* _aidl_return) {
201::android::Parcel _aidl_data;
202::android::Parcel _aidl_reply;
Christopher Wiley10957122015-12-04 14:35:38 -0800203::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800204::android::binder::Status _aidl_status;
205_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
206if (((_aidl_ret_status) != (::android::OK))) {
207goto _aidl_error;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700208}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800209_aidl_ret_status = _aidl_data.writeString16Vector(input);
210if (((_aidl_ret_status) != (::android::OK))) {
211goto _aidl_error;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700212}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800213_aidl_ret_status = remote()->transact(IComplexTypeInterface::STRINGLISTMETHOD, _aidl_data, &_aidl_reply);
214if (((_aidl_ret_status) != (::android::OK))) {
215goto _aidl_error;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700216}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800217_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
218if (((_aidl_ret_status) != (::android::OK))) {
219goto _aidl_error;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800220}
221if (!_aidl_status.isOk()) {
222return _aidl_status;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700223}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800224_aidl_ret_status = _aidl_reply.readString16Vector(_aidl_return);
225if (((_aidl_ret_status) != (::android::OK))) {
226goto _aidl_error;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700227}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800228_aidl_ret_status = _aidl_reply.readString16Vector(output);
229if (((_aidl_ret_status) != (::android::OK))) {
230goto _aidl_error;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700231}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800232_aidl_error:
233_aidl_status.setFromStatusT(_aidl_ret_status);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800234return _aidl_status;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700235}
236
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800237::android::binder::Status BpComplexTypeInterface::BinderListMethod(const ::std::vector<::android::sp<::android::IBinder>>& input, ::std::vector<::android::sp<::android::IBinder>>* output, ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) {
238::android::Parcel _aidl_data;
239::android::Parcel _aidl_reply;
Christopher Wiley10957122015-12-04 14:35:38 -0800240::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800241::android::binder::Status _aidl_status;
242_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
243if (((_aidl_ret_status) != (::android::OK))) {
244goto _aidl_error;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800245}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800246_aidl_ret_status = _aidl_data.writeStrongBinderVector(input);
247if (((_aidl_ret_status) != (::android::OK))) {
248goto _aidl_error;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800249}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800250_aidl_ret_status = remote()->transact(IComplexTypeInterface::BINDERLISTMETHOD, _aidl_data, &_aidl_reply);
251if (((_aidl_ret_status) != (::android::OK))) {
252goto _aidl_error;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800253}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800254_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
255if (((_aidl_ret_status) != (::android::OK))) {
256goto _aidl_error;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800257}
258if (!_aidl_status.isOk()) {
259return _aidl_status;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800260}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800261_aidl_ret_status = _aidl_reply.readStrongBinderVector(_aidl_return);
262if (((_aidl_ret_status) != (::android::OK))) {
263goto _aidl_error;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800264}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800265_aidl_ret_status = _aidl_reply.readStrongBinderVector(output);
266if (((_aidl_ret_status) != (::android::OK))) {
267goto _aidl_error;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800268}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800269_aidl_error:
270_aidl_status.setFromStatusT(_aidl_ret_status);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800271return _aidl_status;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800272}
273
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800274::android::binder::Status BpComplexTypeInterface::TakesAFileDescriptor(const ::ScopedFd& f, ::ScopedFd* _aidl_return) {
275::android::Parcel _aidl_data;
276::android::Parcel _aidl_reply;
Christopher Wiley10957122015-12-04 14:35:38 -0800277::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800278::android::binder::Status _aidl_status;
279_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
280if (((_aidl_ret_status) != (::android::OK))) {
281goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800282}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800283_aidl_ret_status = _aidl_data.writeUniqueFileDescriptor(f);
284if (((_aidl_ret_status) != (::android::OK))) {
285goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800286}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800287_aidl_ret_status = remote()->transact(IComplexTypeInterface::TAKESAFILEDESCRIPTOR, _aidl_data, &_aidl_reply);
288if (((_aidl_ret_status) != (::android::OK))) {
289goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800290}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800291_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
292if (((_aidl_ret_status) != (::android::OK))) {
293goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800294}
295if (!_aidl_status.isOk()) {
296return _aidl_status;
297}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800298_aidl_ret_status = _aidl_reply.readUniqueFileDescriptor(_aidl_return);
299if (((_aidl_ret_status) != (::android::OK))) {
300goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800301}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800302_aidl_error:
303_aidl_status.setFromStatusT(_aidl_ret_status);
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800304return _aidl_status;
305}
306
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800307::android::binder::Status BpComplexTypeInterface::TakesAFileDescriptorArray(const ::std::vector<::ScopedFd>& f, ::std::vector<::ScopedFd>* _aidl_return) {
308::android::Parcel _aidl_data;
309::android::Parcel _aidl_reply;
Christopher Wiley10957122015-12-04 14:35:38 -0800310::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800311::android::binder::Status _aidl_status;
312_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
313if (((_aidl_ret_status) != (::android::OK))) {
314goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800315}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800316_aidl_ret_status = _aidl_data.writeUniqueFileDescriptorVector(f);
317if (((_aidl_ret_status) != (::android::OK))) {
318goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800319}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800320_aidl_ret_status = remote()->transact(IComplexTypeInterface::TAKESAFILEDESCRIPTORARRAY, _aidl_data, &_aidl_reply);
321if (((_aidl_ret_status) != (::android::OK))) {
322goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800323}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800324_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
325if (((_aidl_ret_status) != (::android::OK))) {
326goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800327}
328if (!_aidl_status.isOk()) {
329return _aidl_status;
330}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800331_aidl_ret_status = _aidl_reply.readUniqueFileDescriptorVector(_aidl_return);
332if (((_aidl_ret_status) != (::android::OK))) {
333goto _aidl_error;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800334}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800335_aidl_error:
336_aidl_status.setFromStatusT(_aidl_ret_status);
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800337return _aidl_status;
338}
339
Casey Dahlinb0966612015-10-19 16:35:26 -0700340} // namespace os
341
342} // namespace android
343)";
344
345const char kExpectedComplexTypeServerHeaderOutput[] =
346R"(#ifndef AIDL_GENERATED_ANDROID_OS_BN_COMPLEX_TYPE_INTERFACE_H_
347#define AIDL_GENERATED_ANDROID_OS_BN_COMPLEX_TYPE_INTERFACE_H_
348
349#include <binder/IInterface.h>
350#include <android/os/IComplexTypeInterface.h>
351
352namespace android {
353
354namespace os {
355
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800356class BnComplexTypeInterface : public ::android::BnInterface<IComplexTypeInterface> {
Casey Dahlinb0966612015-10-19 16:35:26 -0700357public:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800358::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags = 0) override;
Casey Dahlinb0966612015-10-19 16:35:26 -0700359}; // class BnComplexTypeInterface
360
361} // namespace os
362
363} // namespace android
364
365#endif // AIDL_GENERATED_ANDROID_OS_BN_COMPLEX_TYPE_INTERFACE_H_)";
366
367const char kExpectedComplexTypeServerSourceOutput[] =
368R"(#include <android/os/BnComplexTypeInterface.h>
369#include <binder/Parcel.h>
370
371namespace android {
372
373namespace os {
374
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800375::android::status_t BnComplexTypeInterface::onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) {
Christopher Wiley10957122015-12-04 14:35:38 -0800376::android::status_t _aidl_ret_status = ::android::OK;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800377switch (_aidl_code) {
Casey Dahlinb0966612015-10-19 16:35:26 -0700378case Call::SEND:
379{
Casey Dahlin57dbe242015-12-04 11:44:02 -0800380::std::unique_ptr<::std::vector<int32_t>> in_goes_in;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800381::std::vector<double> in_goes_in_and_out;
382::std::vector<bool> out_goes_out;
383::std::vector<int32_t> _aidl_return;
384if (!(_aidl_data.checkInterface(this))) {
385_aidl_ret_status = ::android::BAD_TYPE;
Christopher Wiley8993cb52015-10-21 09:53:24 -0700386break;
387}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800388_aidl_ret_status = _aidl_data.readInt32Vector(&in_goes_in);
389if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700390break;
391}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800392_aidl_ret_status = _aidl_data.readDoubleVector(&in_goes_in_and_out);
393if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wileybb967822015-10-27 14:04:53 -0700394break;
395}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800396::android::binder::Status _aidl_status(Send(in_goes_in, &in_goes_in_and_out, &out_goes_out, &_aidl_return));
397_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
398if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700399break;
400}
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800401if (!_aidl_status.isOk()) {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700402break;
403}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800404_aidl_ret_status = _aidl_reply->writeInt32Vector(_aidl_return);
405if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700406break;
407}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800408_aidl_ret_status = _aidl_reply->writeDoubleVector(in_goes_in_and_out);
409if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wileybb967822015-10-27 14:04:53 -0700410break;
411}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800412_aidl_ret_status = _aidl_reply->writeBoolVector(out_goes_out);
413if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700414break;
415}
Casey Dahlinb0966612015-10-19 16:35:26 -0700416}
417break;
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700418case Call::PIFF:
419{
420int32_t in_times;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800421if (!(_aidl_data.checkInterface(this))) {
422_aidl_ret_status = ::android::BAD_TYPE;
Christopher Wiley8993cb52015-10-21 09:53:24 -0700423break;
424}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800425_aidl_ret_status = _aidl_data.readInt32(&in_times);
426if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700427break;
428}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800429::android::binder::Status _aidl_status(Piff(in_times));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700430}
431break;
Casey Dahlin389781f2015-10-22 13:13:21 -0700432case Call::TAKESABINDER:
433{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800434::android::sp<::foo::IFooType> in_f;
435::android::sp<::foo::IFooType> _aidl_return;
436if (!(_aidl_data.checkInterface(this))) {
437_aidl_ret_status = ::android::BAD_TYPE;
Casey Dahlin389781f2015-10-22 13:13:21 -0700438break;
439}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800440_aidl_ret_status = _aidl_data.readStrongBinder(&in_f);
441if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700442break;
443}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800444::android::binder::Status _aidl_status(TakesABinder(in_f, &_aidl_return));
445_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
446if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700447break;
448}
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800449if (!_aidl_status.isOk()) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700450break;
451}
Casey Dahlin2dbab062016-01-05 17:41:08 -0800452_aidl_ret_status = _aidl_reply->writeStrongBinder(::foo::IFooType::asBinder(_aidl_return));
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800453if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700454break;
455}
456}
457break;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700458case Call::STRINGLISTMETHOD:
459{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800460::std::vector<::android::String16> in_input;
461::std::vector<::android::String16> out_output;
462::std::vector<::android::String16> _aidl_return;
463if (!(_aidl_data.checkInterface(this))) {
464_aidl_ret_status = ::android::BAD_TYPE;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700465break;
466}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800467_aidl_ret_status = _aidl_data.readString16Vector(&in_input);
468if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700469break;
470}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800471::android::binder::Status _aidl_status(StringListMethod(in_input, &out_output, &_aidl_return));
472_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
473if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700474break;
475}
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800476if (!_aidl_status.isOk()) {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700477break;
478}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800479_aidl_ret_status = _aidl_reply->writeString16Vector(_aidl_return);
480if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700481break;
482}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800483_aidl_ret_status = _aidl_reply->writeString16Vector(out_output);
484if (((_aidl_ret_status) != (::android::OK))) {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700485break;
486}
487}
488break;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800489case Call::BINDERLISTMETHOD:
490{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800491::std::vector<::android::sp<::android::IBinder>> in_input;
492::std::vector<::android::sp<::android::IBinder>> out_output;
493::std::vector<::android::sp<::android::IBinder>> _aidl_return;
494if (!(_aidl_data.checkInterface(this))) {
495_aidl_ret_status = ::android::BAD_TYPE;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800496break;
497}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800498_aidl_ret_status = _aidl_data.readStrongBinderVector(&in_input);
499if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800500break;
501}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800502::android::binder::Status _aidl_status(BinderListMethod(in_input, &out_output, &_aidl_return));
503_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
504if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800505break;
506}
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800507if (!_aidl_status.isOk()) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800508break;
509}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800510_aidl_ret_status = _aidl_reply->writeStrongBinderVector(_aidl_return);
511if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800512break;
513}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800514_aidl_ret_status = _aidl_reply->writeStrongBinderVector(out_output);
515if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800516break;
517}
518}
519break;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800520case Call::TAKESAFILEDESCRIPTOR:
521{
522::ScopedFd in_f;
523::ScopedFd _aidl_return;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800524if (!(_aidl_data.checkInterface(this))) {
525_aidl_ret_status = ::android::BAD_TYPE;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800526break;
527}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800528_aidl_ret_status = _aidl_data.readUniqueFileDescriptor(&in_f);
529if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800530break;
531}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800532::android::binder::Status _aidl_status(TakesAFileDescriptor(in_f, &_aidl_return));
533_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
534if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800535break;
536}
537if (!_aidl_status.isOk()) {
538break;
539}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800540_aidl_ret_status = _aidl_reply->writeUniqueFileDescriptor(_aidl_return);
541if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800542break;
543}
544}
545break;
546case Call::TAKESAFILEDESCRIPTORARRAY:
547{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800548::std::vector<::ScopedFd> in_f;
549::std::vector<::ScopedFd> _aidl_return;
550if (!(_aidl_data.checkInterface(this))) {
551_aidl_ret_status = ::android::BAD_TYPE;
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800552break;
553}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800554_aidl_ret_status = _aidl_data.readUniqueFileDescriptorVector(&in_f);
555if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800556break;
557}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800558::android::binder::Status _aidl_status(TakesAFileDescriptorArray(in_f, &_aidl_return));
559_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
560if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800561break;
562}
563if (!_aidl_status.isOk()) {
564break;
565}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800566_aidl_ret_status = _aidl_reply->writeUniqueFileDescriptorVector(_aidl_return);
567if (((_aidl_ret_status) != (::android::OK))) {
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800568break;
569}
570}
571break;
Casey Dahlinb0966612015-10-19 16:35:26 -0700572default:
573{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800574_aidl_ret_status = ::android::BBinder::onTransact(_aidl_code, _aidl_data, _aidl_reply, _aidl_flags);
Casey Dahlinb0966612015-10-19 16:35:26 -0700575}
576break;
577}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800578if (_aidl_ret_status == ::android::UNEXPECTED_NULL) {
579_aidl_ret_status = ::android::binder::Status::fromExceptionCode(::android::binder::Status::EX_NULL_POINTER).writeToParcel(_aidl_reply);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800580}
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800581return _aidl_ret_status;
Casey Dahlinb0966612015-10-19 16:35:26 -0700582}
583
584} // namespace os
585
586} // namespace android
587)";
588
589const char kExpectedComplexTypeInterfaceHeaderOutput[] =
590R"(#ifndef AIDL_GENERATED_ANDROID_OS_I_COMPLEX_TYPE_INTERFACE_H_
591#define AIDL_GENERATED_ANDROID_OS_I_COMPLEX_TYPE_INTERFACE_H_
592
593#include <binder/IBinder.h>
594#include <binder/IInterface.h>
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800595#include <binder/Status.h>
Casey Dahlinb0966612015-10-19 16:35:26 -0700596#include <cstdint>
Casey Dahlin389781f2015-10-22 13:13:21 -0700597#include <foo/IFooType.h>
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800598#include <nativehelper/ScopedFd.h>
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700599#include <utils/String16.h>
Casey Dahlin389781f2015-10-22 13:13:21 -0700600#include <utils/StrongPointer.h>
Casey Dahlinb0966612015-10-19 16:35:26 -0700601#include <vector>
602
603namespace android {
604
605namespace os {
606
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800607class IComplexTypeInterface : public ::android::IInterface {
Casey Dahlinb0966612015-10-19 16:35:26 -0700608public:
609DECLARE_META_INTERFACE(ComplexTypeInterface);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800610static constexpr int32_t MY_CONSTANT = 3;
Casey Dahlin57dbe242015-12-04 11:44:02 -0800611virtual ::android::binder::Status Send(const ::std::unique_ptr<::std::vector<int32_t>>& goes_in, ::std::vector<double>* goes_in_and_out, ::std::vector<bool>* goes_out, ::std::vector<int32_t>* _aidl_return) = 0;
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800612virtual ::android::binder::Status Piff(int32_t times) = 0;
613virtual ::android::binder::Status TakesABinder(const ::android::sp<::foo::IFooType>& f, ::android::sp<::foo::IFooType>* _aidl_return) = 0;
614virtual ::android::binder::Status StringListMethod(const ::std::vector<::android::String16>& input, ::std::vector<::android::String16>* output, ::std::vector<::android::String16>* _aidl_return) = 0;
615virtual ::android::binder::Status BinderListMethod(const ::std::vector<::android::sp<::android::IBinder>>& input, ::std::vector<::android::sp<::android::IBinder>>* output, ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) = 0;
616virtual ::android::binder::Status TakesAFileDescriptor(const ::ScopedFd& f, ::ScopedFd* _aidl_return) = 0;
617virtual ::android::binder::Status TakesAFileDescriptorArray(const ::std::vector<::ScopedFd>& f, ::std::vector<::ScopedFd>* _aidl_return) = 0;
Casey Dahlinb0966612015-10-19 16:35:26 -0700618enum Call {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800619 SEND = ::android::IBinder::FIRST_CALL_TRANSACTION + 0,
620 PIFF = ::android::IBinder::FIRST_CALL_TRANSACTION + 1,
621 TAKESABINDER = ::android::IBinder::FIRST_CALL_TRANSACTION + 2,
622 STRINGLISTMETHOD = ::android::IBinder::FIRST_CALL_TRANSACTION + 3,
623 BINDERLISTMETHOD = ::android::IBinder::FIRST_CALL_TRANSACTION + 4,
624 TAKESAFILEDESCRIPTOR = ::android::IBinder::FIRST_CALL_TRANSACTION + 5,
625 TAKESAFILEDESCRIPTORARRAY = ::android::IBinder::FIRST_CALL_TRANSACTION + 6,
Casey Dahlinb0966612015-10-19 16:35:26 -0700626};
627}; // class IComplexTypeInterface
628
629} // namespace os
630
631} // namespace android
632
633#endif // AIDL_GENERATED_ANDROID_OS_I_COMPLEX_TYPE_INTERFACE_H_)";
634
635const char kExpectedComplexTypeInterfaceSourceOutput[] =
636R"(#include <android/os/IComplexTypeInterface.h>
637#include <android/os/BpComplexTypeInterface.h>
638
639namespace android {
640
641namespace os {
642
643IMPLEMENT_META_INTERFACE(ComplexTypeInterface, "android.os.IComplexTypeInterface");
644
645} // namespace os
646
647} // namespace android
648)";
649
Casey Dahlina834dd42015-09-23 11:52:15 -0700650} // namespace
651
Casey Dahlinb0966612015-10-19 16:35:26 -0700652class ASTTest : public ::testing::Test {
Christopher Wiley0c732db2015-09-29 14:36:44 -0700653 protected:
Casey Dahlin389781f2015-10-22 13:13:21 -0700654 ASTTest(string file_path, string file_contents)
655 : file_path_(file_path),
Christopher Wiley56799522015-10-31 10:17:04 -0700656 file_contents_(file_contents) {
657 types_.Init();
658 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700659
Christopher Wiley90be4e32015-10-20 14:55:25 -0700660 unique_ptr<AidlInterface> Parse() {
Casey Dahlin389781f2015-10-22 13:13:21 -0700661 io_delegate_.SetFileContents(file_path_, file_contents_);
Casey Dahlina834dd42015-09-23 11:52:15 -0700662
Christopher Wiley90be4e32015-10-20 14:55:25 -0700663 unique_ptr<AidlInterface> ret;
664 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wiley632801d2015-11-05 14:15:49 -0800665 AidlError err = ::android::aidl::internals::load_and_validate_aidl(
Christopher Wiley90be4e32015-10-20 14:55:25 -0700666 {}, // no preprocessed files
Casey Dahlin389781f2015-10-22 13:13:21 -0700667 {"."},
668 file_path_,
669 io_delegate_,
670 &types_,
Christopher Wiley90be4e32015-10-20 14:55:25 -0700671 &ret,
672 &imports);
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700673
Christopher Wiley632801d2015-11-05 14:15:49 -0800674 if (err != AidlError::OK)
Casey Dahlin2cc93162015-10-02 16:14:17 -0700675 return nullptr;
Casey Dahlina834dd42015-09-23 11:52:15 -0700676
Casey Dahlin2cc93162015-10-02 16:14:17 -0700677 return ret;
Christopher Wiley90be4e32015-10-20 14:55:25 -0700678 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700679
Christopher Wiley0c732db2015-09-29 14:36:44 -0700680 void Compare(Document* doc, const char* expected) {
681 string output;
682 unique_ptr<CodeWriter> cw = GetStringWriter(&output);
683
684 doc->Write(cw.get());
685
Casey Dahlin80ada3d2015-10-20 20:33:56 -0700686 if (expected == output) {
687 return; // Success
688 }
689
690 test::PrintDiff(expected, output);
691 FAIL() << "Document contents did not match expected contents";
Christopher Wiley0c732db2015-09-29 14:36:44 -0700692 }
Casey Dahlin389781f2015-10-22 13:13:21 -0700693
694 const string file_path_;
695 const string file_contents_;
696 FakeIoDelegate io_delegate_;
697 TypeNamespace types_;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700698};
699
Casey Dahlinb0966612015-10-19 16:35:26 -0700700class ComplexTypeInterfaceASTTest : public ASTTest {
Casey Dahlin389781f2015-10-22 13:13:21 -0700701 public:
702 ComplexTypeInterfaceASTTest()
703 : ASTTest("android/os/IComplexTypeInterface.aidl",
704 kComplexTypeInterfaceAIDL) {
705 io_delegate_.SetFileContents("foo/IFooType.aidl",
706 "package foo; interface IFooType {}");
707 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700708};
709
710TEST_F(ComplexTypeInterfaceASTTest, GeneratesClientHeader) {
711 unique_ptr<AidlInterface> interface = Parse();
712 ASSERT_NE(interface, nullptr);
Casey Dahlin389781f2015-10-22 13:13:21 -0700713 unique_ptr<Document> doc = internals::BuildClientHeader(types_, *interface);
Casey Dahlinb0966612015-10-19 16:35:26 -0700714 Compare(doc.get(), kExpectedComplexTypeClientHeaderOutput);
715}
716
717TEST_F(ComplexTypeInterfaceASTTest, GeneratesClientSource) {
718 unique_ptr<AidlInterface> interface = Parse();
719 ASSERT_NE(interface, nullptr);
Casey Dahlin389781f2015-10-22 13:13:21 -0700720 unique_ptr<Document> doc = internals::BuildClientSource(types_, *interface);
Casey Dahlinb0966612015-10-19 16:35:26 -0700721 Compare(doc.get(), kExpectedComplexTypeClientSourceOutput);
722}
723
724TEST_F(ComplexTypeInterfaceASTTest, GeneratesServerHeader) {
725 unique_ptr<AidlInterface> interface = Parse();
726 ASSERT_NE(interface, nullptr);
Casey Dahlin389781f2015-10-22 13:13:21 -0700727 unique_ptr<Document> doc = internals::BuildServerHeader(types_, *interface);
Casey Dahlinb0966612015-10-19 16:35:26 -0700728 Compare(doc.get(), kExpectedComplexTypeServerHeaderOutput);
729}
730
731TEST_F(ComplexTypeInterfaceASTTest, GeneratesServerSource) {
732 unique_ptr<AidlInterface> interface = Parse();
733 ASSERT_NE(interface, nullptr);
Casey Dahlin389781f2015-10-22 13:13:21 -0700734 unique_ptr<Document> doc = internals::BuildServerSource(types_, *interface);
Casey Dahlinb0966612015-10-19 16:35:26 -0700735 Compare(doc.get(), kExpectedComplexTypeServerSourceOutput);
736}
737
738TEST_F(ComplexTypeInterfaceASTTest, GeneratesInterfaceHeader) {
739 unique_ptr<AidlInterface> interface = Parse();
740 ASSERT_NE(interface, nullptr);
Casey Dahlin389781f2015-10-22 13:13:21 -0700741 unique_ptr<Document> doc = internals::BuildInterfaceHeader(types_, *interface);
Casey Dahlinb0966612015-10-19 16:35:26 -0700742 Compare(doc.get(), kExpectedComplexTypeInterfaceHeaderOutput);
743}
744
745TEST_F(ComplexTypeInterfaceASTTest, GeneratesInterfaceSource) {
746 unique_ptr<AidlInterface> interface = Parse();
747 ASSERT_NE(interface, nullptr);
Casey Dahlin389781f2015-10-22 13:13:21 -0700748 unique_ptr<Document> doc = internals::BuildInterfaceSource(types_, *interface);
Casey Dahlinb0966612015-10-19 16:35:26 -0700749 Compare(doc.get(), kExpectedComplexTypeInterfaceSourceOutput);
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700750}
751
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800752namespace test_io_handling {
753
754const char kInputPath[] = "a/IFoo.aidl";
755const char kOutputPath[] = "output.cpp";
756const char kHeaderDir[] = "headers";
757const char kInterfaceHeaderRelPath[] = "a/IFoo.h";
758
759} // namespace test_io_handling
760
761class IoErrorHandlingTest : public ASTTest {
762 public:
763 IoErrorHandlingTest ()
764 : ASTTest(test_io_handling::kInputPath,
765 "package a; interface IFoo {}"),
766 options_(GetOptions()) {}
767
768 const unique_ptr<CppOptions> options_;
769
770 private:
771 static unique_ptr<CppOptions> GetOptions() {
772 using namespace test_io_handling;
773
774 const int argc = 4;
775 const char* cmdline[argc] = {
776 "aidl-cpp", kInputPath, kHeaderDir, kOutputPath
777 };
778 return CppOptions::Parse(argc, cmdline);
779 }
780};
781
782TEST_F(IoErrorHandlingTest, GenerateCorrectlyAbsentErrors) {
783 // Confirm that this is working correctly without I/O problems.
784 const unique_ptr<AidlInterface> interface = Parse();
785 ASSERT_NE(interface, nullptr);
786 ASSERT_TRUE(GenerateCpp(*options_, types_, *interface, io_delegate_));
787}
788
789TEST_F(IoErrorHandlingTest, HandlesBadHeaderWrite) {
790 using namespace test_io_handling;
791 const unique_ptr<AidlInterface> interface = Parse();
792 ASSERT_NE(interface, nullptr);
793
794 // Simulate issues closing the interface header.
795 const string header_path =
796 StringPrintf("%s%c%s", kHeaderDir, OS_PATH_SEPARATOR,
797 kInterfaceHeaderRelPath);
798 io_delegate_.AddBrokenFilePath(header_path);
799 ASSERT_FALSE(GenerateCpp(*options_, types_, *interface, io_delegate_));
800 // We should never attempt to write the C++ file if we fail writing headers.
801 ASSERT_FALSE(io_delegate_.GetWrittenContents(kOutputPath, nullptr));
802 // We should remove partial results.
803 ASSERT_TRUE(io_delegate_.PathWasRemoved(header_path));
804}
805
806TEST_F(IoErrorHandlingTest, HandlesBadCppWrite) {
807 using test_io_handling::kOutputPath;
808 const unique_ptr<AidlInterface> interface = Parse();
809 ASSERT_NE(interface, nullptr);
810
811 // Simulate issues closing the cpp file.
812 io_delegate_.AddBrokenFilePath(kOutputPath);
813 ASSERT_FALSE(GenerateCpp(*options_, types_, *interface, io_delegate_));
814 // We should remove partial results.
815 ASSERT_TRUE(io_delegate_.PathWasRemoved(kOutputPath));
816}
817
Christopher Wileyf944e792015-09-29 10:00:46 -0700818} // namespace cpp
Casey Dahlina834dd42015-09-23 11:52:15 -0700819} // namespace aidl
820} // namespace android