1 /*
2  * Copyright (C) 2018 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 package android.binder.cts;
18 
19 import android.os.Binder;
20 import android.os.IBinder;
21 import android.os.ParcelFileDescriptor;
22 import android.os.PersistableBundle;
23 import android.os.RemoteException;
24 import java.io.FileDescriptor;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.util.concurrent.CountDownLatch;
28 import test_package.Bar;
29 import test_package.Baz;
30 import test_package.Foo;
31 import test_package.GenericBar;
32 import test_package.ICompatTest;
33 import test_package.IEmpty;
34 import test_package.ITest;
35 import test_package.RegularPolygon;
36 import test_package.ExtendableParcelable;
37 import test_package.MyExt;
38 import test_package.SimpleUnion;
39 
40 public class TestImpl extends ITest.Stub {
41   @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)42   protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
43     for (String arg : args) {
44       pw.print(arg);
45     }
46   }
47 
48   @Override
GetName()49   public String GetName() {
50     return "JAVA";
51   }
52 
53   @Override
TestVoidReturn()54   public void TestVoidReturn() {}
55 
56   @Override
TestOneway()57   public void TestOneway() throws RemoteException {
58     throw new RemoteException("Oneway call errors should be ignored");
59   }
60 
61   @Override
GiveMeMyCallingPid()62   public int GiveMeMyCallingPid() {
63     return Binder.getCallingPid();
64   }
65 
66   @Override
GiveMeMyCallingUid()67   public int GiveMeMyCallingUid() {
68     return Binder.getCallingUid();
69   }
70 
71   private CountDownLatch mCachedLatch = new CountDownLatch(1);
72   private int mCachedPid = -1;
73   private int mCachedUid = -1;
74 
waitForCachedOnewayInfo()75   private void waitForCachedOnewayInfo() {
76     try {
77       mCachedLatch.await();
78     } catch (InterruptedException e) {
79       // This thread is never expected to be interrupted during this test. This will be
80       // converted to a RemoteException on the other side and cause the test to fail.
81 
82       throw new RuntimeException(e.toString());
83     }
84   }
85 
86   @Override
CacheCallingInfoFromOneway()87   public void CacheCallingInfoFromOneway() {
88     mCachedPid = Binder.getCallingPid();
89     mCachedUid = Binder.getCallingUid();
90     mCachedLatch.countDown();
91   }
92 
93   @Override
GiveMeMyCallingPidFromOneway()94   public int GiveMeMyCallingPidFromOneway() {
95     waitForCachedOnewayInfo();
96     return mCachedPid;
97   }
98 
99   @Override
GiveMeMyCallingUidFromOneway()100   public int GiveMeMyCallingUidFromOneway() {
101     waitForCachedOnewayInfo();
102     return mCachedUid;
103   }
104 
105   @Override
RepeatInt(int in_value)106   public int RepeatInt(int in_value) {
107     return in_value;
108   }
109 
110   @Override
RepeatLong(long in_value)111   public long RepeatLong(long in_value) {
112     return in_value;
113   }
114 
115   @Override
RepeatFloat(float in_value)116   public float RepeatFloat(float in_value) {
117     return in_value;
118   }
119 
120   @Override
RepeatDouble(double in_value)121   public double RepeatDouble(double in_value) {
122     return in_value;
123   }
124 
125   @Override
RepeatBoolean(boolean in_value)126   public boolean RepeatBoolean(boolean in_value) {
127     return in_value;
128   }
129 
130   @Override
RepeatChar(char in_value)131   public char RepeatChar(char in_value) {
132     return in_value;
133   }
134 
135   @Override
RepeatByte(byte in_value)136   public byte RepeatByte(byte in_value) {
137     return in_value;
138   }
139 
140   @Override
RepeatByteEnum(byte in_value)141   public byte RepeatByteEnum(byte in_value) {
142     return in_value;
143   }
144 
145   @Override
RepeatIntEnum(int in_value)146   public int RepeatIntEnum(int in_value) {
147     return in_value;
148   }
149 
150   @Override
RepeatLongEnum(long in_value)151   public long RepeatLongEnum(long in_value) {
152     return in_value;
153   }
154 
155   @Override
RepeatBinder(IBinder in_value)156   public IBinder RepeatBinder(IBinder in_value) {
157     return in_value;
158   }
159 
160   @Override
RepeatNullableBinder(IBinder in_value)161   public IBinder RepeatNullableBinder(IBinder in_value) {
162     return in_value;
163   }
164 
165   @Override
RepeatInterface(IEmpty in_value)166   public IEmpty RepeatInterface(IEmpty in_value) {
167     return in_value;
168   }
169 
170   @Override
RepeatNullableInterface(IEmpty in_value)171   public IEmpty RepeatNullableInterface(IEmpty in_value) {
172     return in_value;
173   }
174 
175   @Override
RepeatFd(ParcelFileDescriptor in_value)176   public ParcelFileDescriptor RepeatFd(ParcelFileDescriptor in_value) {
177     return in_value;
178   }
179 
180   @Override
RepeatNullableFd(ParcelFileDescriptor in_value)181   public ParcelFileDescriptor RepeatNullableFd(ParcelFileDescriptor in_value) {
182     return in_value;
183   }
184 
185   @Override
RepeatString(String in_value)186   public String RepeatString(String in_value) {
187     return in_value;
188   }
189 
190   @Override
RepeatNullableString(String in_value)191   public String RepeatNullableString(String in_value) {
192     return in_value;
193   }
194 
195   @Override
RepeatPolygon(RegularPolygon in_value)196   public RegularPolygon RepeatPolygon(RegularPolygon in_value) {
197     return in_value;
198   }
199 
200   @Override
RepeatNullablePolygon(RegularPolygon in_value)201   public RegularPolygon RepeatNullablePolygon(RegularPolygon in_value) {
202     return in_value;
203   }
204 
205   @Override
RepeatPersistableBundle(PersistableBundle in_value)206   public PersistableBundle RepeatPersistableBundle(PersistableBundle in_value) {
207     return in_value;
208   }
209 
210   @Override
RenamePolygon(RegularPolygon value, String name)211   public void RenamePolygon(RegularPolygon value, String name) {
212     value.name = name;
213   }
214 
215   @Override
RepeatBooleanArray(boolean[] in_value, boolean[] repeated)216   public boolean[] RepeatBooleanArray(boolean[] in_value, boolean[] repeated) {
217     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
218     return in_value;
219   }
220 
221   @Override
RepeatByteArray(byte[] in_value, byte[] repeated)222   public byte[] RepeatByteArray(byte[] in_value, byte[] repeated) {
223     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
224     return in_value;
225   }
226 
227   @Override
RepeatCharArray(char[] in_value, char[] repeated)228   public char[] RepeatCharArray(char[] in_value, char[] repeated) {
229     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
230     return in_value;
231   }
232 
233   @Override
RepeatIntArray(int[] in_value, int[] repeated)234   public int[] RepeatIntArray(int[] in_value, int[] repeated) {
235     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
236     return in_value;
237   }
238 
239   @Override
RepeatLongArray(long[] in_value, long[] repeated)240   public long[] RepeatLongArray(long[] in_value, long[] repeated) {
241     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
242     return in_value;
243   }
244 
245   @Override
RepeatFloatArray(float[] in_value, float[] repeated)246   public float[] RepeatFloatArray(float[] in_value, float[] repeated) {
247     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
248     return in_value;
249   }
250 
251   @Override
RepeatDoubleArray(double[] in_value, double[] repeated)252   public double[] RepeatDoubleArray(double[] in_value, double[] repeated) {
253     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
254     return in_value;
255   }
256 
257   @Override
RepeatByteEnumArray(byte[] in_value, byte[] repeated)258   public byte[] RepeatByteEnumArray(byte[] in_value, byte[] repeated) {
259     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
260     return in_value;
261   }
262 
263   @Override
RepeatIntEnumArray(int[] in_value, int[] repeated)264   public int[] RepeatIntEnumArray(int[] in_value, int[] repeated) {
265     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
266     return in_value;
267   }
268 
269   @Override
RepeatLongEnumArray(long[] in_value, long[] repeated)270   public long[] RepeatLongEnumArray(long[] in_value, long[] repeated) {
271     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
272     return in_value;
273   }
274 
275   @Override
RepeatStringArray(String[] in_value, String[] repeated)276   public String[] RepeatStringArray(String[] in_value, String[] repeated) {
277     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
278     return in_value;
279   }
280 
281   @Override
RepeatRegularPolygonArray(RegularPolygon[] in_value, RegularPolygon[] repeated)282   public RegularPolygon[] RepeatRegularPolygonArray(RegularPolygon[] in_value, RegularPolygon[] repeated) {
283     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
284     return in_value;
285   }
286 
287   @Override
RepeatFdArray(ParcelFileDescriptor[] in_value, ParcelFileDescriptor[] repeated)288   public ParcelFileDescriptor[] RepeatFdArray(ParcelFileDescriptor[] in_value, ParcelFileDescriptor[] repeated) {
289     ParcelFileDescriptor[] out = new ParcelFileDescriptor[in_value.length];
290     for (int i = 0; i < in_value.length; i++) {
291       try {
292         repeated[i] = in_value[i].dup();
293         out[i] = in_value[i].dup();
294       } catch (IOException e) {
295         e.printStackTrace();
296       }
297     }
298     return out;
299   }
300 
301   @Override
RepeatBinderArray(IBinder[] in_value, IBinder[] repeated)302   public IBinder[] RepeatBinderArray(IBinder[] in_value, IBinder[] repeated) {
303     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
304     return in_value;
305   }
306 
307   @Override
RepeatInterfaceArray(IEmpty[] in_value, IEmpty[] repeated)308   public IEmpty[] RepeatInterfaceArray(IEmpty[] in_value, IEmpty[] repeated) {
309     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
310     return in_value;
311   }
312 
Repeat2StringList(java.util.List<String> in_value, java.util.List<String> repeated)313   public java.util.List<String> Repeat2StringList(java.util.List<String> in_value, java.util.List<String> repeated) {
314     repeated.addAll(in_value);
315     repeated.addAll(in_value);
316     return repeated;
317   }
318 
319   @Override
Repeat2RegularPolygonList(java.util.List<RegularPolygon> in_value, java.util.List<RegularPolygon> repeated)320   public java.util.List<RegularPolygon> Repeat2RegularPolygonList(java.util.List<RegularPolygon> in_value, java.util.List<RegularPolygon> repeated) {
321     repeated.addAll(in_value);
322     repeated.addAll(in_value);
323     return repeated;
324   }
325 
326   @Override
RepeatNullableBooleanArray(boolean[] in_value)327   public boolean[] RepeatNullableBooleanArray(boolean[] in_value) {
328     return in_value;
329   }
330 
331   @Override
RepeatNullableByteArray(byte[] in_value)332   public byte[] RepeatNullableByteArray(byte[] in_value) {
333     return in_value;
334   }
335 
336   @Override
RepeatNullableCharArray(char[] in_value)337   public char[] RepeatNullableCharArray(char[] in_value) {
338     return in_value;
339   }
340 
341   @Override
RepeatNullableIntArray(int[] in_value)342   public int[] RepeatNullableIntArray(int[] in_value) {
343     return in_value;
344   }
345 
346   @Override
RepeatNullableLongArray(long[] in_value)347   public long[] RepeatNullableLongArray(long[] in_value) {
348     return in_value;
349   }
350 
351   @Override
RepeatNullableFloatArray(float[] in_value)352   public float[] RepeatNullableFloatArray(float[] in_value) {
353     return in_value;
354   }
355 
356   @Override
RepeatNullableDoubleArray(double[] in_value)357   public double[] RepeatNullableDoubleArray(double[] in_value) {
358     return in_value;
359   }
360 
361   @Override
RepeatNullableByteEnumArray(byte[] in_value)362   public byte[] RepeatNullableByteEnumArray(byte[] in_value) {
363     return in_value;
364   }
365 
366   @Override
RepeatNullableIntEnumArray(int[] in_value)367   public int[] RepeatNullableIntEnumArray(int[] in_value) {
368     return in_value;
369   }
370 
371   @Override
RepeatNullableLongEnumArray(long[] in_value)372   public long[] RepeatNullableLongEnumArray(long[] in_value) {
373     return in_value;
374   }
375 
376   @Override
RepeatNullableStringArray(String[] in_value)377   public String[] RepeatNullableStringArray(String[] in_value) {
378     return in_value;
379   }
380 
381   @Override
RepeatNullableBinderArray(IBinder[] in_value)382   public IBinder[] RepeatNullableBinderArray(IBinder[] in_value) {
383     return in_value;
384   }
385 
386   @Override
RepeatNullableInterfaceArray(IEmpty[] in_value)387   public IEmpty[] RepeatNullableInterfaceArray(IEmpty[] in_value) {
388     return in_value;
389   }
390 
391   @Override
DoubleRepeatNullableStringArray(String[] in_value, String[] repeated)392   public String[] DoubleRepeatNullableStringArray(String[] in_value, String[] repeated) {
393     if (in_value == null) {
394       return null; // can't do anything to repeated
395     }
396 
397     System.arraycopy(in_value, 0, repeated, 0, in_value.length);
398     return in_value;
399   }
400 
401   @Override
repeatFoo(Foo inFoo)402   public Foo repeatFoo(Foo inFoo) {
403     return inFoo;
404   }
405 
406   @Override
renameFoo(Foo foo, String name)407   public void renameFoo(Foo foo, String name) {
408     foo.a = name;
409   }
410 
411   @Override
renameBar(Foo foo, String name)412   public void renameBar(Foo foo, String name) {
413     if (foo.d == null) {
414       foo.d = new Bar();
415     }
416     foo.d.a = name;
417   }
418 
419   @Override
getF(Foo foo)420   public int getF(Foo foo) {
421     return foo.f;
422   }
423 
424   @Override
repeatGenericBar(GenericBar<Integer> bar)425   public GenericBar<Integer> repeatGenericBar(GenericBar<Integer> bar) {
426     return bar;
427   }
428 
429   @Override
RepeatExtendableParcelable(ExtendableParcelable in, ExtendableParcelable out)430   public void RepeatExtendableParcelable(ExtendableParcelable in, ExtendableParcelable out) {
431     RepeatExtendableParcelableWithoutExtension(in, out);
432     MyExt ext = in.ext.getParcelable(MyExt.class);
433     MyExt ext2 = new MyExt();
434     ext2.a = ext.a;
435     ext2.b = ext.b;
436     out.ext.setParcelable(ext2);
437   }
438 
439   @Override
RepeatExtendableParcelableWithoutExtension(ExtendableParcelable in, ExtendableParcelable out)440   public void RepeatExtendableParcelableWithoutExtension(ExtendableParcelable in, ExtendableParcelable out) {
441     out.a = in.a;
442     out.b = in.b;
443     out.c = in.c;
444   }
445 
RepeatSimpleUnion(SimpleUnion in_u)446   public SimpleUnion RepeatSimpleUnion(SimpleUnion in_u) {
447     return in_u;
448   }
449 
450   private static class CompatTest extends ICompatTest.Stub {
451     @Override
getInterfaceVersion()452     public int getInterfaceVersion() { return CompatTest.VERSION; }
453 
454     @Override
getInterfaceHash()455     public String getInterfaceHash() { return CompatTest.HASH; }
456 
457     @Override
repeatBaz(Baz inBaz)458     public Baz repeatBaz(Baz inBaz) {
459       return inBaz;
460     }
461 
462     @Override
RepeatStringNullableLater(String in_value)463     public String RepeatStringNullableLater(String in_value) {
464       return in_value;
465     }
466 
467     @Override
NewMethodThatReturns10()468     public int NewMethodThatReturns10() {
469       return 10;
470     }
471   }
472 
473   @Override
getICompatTest()474   public IBinder getICompatTest() {
475     return new CompatTest();
476   }
477 
478   @Override
getLegacyBinderTest()479   public IBinder getLegacyBinderTest() {
480     return new ILegacyBinder.Stub();
481   }
482 }
483