summaryrefslogtreecommitdiffstats
path: root/tests/java_app/src/android/aidl/tests/TestServiceClient.java
blob: 4f4d46251cfd5585de0ebe21fea7d2d004d9170e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
 * Copyright (C) 2015, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.aidl.tests;

import android.aidl.tests.SimpleParcelable;
import android.aidl.tests.TestFailException;
import android.aidl.tests.TestLogger;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.ServiceSpecificException;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

// Generated
import android.aidl.tests.INamedCallback;
import android.aidl.tests.ITestService;

public class TestServiceClient extends Activity {
    private static final String TAG = "TestServiceClient";

    private TestLogger mLog;
    private String mSuccessSentinel;
    private String mFailureSentinel;

    private void init() {
        Intent intent = getIntent();
        mLog = new TestLogger(this);
        mLog.log("Reading sentinels from intent...");
        mSuccessSentinel = intent.getStringExtra("sentinel.success");
        mFailureSentinel = intent.getStringExtra("sentinel.failure");
        if (mSuccessSentinel == null || mFailureSentinel == null) {
            String message = "Failed to read intent extra input.";
            Log.e(TAG, message);
            mLog.close();
            throw new RuntimeException(message);
        }
    }

    private ITestService getService() throws TestFailException {
        IBinder service = new ServiceManager().getService(
                ITestService.class.getName());
        if (service == null) {
            mLog.logAndThrow("Failed to obtain binder...");
        }
        ITestService ret = ITestService.Stub.asInterface(service);
        if (ret == null) {
            mLog.logAndThrow("Failed to cast IBinder instance.");
        }
        return ret;
    }

    private void checkPrimitiveRepeat(ITestService service)
            throws TestFailException {
        mLog.log("Checking that service can repeat primitives back...");
        try {
            {
                boolean query = true;
                boolean response = service.RepeatBoolean(query);
                if (query != response) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }
            {
                char query = 'A';
                char response = service.RepeatChar(query);
                if (query != response) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }
            {
                byte query = -128;
                byte response = service.RepeatByte(query);
                if (query != response) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }
            {
                int query = 1 << 30;
                int response = service.RepeatInt(query);
                if (query != response) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }
            {
                int query[] = {ITestService.TEST_CONSTANT,
                               ITestService.TEST_CONSTANT2,
                               ITestService.TEST_CONSTANT3,
                               ITestService.TEST_CONSTANT4,
                               ITestService.TEST_CONSTANT5,
                               ITestService.TEST_CONSTANT6,
                               ITestService.TEST_CONSTANT7,
                               ITestService.TEST_CONSTANT8};
                for (int i = 0; i < query.length; i++) {
                    int response = service.RepeatInt(query[i]);
                    if (query[i] != response) {
                        mLog.logAndThrow("Repeat with " + query[i] +
                                " responded " + response);
                    }
                }
            }
            {
                long query = 1L << 60;
                long response = service.RepeatLong(query);
                if (query != response) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }
            {
                float query = 1.0f/3.0f;
                float response = service.RepeatFloat(query);
                if (query != response) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }
            {
                double query = 1.0/3.0;
                double response = service.RepeatDouble(query);
                if (query != response) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }
            {
                Map<String, Object> query = new HashMap<String, Object>();
                query.put("first_val", new Byte((byte)-128));
                query.put("second_val", new Integer(1<<30));
                query.put("third_val", "OHAI");
                Object response = service.RepeatMap(query);
                if (!query.equals(response)) {
                    mLog.logAndThrow("Repeat with " + query +
                                     " responded " + response);
                }
            }

            List<String> queries = Arrays.asList(
                "not empty", "", "\0",
                ITestService.STRING_TEST_CONSTANT,
                ITestService.STRING_TEST_CONSTANT2);
            for (String query : queries) {
                String response = service.RepeatString(query);
                if (!query.equals(response)) {
                    mLog.logAndThrow("Repeat request with '" + query + "'" +
                                     " of length " + query.length() +
                                     " responded with '" + response + "'" +
                                     " of length " + response.length());
                }
            }
        } catch (RemoteException ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Service failed to repeat a primitive back.");
        }
        mLog.log("...Basic primitive repeating works.");
    }

    private void checkArrayReversal(ITestService service)
            throws TestFailException {
        mLog.log("Checking that service can reverse and return arrays...");
        try {
            {
                boolean[] input = {true, false, false, false};
                boolean echoed[] = new boolean[input.length];
                boolean[] reversed = service.ReverseBoolean(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (input[i] != reversed[j]) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
            {
                byte[] input = {0, 1, 2};
                byte echoed[] = new byte[input.length];
                byte[] reversed = service.ReverseByte(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (input[i] != reversed[j]) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
            {
                char[] input = {'A', 'B', 'C', 'D', 'E'};
                char echoed[] = new char[input.length];
                char[] reversed = service.ReverseChar(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (input[i] != reversed[j]) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
            {
                int[] input = {-1, 0, 1, 2, 3, 4, 5, 6};
                int echoed[] = new int[input.length];
                int[] reversed = service.ReverseInt(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (input[i] != reversed[j]) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
            {
                long[] input = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8};
                long echoed[] = new long[input.length];
                long[] reversed = service.ReverseLong(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (input[i] != reversed[j]) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
            {
                float[] input = {0.0f, 1.0f, -0.3f};
                float echoed[] = new float[input.length];
                float[] reversed = service.ReverseFloat(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (input[i] != reversed[j]) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
            {
                double[] input = {-1.0, -4.0, -2.0};
                double echoed[] = new double[input.length];
                double[] reversed = service.ReverseDouble(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (input[i] != reversed[j]) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
            {
                String[] input = {"For", "relaxing", "times"};
                String echoed[] = new String[input.length];
                String[] reversed = service.ReverseString(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (!input[i].equals(reversed[j])) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
        } catch (RemoteException ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Service failed to reverse an array.");
        }
        mLog.log("...service can reverse and return arrays.");
    }

    private void checkBinderExchange(
                ITestService service) throws TestFailException {
      mLog.log("Checking exchange of binders...");
      try {
          INamedCallback got = service.GetOtherTestService("Smythe");
          mLog.log("Received test service");
          String name = got.GetName();

          if (!name.equals("Smythe")) {
              mLog.logAndThrow("Tried to get service with name 'Smythe'" +
                               " and found service with name '" + name + "'");
          }

          if (!service.VerifyName(got, "Smythe")) {
              mLog.logAndThrow("Test service could not verify name of 'Smythe'");
          }
      } catch (RemoteException ex) {
          mLog.log(ex.toString());
          mLog.logAndThrow("Service failed to exchange binders.");
      }
      mLog.log("...Exchange of binders works");
    }

    private void checkListReversal(ITestService service)
            throws TestFailException {
        mLog.log("Checking that service can reverse and return lists...");
        try {
            {
                List<String> input = Arrays.asList("Walk", "into", "Córdoba");
                List<String> echoed = new ArrayList<String>();
                List<String> reversed = service.ReverseStringList(input, echoed);
                if (!input.equals(echoed)) {
                    mLog.logAndThrow("Failed to echo input List<String> back.");
                }
                Collections.reverse(input);
                if (!input.equals(reversed)) {
                    mLog.logAndThrow("Reversed list is not correct.");
                }
            }
        } catch (RemoteException ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Service failed to reverse an List<String>.");
        }
        mLog.log("...service can reverse and return lists.");
    }

    private void checkSimpleParcelables(ITestService service)
            throws TestFailException {
        mLog.log("Checking that service can repeat and reverse SimpleParcelable objects...");
        try {
            {
                SimpleParcelable input = new SimpleParcelable("foo", 42);
                SimpleParcelable out_param = new SimpleParcelable();
                SimpleParcelable returned =
                        service.RepeatSimpleParcelable(input, out_param);
                if (!input.equals(out_param)) {
                    mLog.log(input.toString() + " != " + out_param.toString());
                    mLog.logAndThrow("out param SimpleParcelable was not equivalent");
                }
                if (!input.equals(returned)) {
                    mLog.log(input.toString() + " != " + returned.toString());
                    mLog.logAndThrow("returned SimpleParcelable was not equivalent");
                }
            }
            {
                SimpleParcelable[] input = new SimpleParcelable[3];
                input[0] = new SimpleParcelable("a", 1);
                input[1] = new SimpleParcelable("b", 2);
                input[2] = new SimpleParcelable("c", 3);
                SimpleParcelable[] repeated = new SimpleParcelable[3];
                SimpleParcelable[] reversed = service.ReverseSimpleParcelables(
                        input, repeated);
                if (!Arrays.equals(input, repeated)) {
                    mLog.logAndThrow(
                            "Repeated list of SimpleParcelable objects did not match.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow(
                            "Reversed list of SimpleParcelable objects had wrong length.");
                }
                for (int i = 0, k = input.length - 1;
                     i < input.length;
                     ++i, --k) {
                    if (!input[i].equals(reversed[k])) {
                        mLog.log(input[i].toString() + " != " +
                                 reversed[k].toString());
                        mLog.logAndThrow("reversed SimpleParcelable was not equivalent");
                    }
                }
            }
        } catch (Exception ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Service failed to handle SimpleParcelable objects.");
        }
        mLog.log("...service can manipulate SimpleParcelable objects.");
    }

    private void checkPersistableBundles(ITestService service)
            throws TestFailException {
        mLog.log("Checking that service can repeat and reverse PersistableBundle objects...");
        try {
            {
                PersistableBundle emptyBundle = new PersistableBundle();
                PersistableBundle returned = service.RepeatPersistableBundle(emptyBundle);
                if (emptyBundle.size() != 0 || returned.size() != 0) {
                    mLog.log(emptyBundle.toString() + " != " + returned.toString());
                    mLog.logAndThrow("returned empty PersistableBundle object was not equivalent");
                }
                mLog.log("...service can repeat and reverse empty PersistableBundle objects...");
            }
            {
                final String testBoolKey = new String("testBool");
                final String testIntKey = new String("testInt");
                final String testNestedIntKey = new String("testNestedInt");
                final String testLongKey = new String("testLong");
                final String testDoubleKey = new String("testDouble");
                final String testStringKey = new String("testString");
                final String testBoolArrayKey = new String("testBoolArray");
                final String testIntArrayKey = new String("testIntArray");
                final String testLongArrayKey = new String("testLongArray");
                final String testDoubleArrayKey = new String("testDoubleArray");
                final String testStringArrayKey = new String("testStringArray");
                final String testPersistableBundleKey = new String("testPersistableBundle");
                PersistableBundle nonEmptyBundle = new PersistableBundle();
                nonEmptyBundle.putBoolean(testBoolKey, false);
                nonEmptyBundle.putInt(testIntKey, 33);
                nonEmptyBundle.putLong(testLongKey, 34359738368L);
                nonEmptyBundle.putDouble(testDoubleKey, 1.1);
                nonEmptyBundle.putString(testStringKey, new String("Woot!"));
                nonEmptyBundle.putBooleanArray(testBoolArrayKey, new boolean[] {true, false, true});
                nonEmptyBundle.putIntArray(testIntArrayKey, new int[] {33, 44, 55, 142});
                nonEmptyBundle.putLongArray(
                    testLongArrayKey, new long[] {34L, 8371L, 34359738375L});
                nonEmptyBundle.putDoubleArray(testDoubleArrayKey, new double[] {2.2, 5.4});
                nonEmptyBundle.putStringArray(testStringArrayKey, new String[] {"hello", "world!"});
                PersistableBundle testNestedPersistableBundle = new PersistableBundle();
                testNestedPersistableBundle.putInt(testNestedIntKey, 345);
                nonEmptyBundle.putPersistableBundle(
                    testPersistableBundleKey, testNestedPersistableBundle);
                PersistableBundle returned = service.RepeatPersistableBundle(nonEmptyBundle);
                if (returned.size() != nonEmptyBundle.size()
                    || returned.getBoolean(testBoolKey) != nonEmptyBundle.getBoolean(testBoolKey)
                    || returned.getInt(testIntKey) != nonEmptyBundle.getInt(testIntKey)
                    || returned.getLong(testLongKey) != nonEmptyBundle.getLong(testLongKey)
                    || returned.getDouble(testDoubleKey) != nonEmptyBundle.getDouble(testDoubleKey)
                    || !returned.getString(testStringKey)
                                .equals(nonEmptyBundle.getString(testStringKey))
                    || !Arrays.equals(nonEmptyBundle.getBooleanArray(testBoolArrayKey),
                                      returned.getBooleanArray(testBoolArrayKey))
                    || !Arrays.equals(nonEmptyBundle.getIntArray(testIntArrayKey),
                                      returned.getIntArray(testIntArrayKey))
                    || !Arrays.equals(nonEmptyBundle.getLongArray(testLongArrayKey),
                                      returned.getLongArray(testLongArrayKey))
                    || !Arrays.equals(nonEmptyBundle.getDoubleArray(testDoubleArrayKey),
                                      returned.getDoubleArray(testDoubleArrayKey))
                    || !Arrays.equals(nonEmptyBundle.getStringArray(testStringArrayKey),
                                      returned.getStringArray(testStringArrayKey))) {
                    PersistableBundle temp =
                        returned.getPersistableBundle(testPersistableBundleKey);
                    if (temp == null
                        || temp.getInt(testNestedIntKey)
                            != testNestedPersistableBundle.getInt(testNestedIntKey)) {
                        mLog.log(nonEmptyBundle.toString() + " != " + returned.toString());
                        mLog.logAndThrow("returned non-empty PersistableBundle " +
                                         "object was not equivalent");
                    }
                }
                mLog.log("...service can repeat and reverse non-empty " +
                         "PersistableBundle objects...");
            }
            {
                PersistableBundle[] input = new PersistableBundle[3];
                PersistableBundle first = new PersistableBundle();
                PersistableBundle second = new PersistableBundle();
                PersistableBundle third = new PersistableBundle();
                final String testIntKey = new String("testInt");
                final String testLongKey = new String("testLong");
                final String testDoubleKey = new String("testDouble");
                first.putInt(testIntKey, 1231);
                second.putLong(testLongKey, 222222L);
                third.putDouble(testDoubleKey, 10.8);
                input[0] = first;
                input[1] = second;
                input[2] = third;
                final int original_input_size = input.length;
                PersistableBundle[] repeated = new PersistableBundle[input.length];
                PersistableBundle[] reversed = service.ReversePersistableBundles(input, repeated);
                if (input.length != repeated.length || input.length != original_input_size) {
                    mLog.logAndThrow("Repeated list of PersistableBundle objects had " +
                                     "wrong length.");
                }
                if (input[0].getInt(testIntKey) != repeated[0].getInt(testIntKey)
                    || input[1].getLong(testLongKey) != repeated[1].getLong(testLongKey)
                    || input[2].getDouble(testDoubleKey) != repeated[2].getDouble(testDoubleKey)) {
                    mLog.logAndThrow("Repeated list of PersistableBundle objects did not match.");
                }
                if (input.length != reversed.length || input.length != original_input_size) {
                    mLog.logAndThrow("Reversed list of PersistableBundle objects had " +
                                     "wrong length.");
                }
                if (input[0].getInt(testIntKey) != reversed[2].getInt(testIntKey)
                    || input[1].getLong(testLongKey) != reversed[1].getLong(testLongKey)
                    || input[2].getDouble(testDoubleKey) != reversed[0].getDouble(testDoubleKey)) {
                    mLog.logAndThrow("reversed PersistableBundle objects were not equivalent");
                }
                mLog.log("...service can repeat and reverse arrays of " +
                         "non-empty PersistableBundle objects...");
            }
        } catch (Exception ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Service failed to handle PersistableBundle objects.");
        }
        mLog.log("...service can manipulate PersistableBundle objects.");
    }

    private void checkFileDescriptorPassing(ITestService service)
            throws TestFailException {
        mLog.log("Checking that service can receive and return file descriptors...");
        try {
            FileOutputStream fileOutputStream =
                    openFileOutput("test-dummy", Context.MODE_PRIVATE);

            FileDescriptor descriptor = fileOutputStream.getFD();
            FileDescriptor journeyed = service.RepeatFileDescriptor(descriptor);
            fileOutputStream.close();

            FileOutputStream journeyedStream = new FileOutputStream(journeyed);

            String testData = "FrazzleSnazzleFlimFlamFlibbityGumboChops";
            byte[] output = testData.getBytes();
            journeyedStream.write(output);
            journeyedStream.close();

            FileInputStream fileInputStream = openFileInput("test-dummy");
            byte[] input = new byte[output.length];
            if (fileInputStream.read(input) != input.length) {
                mLog.logAndThrow("Read short count from file");
            }

            if (!Arrays.equals(input, output)) {
                mLog.logAndThrow("Read incorrect data");
            }
        } catch (RemoteException ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Service failed to repeat a file descriptor.");
        } catch (IOException ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Exception while operating on temporary file");
        }
        mLog.log("...service can receive and return file descriptors.");
    }

    private void checkServiceSpecificExceptions(
                ITestService service) throws TestFailException {
        mLog.log("Checking application exceptions...");
        for (int i = -1; i < 2; ++i) {
            try {
                service.ThrowServiceException(i);
            } catch (RemoteException ex) {
                mLog.logAndThrow("Service threw RemoteException: " +
                                 ex.toString());
            } catch (ServiceSpecificException ex) {
                if (ex.errorCode != i) {
                    mLog.logAndThrow("Service threw wrong error code: " + i);
                }
            }
        }
        mLog.log("...application exceptions work");
    }

    private void checkUtf8Strings(ITestService service)
            throws TestFailException {
        mLog.log("Checking that service can work with UTF8 strings...");
        // Note that Java's underlying encoding is UTF16.
        final List<String> utf8_queries = Arrays.asList(
              "typical string",
              "",
              "\0\0\0",
              // Java doesn't handle unicode code points above U+FFFF well.
              new String(Character.toChars(0x1F701)) + "\u03A9");
        try {
            for (String query : utf8_queries) {
                String response = service.RepeatUtf8CppString(query);
                if (!query.equals(response)) {
                    mLog.logAndThrow("Repeat request with '" + query + "'" +
                                     " of length " + query.length() +
                                     " responded with '" + response + "'" +
                                     " of length " + response.length());
                }
            }
            {
                String[] input = (String[])utf8_queries.toArray();
                String echoed[] = new String[input.length];
                String[] reversed = service.ReverseUtf8CppString(input, echoed);
                if (!Arrays.equals(input, echoed)) {
                    mLog.logAndThrow("Failed to echo utf8 input array back.");
                }
                if (input.length != reversed.length) {
                    mLog.logAndThrow("Reversed utf8 array is the wrong size.");
                }
                for (int i = 0; i < input.length; ++i) {
                    int j = reversed.length - (1 + i);
                    if (!input[i].equals(reversed[j])) {
                        mLog.logAndThrow(
                                "input[" + i + "] = " + input[i] +
                                " but reversed value = " + reversed[j]);
                    }
                }
            }
        } catch (RemoteException ex) {
            mLog.log(ex.toString());
            mLog.logAndThrow("Service failed to handle utf8 strings.");
        }
        mLog.log("...UTF8 annotations work.");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "Starting!");
        try {
          init();
          ITestService service = getService();
          checkPrimitiveRepeat(service);
          checkArrayReversal(service);
          checkBinderExchange(service);
          checkListReversal(service);
          checkSimpleParcelables(service);
          checkPersistableBundles(service);
          checkFileDescriptorPassing(service);
          checkServiceSpecificExceptions(service);
          checkUtf8Strings(service);
          new NullableTests(service, mLog).runTests();

          mLog.log(mSuccessSentinel);
        } catch (TestFailException e) {
            mLog.log(mFailureSentinel);
            throw new RuntimeException(e);
        } finally {
            if (mLog != null) {
                mLog.close();
            }
        }
    }
}