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