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