1 /*
2  * Copyright (C) 2016, 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 "tests/test_data.h"
18 
19 namespace android {
20 namespace aidl {
21 namespace test_data {
22 namespace string_constants {
23 
24 const char kCanonicalName[] = "android.os.IStringConstants";
25 const char kInterfaceDefinition[] = R"(
26 package android.os;
27 
28 interface IStringConstants {
29   const String EXAMPLE_CONSTANT = "foo";
30 }
31 )";
32 
33 const char kJavaOutputPath[] = "some/path/to/output.java";
34 const char kExpectedJavaOutput[] =
35     R"(/*
36  * This file is auto-generated.  DO NOT MODIFY.
37  */
38 package android.os;
39 public interface IStringConstants extends android.os.IInterface
40 {
41   /** Default implementation for IStringConstants. */
42   public static class Default implements android.os.IStringConstants
43   {
44     @Override
45     public android.os.IBinder asBinder() {
46       return null;
47     }
48   }
49   /** Local-side IPC implementation stub class. */
50   public static abstract class Stub extends android.os.Binder implements android.os.IStringConstants
51   {
52     /** Construct the stub at attach it to the interface. */
53     public Stub()
54     {
55       this.attachInterface(this, DESCRIPTOR);
56     }
57     /**
58      * Cast an IBinder object into an android.os.IStringConstants interface,
59      * generating a proxy if needed.
60      */
61     public static android.os.IStringConstants asInterface(android.os.IBinder obj)
62     {
63       if ((obj==null)) {
64         return null;
65       }
66       android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
67       if (((iin!=null)&&(iin instanceof android.os.IStringConstants))) {
68         return ((android.os.IStringConstants)iin);
69       }
70       return new android.os.IStringConstants.Stub.Proxy(obj);
71     }
72     @Override public android.os.IBinder asBinder()
73     {
74       return this;
75     }
76     @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
77     {
78       java.lang.String descriptor = DESCRIPTOR;
79       switch (code)
80       {
81         case INTERFACE_TRANSACTION:
82         {
83           reply.writeString(descriptor);
84           return true;
85         }
86         default:
87         {
88           return super.onTransact(code, data, reply, flags);
89         }
90       }
91     }
92     private static class Proxy implements android.os.IStringConstants
93     {
94       private android.os.IBinder mRemote;
95       Proxy(android.os.IBinder remote)
96       {
97         mRemote = remote;
98       }
99       @Override public android.os.IBinder asBinder()
100       {
101         return mRemote;
102       }
103       public java.lang.String getInterfaceDescriptor()
104       {
105         return DESCRIPTOR;
106       }
107       public static android.os.IStringConstants sDefaultImpl;
108     }
109     public static boolean setDefaultImpl(android.os.IStringConstants impl) {
110       // Only one user of this interface can use this function
111       // at a time. This is a heuristic to detect if two different
112       // users in the same process use this function.
113       if (Stub.Proxy.sDefaultImpl != null) {
114         throw new IllegalStateException("setDefaultImpl() called twice");
115       }
116       if (impl != null) {
117         Stub.Proxy.sDefaultImpl = impl;
118         return true;
119       }
120       return false;
121     }
122     public static android.os.IStringConstants getDefaultImpl() {
123       return Stub.Proxy.sDefaultImpl;
124     }
125   }
126   public static final java.lang.String DESCRIPTOR = "android.os.IStringConstants";
127   public static final String EXAMPLE_CONSTANT = "foo";
128 }
129 )";
130 
131 const char kCppOutputPath[] = "some/path/to/output.cpp";
132 const char kGenHeaderDir[] = "output";
133 const char kGenInterfaceHeaderPath[] = "output/android/os/IStringConstants.h";
134 const char kExpectedIHeaderOutput[] =
135     R"(#ifndef AIDL_GENERATED_ANDROID_OS_I_STRING_CONSTANTS_H_
136 #define AIDL_GENERATED_ANDROID_OS_I_STRING_CONSTANTS_H_
137 
138 #include <binder/IBinder.h>
139 #include <binder/IInterface.h>
140 #include <binder/Status.h>
141 #include <utils/String16.h>
142 #include <utils/StrongPointer.h>
143 
144 namespace android {
145 
146 namespace os {
147 
148 class IStringConstants : public ::android::IInterface {
149 public:
150   DECLARE_META_INTERFACE(StringConstants)
151   static const ::android::String16& EXAMPLE_CONSTANT();
152 };  // class IStringConstants
153 
154 class IStringConstantsDefault : public IStringConstants {
155 public:
156   ::android::IBinder* onAsBinder() override {
157     return nullptr;
158   }
159 };  // class IStringConstantsDefault
160 
161 }  // namespace os
162 
163 }  // namespace android
164 
165 #endif  // AIDL_GENERATED_ANDROID_OS_I_STRING_CONSTANTS_H_
166 )";
167 
168 const char kExpectedCppOutput[] =
169     R"(#include <android/os/IStringConstants.h>
170 #include <android/os/BpStringConstants.h>
171 
172 namespace android {
173 
174 namespace os {
175 
176 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(StringConstants, "android.os.IStringConstants")
177 
178 const ::android::String16& IStringConstants::EXAMPLE_CONSTANT() {
179   static const ::android::String16 value(::android::String16("foo"));
180   return value;
181 }
182 
183 }  // namespace os
184 
185 }  // namespace android
186 #include <android/os/BpStringConstants.h>
187 #include <binder/Parcel.h>
188 #include <android-base/macros.h>
189 
190 namespace android {
191 
192 namespace os {
193 
194 BpStringConstants::BpStringConstants(const ::android::sp<::android::IBinder>& _aidl_impl)
195     : BpInterface<IStringConstants>(_aidl_impl){
196 }
197 
198 }  // namespace os
199 
200 }  // namespace android
201 #include <android/os/BnStringConstants.h>
202 #include <binder/Parcel.h>
203 #include <binder/Stability.h>
204 
205 namespace android {
206 
207 namespace os {
208 
209 BnStringConstants::BnStringConstants()
210 {
211   ::android::internal::Stability::markCompilationUnit(this);
212 }
213 
214 ::android::status_t BnStringConstants::onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) {
215   ::android::status_t _aidl_ret_status = ::android::OK;
216   switch (_aidl_code) {
217   default:
218   {
219     _aidl_ret_status = ::android::BBinder::onTransact(_aidl_code, _aidl_data, _aidl_reply, _aidl_flags);
220   }
221   break;
222   }
223   if (_aidl_ret_status == ::android::UNEXPECTED_NULL) {
224     _aidl_ret_status = ::android::binder::Status::fromExceptionCode(::android::binder::Status::EX_NULL_POINTER).writeToParcel(_aidl_reply);
225   }
226   return _aidl_ret_status;
227 }
228 
229 }  // namespace os
230 
231 }  // namespace android
232 )";
233 
234 const char kExpectedJavaOutputWithVersionAndHash[] =
235     R"(/*
236  * This file is auto-generated.  DO NOT MODIFY.
237  */
238 package android.os;
239 public interface IStringConstants extends android.os.IInterface
240 {
241   /**
242    * The version of this interface that the caller is built against.
243    * This might be different from what {@link #getInterfaceVersion()
244    * getInterfaceVersion} returns as that is the version of the interface
245    * that the remote object is implementing.
246    */
247   public static final int VERSION = 10;
248   public static final String HASH = "abcdefg";
249   /** Default implementation for IStringConstants. */
250   public static class Default implements android.os.IStringConstants
251   {
252     @Override
253     public int getInterfaceVersion() {
254       return 0;
255     }
256     @Override
257     public String getInterfaceHash() {
258       return "";
259     }
260     @Override
261     public android.os.IBinder asBinder() {
262       return null;
263     }
264   }
265   /** Local-side IPC implementation stub class. */
266   public static abstract class Stub extends android.os.Binder implements android.os.IStringConstants
267   {
268     /** Construct the stub at attach it to the interface. */
269     public Stub()
270     {
271       this.attachInterface(this, DESCRIPTOR);
272     }
273     /**
274      * Cast an IBinder object into an android.os.IStringConstants interface,
275      * generating a proxy if needed.
276      */
277     public static android.os.IStringConstants asInterface(android.os.IBinder obj)
278     {
279       if ((obj==null)) {
280         return null;
281       }
282       android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
283       if (((iin!=null)&&(iin instanceof android.os.IStringConstants))) {
284         return ((android.os.IStringConstants)iin);
285       }
286       return new android.os.IStringConstants.Stub.Proxy(obj);
287     }
288     @Override public android.os.IBinder asBinder()
289     {
290       return this;
291     }
292     @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
293     {
294       java.lang.String descriptor = DESCRIPTOR;
295       switch (code)
296       {
297         case INTERFACE_TRANSACTION:
298         {
299           reply.writeString(descriptor);
300           return true;
301         }
302         case TRANSACTION_getInterfaceVersion:
303         {
304           data.enforceInterface(descriptor);
305           reply.writeNoException();
306           reply.writeInt(getInterfaceVersion());
307           return true;
308         }
309         case TRANSACTION_getInterfaceHash:
310         {
311           data.enforceInterface(descriptor);
312           reply.writeNoException();
313           reply.writeString(getInterfaceHash());
314           return true;
315         }
316         default:
317         {
318           return super.onTransact(code, data, reply, flags);
319         }
320       }
321     }
322     private static class Proxy implements android.os.IStringConstants
323     {
324       private android.os.IBinder mRemote;
325       Proxy(android.os.IBinder remote)
326       {
327         mRemote = remote;
328       }
329       private int mCachedVersion = -1;
330       private String mCachedHash = "-1";
331       @Override public android.os.IBinder asBinder()
332       {
333         return mRemote;
334       }
335       public java.lang.String getInterfaceDescriptor()
336       {
337         return DESCRIPTOR;
338       }
339       @Override
340       public int getInterfaceVersion() throws android.os.RemoteException {
341         if (mCachedVersion == -1) {
342           android.os.Parcel data = android.os.Parcel.obtain();
343           android.os.Parcel reply = android.os.Parcel.obtain();
344           try {
345             data.writeInterfaceToken(DESCRIPTOR);
346             boolean _status = mRemote.transact(Stub.TRANSACTION_getInterfaceVersion, data, reply, 0);
347             if (!_status) {
348               if (getDefaultImpl() != null) {
349                 return getDefaultImpl().getInterfaceVersion();
350               }
351             }
352             reply.readException();
353             mCachedVersion = reply.readInt();
354           } finally {
355             reply.recycle();
356             data.recycle();
357           }
358         }
359         return mCachedVersion;
360       }
361       @Override
362       public synchronized String getInterfaceHash() throws android.os.RemoteException {
363         if ("-1".equals(mCachedHash)) {
364           android.os.Parcel data = android.os.Parcel.obtain();
365           android.os.Parcel reply = android.os.Parcel.obtain();
366           try {
367             data.writeInterfaceToken(DESCRIPTOR);
368             boolean _status = mRemote.transact(Stub.TRANSACTION_getInterfaceHash, data, reply, 0);
369             if (!_status) {
370               if (getDefaultImpl() != null) {
371                 return getDefaultImpl().getInterfaceHash();
372               }
373             }
374             reply.readException();
375             mCachedHash = reply.readString();
376           } finally {
377             reply.recycle();
378             data.recycle();
379           }
380         }
381         return mCachedHash;
382       }
383       public static android.os.IStringConstants sDefaultImpl;
384     }
385     static final int TRANSACTION_getInterfaceVersion = (android.os.IBinder.FIRST_CALL_TRANSACTION + 16777214);
386     static final int TRANSACTION_getInterfaceHash = (android.os.IBinder.FIRST_CALL_TRANSACTION + 16777213);
387     public static boolean setDefaultImpl(android.os.IStringConstants impl) {
388       // Only one user of this interface can use this function
389       // at a time. This is a heuristic to detect if two different
390       // users in the same process use this function.
391       if (Stub.Proxy.sDefaultImpl != null) {
392         throw new IllegalStateException("setDefaultImpl() called twice");
393       }
394       if (impl != null) {
395         Stub.Proxy.sDefaultImpl = impl;
396         return true;
397       }
398       return false;
399     }
400     public static android.os.IStringConstants getDefaultImpl() {
401       return Stub.Proxy.sDefaultImpl;
402     }
403   }
404   public static final java.lang.String DESCRIPTOR = "android.os.IStringConstants";
405   public static final String EXAMPLE_CONSTANT = "foo";
406   public int getInterfaceVersion() throws android.os.RemoteException;
407   public String getInterfaceHash() throws android.os.RemoteException;
408 }
409 )";
410 
411 const char kExpectedIHeaderOutputWithVersionAndHash[] =
412     R"(#ifndef AIDL_GENERATED_ANDROID_OS_I_STRING_CONSTANTS_H_
413 #define AIDL_GENERATED_ANDROID_OS_I_STRING_CONSTANTS_H_
414 
415 #include <binder/IBinder.h>
416 #include <binder/IInterface.h>
417 #include <binder/Status.h>
418 #include <cstdint>
419 #include <utils/String16.h>
420 #include <utils/StrongPointer.h>
421 
422 namespace android {
423 
424 namespace os {
425 
426 class IStringConstants : public ::android::IInterface {
427 public:
428   DECLARE_META_INTERFACE(StringConstants)
429   const int32_t VERSION = 10;
430   const std::string HASH = "abcdefg";
431   static const ::android::String16& EXAMPLE_CONSTANT();
432   virtual int32_t getInterfaceVersion() = 0;
433   virtual std::string getInterfaceHash() = 0;
434 };  // class IStringConstants
435 
436 class IStringConstantsDefault : public IStringConstants {
437 public:
438   ::android::IBinder* onAsBinder() override {
439     return nullptr;
440   }
441   int32_t getInterfaceVersion() override {
442     return 0;
443   }
444   std::string getInterfaceHash() override {
445     return "";
446   }
447 };  // class IStringConstantsDefault
448 
449 }  // namespace os
450 
451 }  // namespace android
452 
453 #endif  // AIDL_GENERATED_ANDROID_OS_I_STRING_CONSTANTS_H_
454 )";
455 
456 const char kExpectedCppOutputWithVersionAndHash[] =
457     R"(#include <android/os/IStringConstants.h>
458 #include <android/os/BpStringConstants.h>
459 
460 namespace android {
461 
462 namespace os {
463 
464 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(StringConstants, "android.os.IStringConstants")
465 
466 const ::android::String16& IStringConstants::EXAMPLE_CONSTANT() {
467   static const ::android::String16 value(::android::String16("foo"));
468   return value;
469 }
470 
471 }  // namespace os
472 
473 }  // namespace android
474 #include <android/os/BpStringConstants.h>
475 #include <binder/Parcel.h>
476 #include <android-base/macros.h>
477 
478 namespace android {
479 
480 namespace os {
481 
482 BpStringConstants::BpStringConstants(const ::android::sp<::android::IBinder>& _aidl_impl)
483     : BpInterface<IStringConstants>(_aidl_impl){
484 }
485 
486 int32_t BpStringConstants::getInterfaceVersion() {
487   if (cached_version_ == -1) {
488     ::android::Parcel data;
489     ::android::Parcel reply;
490     data.writeInterfaceToken(getInterfaceDescriptor());
491     ::android::status_t err = remote()->transact(::android::IBinder::FIRST_CALL_TRANSACTION + 16777214 /* getInterfaceVersion */, data, &reply);
492     if (err == ::android::OK) {
493       ::android::binder::Status _aidl_status;
494       err = _aidl_status.readFromParcel(reply);
495       if (err == ::android::OK && _aidl_status.isOk()) {
496         cached_version_ = reply.readInt32();
497       }
498     }
499   }
500   return cached_version_;
501 }
502 
503 std::string BpStringConstants::getInterfaceHash() {
504   std::lock_guard<std::mutex> lockGuard(cached_hash_mutex_);
505   if (cached_hash_ == "-1") {
506     ::android::Parcel data;
507     ::android::Parcel reply;
508     data.writeInterfaceToken(getInterfaceDescriptor());
509     ::android::status_t err = remote()->transact(::android::IBinder::FIRST_CALL_TRANSACTION + 16777213 /* getInterfaceHash */, data, &reply);
510     if (err == ::android::OK) {
511       ::android::binder::Status _aidl_status;
512       err = _aidl_status.readFromParcel(reply);
513       if (err == ::android::OK && _aidl_status.isOk()) {
514         reply.readUtf8FromUtf16(&cached_hash_);
515       }
516     }
517   }
518   return cached_hash_;
519 }
520 
521 }  // namespace os
522 
523 }  // namespace android
524 #include <android/os/BnStringConstants.h>
525 #include <binder/Parcel.h>
526 #include <binder/Stability.h>
527 
528 namespace android {
529 
530 namespace os {
531 
532 BnStringConstants::BnStringConstants()
533 {
534   ::android::internal::Stability::markCompilationUnit(this);
535 }
536 
537 ::android::status_t BnStringConstants::onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) {
538   ::android::status_t _aidl_ret_status = ::android::OK;
539   switch (_aidl_code) {
540   case ::android::IBinder::FIRST_CALL_TRANSACTION + 16777214 /* getInterfaceVersion */:
541   {
542     _aidl_data.checkInterface(this);
543     _aidl_reply->writeNoException();
544     _aidl_reply->writeInt32(IStringConstants::VERSION);
545   }
546   break;
547   case ::android::IBinder::FIRST_CALL_TRANSACTION + 16777213 /* getInterfaceHash */:
548   {
549     _aidl_data.checkInterface(this);
550     _aidl_reply->writeNoException();
551     _aidl_reply->writeUtf8AsUtf16(IStringConstants::HASH);
552   }
553   break;
554   default:
555   {
556     _aidl_ret_status = ::android::BBinder::onTransact(_aidl_code, _aidl_data, _aidl_reply, _aidl_flags);
557   }
558   break;
559   }
560   if (_aidl_ret_status == ::android::UNEXPECTED_NULL) {
561     _aidl_ret_status = ::android::binder::Status::fromExceptionCode(::android::binder::Status::EX_NULL_POINTER).writeToParcel(_aidl_reply);
562   }
563   return _aidl_ret_status;
564 }
565 
566 int32_t BnStringConstants::getInterfaceVersion() {
567   return IStringConstants::VERSION;
568 }
569 
570 std::string BnStringConstants::getInterfaceHash() {
571   return IStringConstants::HASH;
572 }
573 
574 }  // namespace os
575 
576 }  // namespace android
577 )";
578 
579 }  // namespace string_constants
580 }  // namespace test_data
581 }  // namespace aidl
582 }  // namespace android
583