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