1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.binder.cts; 18 19 import android.os.Binder; 20 import android.os.IBinder; 21 import android.os.ParcelFileDescriptor; 22 import android.os.RemoteException; 23 import java.io.IOException; 24 25 import test_package.IEmpty; 26 import test_package.ITest; 27 import test_package.RegularPolygon; 28 import test_package.Foo; 29 import test_package.Bar; 30 31 import java.util.concurrent.CountDownLatch; 32 import java.io.FileDescriptor; 33 import java.io.PrintWriter; 34 35 public class TestImpl extends ITest.Stub { 36 @Override getInterfaceVersion()37 public int getInterfaceVersion() { return TestImpl.VERSION; } 38 39 @Override getInterfaceHash()40 public String getInterfaceHash() { return TestImpl.HASH; } 41 42 @Override dump(FileDescriptor fd, PrintWriter pw, String[] args)43 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 44 for (String arg : args) { 45 pw.print(arg); 46 } 47 } 48 49 @Override GetName()50 public String GetName() { 51 return "JAVA"; 52 } 53 54 @Override TestVoidReturn()55 public void TestVoidReturn() {} 56 57 @Override TestOneway()58 public void TestOneway() {} 59 60 @Override GiveMeMyCallingPid()61 public int GiveMeMyCallingPid() { 62 return Binder.getCallingPid(); 63 } 64 65 @Override GiveMeMyCallingUid()66 public int GiveMeMyCallingUid() { 67 return Binder.getCallingUid(); 68 } 69 70 private CountDownLatch mCachedLatch = new CountDownLatch(1); 71 private int mCachedPid = -1; 72 private int mCachedUid = -1; 73 waitForCachedOnewayInfo()74 private void waitForCachedOnewayInfo() { 75 try { 76 mCachedLatch.await(); 77 } catch (InterruptedException e) { 78 // This thread is never expected to be interrupted during this test. This will be 79 // converted to a RemoteException on the other side and cause the test to fail. 80 81 throw new RuntimeException(e.toString()); 82 } 83 } 84 85 @Override CacheCallingInfoFromOneway()86 public void CacheCallingInfoFromOneway() { 87 mCachedPid = Binder.getCallingPid(); 88 mCachedUid = Binder.getCallingUid(); 89 mCachedLatch.countDown(); 90 } 91 92 @Override GiveMeMyCallingPidFromOneway()93 public int GiveMeMyCallingPidFromOneway() { 94 waitForCachedOnewayInfo(); 95 return mCachedPid; 96 } 97 98 @Override GiveMeMyCallingUidFromOneway()99 public int GiveMeMyCallingUidFromOneway() { 100 waitForCachedOnewayInfo(); 101 return mCachedUid; 102 } 103 104 @Override RepeatInt(int in_value)105 public int RepeatInt(int in_value) { 106 return in_value; 107 } 108 109 @Override RepeatLong(long in_value)110 public long RepeatLong(long in_value) { 111 return in_value; 112 } 113 114 @Override RepeatFloat(float in_value)115 public float RepeatFloat(float in_value) { 116 return in_value; 117 } 118 119 @Override RepeatDouble(double in_value)120 public double RepeatDouble(double in_value) { 121 return in_value; 122 } 123 124 @Override RepeatBoolean(boolean in_value)125 public boolean RepeatBoolean(boolean in_value) { 126 return in_value; 127 } 128 129 @Override RepeatChar(char in_value)130 public char RepeatChar(char in_value) { 131 return in_value; 132 } 133 134 @Override RepeatByte(byte in_value)135 public byte RepeatByte(byte in_value) { 136 return in_value; 137 } 138 139 @Override RepeatByteEnum(byte in_value)140 public byte RepeatByteEnum(byte in_value) { 141 return in_value; 142 } 143 144 @Override RepeatIntEnum(int in_value)145 public int RepeatIntEnum(int in_value) { 146 return in_value; 147 } 148 149 @Override RepeatLongEnum(long in_value)150 public long RepeatLongEnum(long in_value) { 151 return in_value; 152 } 153 154 @Override RepeatBinder(IBinder in_value)155 public IBinder RepeatBinder(IBinder in_value) { 156 return in_value; 157 } 158 159 @Override RepeatNullableBinder(IBinder in_value)160 public IBinder RepeatNullableBinder(IBinder in_value) { 161 return in_value; 162 } 163 164 @Override RepeatInterface(IEmpty in_value)165 public IEmpty RepeatInterface(IEmpty in_value) { 166 return in_value; 167 } 168 169 @Override RepeatNullableInterface(IEmpty in_value)170 public IEmpty RepeatNullableInterface(IEmpty in_value) { 171 return in_value; 172 } 173 174 @Override RepeatFd(ParcelFileDescriptor in_value)175 public ParcelFileDescriptor RepeatFd(ParcelFileDescriptor in_value) { 176 return in_value; 177 } 178 179 @Override RepeatNullableFd(ParcelFileDescriptor in_value)180 public ParcelFileDescriptor RepeatNullableFd(ParcelFileDescriptor in_value) { 181 return in_value; 182 } 183 184 @Override RepeatString(String in_value)185 public String RepeatString(String in_value) { 186 return in_value; 187 } 188 189 @Override RepeatNullableString(String in_value)190 public String RepeatNullableString(String in_value) { 191 return in_value; 192 } 193 194 @Override RepeatPolygon(RegularPolygon in_value)195 public RegularPolygon RepeatPolygon(RegularPolygon in_value) { 196 return in_value; 197 } 198 199 @Override RepeatNullablePolygon(RegularPolygon in_value)200 public RegularPolygon RepeatNullablePolygon(RegularPolygon in_value) { 201 return in_value; 202 } 203 204 @Override RenamePolygon(RegularPolygon value, String name)205 public void RenamePolygon(RegularPolygon value, String name) { 206 value.name = name; 207 } 208 209 @Override RepeatBooleanArray(boolean[] in_value, boolean[] repeated)210 public boolean[] RepeatBooleanArray(boolean[] in_value, boolean[] repeated) { 211 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 212 return in_value; 213 } 214 215 @Override RepeatByteArray(byte[] in_value, byte[] repeated)216 public byte[] RepeatByteArray(byte[] in_value, byte[] repeated) { 217 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 218 return in_value; 219 } 220 221 @Override RepeatCharArray(char[] in_value, char[] repeated)222 public char[] RepeatCharArray(char[] in_value, char[] repeated) { 223 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 224 return in_value; 225 } 226 227 @Override RepeatIntArray(int[] in_value, int[] repeated)228 public int[] RepeatIntArray(int[] in_value, int[] repeated) { 229 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 230 return in_value; 231 } 232 233 @Override RepeatLongArray(long[] in_value, long[] repeated)234 public long[] RepeatLongArray(long[] in_value, long[] repeated) { 235 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 236 return in_value; 237 } 238 239 @Override RepeatFloatArray(float[] in_value, float[] repeated)240 public float[] RepeatFloatArray(float[] in_value, float[] repeated) { 241 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 242 return in_value; 243 } 244 245 @Override RepeatDoubleArray(double[] in_value, double[] repeated)246 public double[] RepeatDoubleArray(double[] in_value, double[] repeated) { 247 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 248 return in_value; 249 } 250 251 @Override RepeatByteEnumArray(byte[] in_value, byte[] repeated)252 public byte[] RepeatByteEnumArray(byte[] in_value, byte[] repeated) { 253 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 254 return in_value; 255 } 256 257 @Override RepeatIntEnumArray(int[] in_value, int[] repeated)258 public int[] RepeatIntEnumArray(int[] in_value, int[] repeated) { 259 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 260 return in_value; 261 } 262 263 @Override RepeatLongEnumArray(long[] in_value, long[] repeated)264 public long[] RepeatLongEnumArray(long[] in_value, long[] repeated) { 265 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 266 return in_value; 267 } 268 269 @Override RepeatStringArray(String[] in_value, String[] repeated)270 public String[] RepeatStringArray(String[] in_value, String[] repeated) { 271 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 272 return in_value; 273 } 274 275 @Override RepeatRegularPolygonArray(RegularPolygon[] in_value, RegularPolygon[] repeated)276 public RegularPolygon[] RepeatRegularPolygonArray(RegularPolygon[] in_value, RegularPolygon[] repeated) { 277 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 278 return in_value; 279 } 280 281 @Override RepeatFdArray(ParcelFileDescriptor[] in_value, ParcelFileDescriptor[] repeated)282 public ParcelFileDescriptor[] RepeatFdArray(ParcelFileDescriptor[] in_value, ParcelFileDescriptor[] repeated) { 283 ParcelFileDescriptor[] out = new ParcelFileDescriptor[in_value.length]; 284 for (int i = 0; i < in_value.length; i++) { 285 try { 286 repeated[i] = in_value[i].dup(); 287 out[i] = in_value[i].dup(); 288 } catch (IOException e) { 289 e.printStackTrace(); 290 } 291 } 292 return out; 293 } 294 Repeat2StringList(java.util.List<String> in_value, java.util.List<String> repeated)295 public java.util.List<String> Repeat2StringList(java.util.List<String> in_value, java.util.List<String> repeated) { 296 repeated.addAll(in_value); 297 repeated.addAll(in_value); 298 return repeated; 299 } 300 301 @Override Repeat2RegularPolygonList(java.util.List<RegularPolygon> in_value, java.util.List<RegularPolygon> repeated)302 public java.util.List<RegularPolygon> Repeat2RegularPolygonList(java.util.List<RegularPolygon> in_value, java.util.List<RegularPolygon> repeated) { 303 repeated.addAll(in_value); 304 repeated.addAll(in_value); 305 return repeated; 306 } 307 308 @Override RepeatNullableBooleanArray(boolean[] in_value)309 public boolean[] RepeatNullableBooleanArray(boolean[] in_value) { 310 return in_value; 311 } 312 313 @Override RepeatNullableByteArray(byte[] in_value)314 public byte[] RepeatNullableByteArray(byte[] in_value) { 315 return in_value; 316 } 317 318 @Override RepeatNullableCharArray(char[] in_value)319 public char[] RepeatNullableCharArray(char[] in_value) { 320 return in_value; 321 } 322 323 @Override RepeatNullableIntArray(int[] in_value)324 public int[] RepeatNullableIntArray(int[] in_value) { 325 return in_value; 326 } 327 328 @Override RepeatNullableLongArray(long[] in_value)329 public long[] RepeatNullableLongArray(long[] in_value) { 330 return in_value; 331 } 332 333 @Override RepeatNullableFloatArray(float[] in_value)334 public float[] RepeatNullableFloatArray(float[] in_value) { 335 return in_value; 336 } 337 338 @Override RepeatNullableDoubleArray(double[] in_value)339 public double[] RepeatNullableDoubleArray(double[] in_value) { 340 return in_value; 341 } 342 343 @Override RepeatNullableByteEnumArray(byte[] in_value)344 public byte[] RepeatNullableByteEnumArray(byte[] in_value) { 345 return in_value; 346 } 347 348 @Override RepeatNullableIntEnumArray(int[] in_value)349 public int[] RepeatNullableIntEnumArray(int[] in_value) { 350 return in_value; 351 } 352 353 @Override RepeatNullableLongEnumArray(long[] in_value)354 public long[] RepeatNullableLongEnumArray(long[] in_value) { 355 return in_value; 356 } 357 358 @Override RepeatNullableStringArray(String[] in_value)359 public String[] RepeatNullableStringArray(String[] in_value) { 360 return in_value; 361 } 362 363 @Override DoubleRepeatNullableStringArray(String[] in_value, String[] repeated)364 public String[] DoubleRepeatNullableStringArray(String[] in_value, String[] repeated) { 365 if (in_value == null) { 366 return null; // can't do anything to repeated 367 } 368 369 System.arraycopy(in_value, 0, repeated, 0, in_value.length); 370 return in_value; 371 } 372 373 @Override repeatFoo(Foo inFoo)374 public Foo repeatFoo(Foo inFoo) { 375 return inFoo; 376 } 377 378 @Override renameFoo(Foo foo, String name)379 public void renameFoo(Foo foo, String name) { 380 foo.a = name; 381 } 382 383 @Override renameBar(Foo foo, String name)384 public void renameBar(Foo foo, String name) { 385 if (foo.d == null) { 386 foo.d = new Bar(); 387 } 388 foo.d.a = name; 389 } 390 391 @Override getF(Foo foo)392 public int getF(Foo foo) { 393 return foo.f; 394 } 395 396 @Override RepeatStringNullableLater(String in_value)397 public String RepeatStringNullableLater(String in_value) { 398 return in_value; 399 } 400 401 @Override NewMethodThatReturns10()402 public int NewMethodThatReturns10() { 403 return 10; 404 } 405 } 406