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 package com.android.commands.hidl_test_java;
18 
19 import static android.system.OsConstants.MAP_SHARED;
20 import static android.system.OsConstants.PROT_READ;
21 import static android.system.OsConstants.PROT_WRITE;
22 
23 import android.hardware.tests.baz.V1_0.IBase;
24 import android.hardware.tests.baz.V1_0.IBaz;
25 import android.hardware.tests.baz.V1_0.IBaz.MyHandle;
26 import android.hardware.tests.baz.V1_0.IBaz.NestedStruct;
27 import android.hardware.tests.baz.V1_0.IBazCallback;
28 import android.hardware.tests.baz.V1_0.IQuux;
29 import android.hardware.tests.memory.V2_0.IMemoryInterface;
30 import android.hardware.tests.memory.V2_0.TwoMemory;
31 import android.hardware.tests.safeunion.V1_0.IOtherInterface;
32 import android.hardware.tests.safeunion.V1_0.ISafeUnion;
33 import android.hardware.tests.safeunion.V1_0.ISafeUnion.HandleTypeSafeUnion;
34 import android.hardware.tests.safeunion.V1_0.ISafeUnion.InterfaceTypeSafeUnion;
35 import android.hardware.tests.safeunion.V1_0.ISafeUnion.LargeSafeUnion;
36 import android.hardware.tests.safeunion.V1_0.ISafeUnion.SmallSafeUnion;
37 import android.hidl.manager.V1_0.IServiceManager;
38 import android.os.DeadObjectException;
39 import android.os.HidlMemory;
40 import android.os.HidlMemoryUtil;
41 import android.os.HidlSupport;
42 import android.os.HwBinder;
43 import android.os.HwParcel;
44 import android.os.IBinder;
45 import android.os.IHwBinder;
46 import android.os.NativeHandle;
47 import android.os.RemoteException;
48 import android.os.SharedMemory;
49 import android.system.ErrnoException;
50 import android.system.Os;
51 import android.util.Log;
52 import java.io.File;
53 import java.io.FileDescriptor;
54 import java.io.FileInputStream;
55 import java.io.FileOutputStream;
56 import java.io.IOException;
57 import java.nio.ByteBuffer;
58 import java.nio.DirectByteBuffer;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.NoSuchElementException;
62 import java.util.Objects;
63 
64 public final class HidlTestJava {
65     private static final String TAG = "HidlTestJava";
66 
main(String[] args)67     public static void main(String[] args) {
68         int exitCode = 1;
69         try {
70             exitCode = new HidlTestJava().run(args);
71         } catch (Exception e) {
72             e.printStackTrace();
73             Log.e(TAG, "Error ", e);
74         }
75         System.exit(exitCode);
76     }
77 
run(String[] args)78     public int run(String[] args) throws RemoteException, IOException, ErrnoException {
79         HwBinder.setTrebleTestingOverride(true);
80 
81         if (args[0].equals("-c")) {
82             client();
83         } else if (args[0].equals("-s")) {
84             server();
85         } else {
86             Log.e(TAG, "Usage: HidlTestJava  -c(lient) | -s(erver)");
87             System.err.printf("Usage: HidlTestJava  -c(lient) | -s(erver)\n");
88             return 1;
89         }
90 
91         return 0;
92     }
93 
94     final class HidlDeathRecipient implements HwBinder.DeathRecipient {
95         final Object mLock = new Object();
96         boolean mCalled = false;
97         long mCookie = 0;
98 
99         @Override
serviceDied(long cookie)100         public void serviceDied(long cookie) {
101             synchronized (mLock) {
102                 mCalled = true;
103                 mCookie = cookie;
104                 mLock.notify();
105             }
106         }
107 
cookieMatches(long cookie)108         public boolean cookieMatches(long cookie) {
109             synchronized (mLock) {
110                 return mCookie == cookie;
111             }
112         }
113 
waitUntilServiceDied(long timeoutMillis)114         public boolean waitUntilServiceDied(long timeoutMillis) {
115             synchronized(mLock) {
116                 while (!mCalled) {
117                     try {
118                         mLock.wait(timeoutMillis);
119                     } catch (InterruptedException e) {
120                         continue; // Spin for another loop
121                     }
122                     break; // got notified or timeout hit
123                 }
124                 return mCalled;
125             }
126         }
127     };
128 
ExpectTrue(boolean x)129     private void ExpectTrue(boolean x) {
130         if (x) {
131             return;
132         }
133 
134         throw new RuntimeException();
135     }
136 
ExpectFalse(boolean x)137     private void ExpectFalse(boolean x) {
138         ExpectTrue(!x);
139     }
140 
Expect(String result, String s)141     private void Expect(String result, String s) {
142         if (result.equals(s)) {
143             return;
144         }
145 
146         System.err.printf("Expected '%s', got '%s'\n", s, result);
147         Log.e(TAG, "Expected '" + s + "', got '" + result + "'");
148         throw new RuntimeException();
149     }
150 
151     // .equals and HidlSupport.interfacesEqual should have the same behavior.
ExpectEqual(android.hidl.base.V1_0.IBase l, android.hidl.base.V1_0.IBase r)152     private void ExpectEqual(android.hidl.base.V1_0.IBase l, android.hidl.base.V1_0.IBase r) {
153         ExpectTrue(Objects.equals(l, r));
154         ExpectTrue(Objects.equals(r, l));
155         ExpectTrue(HidlSupport.interfacesEqual(l, r));
156         ExpectTrue(HidlSupport.interfacesEqual(r, l));
157     }
ExpectNotEqual(android.hidl.base.V1_0.IBase l, android.hidl.base.V1_0.IBase r)158     private void ExpectNotEqual(android.hidl.base.V1_0.IBase l, android.hidl.base.V1_0.IBase r) {
159         ExpectFalse(Objects.equals(l, r));
160         ExpectFalse(Objects.equals(r, l));
161         ExpectFalse(HidlSupport.interfacesEqual(l, r));
162         ExpectFalse(HidlSupport.interfacesEqual(r, l));
163     }
164 
165     class BazCallback extends IBazCallback.Stub {
166         private boolean mCalled;
167 
BazCallback()168         public BazCallback() {
169             mCalled = false;
170         }
171 
wasCalled()172         boolean wasCalled() {
173             return mCalled;
174         }
175 
heyItsMe(IBazCallback cb)176         public void heyItsMe(IBazCallback cb) throws RemoteException {
177             mCalled = true;
178 
179             cb.heyItsMe(null);
180         }
181 
hey()182         public void hey() {
183             mCalled = true;
184         }
185 
equals(Object other)186         @Override public boolean equals(Object other) {
187             return other != null && other.getClass() == BazCallback.class &&
188                 ((BazCallback) other).mCalled == mCalled;
189         }
hashCode()190         @Override public int hashCode() { return mCalled ? 1 : 0; }
191     }
192 
numberToEnglish(int x)193     private String numberToEnglish(int x) {
194         final String[] kDigits = {
195             "zero",
196             "one",
197             "two",
198             "three",
199             "four",
200             "five",
201             "six",
202             "seven",
203             "eight",
204             "nine",
205         };
206 
207         if (x < 0) {
208             return "negative " + numberToEnglish(-x);
209         }
210 
211         if (x < 10) {
212             return kDigits[x];
213         }
214 
215         if (x <= 15) {
216             final String[] kSpecialTens = {
217                 "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
218             };
219 
220             return kSpecialTens[x - 10];
221         }
222 
223         if (x < 20) {
224             return kDigits[x % 10] + "teen";
225         }
226 
227         if (x < 100) {
228             final String[] kDecades = {
229                 "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
230                 "eighty", "ninety",
231             };
232 
233             return kDecades[x / 10 - 2] + kDigits[x % 10];
234         }
235 
236         return "positively huge!";
237     }
238 
ExpectDeepEq(Object l, Object r)239     private void ExpectDeepEq(Object l, Object r) {
240         ExpectTrue(HidlSupport.deepEquals(l, r));
241         ExpectTrue(HidlSupport.deepHashCode(l) == HidlSupport.deepHashCode(r));
242     }
243 
ExpectDeepNe(Object l, Object r)244     private void ExpectDeepNe(Object l, Object r) {
245         ExpectTrue(!HidlSupport.deepEquals(l, r));
246     }
247 
runClientMemoryTests()248     private void runClientMemoryTests() throws RemoteException, IOException, ErrnoException {
249         IMemoryInterface memoryInterface = IMemoryInterface.getService();
250 
251         {
252             HidlMemory hidlMem = HidlMemoryUtil.byteArrayToHidlMemory(
253                     new byte[]{0x00, 0x12, 0x34, 0x56});
254             memoryInterface.bitwiseNot(hidlMem);
255             byte[] result = HidlMemoryUtil.hidlMemoryToByteArray(hidlMem);
256 
257             ExpectTrue(Arrays.equals(result,
258                     new byte[]{(byte) 0xFF, (byte) 0xED, (byte) 0xCB, (byte) 0xA9}));
259 
260             hidlMem.close();
261         }
262 
263         {
264             HidlMemory hidlMem = memoryInterface.getTestMem();
265             byte[] data = HidlMemoryUtil.hidlMemoryToByteArray(hidlMem);
266             for (int i = 0; i < 8; ++i) {
267                 ExpectTrue(data[i] == (byte) i);
268             }
269             hidlMem.close();
270         }
271 
272         {
273             TwoMemory in = new TwoMemory();
274             in.mem1 = HidlMemoryUtil.byteArrayToHidlMemory(new byte[]{10, 11, 12, 13});
275             in.mem2 = HidlMemoryUtil.byteArrayToHidlMemory(new byte[]{2, 4, 6, 8});
276             TwoMemory out = memoryInterface.getSumDiff(in);
277             ExpectTrue(Arrays.equals(HidlMemoryUtil.hidlMemoryToByteArray(out.mem1),
278                     new byte[]{12, 15, 18, 21}));
279             ExpectTrue(Arrays.equals(HidlMemoryUtil.hidlMemoryToByteArray(out.mem2),
280                     new byte[]{8, 7, 6, 5}));
281             in.mem1.close();
282             in.mem2.close();
283             out.mem1.close();
284             out.mem2.close();
285         }
286     }
287 
runClientSafeUnionTests()288     private void runClientSafeUnionTests() throws RemoteException, IOException {
289         ISafeUnion safeunionInterface = ISafeUnion.getService();
290 
291         {
292             // SafeUnionNoInitTest
293             LargeSafeUnion safeUnion = safeunionInterface.newLargeSafeUnion();
294             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.noinit);
295         }
296         {
297             // SafeUnionSimpleTest
298             LargeSafeUnion safeUnion = safeunionInterface.newLargeSafeUnion();
299 
300             safeUnion = safeunionInterface.setA(safeUnion, (byte) -5);
301             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.a);
302             ExpectTrue(safeUnion.a() == (byte) -5);
303 
304             safeUnion = safeunionInterface.setD(safeUnion, Long.MAX_VALUE);
305             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.d);
306             ExpectTrue(safeUnion.d() == Long.MAX_VALUE);
307         }
308         {
309             // SafeUnionArrayLikeTypesTest
310             long[] testArray = new long[] {1, -2, 3, -4, 5};
311             ArrayList<Long> testVector = new ArrayList<Long>(Arrays.asList(Long.MAX_VALUE));
312 
313             LargeSafeUnion safeUnion = safeunionInterface.newLargeSafeUnion();
314             safeUnion = safeunionInterface.setF(safeUnion, testArray);
315             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.f);
316             ExpectDeepEq(testArray, safeUnion.f());
317 
318             safeUnion = safeunionInterface.newLargeSafeUnion();
319             safeUnion = safeunionInterface.setI(safeUnion, testVector);
320             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.i);
321             ExpectDeepEq(testVector, safeUnion.i());
322         }
323         {
324             // SafeUnionStringTypeTest
325             String testString = "This is an inordinately long test string.";
326 
327             LargeSafeUnion safeUnion = safeunionInterface.newLargeSafeUnion();
328             safeUnion = safeunionInterface.setG(safeUnion, testString);
329             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.g);
330             ExpectDeepEq(testString, safeUnion.g());
331         }
332         {
333             // SafeUnionNestedTest
334             SmallSafeUnion smallSafeUnion = new SmallSafeUnion();
335             smallSafeUnion.a((byte) 1);
336 
337             LargeSafeUnion safeUnion = safeunionInterface.newLargeSafeUnion();
338             safeUnion = safeunionInterface.setL(safeUnion, smallSafeUnion);
339             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.l);
340             ExpectTrue(safeUnion.l().getDiscriminator() == SmallSafeUnion.hidl_discriminator.a);
341             ExpectTrue(safeUnion.l().a() == (byte) 1);
342         }
343         {
344             // SafeUnionEnumTest
345             LargeSafeUnion safeUnion = safeunionInterface.newLargeSafeUnion();
346             safeUnion = safeunionInterface.setM(safeUnion, ISafeUnion.BitField.V1);
347             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.m);
348             ExpectTrue(safeUnion.m() == ISafeUnion.BitField.V1);
349         }
350         {
351             // SafeUnionBitFieldTest
352             LargeSafeUnion safeUnion = safeunionInterface.newLargeSafeUnion();
353             safeUnion = safeunionInterface.setN(safeUnion, ISafeUnion.BitField.V1);
354             ExpectTrue(safeUnion.getDiscriminator() == LargeSafeUnion.hidl_discriminator.n);
355             ExpectTrue(safeUnion.n() == ISafeUnion.BitField.V1);
356         }
357         {
358             // SafeUnionInterfaceNullNativeHandleTest
359             InterfaceTypeSafeUnion safeUnion = new InterfaceTypeSafeUnion();
360 
361             safeUnion = safeunionInterface.setInterfaceF(safeUnion, null);
362             ExpectTrue(safeUnion.getDiscriminator() == InterfaceTypeSafeUnion.hidl_discriminator.f);
363             ExpectTrue(safeUnion.f() == null);
364         }
365         {
366             // SafeUnionInterfaceTest
367             byte[] testArray = new byte[] {-1, -2, -3, 0, 1, 2, 3};
368             ArrayList<String> testVector = new ArrayList(Arrays.asList("So", "Many", "Words"));
369             String testStringA = "Hello";
370             String testStringB = "World";
371 
372             IOtherInterface otherInterface = IOtherInterface.getService();
373 
374             ArrayList<NativeHandle> testHandlesVector = new ArrayList<>();
375             for (int i = 0; i < 128; i++) {
376                 testHandlesVector.add(new NativeHandle());
377             }
378 
379             InterfaceTypeSafeUnion safeUnion = safeunionInterface.newInterfaceTypeSafeUnion();
380             safeUnion = safeunionInterface.setInterfaceB(safeUnion, testArray);
381             ExpectTrue(safeUnion.getDiscriminator() == InterfaceTypeSafeUnion.hidl_discriminator.b);
382             ExpectDeepEq(testArray, safeUnion.b());
383 
384             safeUnion.c(otherInterface);
385             ExpectTrue(safeUnion.getDiscriminator() == InterfaceTypeSafeUnion.hidl_discriminator.c);
386             ExpectTrue(HidlSupport.interfacesEqual(otherInterface, safeUnion.c()));
387             String result = safeUnion.c().concatTwoStrings(testStringA, testStringB);
388             Expect(result, testStringA + testStringB);
389 
390             safeUnion = safeunionInterface.setInterfaceD(safeUnion, testStringA);
391             ExpectTrue(safeUnion.getDiscriminator() == InterfaceTypeSafeUnion.hidl_discriminator.d);
392             Expect(testStringA, safeUnion.d());
393 
394             safeUnion = safeunionInterface.setInterfaceE(safeUnion, testVector);
395             ExpectTrue(safeUnion.getDiscriminator() == InterfaceTypeSafeUnion.hidl_discriminator.e);
396             ExpectDeepEq(testVector, safeUnion.e());
397 
398             safeUnion = safeunionInterface.setInterfaceG(safeUnion, testHandlesVector);
399             ExpectTrue(safeUnion.getDiscriminator() == InterfaceTypeSafeUnion.hidl_discriminator.g);
400             ExpectTrue(safeUnion.g().size() == testHandlesVector.size());
401 
402             for (int i = 0; i < testHandlesVector.size(); i++) {
403                 ExpectFalse(safeUnion.g().get(i).hasSingleFileDescriptor());
404             }
405         }
406         {
407             // SafeUnionNullNativeHandleTest
408             HandleTypeSafeUnion safeUnion = new HandleTypeSafeUnion();
409 
410             safeUnion = safeunionInterface.setHandleA(safeUnion, null);
411             ExpectTrue(safeUnion.getDiscriminator() == HandleTypeSafeUnion.hidl_discriminator.a);
412             ExpectTrue(safeUnion.a() == null);
413         }
414         {
415             // SafeUnionDefaultNativeHandleTest
416             NativeHandle[] testHandlesArray = new NativeHandle[5];
417             for (int i = 0; i < testHandlesArray.length; i++) {
418                 testHandlesArray[i] = new NativeHandle();
419             }
420 
421             ArrayList<NativeHandle> testHandlesList = new ArrayList<NativeHandle>(
422                 Arrays.asList(testHandlesArray));
423 
424             HandleTypeSafeUnion safeUnion = safeunionInterface.newHandleTypeSafeUnion();
425             safeUnion = safeunionInterface.setHandleA(safeUnion, new NativeHandle());
426             ExpectTrue(safeUnion.getDiscriminator() == HandleTypeSafeUnion.hidl_discriminator.a);
427             ExpectFalse(safeUnion.a().hasSingleFileDescriptor());
428 
429             safeUnion = safeunionInterface.setHandleB(safeUnion, testHandlesArray);
430             ExpectTrue(safeUnion.getDiscriminator() == HandleTypeSafeUnion.hidl_discriminator.b);
431             ExpectTrue(safeUnion.b().length == testHandlesArray.length);
432 
433             for (int i = 0; i < testHandlesArray.length; i++) {
434                 ExpectFalse(safeUnion.b()[i].hasSingleFileDescriptor());
435             }
436 
437             safeUnion = safeunionInterface.setHandleC(safeUnion, testHandlesList);
438             ExpectTrue(safeUnion.getDiscriminator() == HandleTypeSafeUnion.hidl_discriminator.c);
439             ExpectTrue(safeUnion.c().size() == testHandlesList.size());
440 
441             for (int i = 0; i < testHandlesList.size(); i++) {
442                 ExpectFalse(safeUnion.c().get(i).hasSingleFileDescriptor());
443             }
444         }
445         {
446             // SafeUnionNativeHandleWithFdTest
447             final String testFileName = "/data/local/tmp/SafeUnionNativeHandleWithFdTest";
448             final String[] testStrings = {"This ", "is ", "so ", "much ", "data!\n"};
449             File file = new File(testFileName);
450 
451             if (file.exists()) { ExpectTrue(file.delete()); }
452             ExpectTrue(file.createNewFile());
453 
454             StringBuilder builder = new StringBuilder();
455             for (String testString : testStrings) {
456                 builder.append(testString);
457             }
458             final String goldenResult = builder.toString();
459 
460             ArrayList<NativeHandle> testHandlesList = new ArrayList<NativeHandle>();
461             FileOutputStream fos = new FileOutputStream(file);
462             for (int i = 0; i < testStrings.length; i++) {
463                 testHandlesList.add(new NativeHandle(fos.getFD(), false /*own*/));
464             }
465 
466             HandleTypeSafeUnion safeUnion = safeunionInterface.newHandleTypeSafeUnion();
467             safeUnion = safeunionInterface.setHandleC(safeUnion, testHandlesList);
468             for (int i = 0; i < safeUnion.c().size(); i++) {
469                 ExpectTrue(safeUnion.c().get(i).hasSingleFileDescriptor());
470 
471                 // If you want to copy it out of the binder buffer or save it, it needs to be duped.
472                 // This isn't necessary for the test since it is kept open for the binder window.
473                 NativeHandle handle = safeUnion.c().get(i);
474                 if (i%2 == 0) handle = handle.dup();
475 
476                 // Original fd is duped if not dup'd above
477                 FileDescriptor resultFd = handle.getFileDescriptor();
478                 ExpectTrue(resultFd.getInt$() != fos.getFD().getInt$());
479 
480                 FileOutputStream otherFos = new FileOutputStream(resultFd);
481                 otherFos.write(testStrings[i].getBytes());
482                 otherFos.flush();
483 
484                 otherFos.close();
485 
486                 if (i%2 == 0) handle.close();
487             }
488 
489             byte[] resultData = new byte[(int) file.length()];
490             FileInputStream fis = new FileInputStream(file);
491             fis.read(resultData);
492 
493             String result = new String(resultData);
494             Expect(result, goldenResult);
495 
496             fis.close();
497             fos.close();
498             ExpectTrue(file.delete());
499         }
500         {
501             // SafeUnionEqualityTest
502             LargeSafeUnion one = safeunionInterface.newLargeSafeUnion();
503             LargeSafeUnion two = safeunionInterface.newLargeSafeUnion();
504             ExpectTrue(one.equals(two));
505 
506             one = safeunionInterface.setA(one, (byte) 1);
507             ExpectFalse(one.equals(two));
508 
509             two = safeunionInterface.setB(two, (byte) 1);
510             ExpectFalse(one.equals(two));
511 
512             two = safeunionInterface.setA(two, (byte) 2);
513             ExpectFalse(one.equals(two));
514 
515             two = safeunionInterface.setA(two, (byte) 1);
516             ExpectTrue(one.equals(two));
517         }
518         {
519             // SafeUnionDeepEqualityTest
520             ArrayList<Long> testVectorA = new ArrayList(Arrays.asList(1L, 2L, 3L));
521             ArrayList<Long> testVectorB = new ArrayList(Arrays.asList(2L, 1L, 3L));
522 
523             LargeSafeUnion one = safeunionInterface.newLargeSafeUnion();
524             LargeSafeUnion two = safeunionInterface.newLargeSafeUnion();
525 
526             one = safeunionInterface.setI(one, testVectorA);
527             two = safeunionInterface.setI(two, testVectorB);
528             ExpectFalse(one.equals(two));
529 
530             two = safeunionInterface.setI(two, (ArrayList<Long>) testVectorA.clone());
531             ExpectTrue(one.equals(two));
532         }
533         {
534             // SafeUnionHashCodeTest
535             ArrayList<Boolean> testVector =
536                 new ArrayList(Arrays.asList(true, false, false, true, true));
537 
538             LargeSafeUnion one = safeunionInterface.newLargeSafeUnion();
539             LargeSafeUnion two = safeunionInterface.newLargeSafeUnion();
540 
541             one = safeunionInterface.setH(one, testVector);
542             two = safeunionInterface.setA(two, (byte) -5);
543             ExpectFalse(one.hashCode() == two.hashCode());
544 
545             two = safeunionInterface.setH(two, (ArrayList<Boolean>) testVector.clone());
546             ExpectTrue(one.hashCode() == two.hashCode());
547         }
548     }
549 
client()550     private void client() throws RemoteException, IOException, ErrnoException {
551 
552         ExpectDeepEq(null, null);
553         ExpectDeepNe(null, new String());
554         ExpectDeepNe(new String(), null);
555         ExpectDeepEq(new String(), new String());
556         ExpectDeepEq("hey", "hey");
557 
558         ExpectDeepEq(new int[]{1,2}, new int[]{1,2});
559         ExpectDeepNe(new int[]{1,2}, new int[]{1,3});
560         ExpectDeepNe(new int[]{1,2}, new int[]{1,2,3});
561         ExpectDeepEq(new int[][]{{1,2},{3,4}}, new int[][]{{1,2},{3,4}});
562         ExpectDeepNe(new int[][]{{1,2},{3,4}}, new int[][]{{1,2},{3,5}});
563         ExpectDeepNe(new int[][]{{1,2},{3,4}}, new int[][]{{1,2,3},{4,5,6}});
564         ExpectDeepNe(new int[][]{{1,2},{3,4}}, new int[][]{{1,2},{3,4,5}});
565 
566         ExpectDeepEq(new Integer[]{1,2}, new Integer[]{1,2});
567         ExpectDeepNe(new Integer[]{1,2}, new Integer[]{1,3});
568         ExpectDeepNe(new Integer[]{1,2}, new Integer[]{1,2,3});
569         ExpectDeepEq(new Integer[][]{{1,2},{3,4}}, new Integer[][]{{1,2},{3,4}});
570         ExpectDeepNe(new Integer[][]{{1,2},{3,4}}, new Integer[][]{{1,2},{3,5}});
571         ExpectDeepNe(new Integer[][]{{1,2},{3,4}}, new Integer[][]{{1,2,3},{4,5,6}});
572         ExpectDeepNe(new Integer[][]{{1,2},{3,4}}, new Integer[][]{{1,2},{3,4,5}});
573 
574         ExpectDeepEq(new ArrayList(Arrays.asList(1, 2)),
575                      new ArrayList(Arrays.asList(1, 2)));
576         ExpectDeepNe(new ArrayList(Arrays.asList(1, 2)),
577                      new ArrayList(Arrays.asList(1, 2, 3)));
578 
579         ExpectDeepEq(new ArrayList(Arrays.asList(new int[]{1,2}, new int[]{3,4})),
580                      new ArrayList(Arrays.asList(new int[]{1,2}, new int[]{3,4})));
581         ExpectDeepNe(new ArrayList(Arrays.asList(new int[]{1,2}, new int[]{3,4})),
582                      new ArrayList(Arrays.asList(new int[]{1,2}, new int[]{3,5})));
583 
584         ExpectDeepEq(new ArrayList(Arrays.asList(new Integer[]{1,2}, new Integer[]{3,4})),
585                      new ArrayList(Arrays.asList(new Integer[]{1,2}, new Integer[]{3,4})));
586         ExpectDeepNe(new ArrayList(Arrays.asList(new Integer[]{1,2}, new Integer[]{3,4})),
587                      new ArrayList(Arrays.asList(new Integer[]{1,2}, new Integer[]{3,5})));
588 
589         ExpectDeepEq(new ArrayList[]{new ArrayList(Arrays.asList(1,2)),
590                                      new ArrayList(Arrays.asList(3,4))},
591                      new ArrayList[]{new ArrayList(Arrays.asList(1,2)),
592                                      new ArrayList(Arrays.asList(3,4))});
593 
594         {
595             // Test proper exceptions are thrown
596             try {
597                 IBase proxy = IBase.getService("this-doesn't-exist");
598                 // this should never run
599                 ExpectTrue(false);
600             } catch (Exception e) {
601                 ExpectTrue(e instanceof NoSuchElementException);
602             }
603         }
604 
605         {
606             // Test proper exceptions are thrown
607             try {
608                 // not in manifest, so won't wait
609                 IBase proxy = IBase.getService("this-doesn't-exist", true /*retry*/);
610                 // this should never run
611                 ExpectTrue(false);
612             } catch (Exception e) {
613                 ExpectTrue(e instanceof NoSuchElementException);
614             }
615         }
616 
617         {
618             // Test access through base interface binder.
619             IBase baseProxy = IBase.getService();
620             baseProxy.someBaseMethod();
621 
622             IBaz bazProxy = IBaz.castFrom(baseProxy);
623             ExpectTrue(bazProxy != null);
624 
625             // IQuux is completely unrelated to IBase/IBaz, so the following
626             // should fail, i.e. return null.
627             IQuux quuxProxy = IQuux.castFrom(baseProxy);
628             ExpectTrue(quuxProxy == null);
629         }
630 
631         {
632             // Test waiting API
633             IBase baseProxyA = IBaz.getService(true /* retry */);
634             ExpectTrue(baseProxyA != null);
635             IBase baseProxyB = IBaz.getService(false /* retry */);
636             ExpectTrue(baseProxyB != null);
637         }
638 
639         IBaz proxy = IBaz.getService();
640 
641         proxy.ping();
642 
643         proxy.someBaseMethod();
644 
645         {
646             Expect(proxy.interfaceDescriptor(), IBaz.kInterfaceName);
647         }
648 
649         {
650             // Tests calling a two-way method with oneway enabled.
651             IHwBinder binder = proxy.asBinder();
652             HwParcel request = new HwParcel();
653             HwParcel reply = new HwParcel();
654 
655             request.writeInterfaceToken(IBaz.kInterfaceName);
656             request.writeInt64(1234);
657             // IBaz::doThatAndReturnSomething is not oneway but we call it using FLAG_ONEWAY.
658             binder.transact(18 /*doThatAndReturnSomething*/, request, reply, IBinder.FLAG_ONEWAY);
659 
660             try {
661                 reply.verifySuccess();
662                 // This should never run.
663                 ExpectTrue(false);
664             } catch (Exception e) {
665                 ExpectTrue(e instanceof RemoteException);
666             }
667 
668             proxy.ping();
669         }
670 
671         {
672             // Tests calling a oneway method with oneway disabled.
673             IHwBinder binder = proxy.asBinder();
674             HwParcel request = new HwParcel();
675             HwParcel reply = new HwParcel();
676 
677             request.writeInterfaceToken(IBaz.kInterfaceName);
678             request.writeFloat(1.0f);
679             // IBaz::doThis is oneway but we call it without using FLAG_ONEWAY.
680             // This does not raise an exception because
681             // IPCThreadState::executeCommand for BR_TRANSACTION sends an empty
682             // reply for two-way transactions if the transaction itself did not
683             // send a reply.
684             binder.transact(17 /*doThis*/, request, reply, 0 /* Not FLAG_ONEWAY */);
685 
686             proxy.ping();
687         }
688 
689         {
690             IBase.Foo foo = new IBase.Foo();
691             foo.x = 1;
692 
693             for (int i = 0; i < 5; ++i) {
694                 IBase.Foo.Bar bar = new IBase.Foo.Bar();
695                 bar.z = 1.0f + (float)i * 0.01f;
696                 bar.s = "Hello, world " + i;
697                 foo.aaa.add(bar);
698             }
699 
700             foo.y.z = 3.14f;
701             foo.y.s = "Lorem ipsum...";
702 
703             IBase.Foo result = proxy.someOtherBaseMethod(foo);
704             ExpectTrue(result.equals(foo));
705         }
706 
707         {
708             IBase.Foo[] inputArray = new IBase.Foo[2];
709 
710             IBase.Foo foo = new IBase.Foo();
711             foo.x = 1;
712 
713             for (int i = 0; i < 5; ++i) {
714                 IBase.Foo.Bar bar = new IBase.Foo.Bar();
715                 bar.z = 1.0f + (float)i * 0.01f;
716                 bar.s = "Hello, world " + i;
717                 foo.aaa.add(bar);
718             }
719 
720             foo.y.z = 3.14f;
721             foo.y.s = "Lorem ipsum...";
722 
723             inputArray[0] = foo;
724 
725             foo = new IBase.Foo();
726             foo.x = 2;
727 
728             for (int i = 0; i < 3; ++i) {
729                 IBase.Foo.Bar bar = new IBase.Foo.Bar();
730                 bar.z = 2.0f - (float)i * 0.01f;
731                 bar.s = "Lorem ipsum " + i;
732                 foo.aaa.add(bar);
733             }
734 
735             foo.y.z = 1.1414f;
736             foo.y.s = "Et tu brute?";
737 
738             inputArray[1] = foo;
739 
740             IBase.Foo[] expectedOutputArray = new IBase.Foo[2];
741             expectedOutputArray[0] = inputArray[1];
742             expectedOutputArray[1] = inputArray[0];
743 
744             IBase.Foo[] outputArray = proxy.someMethodWithFooArrays(inputArray);
745 
746             ExpectTrue(java.util.Objects.deepEquals(outputArray, expectedOutputArray));
747         }
748 
749         {
750             ArrayList<IBase.Foo> inputVec = new ArrayList<IBase.Foo>();
751 
752             IBase.Foo foo = new IBase.Foo();
753             foo.x = 1;
754 
755             for (int i = 0; i < 5; ++i) {
756                 IBase.Foo.Bar bar = new IBase.Foo.Bar();
757                 bar.z = 1.0f + (float)i * 0.01f;
758                 bar.s = "Hello, world " + i;
759                 foo.aaa.add(bar);
760             }
761 
762             foo.y.z = 3.14f;
763             foo.y.s = "Lorem ipsum...";
764 
765             inputVec.add(foo);
766 
767             foo = new IBase.Foo();
768             foo.x = 2;
769 
770             for (int i = 0; i < 3; ++i) {
771                 IBase.Foo.Bar bar = new IBase.Foo.Bar();
772                 bar.z = 2.0f - (float)i * 0.01f;
773                 bar.s = "Lorem ipsum " + i;
774                 foo.aaa.add(bar);
775             }
776 
777             foo.y.z = 1.1414f;
778             foo.y.s = "Et tu brute?";
779 
780             inputVec.add(foo);
781 
782             ArrayList<IBase.Foo> expectedOutputVec = new ArrayList<IBase.Foo>();
783             expectedOutputVec.add(inputVec.get(1));
784             expectedOutputVec.add(inputVec.get(0));
785 
786             ArrayList<IBase.Foo> outputVec =
787                 proxy.someMethodWithFooVectors(inputVec);
788 
789             ExpectTrue(java.util.Objects.deepEquals(outputVec, expectedOutputVec));
790         }
791 
792         {
793             IBase.VectorOfArray in = new IBase.VectorOfArray();
794 
795             int k = 0;
796             for (int i = 0; i < 3; ++i) {
797                 byte[] mac = new byte[6];
798                 for (int j = 0; j < 6; ++j, ++k) {
799                     mac[j] = (byte)k;
800                 }
801 
802                 in.addresses.add(mac);
803             }
804 
805             IBase.VectorOfArray expectedOut = new IBase.VectorOfArray();
806             int n = in.addresses.size();
807 
808             for (int i = 0; i < n; ++i) {
809                 expectedOut.addresses.add(in.addresses.get(n - 1 - i));
810             }
811 
812             IBase.VectorOfArray out = proxy.someMethodWithVectorOfArray(in);
813             ExpectTrue(out.equals(expectedOut));
814         }
815 
816         {
817             ArrayList<byte[]> in = new ArrayList<byte[]>();
818 
819             int k = 0;
820             for (int i = 0; i < 3; ++i) {
821                 byte[] mac = new byte[6];
822                 for (int j = 0; j < 6; ++j, ++k) {
823                     mac[j] = (byte)k;
824                 }
825 
826                 in.add(mac);
827             }
828 
829             ArrayList<byte[]> expectedOut = new ArrayList<byte[]>();
830 
831             int n = in.size();
832             for (int i = 0; i < n; ++i) {
833                 expectedOut.add(in.get(n - 1 - i));
834             }
835 
836             ArrayList<byte[]> out = proxy.someMethodTakingAVectorOfArray(in);
837 
838             ExpectTrue(out.size() == expectedOut.size());
839             for  (int i = 0; i < n; ++i) {
840                 ExpectTrue(java.util.Objects.deepEquals(out.get(i), expectedOut.get(i)));
841             }
842         }
843 
844         {
845             IBase.StringMatrix5x3 in = new IBase.StringMatrix5x3();
846             IBase.StringMatrix3x5 expectedOut = new IBase.StringMatrix3x5();
847 
848             for (int i = 0; i < 5; ++i) {
849                 for (int j = 0; j < 3; ++j) {
850                     in.s[i][j] = numberToEnglish(3 * i + j + 1);
851                     expectedOut.s[j][i] = in.s[i][j];
852                 }
853             }
854 
855             IBase.StringMatrix3x5 out = proxy.transpose(in);
856 
857             // [[1 2 3] [4 5 6] [7 8 9] [10 11 12] [13 14 15]]^T
858             // = [[1 4 7 10 13] [2 5 8 11 14] [3 6 9 12 15]]
859             ExpectTrue(out.equals(expectedOut));
860         }
861 
862         {
863             String[][] in = new String[5][3];
864             String[][] expectedOut = new String[3][5];
865             for (int i = 0; i < 5; ++i) {
866                 for (int j = 0; j < 3; ++j) {
867                     in[i][j] = numberToEnglish(3 * i + j + 1);
868                     expectedOut[j][i] = in[i][j];
869                 }
870             }
871 
872             String[][] out = proxy.transpose2(in);
873 
874             // [[1 2 3] [4 5 6] [7 8 9] [10 11 12] [13 14 15]]^T
875             // = [[1 4 7 10 13] [2 5 8 11 14] [3 6 9 12 15]]
876             ExpectTrue(java.util.Arrays.deepEquals(out, expectedOut));
877         }
878 
879         ExpectTrue(proxy.someBoolMethod(true) == false);
880 
881         {
882             boolean[] someBoolArray = new boolean[3];
883             someBoolArray[0] = true;
884             someBoolArray[1] = false;
885             someBoolArray[2] = true;
886 
887             boolean[] resultArray = proxy.someBoolArrayMethod(someBoolArray);
888             ExpectTrue(resultArray[0] == false);
889             ExpectTrue(resultArray[1] == true);
890             ExpectTrue(resultArray[2] == false);
891 
892             ArrayList<Boolean> someBoolVec = new ArrayList<Boolean>();
893             someBoolVec.add(true);
894             someBoolVec.add(false);
895             someBoolVec.add(true);
896 
897             ArrayList<Boolean> resultVec = proxy.someBoolVectorMethod(someBoolVec);
898             ExpectTrue(resultVec.get(0) == false);
899             ExpectTrue(resultVec.get(1) == true);
900             ExpectTrue(resultVec.get(2) == false);
901         }
902 
903         proxy.doThis(1.0f);
904 
905         ExpectTrue(proxy.doThatAndReturnSomething(1) == 666);
906         ExpectTrue(proxy.doQuiteABit(1, 2L, 3.0f, 4.0) == 666.5);
907 
908         {
909             int[] paramArray = new int[15];
910             int[] expectedOutArray = new int[32];
911             ArrayList<Integer> paramVec = new ArrayList<Integer>();
912             ArrayList<Integer> expectedOutVec = new ArrayList<Integer>();
913 
914             for (int i = 0; i < paramArray.length; ++i) {
915                 paramArray[i] = i;
916                 paramVec.add(i);
917 
918                 expectedOutArray[i] = 2 * i;
919                 expectedOutArray[15 + i] = i;
920 
921                 expectedOutVec.add(2 * i);
922             }
923 
924             expectedOutArray[30] = 1;
925             expectedOutArray[31] = 2;
926 
927 
928             int[] outArray = proxy.doSomethingElse(paramArray);
929             ExpectTrue(java.util.Objects.deepEquals(outArray, expectedOutArray));
930 
931             ArrayList<Integer> outVec = proxy.mapThisVector(paramVec);
932             java.util.Objects.equals(outVec, expectedOutVec);
933 
934         }
935 
936         Expect(proxy.doStuffAndReturnAString(), "Hello, world!");
937 
938         BazCallback cb = new BazCallback();
939         ExpectTrue(!cb.wasCalled());
940         proxy.callMe(cb);
941         ExpectTrue(cb.wasCalled());
942 
943         ExpectTrue(proxy.useAnEnum(IBaz.SomeEnum.goober) == -64);
944 
945         {
946             String[] stringArray = new String[3];
947             stringArray[0] = "one";
948             stringArray[1] = "two";
949             stringArray[2] = "three";
950 
951             String[] expectedOutArray = new String[2];
952             expectedOutArray[0] = "Hello";
953             expectedOutArray[1] = "World";
954 
955             String[] outArray = proxy.haveSomeStrings(stringArray);
956             ExpectTrue(java.util.Arrays.deepEquals(outArray, expectedOutArray));
957 
958             ArrayList<String> stringVec = new ArrayList<String>();
959             stringVec.add("one");
960             stringVec.add("two");
961             stringVec.add("three");
962 
963             ArrayList<String> expectedOutVec = new ArrayList<String>();
964             expectedOutVec.add("Hello");
965             expectedOutVec.add("World");
966 
967             ExpectTrue(expectedOutVec.equals(proxy.haveAStringVec(stringVec)));
968         }
969 
970         {
971             ArrayList<Byte> bytes = new ArrayList<Byte>();
972             bytes.add(IBaz.BitField.V1);
973             bytes.add(IBaz.BitField.V2);
974             ExpectTrue(bytes.equals(proxy.repeatBitfieldVec(bytes)));
975         }
976 
977         proxy.returnABunchOfStrings(
978                 new IBaz.returnABunchOfStringsCallback() {
979                     @Override
980                     public void onValues(String a, String b, String c) {
981                         Expect(a, "Eins");
982                         Expect(b, "Zwei");
983                         Expect(c, "Drei");
984                     }
985                 });
986 
987         proxy.returnABunchOfStrings((a,b,c) -> Expect(a + b + c, "EinsZweiDrei"));
988 
989         proxy.callMeLater(new BazCallback());
990         System.gc();
991         proxy.iAmFreeNow();
992 
993         {
994             IBaz.T t1 = new IBaz.T();
995             IBaz.T t2 = new IBaz.T();
996             for (int i = 0; i < 5; i++) {
997                 for (int j = 0; j < 3; j++) {
998                     t1.matrix5x3[i][j] = t2.matrix5x3[i][j] = (i + 1) * (j + 1);
999                 }
1000             }
1001             ExpectTrue(t1.equals(t2));
1002             ExpectTrue(t1.hashCode() == t2.hashCode());
1003             t2.matrix5x3[4][2] = -60;
1004             ExpectTrue(!t1.equals(t2));
1005         }
1006 
1007         ArrayList<NestedStruct> structs = proxy.getNestedStructs();
1008         ExpectTrue(structs.size() == 5);
1009         ExpectTrue(structs.get(1).matrices.size() == 6);
1010 
1011         {
1012             IBaz.Everything e = new IBaz.Everything();
1013             Expect(e.toString(),
1014                 "{.number = 0, .anotherNumber = 0, .s = , " +
1015                 ".vs = [], .multidimArray = [[null, null], [null, null]], " +
1016                 ".sArray = [null, null, null], .anotherStruct = {.first = , .last = }, .bf = }");
1017             e.s = "string!";
1018             e.number = 127;
1019             e.anotherNumber = 100;
1020             e.vs.addAll(Arrays.asList("One", "Two", "Three"));
1021             for (int i = 0; i < e.multidimArray.length; i++)
1022                 for (int j = 0; j < e.multidimArray[i].length; j++)
1023                     e.multidimArray[i][j] = Integer.toString(i) + Integer.toString(j);
1024             e.bf = IBaz.BitField.VALL;
1025             e.anotherStruct.first = "James";
1026             e.anotherStruct.last = "Bond";
1027             Expect(e.toString(),
1028                 "{.number = 127, .anotherNumber = 100, .s = string!, " +
1029                 ".vs = [One, Two, Three], .multidimArray = [[00, 01], [10, 11]], " +
1030                 ".sArray = [null, null, null], .anotherStruct = {.first = James, .last = Bond}, " +
1031                 ".bf = V0 | V1 | V2 | V3 | VALL}");
1032             Expect(IBaz.BitField.toString(IBaz.BitField.VALL), "VALL");
1033             Expect(IBaz.BitField.toString((byte)(IBaz.BitField.V0 | IBaz.BitField.V2)), "0x5");
1034             Expect(IBaz.BitField.dumpBitfield(IBaz.BitField.VALL), "V0 | V1 | V2 | V3 | VALL");
1035             Expect(IBaz.BitField.dumpBitfield((byte)(IBaz.BitField.V1 | IBaz.BitField.V3 | 0xF0)),
1036                 "V1 | V3 | 0xf0");
1037 
1038             Expect(proxy.toString(), IBaz.kInterfaceName + "@Proxy");
1039         }
1040 
1041         {
1042             // Ensure that native parcel is cleared even if the corresponding
1043             // Java object isn't GC'd.
1044             ArrayList<Integer> data4K = new ArrayList<>(1024);
1045             for (int i = 0; i < 1024; i++) {
1046                 data4K.add(i);
1047             }
1048 
1049             for (int i = 0; i < 1024; i++) {
1050                 // If they are not properly cleaned up, these calls will put 4MB of data in
1051                 // kernel binder buffer, and will fail.
1052                 try {
1053                     proxy.mapThisVector(data4K);
1054                 } catch (RemoteException ex) {
1055                     throw new RuntimeException("Failed at call #" + Integer.toString(i), ex);
1056                 }
1057             }
1058         }
1059 
1060         {
1061             // TestArrays
1062             IBase.LotsOfPrimitiveArrays in = new IBase.LotsOfPrimitiveArrays();
1063 
1064             for (int i = 0; i < 128; ++i) {
1065                 in.byte1[i] = (byte)i;
1066                 in.boolean1[i] = (i & 4) != 0;
1067                 in.double1[i] = i;
1068             }
1069 
1070             int m = 0;
1071             for (int i = 0; i < 8; ++i) {
1072                 for (int j = 0; j < 128; ++j, ++m) {
1073                     in.byte2[i][j] = (byte)m;
1074                     in.boolean2[i][j] = (m & 4) != 0;
1075                     in.double2[i][j] = m;
1076                 }
1077             }
1078 
1079             m = 0;
1080             for (int i = 0; i < 8; ++i) {
1081                 for (int j = 0; j < 16; ++j) {
1082                     for (int k = 0; k < 128; ++k, ++m) {
1083                         in.byte3[i][j][k] = (byte)m;
1084                         in.boolean3[i][j][k] = (m & 4) != 0;
1085                         in.double3[i][j][k] = m;
1086                     }
1087                 }
1088             }
1089 
1090             IBase.LotsOfPrimitiveArrays out = proxy.testArrays(in);
1091             ExpectTrue(in.equals(out));
1092         }
1093 
1094         {
1095             // testByteVecs
1096 
1097             ArrayList<byte[]> in = new ArrayList<byte[]>();
1098 
1099             int k = 0;
1100             for (int i = 0; i < 8; ++i) {
1101                 byte[] elem = new byte[128];
1102                 for (int j = 0; j < 128; ++j, ++k) {
1103                     elem[j] = (byte)k;
1104                 }
1105                 in.add(elem);
1106             }
1107 
1108             ArrayList<byte[]> out = proxy.testByteVecs(in);
1109 
1110             ExpectDeepEq(in, out);
1111         }
1112 
1113         {
1114             // testByteVecs w/ mismatched element lengths.
1115 
1116             ArrayList<byte[]> in = new ArrayList<byte[]>();
1117 
1118             int k = 0;
1119             for (int i = 0; i < 8; ++i) {
1120                 byte[] elem = new byte[128 - i];
1121                 for (int j = 0; j < (128 - i); ++j, ++k) {
1122                     elem[j] = (byte)k;
1123                 }
1124                 in.add(elem);
1125             }
1126 
1127             boolean failedAsItShould = false;
1128 
1129             try {
1130                 ArrayList<byte[]> out = proxy.testByteVecs(in);
1131             }
1132             catch (IllegalArgumentException e) {
1133                 failedAsItShould = true;
1134             }
1135 
1136             ExpectTrue(failedAsItShould);
1137         }
1138 
1139         {
1140             // testBooleanVecs
1141 
1142             ArrayList<boolean[]> in = new ArrayList<boolean[]>();
1143 
1144             int k = 0;
1145             for (int i = 0; i < 8; ++i) {
1146                 boolean[] elem = new boolean[128];
1147                 for (int j = 0; j < 128; ++j, ++k) {
1148                     elem[j] = (k & 4) != 0;
1149                 }
1150                 in.add(elem);
1151             }
1152 
1153             ArrayList<boolean[]> out = proxy.testBooleanVecs(in);
1154             ExpectDeepEq(in, out);
1155         }
1156 
1157         {
1158             // testDoubleVecs
1159 
1160             ArrayList<double[]> in = new ArrayList<double[]>();
1161 
1162             int k = 0;
1163             for (int i = 0; i < 8; ++i) {
1164                 double[] elem = new double[128];
1165                 for (int j = 0; j < 128; ++j, ++k) {
1166                     elem[j] = k;
1167                 }
1168                 in.add(elem);
1169             }
1170 
1171             ArrayList<double[]> out = proxy.testDoubleVecs(in);
1172             ExpectDeepEq(in, out);
1173         }
1174         {
1175             // testProxyEquals
1176             // TODO(b/68727931): test passthrough services as well.
1177 
1178             IBase proxy1 = IBase.getService();
1179             IBase proxy2 = IBase.getService();
1180             IBaz proxy3 = IBaz.getService();
1181             IBazCallback callback1 = new BazCallback();
1182             IBazCallback callback2 = new BazCallback();
1183             IServiceManager manager = IServiceManager.getService();
1184 
1185             // test hwbinder proxies
1186             ExpectEqual(proxy1, proxy2); // same proxy class
1187             ExpectEqual(proxy1, proxy3); // different proxy class
1188 
1189             // negative tests
1190             ExpectNotEqual(proxy1, null);
1191             ExpectNotEqual(proxy1, callback1); // proxy != stub
1192             ExpectNotEqual(proxy1, manager);
1193 
1194             // HidlSupport.interfacesEqual use overridden .equals for stubs
1195             ExpectEqual(callback1, callback1);
1196             ExpectEqual(callback1, callback2);
1197             callback1.hey();
1198             ExpectNotEqual(callback1, callback2);
1199             callback2.hey();
1200             ExpectEqual(callback1, callback2);
1201 
1202             // test hash for proxies
1203             java.util.HashSet<IBase> set = new java.util.HashSet<>();
1204             set.add(proxy1);
1205             ExpectTrue(set.contains(proxy1)); // hash is stable
1206             ExpectTrue(set.contains(proxy2));
1207             ExpectFalse(set.contains(manager));
1208         }
1209         {
1210             IBaz baz = IBaz.getService();
1211             ExpectTrue(baz != null);
1212             IBaz.StructWithInterface swi = new IBaz.StructWithInterface();
1213             swi.dummy = baz;
1214             swi.number = 12345678;
1215             IBaz.StructWithInterface swi_back = baz.haveSomeStructWithInterface(swi);
1216             ExpectTrue(swi_back != null);
1217             ExpectTrue(swi_back.dummy != null);
1218             ExpectTrue(HidlSupport.interfacesEqual(baz, swi_back.dummy));
1219             ExpectTrue(swi_back.number == 12345678);
1220         }
1221 
1222         runClientSafeUnionTests();
1223         runClientMemoryTests();
1224 
1225         // --- DEATH RECIPIENT TESTING ---
1226         // This must always be done last, since it will kill the native server process
1227         HidlDeathRecipient recipient1 = new HidlDeathRecipient();
1228         HidlDeathRecipient recipient2 = new HidlDeathRecipient();
1229 
1230         final int cookie1 = 0x1481;
1231         final int cookie2 = 0x1482;
1232         final int cookie3 = 0x1483;
1233         ExpectTrue(proxy.linkToDeath(recipient1, cookie1));
1234 
1235         ExpectTrue(proxy.linkToDeath(recipient1, cookie3));
1236         ExpectTrue(proxy.unlinkToDeath(recipient1));
1237 
1238         ExpectTrue(proxy.linkToDeath(recipient2, cookie2));
1239         ExpectTrue(proxy.unlinkToDeath(recipient2));
1240         try {
1241             proxy.dieNow();
1242         } catch (DeadObjectException e) {
1243             // Expected
1244         }
1245         ExpectTrue(recipient1.waitUntilServiceDied(2000 /*timeoutMillis*/));
1246         ExpectTrue(!recipient2.waitUntilServiceDied(2000 /*timeoutMillis*/));
1247         ExpectTrue(recipient1.cookieMatches(cookie1));
1248         Log.d(TAG, "OK, exiting");
1249     }
1250 
1251     class Baz extends IBaz.Stub {
1252         // from IBase
someBaseMethod()1253         public void someBaseMethod() {
1254             Log.d(TAG, "Baz someBaseMethod");
1255         }
1256 
someOtherBaseMethod(IBase.Foo foo)1257         public IBase.Foo someOtherBaseMethod(IBase.Foo foo) {
1258             Log.d(TAG, "Baz someOtherBaseMethod " + foo.toString());
1259             return foo;
1260         }
1261 
someMethodWithFooArrays(IBase.Foo[] fooInput)1262         public IBase.Foo[] someMethodWithFooArrays(IBase.Foo[] fooInput) {
1263             Log.d(TAG, "Baz someMethodWithFooArrays " + fooInput.toString());
1264 
1265             IBase.Foo[] fooOutput = new IBase.Foo[2];
1266             fooOutput[0] = fooInput[1];
1267             fooOutput[1] = fooInput[0];
1268 
1269             return fooOutput;
1270         }
1271 
someMethodWithFooVectors( ArrayList<IBase.Foo> fooInput)1272         public ArrayList<IBase.Foo> someMethodWithFooVectors(
1273                 ArrayList<IBase.Foo> fooInput) {
1274             Log.d(TAG, "Baz someMethodWithFooVectors " + fooInput.toString());
1275 
1276             ArrayList<IBase.Foo> fooOutput = new ArrayList<IBase.Foo>();
1277             fooOutput.add(fooInput.get(1));
1278             fooOutput.add(fooInput.get(0));
1279 
1280             return fooOutput;
1281         }
1282 
someMethodWithVectorOfArray( IBase.VectorOfArray in)1283         public IBase.VectorOfArray someMethodWithVectorOfArray(
1284                 IBase.VectorOfArray in) {
1285             Log.d(TAG, "Baz someMethodWithVectorOfArray " + in.toString());
1286 
1287             IBase.VectorOfArray out = new IBase.VectorOfArray();
1288             int n = in.addresses.size();
1289             for (int i = 0; i < n; ++i) {
1290                 out.addresses.add(in.addresses.get(n - i - 1));
1291             }
1292 
1293             return out;
1294         }
1295 
someMethodTakingAVectorOfArray( ArrayList<byte[ ]> in)1296         public ArrayList<byte[/* 6 */]> someMethodTakingAVectorOfArray(
1297                 ArrayList<byte[/* 6 */]> in) {
1298             Log.d(TAG, "Baz someMethodTakingAVectorOfArray");
1299 
1300             int n = in.size();
1301             ArrayList<byte[]> out = new ArrayList<byte[]>();
1302             for (int i = 0; i < n; ++i) {
1303                 out.add(in.get(n - i - 1));
1304             }
1305 
1306             return out;
1307         }
1308 
transpose(IBase.StringMatrix5x3 in)1309         public IBase.StringMatrix3x5 transpose(IBase.StringMatrix5x3 in) {
1310             Log.d(TAG, "Baz transpose " + in.toString());
1311 
1312             IBase.StringMatrix3x5 out = new IBase.StringMatrix3x5();
1313             for (int i = 0; i < 3; ++i) {
1314                 for (int j = 0; j < 5; ++j) {
1315                     out.s[i][j] = in.s[j][i];
1316                 }
1317             }
1318 
1319             return out;
1320         }
1321 
transpose2(String[][] in)1322         public String[][] transpose2(String[][] in) {
1323             Log.d(TAG, "Baz transpose2 " + in.toString());
1324 
1325             String[][] out = new String[3][5];
1326             for (int i = 0; i < 3; ++i) {
1327                 for (int j = 0; j < 5; ++j) {
1328                     out[i][j] = in[j][i];
1329                 }
1330             }
1331 
1332             return out;
1333         }
1334 
someBoolMethod(boolean x)1335         public boolean someBoolMethod(boolean x) {
1336             Log.d(TAG, "Baz someBoolMethod(" + x + ")");
1337 
1338             return !x;
1339         }
1340 
someBoolArrayMethod(boolean[] x)1341         public boolean[] someBoolArrayMethod(boolean[] x) {
1342             Log.d(TAG, "Baz someBoolArrayMethod("
1343                     + x.toString() + ")");
1344 
1345             boolean[] out = new boolean[4];
1346             out[0] = !x[0];
1347             out[1] = !x[1];
1348             out[2] = !x[2];
1349             out[3] = true;
1350 
1351             return out;
1352         }
1353 
someBoolVectorMethod(ArrayList<Boolean> x)1354         public ArrayList<Boolean> someBoolVectorMethod(ArrayList<Boolean> x) {
1355             Log.d(TAG, "Baz someBoolVectorMethod(" + x.toString() + ")");
1356 
1357             ArrayList<Boolean> out = new ArrayList<Boolean>();
1358             for (int i = 0; i < x.size(); ++i) {
1359                 out.add(!x.get(i));
1360             }
1361 
1362             return out;
1363         }
1364 
doThis(float param)1365         public void doThis(float param) {
1366             Log.d(TAG, "Baz doThis " + param);
1367         }
1368 
doThatAndReturnSomething(long param)1369         public int doThatAndReturnSomething(long param) {
1370             Log.d(TAG, "Baz doThatAndReturnSomething " + param);
1371             return 666;
1372         }
1373 
doQuiteABit(int a, long b, float c, double d)1374         public double doQuiteABit(int a, long b, float c, double d) {
1375             Log.d(TAG, "Baz doQuiteABit " + a + ", " + b + ", " + c + ", " + d);
1376             return 666.5;
1377         }
1378 
doSomethingElse(int[] param)1379         public int[] doSomethingElse(int[] param) {
1380             Log.d(TAG, "Baz doSomethingElse " + param.toString());
1381 
1382             int[] something = new int[32];
1383             for (int i = 0; i < 15; ++i) {
1384                 something[i] = 2 * param[i];
1385                 something[15 + i] = param[i];
1386             }
1387             something[30] = 1;
1388             something[31] = 2;
1389 
1390             return something;
1391         }
1392 
doStuffAndReturnAString()1393         public String doStuffAndReturnAString() {
1394             Log.d(TAG, "doStuffAndReturnAString");
1395             return "Hello, world!";
1396         }
1397 
mapThisVector(ArrayList<Integer> param)1398         public ArrayList<Integer> mapThisVector(ArrayList<Integer> param) {
1399             Log.d(TAG, "mapThisVector " + param.toString());
1400 
1401             ArrayList<Integer> out = new ArrayList<Integer>();
1402 
1403             for (int i = 0; i < param.size(); ++i) {
1404                 out.add(2 * param.get(i));
1405             }
1406 
1407             return out;
1408         }
1409 
takeAMask(byte bf, byte first, IBase.MyMask second, byte third, takeAMaskCallback cb)1410         public void takeAMask(byte bf, byte first, IBase.MyMask second, byte third,
1411                 takeAMaskCallback cb) {
1412             cb.onValues(bf, (byte)(bf | first),
1413                     (byte)(second.value & bf), (byte)((bf | bf) & third));
1414         }
1415 
testArrays(LotsOfPrimitiveArrays in)1416         public LotsOfPrimitiveArrays testArrays(LotsOfPrimitiveArrays in) {
1417             return in;
1418         }
1419 
testByteVecs(ArrayList<byte[]> in)1420         public ArrayList<byte[]> testByteVecs(ArrayList<byte[]> in) {
1421             return in;
1422         }
1423 
testBooleanVecs(ArrayList<boolean[]> in)1424         public ArrayList<boolean[]> testBooleanVecs(ArrayList<boolean[]> in) {
1425             return in;
1426         }
1427 
testDoubleVecs(ArrayList<double[]> in)1428         public ArrayList<double[]> testDoubleVecs(ArrayList<double[]> in) {
1429             return in;
1430         }
1431 
returnABitField()1432         public byte returnABitField() {
1433             return 0;
1434         }
1435 
size(int size)1436         public int size(int size) {
1437             return size;
1438         }
1439 
1440         @Override
getNestedStructs()1441         public ArrayList<NestedStruct> getNestedStructs() throws RemoteException {
1442             return new ArrayList<>();
1443         }
1444 
1445         class BazCallback extends IBazCallback.Stub {
heyItsMe(IBazCallback cb)1446             public void heyItsMe(IBazCallback cb) {
1447                 Log.d(TAG, "SERVER: heyItsMe");
1448             }
1449 
hey()1450             public void hey() {
1451                 Log.d(TAG, "SERVER: hey");
1452             }
1453         }
1454 
callMe(IBazCallback cb)1455         public void callMe(IBazCallback cb) throws RemoteException {
1456             Log.d(TAG, "callMe");
1457             cb.heyItsMe(new BazCallback());
1458         }
1459 
1460         private IBazCallback mStoredCallback;
callMeLater(IBazCallback cb)1461         public void callMeLater(IBazCallback cb) {
1462             mStoredCallback = cb;
1463         }
1464 
iAmFreeNow()1465         public void iAmFreeNow() throws RemoteException {
1466             if (mStoredCallback != null) {
1467                 mStoredCallback.hey();
1468             }
1469         }
1470 
dieNow()1471         public void dieNow() {
1472             // Not tested in Java
1473         }
1474 
useAnEnum(byte zzz)1475         public byte useAnEnum(byte zzz) {
1476             Log.d(TAG, "useAnEnum " + zzz);
1477             return SomeEnum.quux;
1478         }
1479 
haveSomeStrings(String[] array)1480         public String[] haveSomeStrings(String[] array) {
1481             Log.d(TAG, "haveSomeStrings ["
1482                         + "\"" + array[0] + "\", "
1483                         + "\"" + array[1] + "\", "
1484                         + "\"" + array[2] + "\"]");
1485 
1486             String[] result = new String[2];
1487             result[0] = "Hello";
1488             result[1] = "World";
1489 
1490             return result;
1491         }
1492 
haveAStringVec(ArrayList<String> vector)1493         public ArrayList<String> haveAStringVec(ArrayList<String> vector) {
1494             Log.d(TAG, "haveAStringVec ["
1495                         + "\"" + vector.get(0) + "\", "
1496                         + "\"" + vector.get(1) + "\", "
1497                         + "\"" + vector.get(2) + "\"]");
1498 
1499             ArrayList<String> result = new ArrayList<String>();
1500             result.add("Hello");
1501             result.add("World");
1502 
1503             return result;
1504         }
1505 
repeatBitfieldVec(ArrayList<Byte> vector)1506         public ArrayList<Byte> repeatBitfieldVec(ArrayList<Byte> vector) { return vector; }
1507 
returnABunchOfStrings(returnABunchOfStringsCallback cb)1508         public void returnABunchOfStrings(returnABunchOfStringsCallback cb) {
1509             cb.onValues("Eins", "Zwei", "Drei");
1510         }
1511 
haveSomeStructWithInterface(StructWithInterface swi)1512         public StructWithInterface haveSomeStructWithInterface(StructWithInterface swi) {
1513             return swi;
1514         }
1515     }
1516 
1517     class SafeUnion extends ISafeUnion.Stub {
1518         @Override
newLargeSafeUnion()1519         public LargeSafeUnion newLargeSafeUnion() {
1520             Log.d(TAG, "SERVER: newLargeSafeUnion");
1521             return new LargeSafeUnion();
1522         }
1523 
1524         @Override
setA(LargeSafeUnion safeUnion, byte a)1525         public LargeSafeUnion setA(LargeSafeUnion safeUnion, byte a) {
1526             Log.d(TAG, "SERVER: setA(" + a + ")");
1527             safeUnion.a(a);
1528 
1529             return safeUnion;
1530         }
1531 
1532         @Override
setB(LargeSafeUnion safeUnion, short b)1533         public LargeSafeUnion setB(LargeSafeUnion safeUnion, short b) {
1534             Log.d(TAG, "SERVER: setB(" + b + ")");
1535             safeUnion.b(b);
1536 
1537             return safeUnion;
1538         }
1539 
1540         @Override
setC(LargeSafeUnion safeUnion, int c)1541         public LargeSafeUnion setC(LargeSafeUnion safeUnion, int c) {
1542             Log.d(TAG, "SERVER: setC(" + c + ")");
1543             safeUnion.c(c);
1544 
1545             return safeUnion;
1546         }
1547 
1548         @Override
setD(LargeSafeUnion safeUnion, long d)1549         public LargeSafeUnion setD(LargeSafeUnion safeUnion, long d) {
1550             Log.d(TAG, "SERVER: setD(" + d + ")");
1551             safeUnion.d(d);
1552 
1553             return safeUnion;
1554         }
1555 
1556         @Override
setE(LargeSafeUnion safeUnion, byte[ ] e)1557         public LargeSafeUnion setE(LargeSafeUnion safeUnion, byte[/* 13 */] e) {
1558             Log.d(TAG, "SERVER: setE(" + e + ")");
1559             safeUnion.e(e);
1560 
1561             return safeUnion;
1562         }
1563 
1564         @Override
setF(LargeSafeUnion safeUnion, long[ ] f)1565         public LargeSafeUnion setF(LargeSafeUnion safeUnion, long[/* 5 */] f) {
1566             Log.d(TAG, "SERVER: setF(" + f + ")");
1567             safeUnion.f(f);
1568 
1569             return safeUnion;
1570         }
1571 
1572         @Override
setG(LargeSafeUnion safeUnion, String g)1573         public LargeSafeUnion setG(LargeSafeUnion safeUnion, String g) {
1574             Log.d(TAG, "SERVER: setG(" + g + ")");
1575             safeUnion.g(g);
1576 
1577             return safeUnion;
1578         }
1579 
1580         @Override
setH(LargeSafeUnion safeUnion, ArrayList<Boolean> h)1581         public LargeSafeUnion setH(LargeSafeUnion safeUnion, ArrayList<Boolean> h) {
1582             Log.d(TAG, "SERVER: setH(" + h + ")");
1583             safeUnion.h(h);
1584 
1585             return safeUnion;
1586         }
1587 
1588         @Override
setI(LargeSafeUnion safeUnion, ArrayList<Long> i)1589         public LargeSafeUnion setI(LargeSafeUnion safeUnion, ArrayList<Long> i) {
1590             Log.d(TAG, "SERVER: setI(" + i + ")");
1591             safeUnion.i(i);
1592 
1593             return safeUnion;
1594         }
1595 
1596         @Override
setJ(LargeSafeUnion safeUnion, ISafeUnion.J j)1597         public LargeSafeUnion setJ(LargeSafeUnion safeUnion, ISafeUnion.J j) {
1598             Log.d(TAG, "SERVER: setJ(" + j + ")");
1599             safeUnion.j(j);
1600 
1601             return safeUnion;
1602         }
1603 
1604         @Override
setK(LargeSafeUnion safeUnion, LargeSafeUnion.K k)1605         public LargeSafeUnion setK(LargeSafeUnion safeUnion, LargeSafeUnion.K k) {
1606             Log.d(TAG, "SERVER: setK(" + k + ")");
1607             safeUnion.k(k);
1608 
1609             return safeUnion;
1610         }
1611 
1612         @Override
setL(LargeSafeUnion safeUnion, SmallSafeUnion l)1613         public LargeSafeUnion setL(LargeSafeUnion safeUnion, SmallSafeUnion l) {
1614             Log.d(TAG, "SERVER: setL(" + l + ")");
1615             safeUnion.l(l);
1616 
1617             return safeUnion;
1618         }
1619 
1620         @Override
setM(LargeSafeUnion safeUnion, byte m)1621         public LargeSafeUnion setM(LargeSafeUnion safeUnion, byte m) {
1622             Log.d(TAG, "SERVER: setM(" + m + ")");
1623             safeUnion.m(m);
1624 
1625             return safeUnion;
1626         }
1627 
1628         @Override
setN(LargeSafeUnion safeUnion, byte n)1629         public LargeSafeUnion setN(LargeSafeUnion safeUnion, byte n) {
1630             Log.d(TAG, "SERVER: setN(" + n + ")");
1631             safeUnion.n(n);
1632 
1633             return safeUnion;
1634         }
1635 
1636         @Override
newInterfaceTypeSafeUnion()1637         public InterfaceTypeSafeUnion newInterfaceTypeSafeUnion() {
1638             Log.d(TAG, "SERVER: newInterfaceTypeSafeUnion");
1639             return new InterfaceTypeSafeUnion();
1640         }
1641 
1642         @Override
setInterfaceA(InterfaceTypeSafeUnion safeUnion, int a)1643         public InterfaceTypeSafeUnion setInterfaceA(InterfaceTypeSafeUnion safeUnion, int a) {
1644             Log.d(TAG, "SERVER: setInterfaceA(" + a + ")");
1645             safeUnion.a(a);
1646 
1647             return safeUnion;
1648         }
1649 
1650         @Override
setInterfaceB( InterfaceTypeSafeUnion safeUnion, byte[ ] b)1651         public InterfaceTypeSafeUnion setInterfaceB(
1652             InterfaceTypeSafeUnion safeUnion, byte[/* 7 */] b) {
1653             Log.d(TAG, "SERVER: setInterfaceB(" + b + ")");
1654             safeUnion.b(b);
1655 
1656             return safeUnion;
1657         }
1658 
1659         @Override
setInterfaceC( InterfaceTypeSafeUnion safeUnion, IOtherInterface c)1660         public InterfaceTypeSafeUnion setInterfaceC(
1661             InterfaceTypeSafeUnion safeUnion, IOtherInterface c) {
1662             Log.d(TAG, "SERVER: setInterfaceC(" + c + ")");
1663             safeUnion.c(c);
1664 
1665             return safeUnion;
1666         }
1667 
1668         @Override
setInterfaceD(InterfaceTypeSafeUnion safeUnion, String d)1669         public InterfaceTypeSafeUnion setInterfaceD(InterfaceTypeSafeUnion safeUnion, String d) {
1670             Log.d(TAG, "SERVER: setInterfaceD(" + d + ")");
1671             safeUnion.d(d);
1672 
1673             return safeUnion;
1674         }
1675 
1676         @Override
setInterfaceE( InterfaceTypeSafeUnion safeUnion, ArrayList<String> e)1677         public InterfaceTypeSafeUnion setInterfaceE(
1678             InterfaceTypeSafeUnion safeUnion, ArrayList<String> e) {
1679             Log.d(TAG, "SERVER: setInterfaceE(" + e + ")");
1680             safeUnion.e(e);
1681 
1682             return safeUnion;
1683         }
1684 
1685         @Override
setInterfaceF( InterfaceTypeSafeUnion safeUnion, NativeHandle f)1686         public InterfaceTypeSafeUnion setInterfaceF(
1687             InterfaceTypeSafeUnion safeUnion, NativeHandle f) {
1688             Log.d(TAG, "SERVER: setInterfaceF(" + f + ")");
1689             safeUnion.f(f);
1690 
1691             return safeUnion;
1692         }
1693 
1694         @Override
setInterfaceG( InterfaceTypeSafeUnion safeUnion, ArrayList<NativeHandle> g)1695         public InterfaceTypeSafeUnion setInterfaceG(
1696             InterfaceTypeSafeUnion safeUnion, ArrayList<NativeHandle> g) {
1697             Log.d(TAG, "SERVER: setInterfaceG(" + g + ")");
1698             safeUnion.g(g);
1699 
1700             return safeUnion;
1701         }
1702 
1703         @Override
newHandleTypeSafeUnion()1704         public HandleTypeSafeUnion newHandleTypeSafeUnion() {
1705             Log.d(TAG, "SERVER: newHandleTypeSafeUnion");
1706             return new HandleTypeSafeUnion();
1707         }
1708 
1709         @Override
setHandleA(HandleTypeSafeUnion safeUnion, NativeHandle a)1710         public HandleTypeSafeUnion setHandleA(HandleTypeSafeUnion safeUnion, NativeHandle a) {
1711             Log.d(TAG, "SERVER: setHandleA(" + a + ")");
1712             safeUnion.a(a);
1713 
1714             return safeUnion;
1715         }
1716 
1717         @Override
setHandleB(HandleTypeSafeUnion safeUnion, NativeHandle[] b)1718         public HandleTypeSafeUnion setHandleB(HandleTypeSafeUnion safeUnion, NativeHandle[] b) {
1719             Log.d(TAG, "SERVER: setHandleB(" + b + ")");
1720             safeUnion.b(b);
1721 
1722             return safeUnion;
1723         }
1724 
1725         @Override
setHandleC(HandleTypeSafeUnion safeUnion, ArrayList<NativeHandle> c)1726         public HandleTypeSafeUnion setHandleC(HandleTypeSafeUnion safeUnion,
1727                                               ArrayList<NativeHandle> c) {
1728             Log.d(TAG, "SERVER: setHandleC(" + c + ")");
1729             safeUnion.c(c);
1730 
1731             return safeUnion;
1732         }
1733     }
1734 
1735     class OtherInterface extends IOtherInterface.Stub {
1736         @Override
concatTwoStrings(String a, String b)1737         public String concatTwoStrings(String a, String b) {
1738             return a.concat(b);
1739         }
1740     }
1741 
server()1742     private void server() throws RemoteException {
1743         HwBinder.configureRpcThreadpool(1, true);
1744 
1745         Baz baz = new Baz();
1746         baz.registerAsService("default");
1747 
1748         SafeUnion safeunionInterface = new SafeUnion();
1749         safeunionInterface.registerAsService("default");
1750 
1751         OtherInterface otherInterface = new OtherInterface();
1752         otherInterface.registerAsService("default");
1753 
1754         HwBinder.joinRpcThreadpool();
1755     }
1756 }
1757