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 android.aidl.tests;
18 
19 import android.aidl.tests.TestFailException;
20 import android.os.IBinder;
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 class NullableTests {
25     private static final String TAG = "TestServiceClient";
26     private ITestService mService;
27     private TestLogger mLog;
28 
NullableTests(ITestService service, TestLogger logger)29     public NullableTests(ITestService service, TestLogger logger) {
30         mService = service;
31         mLog = logger;
32     }
33 
checkNullHandling()34     public void checkNullHandling() throws TestFailException {
35         mLog.log("Checking that sending null strings reports an error...");
36         try {
37             String response = mService.RepeatString(null);
38             mLog.logAndThrow("Expected to fail on null string input!");
39         } catch (NullPointerException ex) {
40             mLog.log("Caught an exception on null string parameter (expected)");
41             mLog.log("null strings behave as expected");
42             return;
43         } catch (Exception ex) {
44             mLog.logAndThrow("Expected to receive NullPointerException on " +
45                              "null parameter, but got " + ex.toString());
46         }
47         mLog.logAndThrow("Expected to receive NullPointerException on " +
48                          "null parameter, but nothing was thrown??");
49     }
50 
checkNullBinderDetection()51     public void checkNullBinderDetection() throws TestFailException {
52         mLog.log("Checking that service handles @nullable IBinder...");
53         try {
54             mService.TakesAnIBinder(null);
55             mLog.logAndThrow("Expected to fail on null Binder!");
56         } catch (NullPointerException ex) {
57             mLog.log("Caught an exception on null Binder parameter (expected)");
58             return;
59         } catch (Exception ex) {
60             mLog.logAndThrow("Expected to receive NullPointerException," +
61                              "but got " + ex.toString());
62         }
63         mLog.logAndThrow("Expected to receive NullPointerException on " +
64                          "null parameter, but nothing was thrown??");
65     }
66 
checkNullBinderInListDetection()67     public void checkNullBinderInListDetection() throws TestFailException {
68         List<IBinder> listWithNulls = new ArrayList<IBinder>();
69         listWithNulls.add(null);
70         try {
71             mService.TakesAnIBinderList(listWithNulls);
72             mLog.logAndThrow("Expected to fail on list with null Binder!");
73         } catch (NullPointerException ex) {
74             mLog.log("Caught an exception on list with null Binder (expected)");
75             return;
76         } catch (Exception ex) {
77             mLog.logAndThrow("Expected to receive NullPointerException," +
78                              "but got " + ex.toString());
79         }
80         mLog.logAndThrow("Expected to receive NullPointerException on " +
81                          "null parameter, but nothing was thrown??");
82     }
83 
checkNullInterfaceHandling()84     public void checkNullInterfaceHandling() throws TestFailException {
85         mLog.log("Checking @nullable IInterface handling...");
86         try {
87             INamedCallback callback  = mService.GetCallback(false);
88             if (callback == null) {
89                 mLog.logAndThrow("Expected to get non-null INamedCallback.");
90             }
91             callback  = mService.GetCallback(true);
92             if (callback != null) {
93                 mLog.logAndThrow("Expected to get null INamedCallback.");
94             }
95         } catch (Exception ex) {
96             mLog.logAndThrow("Unexpected exception during @nullable IInterface test: " +
97                              ex.toString());
98         }
99         mLog.log("@nullable IInterface handling works as expected.");
100     }
101 
runTests()102     public void runTests() throws TestFailException {
103         checkNullHandling();
104         checkNullBinderDetection();
105         checkNullBinderInListDetection();
106         checkNullInterfaceHandling();
107     }
108 }
109