• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private static final java.lang.String DESCRIPTOR = "android.os.IStringConstants";
53     /** Construct the stub at attach it to the interface. */
54     public Stub()
55     {
56       this.attachInterface(this, DESCRIPTOR);
57     }
58     /**
59      * Cast an IBinder object into an android.os.IStringConstants interface,
60      * generating a proxy if needed.
61      */
62     public static android.os.IStringConstants asInterface(android.os.IBinder obj)
63     {
64       if ((obj==null)) {
65         return null;
66       }
67       android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
68       if (((iin!=null)&&(iin instanceof android.os.IStringConstants))) {
69         return ((android.os.IStringConstants)iin);
70       }
71       return new android.os.IStringConstants.Stub.Proxy(obj);
72     }
73     @Override public android.os.IBinder asBinder()
74     {
75       return this;
76     }
77     @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
78     {
79       java.lang.String descriptor = DESCRIPTOR;
80       switch (code)
81       {
82         case INTERFACE_TRANSACTION:
83         {
84           reply.writeString(descriptor);
85           return true;
86         }
87         default:
88         {
89           return super.onTransact(code, data, reply, flags);
90         }
91       }
92     }
93     private static class Proxy implements android.os.IStringConstants
94     {
95       private android.os.IBinder mRemote;
96       Proxy(android.os.IBinder remote)
97       {
98         mRemote = remote;
99       }
100       @Override public android.os.IBinder asBinder()
101       {
102         return mRemote;
103       }
104       public java.lang.String getInterfaceDescriptor()
105       {
106         return DESCRIPTOR;
107       }
108       public static android.os.IStringConstants sDefaultImpl;
109     }
110     public static boolean setDefaultImpl(android.os.IStringConstants impl) {
111       // Only one user of this interface can use this function
112       // at a time. This is a heuristic to detect if two different
113       // users in the same process use this function.
114       if (Stub.Proxy.sDefaultImpl != null) {
115         throw new IllegalStateException("setDefaultImpl() called twice");
116       }
117       if (impl != null) {
118         Stub.Proxy.sDefaultImpl = impl;
119         return true;
120       }
121       return false;
122     }
123     public static android.os.IStringConstants getDefaultImpl() {
124       return Stub.Proxy.sDefaultImpl;
125     }
126   }
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     private static final java.lang.String DESCRIPTOR = "android.os.IStringConstants";
269     /** Construct the stub at attach it to the interface. */
270     public Stub()
271     {
272       this.attachInterface(this, DESCRIPTOR);
273     }
274     /**
275      * Cast an IBinder object into an android.os.IStringConstants interface,
276      * generating a proxy if needed.
277      */
278     public static android.os.IStringConstants asInterface(android.os.IBinder obj)
279     {
280       if ((obj==null)) {
281         return null;
282       }
283       android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
284       if (((iin!=null)&&(iin instanceof android.os.IStringConstants))) {
285         return ((android.os.IStringConstants)iin);
286       }
287       return new android.os.IStringConstants.Stub.Proxy(obj);
288     }
289     @Override public android.os.IBinder asBinder()
290     {
291       return this;
292     }
293     @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
294     {
295       java.lang.String descriptor = DESCRIPTOR;
296       switch (code)
297       {
298         case INTERFACE_TRANSACTION:
299         {
300           reply.writeString(descriptor);
301           return true;
302         }
303         case TRANSACTION_getInterfaceVersion:
304         {
305           data.enforceInterface(descriptor);
306           reply.writeNoException();
307           reply.writeInt(getInterfaceVersion());
308           return true;
309         }
310         case TRANSACTION_getInterfaceHash:
311         {
312           data.enforceInterface(descriptor);
313           reply.writeNoException();
314           reply.writeString(getInterfaceHash());
315           return true;
316         }
317         default:
318         {
319           return super.onTransact(code, data, reply, flags);
320         }
321       }
322     }
323     private static class Proxy implements android.os.IStringConstants
324     {
325       private android.os.IBinder mRemote;
326       Proxy(android.os.IBinder remote)
327       {
328         mRemote = remote;
329       }
330       private int mCachedVersion = -1;
331       private String mCachedHash = "-1";
332       @Override public android.os.IBinder asBinder()
333       {
334         return mRemote;
335       }
336       public java.lang.String getInterfaceDescriptor()
337       {
338         return DESCRIPTOR;
339       }
340       @Override
341       public int getInterfaceVersion() throws android.os.RemoteException {
342         if (mCachedVersion == -1) {
343           android.os.Parcel data = android.os.Parcel.obtain();
344           android.os.Parcel reply = android.os.Parcel.obtain();
345           try {
346             data.writeInterfaceToken(DESCRIPTOR);
347             boolean _status = mRemote.transact(Stub.TRANSACTION_getInterfaceVersion, data, reply, 0);
348             if (!_status) {
349               if (getDefaultImpl() != null) {
350                 return getDefaultImpl().getInterfaceVersion();
351               }
352             }
353             reply.readException();
354             mCachedVersion = reply.readInt();
355           } finally {
356             reply.recycle();
357             data.recycle();
358           }
359         }
360         return mCachedVersion;
361       }
362       @Override
363       public synchronized String getInterfaceHash() throws android.os.RemoteException {
364         if ("-1".equals(mCachedHash)) {
365           android.os.Parcel data = android.os.Parcel.obtain();
366           android.os.Parcel reply = android.os.Parcel.obtain();
367           try {
368             data.writeInterfaceToken(DESCRIPTOR);
369             boolean _status = mRemote.transact(Stub.TRANSACTION_getInterfaceHash, data, reply, 0);
370             if (!_status) {
371               if (getDefaultImpl() != null) {
372                 return getDefaultImpl().getInterfaceHash();
373               }
374             }
375             reply.readException();
376             mCachedHash = reply.readString();
377           } finally {
378             reply.recycle();
379             data.recycle();
380           }
381         }
382         return mCachedHash;
383       }
384       public static android.os.IStringConstants sDefaultImpl;
385     }
386     static final int TRANSACTION_getInterfaceVersion = (android.os.IBinder.FIRST_CALL_TRANSACTION + 16777214);
387     static final int TRANSACTION_getInterfaceHash = (android.os.IBinder.FIRST_CALL_TRANSACTION + 16777213);
388     public static boolean setDefaultImpl(android.os.IStringConstants impl) {
389       // Only one user of this interface can use this function
390       // at a time. This is a heuristic to detect if two different
391       // users in the same process use this function.
392       if (Stub.Proxy.sDefaultImpl != null) {
393         throw new IllegalStateException("setDefaultImpl() called twice");
394       }
395       if (impl != null) {
396         Stub.Proxy.sDefaultImpl = impl;
397         return true;
398       }
399       return false;
400     }
401     public static android.os.IStringConstants getDefaultImpl() {
402       return Stub.Proxy.sDefaultImpl;
403     }
404   }
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