• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assume.assumeTrue;
24 
25 import android.os.IBinder;
26 import android.os.ParcelFileDescriptor;
27 import android.os.Process;
28 import android.os.RemoteException;
29 import android.util.Log;
30 import androidx.test.InstrumentationRegistry;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.function.BiPredicate;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.Parameterized;
44 import test_package.Bar;
45 import test_package.Baz;
46 import test_package.ByteEnum;
47 import test_package.ExtendableParcelable;
48 import test_package.Foo;
49 import test_package.GenericBar;
50 import test_package.GenericFoo;
51 import test_package.ICompatTest;
52 import test_package.IEmpty;
53 import test_package.ITest;
54 import test_package.IntEnum;
55 import test_package.LongEnum;
56 import test_package.MyExt;
57 import test_package.RegularPolygon;
58 import test_package.SimpleUnion;
59 
60 @RunWith(Parameterized.class)
61 public class JavaClientTest {
62     private final String TAG = "JavaClientTest";
63 
64     private Class mServiceClass;
65     private ITest mInterface;
66     private String mExpectedName;
67     private boolean mShouldBeRemote;
68     private boolean mShouldBeOld;
69 
JavaClientTest(Class serviceClass, String expectedName, boolean shouldBeRemote, boolean shouldBeOld)70     public JavaClientTest(Class serviceClass, String expectedName, boolean shouldBeRemote, boolean shouldBeOld) {
71         mServiceClass = serviceClass;
72         mExpectedName = expectedName;
73         mShouldBeRemote = shouldBeRemote;
74         mShouldBeOld = shouldBeOld;
75     }
76 
77     @Parameterized.Parameters( name = "{0}" )
data()78     public static Collection<Object[]> data() {
79         // For local interfaces, this test will parcel the data locally.
80         // Whenever possible, the desired service should be accessed directly
81         // in order to avoid this additional overhead.
82         return Arrays.asList(new Object[][] {
83                 {NativeService.Local.class, "CPP", false /*shouldBeRemote*/, false /*shouldBeOld*/},
84                 {JavaService.Local.class, "JAVA", false /*shouldBeRemote*/, false /*shouldBeOld*/},
85                 {NativeService.Remote.class, "CPP", true /*shouldBeRemote*/, false /*shouldBeOld*/},
86                 {NativeService.RemoteOld.class, "CPP", true /*shouldBeRemote*/, true /*shouldBeOld*/},
87                 {JavaService.Remote.class, "JAVA", true /*shouldBeRemote*/, false /*shouldBeOld*/},
88             });
89     }
90 
91     @Before
setUp()92     public void setUp() {
93         Log.e(TAG, "Setting up");
94 
95         SyncTestServiceConnection connection = new SyncTestServiceConnection(
96             InstrumentationRegistry.getTargetContext(), mServiceClass);
97 
98         mInterface = connection.get();
99         assertNotEquals(null, mInterface);
100     }
101 
102     @Test
testSanityCheckSource()103     public void testSanityCheckSource() throws RemoteException {
104         String name = mInterface.GetName();
105 
106         Log.i(TAG, "Service GetName: " + name);
107         assertEquals(mExpectedName, name);
108     }
109 
110     @Test
testVoidReturn()111     public void testVoidReturn() throws RemoteException {
112         mInterface.TestVoidReturn();
113     }
114 
115     @Test
testOneway()116     public void testOneway() throws RemoteException {
117         boolean isLocalJava = !mShouldBeRemote && mExpectedName == "JAVA";
118         try {
119             mInterface.TestOneway();
120             assertFalse("local Java should throw exception", isLocalJava);
121         } catch (RemoteException e) {
122             assertTrue("only local Java should report error", isLocalJava);
123         }
124     }
125 
checkDump(String expected, String[] args)126     private void checkDump(String expected, String[] args) throws RemoteException, IOException {
127         ParcelFileDescriptor[] sockets = ParcelFileDescriptor.createReliableSocketPair();
128         ParcelFileDescriptor socketIn = sockets[0];
129         ParcelFileDescriptor socketOut = sockets[1];
130 
131         mInterface.asBinder().dump(socketIn.getFileDescriptor(), args);
132         socketIn.close();
133 
134         FileInputStream fileInputStream = new ParcelFileDescriptor.AutoCloseInputStream(socketOut);
135 
136         byte[] expectedBytes = expected.getBytes();
137         byte[] input = new byte[expectedBytes.length];
138 
139         assertEquals(input.length, fileInputStream.read(input));
140         Assert.assertArrayEquals(input, expectedBytes);
141     }
142 
143     @Test
testDump()144     public void testDump() throws RemoteException, IOException {
145         checkDump("", new String[]{});
146         checkDump("", new String[]{"", ""});
147         checkDump("Hello World!", new String[]{"Hello ", "World!"});
148         checkDump("ABC", new String[]{"A", "B", "C"});
149     }
150 
151     @Test
testCallingInfo()152     public void testCallingInfo() throws RemoteException {
153       mInterface.CacheCallingInfoFromOneway();
154 
155       assertEquals(Process.myPid(), mInterface.GiveMeMyCallingPid());
156       assertEquals(Process.myUid(), mInterface.GiveMeMyCallingUid());
157 
158       if (mShouldBeRemote) {
159         // PID is hidden from oneway calls
160         assertEquals(0, mInterface.GiveMeMyCallingPidFromOneway());
161       } else {
162         assertEquals(Process.myPid(), mInterface.GiveMeMyCallingPidFromOneway());
163       }
164 
165       assertEquals(Process.myUid(), mInterface.GiveMeMyCallingUidFromOneway());
166     }
167 
168     @Test
testRepeatPrimitives()169     public void testRepeatPrimitives() throws RemoteException {
170         assertEquals(1, mInterface.RepeatInt(1));
171         assertEquals(2, mInterface.RepeatLong(2));
172         assertEquals(1.0f, mInterface.RepeatFloat(1.0f), 0.0f);
173         assertEquals(2.0, mInterface.RepeatDouble(2.0), 0.0);
174         assertEquals(true, mInterface.RepeatBoolean(true));
175         assertEquals('a', mInterface.RepeatChar('a'));
176         assertEquals((byte)3, mInterface.RepeatByte((byte)3));
177         assertEquals(ByteEnum.FOO, mInterface.RepeatByteEnum(ByteEnum.FOO));
178         assertEquals(IntEnum.FOO, mInterface.RepeatIntEnum(IntEnum.FOO));
179         assertEquals(LongEnum.FOO, mInterface.RepeatLongEnum(LongEnum.FOO));
180     }
181 
182     @Test
testRepeatBinder()183     public void testRepeatBinder() throws RemoteException {
184         IBinder binder = mInterface.asBinder();
185 
186         assertEquals(binder, mInterface.RepeatBinder(binder));
187         assertEquals(binder, mInterface.RepeatNullableBinder(binder));
188         assertEquals(null, mInterface.RepeatNullableBinder(null));
189     }
190 
191     private static class Empty extends IEmpty.Stub {}
192 
193     @Test
testRepeatInterface()194     public void testRepeatInterface() throws RemoteException {
195         IEmpty empty = new Empty();
196 
197         assertEquals(empty, mInterface.RepeatInterface(empty));
198         assertEquals(empty, mInterface.RepeatNullableInterface(empty));
199         assertEquals(null, mInterface.RepeatNullableInterface(null));
200     }
201 
202     private static interface IRepeatFd {
repeat(ParcelFileDescriptor fd)203         ParcelFileDescriptor repeat(ParcelFileDescriptor fd) throws RemoteException;
204     }
205 
checkFdRepeated(IRepeatFd transformer)206     private void checkFdRepeated(IRepeatFd transformer) throws RemoteException, IOException {
207         ParcelFileDescriptor[] sockets = ParcelFileDescriptor.createReliableSocketPair();
208         ParcelFileDescriptor socketIn = sockets[0];
209         ParcelFileDescriptor socketOut = sockets[1];
210 
211         ParcelFileDescriptor repeatFd = transformer.repeat(socketIn);
212 
213         boolean isNativeRemote = mInterface.GetName().equals("CPP");
214         try {
215             socketOut.checkError();
216 
217             // Either native didn't properly call detach, or native properly handles detach, and
218             // we should change the test to enforce that socket comms work.
219             assertFalse("Native doesn't implement comm fd but did not get detach.", isNativeRemote);
220         } catch (ParcelFileDescriptor.FileDescriptorDetachedException e) {
221             assertTrue("Detach, so remote should be native", isNativeRemote);
222         }
223 
224         // Both backends support these.
225         socketIn.checkError();
226         repeatFd.checkError();
227 
228         checkInOutSockets(repeatFd, socketOut);
229     }
230 
231     @Test
testRepeatFd()232     public void testRepeatFd() throws RemoteException, IOException {
233         checkFdRepeated((fd) -> mInterface.RepeatFd(fd));
234     }
235 
236     @Test
testRepeatFdNull()237     public void testRepeatFdNull() throws RemoteException {
238         boolean isNativeRemote = mInterface.GetName().equals("CPP");
239 
240         try {
241             mInterface.RepeatFd(null);
242             assertFalse("Native shouldn't accept null here", isNativeRemote);
243         } catch (java.lang.NullPointerException e) {
244             assertTrue("Java should accept null here", isNativeRemote);
245         }
246     }
247 
248     @Test
testRepeatNullableFd()249     public void testRepeatNullableFd() throws RemoteException, IOException {
250         checkFdRepeated((fd) -> mInterface.RepeatNullableFd(fd));
251         assertEquals(null, mInterface.RepeatNullableFd(null));
252     }
253 
checkInOutSockets(ParcelFileDescriptor in, ParcelFileDescriptor out)254     private void checkInOutSockets(ParcelFileDescriptor in, ParcelFileDescriptor out) throws IOException {
255         FileOutputStream repeatFdStream = new ParcelFileDescriptor.AutoCloseOutputStream(in);
256         String testData = "asdf";
257         byte[] output = testData.getBytes();
258         repeatFdStream.write(output);
259         repeatFdStream.close();
260 
261         FileInputStream fileInputStream = new ParcelFileDescriptor.AutoCloseInputStream(out);
262         byte[] input = new byte[output.length];
263 
264         assertEquals(input.length, fileInputStream.read(input));
265         Assert.assertArrayEquals(input, output);
266     }
267 
268     @Test
testRepeatFdArray()269     public void testRepeatFdArray() throws RemoteException, IOException {
270         ParcelFileDescriptor[] sockets1 = ParcelFileDescriptor.createReliableSocketPair();
271         ParcelFileDescriptor[] sockets2 = ParcelFileDescriptor.createReliableSocketPair();
272         ParcelFileDescriptor[] inputs = {sockets1[0], sockets2[0]};
273         ParcelFileDescriptor[] repeatFdArray = new ParcelFileDescriptor[inputs.length];
274         mInterface.RepeatFdArray(inputs, repeatFdArray);
275 
276         checkInOutSockets(repeatFdArray[0], sockets1[1]);
277         checkInOutSockets(repeatFdArray[1], sockets2[1]);
278     }
279 
280     @Test
testRepeatString()281     public void testRepeatString() throws RemoteException {
282         assertEquals("", mInterface.RepeatString(""));
283         assertEquals("a", mInterface.RepeatString("a"));
284         assertEquals("foo", mInterface.RepeatString("foo"));
285 
286         String stringWithNulls = "a\0df";
287         assertEquals(stringWithNulls, mInterface.RepeatString(stringWithNulls));
288     }
289 
290     @Test
testRepeatNullableString()291     public void testRepeatNullableString() throws RemoteException {
292         assertEquals(null, mInterface.RepeatNullableString(null));
293         assertEquals("", mInterface.RepeatNullableString(""));
294         assertEquals("a", mInterface.RepeatNullableString("a"));
295         assertEquals("foo", mInterface.RepeatNullableString("foo"));
296     }
297 
assertPolygonEquals(RegularPolygon lhs, RegularPolygon rhs)298     public void assertPolygonEquals(RegularPolygon lhs, RegularPolygon rhs) {
299         assertEquals(lhs.name, rhs.name);
300         assertEquals(lhs.numSides, rhs.numSides);
301         assertEquals(lhs.sideLength, rhs.sideLength, 0.0f);
302     }
assertPolygonEquals(RegularPolygon[] lhs, RegularPolygon[] rhs)303     public void assertPolygonEquals(RegularPolygon[] lhs, RegularPolygon[] rhs) {
304         assertEquals(lhs.length, rhs.length);
305         for (int i = 0; i < lhs.length; i++) {
306             assertPolygonEquals(lhs[i], rhs[i]);
307         }
308     }
309 
310     @Test
testRepeatPolygon()311     public void testRepeatPolygon() throws RemoteException {
312         RegularPolygon polygon = new RegularPolygon();
313         polygon.name = "hexagon";
314         polygon.numSides = 6;
315         polygon.sideLength = 1.0f;
316 
317         RegularPolygon result = mInterface.RepeatPolygon(polygon);
318 
319         assertPolygonEquals(polygon, result);
320     }
321 
322     @Test
testRepeatUnexpectedNullPolygon()323     public void testRepeatUnexpectedNullPolygon() throws RemoteException {
324         try {
325            RegularPolygon result = mInterface.RepeatPolygon(null);
326         } catch (NullPointerException e) {
327            // non-@nullable C++ result can't handle null Polygon
328            return;
329         }
330         // Java always works w/ nullptr
331         assertEquals("JAVA", mExpectedName);
332     }
333 
334     @Test
testRepeatNullNullablePolygon()335     public void testRepeatNullNullablePolygon() throws RemoteException {
336         RegularPolygon result = mInterface.RepeatNullablePolygon(null);
337         assertEquals(null, result);
338     }
339 
340     @Test
testRepeatPresentNullablePolygon()341     public void testRepeatPresentNullablePolygon() throws RemoteException {
342         RegularPolygon polygon = new RegularPolygon();
343         polygon.name = "septagon";
344         polygon.numSides = 7;
345         polygon.sideLength = 9.0f;
346 
347         RegularPolygon result = mInterface.RepeatNullablePolygon(polygon);
348 
349         assertPolygonEquals(polygon, result);
350     }
351 
352     @Test
testInsAndOuts()353     public void testInsAndOuts() throws RemoteException {
354         RegularPolygon polygon = new RegularPolygon();
355         mInterface.RenamePolygon(polygon, "Jerry");
356         assertEquals("Jerry", polygon.name);
357     }
358 
359     @Test
testArrays()360     public void testArrays() throws RemoteException {
361         {
362             boolean[] value = {};
363             boolean[] out1 = new boolean[value.length];
364             boolean[] out2 = mInterface.RepeatBooleanArray(value, out1);
365 
366             Assert.assertArrayEquals(value, out1);
367             Assert.assertArrayEquals(value, out2);
368         }
369         {
370             boolean[] value = {false, true, false};
371             boolean[] out1 = new boolean[value.length];
372             boolean[] out2 = mInterface.RepeatBooleanArray(value, out1);
373 
374             Assert.assertArrayEquals(value, out1);
375             Assert.assertArrayEquals(value, out2);
376         }
377         {
378             byte[] value = {1, 2, 3};
379             byte[] out1 = new byte[value.length];
380             byte[] out2 = mInterface.RepeatByteArray(value, out1);
381 
382             Assert.assertArrayEquals(value, out1);
383             Assert.assertArrayEquals(value, out2);
384         }
385         {
386             char[] value = {'h', 'a', '!'};
387             char[] out1 = new char[value.length];
388             char[] out2 = mInterface.RepeatCharArray(value, out1);
389 
390             Assert.assertArrayEquals(value, out1);
391             Assert.assertArrayEquals(value, out2);
392         }
393         {
394             int[] value = {1, 2, 3};
395             int[] out1 = new int[value.length];
396             int[] out2 = mInterface.RepeatIntArray(value, out1);
397 
398             Assert.assertArrayEquals(value, out1);
399             Assert.assertArrayEquals(value, out2);
400         }
401         {
402             long[] value = {1, 2, 3};
403             long[] out1 = new long[value.length];
404             long[] out2 = mInterface.RepeatLongArray(value, out1);
405 
406             Assert.assertArrayEquals(value, out1);
407             Assert.assertArrayEquals(value, out2);
408         }
409         {
410             float[] value = {1.0f, 2.0f, 3.0f};
411             float[] out1 = new float[value.length];
412             float[] out2 = mInterface.RepeatFloatArray(value, out1);
413 
414             Assert.assertArrayEquals(value, out1, 0.0f);
415             Assert.assertArrayEquals(value, out2, 0.0f);
416         }
417         {
418             double[] value = {1.0, 2.0, 3.0};
419             double[] out1 = new double[value.length];
420             double[] out2 = mInterface.RepeatDoubleArray(value, out1);
421 
422             Assert.assertArrayEquals(value, out1, 0.0);
423             Assert.assertArrayEquals(value, out2, 0.0);
424         }
425         {
426             byte[] value = {ByteEnum.FOO, ByteEnum.BAR};
427             byte[] out1 = new byte[value.length];
428             byte[] out2 = mInterface.RepeatByteEnumArray(value, out1);
429 
430             Assert.assertArrayEquals(value, out1);
431             Assert.assertArrayEquals(value, out2);
432         }
433         {
434             int[] value = {IntEnum.FOO, IntEnum.BAR};
435             int[] out1 = new int[value.length];
436             int[] out2 = mInterface.RepeatIntEnumArray(value, out1);
437 
438             Assert.assertArrayEquals(value, out1);
439             Assert.assertArrayEquals(value, out2);
440         }
441         {
442             long[] value = {LongEnum.FOO, LongEnum.BAR};
443             long[] out1 = new long[value.length];
444             long[] out2 = mInterface.RepeatLongEnumArray(value, out1);
445 
446             Assert.assertArrayEquals(value, out1);
447             Assert.assertArrayEquals(value, out2);
448         }
449         {
450             String[] value = {"", "aoeu", "lol", "brb"};
451             String[] out1 = new String[value.length];
452             String[] out2 = mInterface.RepeatStringArray(value, out1);
453 
454             Assert.assertArrayEquals(value, out1);
455             Assert.assertArrayEquals(value, out2);
456         }
457         {
458 
459             RegularPolygon septagon = new RegularPolygon();
460             septagon.name = "septagon";
461             septagon.numSides = 7;
462             septagon.sideLength = 1.0f;
463 
464             RegularPolygon[] value = {septagon, new RegularPolygon(), new RegularPolygon()};
465             RegularPolygon[] out1 = new RegularPolygon[value.length];
466             RegularPolygon[] out2 = mInterface.RepeatRegularPolygonArray(value, out1);
467 
468             assertPolygonEquals(value, out1);
469             assertPolygonEquals(value, out2);
470         }
471     }
472 
473     @Test
testLists()474     public void testLists() throws RemoteException {
475         {
476             List<String> value = Arrays.asList("", "aoeu", "lol", "brb");
477             List<String> out1 = new ArrayList<>();
478             List<String> out2 = mInterface.Repeat2StringList(value, out1);
479 
480             List<String> expected = new ArrayList<>();
481             expected.addAll(value);
482             expected.addAll(value);
483             String[] expectedArray = expected.toArray(new String[0]);
484 
485             Assert.assertArrayEquals(expectedArray, out1.toArray(new String[0]));
486             Assert.assertArrayEquals(expectedArray, out2.toArray(new String[0]));
487         }
488         {
489             RegularPolygon septagon = new RegularPolygon();
490             septagon.name = "septagon";
491             septagon.numSides = 7;
492             septagon.sideLength = 1.0f;
493 
494             List<RegularPolygon> value = Arrays.asList(septagon, new RegularPolygon(), new RegularPolygon());
495             List<RegularPolygon> out1 = new ArrayList<>();
496             List<RegularPolygon> out2 = mInterface.Repeat2RegularPolygonList(value, out1);
497 
498             List<RegularPolygon> expected = new ArrayList<>();
499             expected.addAll(value);
500             expected.addAll(value);
501             RegularPolygon[] expectedArray = expected.toArray(new RegularPolygon[0]);
502 
503             assertPolygonEquals(expectedArray, out1.toArray(new RegularPolygon[0]));
504             assertPolygonEquals(expectedArray, out1.toArray(new RegularPolygon[0]));
505         }
506     }
507 
508     @Test
testNullableArrays()509     public void testNullableArrays() throws RemoteException {
510         {
511             boolean[] emptyValue = {};
512             boolean[] value = {false, true, false};
513             Assert.assertArrayEquals(null, mInterface.RepeatNullableBooleanArray(null));
514             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableBooleanArray(emptyValue));
515             Assert.assertArrayEquals(value, mInterface.RepeatNullableBooleanArray(value));
516         }
517         {
518             byte[] emptyValue = {};
519             byte[] value = {1, 2, 3};
520             Assert.assertArrayEquals(null, mInterface.RepeatNullableByteArray(null));
521             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableByteArray(emptyValue));
522             Assert.assertArrayEquals(value, mInterface.RepeatNullableByteArray(value));
523         }
524         {
525             char[] emptyValue = {};
526             char[] value = {'h', 'a', '!'};
527             Assert.assertArrayEquals(null, mInterface.RepeatNullableCharArray(null));
528             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableCharArray(emptyValue));
529             Assert.assertArrayEquals(value, mInterface.RepeatNullableCharArray(value));
530         }
531         {
532             int[] emptyValue = {};
533             int[] value = {1, 2, 3};
534             Assert.assertArrayEquals(null, mInterface.RepeatNullableIntArray(null));
535             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableIntArray(emptyValue));
536             Assert.assertArrayEquals(value, mInterface.RepeatNullableIntArray(value));
537         }
538         {
539             long[] emptyValue = {};
540             long[] value = {1, 2, 3};
541             Assert.assertArrayEquals(null, mInterface.RepeatNullableLongArray(null));
542             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableLongArray(emptyValue));
543             Assert.assertArrayEquals(value, mInterface.RepeatNullableLongArray(value));
544         }
545         {
546             float[] emptyValue = {};
547             float[] value = {1.0f, 2.0f, 3.0f};
548             Assert.assertArrayEquals(null, mInterface.RepeatNullableFloatArray(null), 0.0f);
549             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableFloatArray(emptyValue), 0.0f);
550             Assert.assertArrayEquals(value, mInterface.RepeatNullableFloatArray(value), 0.0f);
551         }
552         {
553             double[] emptyValue = {};
554             double[] value = {1.0, 2.0, 3.0};
555             Assert.assertArrayEquals(null, mInterface.RepeatNullableDoubleArray(null), 0.0);
556             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableDoubleArray(emptyValue), 0.0);
557             Assert.assertArrayEquals(value, mInterface.RepeatNullableDoubleArray(value), 0.0);
558         }
559         {
560             byte[] emptyValue = {};
561             byte[] value = {ByteEnum.FOO, ByteEnum.BAR};
562             Assert.assertArrayEquals(null, mInterface.RepeatNullableByteEnumArray(null));
563             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableByteEnumArray(emptyValue));
564             Assert.assertArrayEquals(value, mInterface.RepeatNullableByteEnumArray(value));
565         }
566         {
567             int[] emptyValue = {};
568             int[] value = {IntEnum.FOO, IntEnum.BAR};
569             Assert.assertArrayEquals(null, mInterface.RepeatNullableIntEnumArray(null));
570             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableIntEnumArray(emptyValue));
571             Assert.assertArrayEquals(value, mInterface.RepeatNullableIntEnumArray(value));
572         }
573         {
574             long[] emptyValue = {};
575             long[] value = {LongEnum.FOO, LongEnum.BAR};
576             Assert.assertArrayEquals(null, mInterface.RepeatNullableLongEnumArray(null));
577             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableLongEnumArray(emptyValue));
578             Assert.assertArrayEquals(value, mInterface.RepeatNullableLongEnumArray(value));
579         }
580         {
581             String[] emptyValue = {};
582             String[] value = {"", "aoeu", null, "brb"};
583             Assert.assertArrayEquals(null, mInterface.RepeatNullableStringArray(null));
584             Assert.assertArrayEquals(emptyValue, mInterface.RepeatNullableStringArray(emptyValue));
585             Assert.assertArrayEquals(value, mInterface.RepeatNullableStringArray(value));
586         }
587         {
588             String[] emptyValue = {};
589             String[] value = {"", "aoeu", null, "brb"};
590             String[] out1 = new String[value.length];
591             String[] out2 = mInterface.DoubleRepeatNullableStringArray(value, out1);
592 
593             Assert.assertArrayEquals(value, out1);
594             Assert.assertArrayEquals(value, out2);
595         }
596     }
597 
598     @Test
testGetLastItem()599     public void testGetLastItem() throws RemoteException {
600         Foo foo = new Foo();
601         foo.d = new Bar();
602         foo.e = new Bar();
603         foo.f = 15;
604         foo.shouldContainTwoByteFoos = new byte[]{};
605         foo.shouldContainTwoIntFoos = new int[]{};
606         foo.shouldContainTwoLongFoos = new long[]{};
607 
608         assertEquals(foo.f, mInterface.getF(foo));
609     }
610 
611     @Test
testRepeatFoo()612     public void testRepeatFoo() throws RemoteException {
613         Foo foo = new Foo();
614 
615         foo.a = "NEW FOO";
616         foo.b = 57;
617 
618         foo.d = new Bar();
619         foo.d.b = "a";
620 
621         foo.e = new Bar();
622         foo.e.d = 99;
623 
624         foo.shouldBeByteBar = ByteEnum.BAR;
625         foo.shouldBeIntBar = IntEnum.BAR;
626         foo.shouldBeLongBar = LongEnum.BAR;
627 
628         foo.shouldContainTwoByteFoos = new byte[]{ByteEnum.FOO, ByteEnum.FOO};
629         foo.shouldContainTwoIntFoos = new int[]{IntEnum.FOO, IntEnum.FOO};
630         foo.shouldContainTwoLongFoos = new long[]{LongEnum.FOO, LongEnum.FOO};
631 
632         foo.u = SimpleUnion.e(new byte[]{ByteEnum.FOO, ByteEnum.FOO});
633 
634         foo.shouldSetBit0AndBit2 = Foo.BIT0 | Foo.BIT2;
635         foo.shouldBeConstS1 = SimpleUnion.c(SimpleUnion.S1);
636 
637         Foo repeatedFoo = mInterface.repeatFoo(foo);
638 
639         assertEquals(foo.a, repeatedFoo.a);
640         assertEquals(foo.b, repeatedFoo.b);
641         assertEquals(foo.d.b, repeatedFoo.d.b);
642         assertEquals(foo.e.d, repeatedFoo.e.d);
643         assertEquals(foo.shouldBeByteBar, repeatedFoo.shouldBeByteBar);
644         assertEquals(foo.shouldBeIntBar, repeatedFoo.shouldBeIntBar);
645         assertEquals(foo.shouldBeLongBar, repeatedFoo.shouldBeLongBar);
646         Assert.assertArrayEquals(foo.shouldContainTwoByteFoos, repeatedFoo.shouldContainTwoByteFoos);
647         Assert.assertArrayEquals(foo.shouldContainTwoIntFoos, repeatedFoo.shouldContainTwoIntFoos);
648         Assert.assertArrayEquals(foo.shouldContainTwoLongFoos, repeatedFoo.shouldContainTwoLongFoos);
649         Assert.assertArrayEquals(foo.u.getE(), repeatedFoo.u.getE());
650         assertEquals(foo.shouldSetBit0AndBit2, repeatedFoo.shouldSetBit0AndBit2);
651         assertEquals(foo.shouldBeConstS1.getC(), repeatedFoo.shouldBeConstS1.getC());
652     }
653 
654     @Test
testRepeatGenericBar()655     public void testRepeatGenericBar() throws RemoteException {
656       GenericBar<Integer> bar = new GenericBar<Integer>();
657       bar.shouldBeGenericFoo = new GenericFoo<Integer, Bar, Integer>();
658       bar.shouldBeGenericFoo.a = 41;
659       bar.shouldBeGenericFoo.b = 42;
660       GenericBar<Integer> repeatedBar = new GenericBar<Integer>();
661       repeatedBar = mInterface.repeatGenericBar(bar);
662 
663       assertEquals(bar.a, repeatedBar.a);
664       assertEquals(bar.shouldBeGenericFoo.a, repeatedBar.shouldBeGenericFoo.a);
665       assertEquals(bar.shouldBeGenericFoo.b, repeatedBar.shouldBeGenericFoo.b);
666     }
667 
668     @Test
testNewField()669     public void testNewField() throws RemoteException {
670         Baz baz = new Baz();
671         baz.d = new String[]{"a", "b", "c"};
672         ICompatTest compatTest = ICompatTest.Stub.asInterface(mInterface.getICompatTest());
673         Baz newBaz = compatTest.repeatBaz(baz);
674         if (mShouldBeOld) {
675             assertEquals(null, newBaz.d);
676         } else {
677             Assert.assertArrayEquals(baz.d, newBaz.d);
678         }
679     }
680 
681     @Test
testGetInterfaceVersion()682     public void testGetInterfaceVersion() throws RemoteException {
683         ICompatTest compatTest = ICompatTest.Stub.asInterface(mInterface.getICompatTest());
684         if (mShouldBeOld) {
685             assertEquals(1, compatTest.getInterfaceVersion());
686         } else {
687             assertEquals(3, compatTest.getInterfaceVersion());
688         }
689     }
690 
691     @Test
testGetInterfaceHash()692     public void testGetInterfaceHash() throws RemoteException {
693         ICompatTest compatTest = ICompatTest.Stub.asInterface(mInterface.getICompatTest());
694         if (mShouldBeOld) {
695             assertEquals("b663b681b3e0d66f9b5428c2f23365031b7d4ba0", compatTest.getInterfaceHash());
696         } else {
697             assertEquals("notfrozen", compatTest.getInterfaceHash());
698         }
699     }
700 
701     @Test
testLegacyBinder()702     public void testLegacyBinder() throws RemoteException {
703         ILegacyBinder compatTest = ILegacyBinder.Stub.asInterface(mInterface.getLegacyBinderTest());
704         assertEquals(42, compatTest.RepeatInt(42));
705     }
706 
707     @Test
testRenameFoo()708     public void testRenameFoo() throws RemoteException {
709         Foo foo = new Foo();
710         foo.d = new Bar();
711         foo.e = new Bar();
712         foo.shouldContainTwoByteFoos = new byte[]{};
713         foo.shouldContainTwoIntFoos = new int[]{};
714         foo.shouldContainTwoLongFoos = new long[]{};
715         mInterface.renameFoo(foo, "MYFOO");
716         assertEquals("MYFOO", foo.a);
717     }
718     @Test
testRenameBar()719     public void testRenameBar() throws RemoteException {
720         Foo foo = new Foo();
721         foo.d = new Bar();
722         foo.e = new Bar();
723         foo.shouldContainTwoByteFoos = new byte[]{};
724         foo.shouldContainTwoIntFoos = new int[]{};
725         foo.shouldContainTwoLongFoos = new long[]{};
726         mInterface.renameBar(foo, "MYBAR");
727         assertEquals("MYBAR", foo.d.a);
728     }
729 
730     @Test
testRepeatStringNullableLater()731     public void testRepeatStringNullableLater() throws RemoteException {
732         // see notes in native NdkBinderTest_Aidl RepeatStringNullableLater
733         boolean handlesNull = !mShouldBeOld || mExpectedName == "JAVA";
734         ICompatTest compatTest = ICompatTest.Stub.asInterface(mInterface.getICompatTest());
735 
736         try {
737             assertEquals(null, compatTest.RepeatStringNullableLater(null));
738             assertTrue("should reach here if null is handled", handlesNull);
739         } catch (NullPointerException e) {
740             assertFalse("should reach here if null isn't handled", handlesNull);
741         }
742         assertEquals("", compatTest.RepeatStringNullableLater(""));
743         assertEquals("a", compatTest.RepeatStringNullableLater("a"));
744         assertEquals("foo", compatTest.RepeatStringNullableLater("foo"));
745     }
746 
747     @Test
testParcelableHolder()748     public void testParcelableHolder() throws RemoteException {
749         ExtendableParcelable ep = new ExtendableParcelable();
750         ep.c = 42L;
751         MyExt myext1 = new MyExt();
752         myext1.a = 42;
753         myext1.b = "mystr";
754         ep.ext.setParcelable(myext1);
755 
756         ExtendableParcelable ep2 = new ExtendableParcelable();
757         mInterface.RepeatExtendableParcelable(ep, ep2);
758         MyExt myext2 = ep2.ext.getParcelable(MyExt.class);
759         assertEquals(42L, ep2.c);
760         assertNotEquals(null, myext2);
761         assertEquals(42, myext2.a);
762         assertEquals("mystr", myext2.b);
763     }
764 
765     @Test
testEmptyParcelableHolder()766     public void testEmptyParcelableHolder() throws RemoteException {
767         ExtendableParcelable ep = new ExtendableParcelable();
768         ep.c = 42L;
769         ExtendableParcelable ep2 = new ExtendableParcelable();
770         mInterface.RepeatExtendableParcelableWithoutExtension(ep, ep2);
771         assertEquals(42L, ep2.c);
772     }
773 
774     @Test
testRepeatSimpleUnion()775     public void testRepeatSimpleUnion() throws RemoteException {
776         final int[] intArray = { 1, 2, 3 };
777         SimpleUnion origin = SimpleUnion.b(intArray);
778         SimpleUnion ret = mInterface.RepeatSimpleUnion(origin);
779         assertEquals(SimpleUnion.b, ret.getTag());
780         Assert.assertArrayEquals(intArray, ret.getB());
781     }
782 }
783