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