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.os;
18 
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 
23 import android.platform.test.annotations.IgnoreUnderRavenwood;
24 import android.platform.test.ravenwood.RavenwoodRule;
25 import android.util.ArrayMap;
26 
27 import androidx.test.filters.SmallTest;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 
39 @SmallTest
40 @RunWith(AndroidJUnit4.class)
41 public final class ParcelNullabilityTest {
42     @Rule
43     public final RavenwoodRule mRavenwood = new RavenwoodRule();
44 
45     @Test
nullByteArray()46     public void nullByteArray() {
47         Parcel p = Parcel.obtain();
48         p.writeByteArray(null);
49 
50         assertNull(throughBytes(p).createByteArray());
51     }
52 
53     @Test
nullBlob()54     public void nullBlob() {
55         Parcel p = Parcel.obtain();
56         p.writeBlob(null);
57 
58         assertNull(throughBytes(p).readBlob());
59     }
60 
61     @Test
nullString()62     public void nullString() {
63         Parcel p = Parcel.obtain();
64         p.writeString(null);
65 
66         assertNull(throughBytes(p).readString());
67     }
68 
69     @Test
70     @IgnoreUnderRavenwood(blockedBy = Parcel.class)
nullCharSequence()71     public void nullCharSequence() {
72         Parcel p = Parcel.obtain();
73         p.writeCharSequence(null);
74 
75         assertNull(throughBytes(p).readCharSequence());
76     }
77 
78     @Test
79     @IgnoreUnderRavenwood(blockedBy = Parcel.class)
nullStrongBinder()80     public void nullStrongBinder() {
81         Parcel p = Parcel.obtain();
82         p.writeStrongBinder(null);
83 
84         assertNull(throughBytes(p).readStrongBinder());
85     }
86 
87     @Test
88     @IgnoreUnderRavenwood(blockedBy = Parcel.class)
nullStringInterface()89     public void nullStringInterface() {
90         Parcel p = Parcel.obtain();
91         p.writeStrongInterface(null);
92 
93         assertNull(throughBytes(p).readStrongBinder());
94     }
95 
96     @Test
nullFileDescriptor()97     public void nullFileDescriptor() {
98         Parcel p = Parcel.obtain();
99         try {
100             p.writeFileDescriptor(null);
101             fail();
102         } catch (RuntimeException expected) {
103         }
104     }
105 
106     @Test
nullRawFileDescriptor()107     public void nullRawFileDescriptor() {
108         Parcel p = Parcel.obtain();
109         try {
110             p.writeRawFileDescriptor(null);
111             fail();
112         } catch (RuntimeException expected) {
113         }
114     }
115 
116     @Test
nullRawFileDescriptorArray()117     public void nullRawFileDescriptorArray() {
118         Parcel p = Parcel.obtain();
119         p.writeRawFileDescriptorArray(null);
120 
121         assertNull(throughBytes(p).createRawFileDescriptorArray());
122     }
123 
124     @Test
nullMap()125     public void nullMap() {
126         Parcel p = Parcel.obtain();
127         p.writeMap(null);
128 
129         Map<Object, Object> map = new HashMap<>();
130         throughBytes(p).readMap(map, null);
131         assertTrue(map.isEmpty());
132     }
133 
134     @Test
nullArrayMap()135     public void nullArrayMap() {
136         Parcel p = Parcel.obtain();
137         p.writeArrayMap(null);
138 
139         ArrayMap<Object, Object> map = new ArrayMap<>();
140         throughBytes(p).readArrayMap(map, null);
141         assertTrue(map.isEmpty());
142     }
143 
144     @Test
nullArraySet()145     public void nullArraySet() {
146         Parcel p = Parcel.obtain();
147         p.writeArraySet(null);
148 
149         assertNull(throughBytes(p).readArraySet(null));
150     }
151 
152     @Test
nullBundle()153     public void nullBundle() {
154         Parcel p = Parcel.obtain();
155         p.writeBundle(null);
156 
157         assertNull(throughBytes(p).readBundle());
158     }
159 
160     @Test
nullPersistableBundle()161     public void nullPersistableBundle() {
162         Parcel p = Parcel.obtain();
163         p.writePersistableBundle(null);
164 
165         assertNull(throughBytes(p).readPersistableBundle());
166     }
167 
168     @Test
nullSize()169     public void nullSize() {
170         Parcel p = Parcel.obtain();
171         try {
172             p.writeSize(null);
173             fail();
174         } catch (RuntimeException expected) {
175         }
176     }
177 
178     @Test
nullSizeF()179     public void nullSizeF() {
180         Parcel p = Parcel.obtain();
181         try {
182             p.writeSizeF(null);
183             fail();
184         } catch (RuntimeException expected) {
185         }
186     }
187 
188     @Test
nullList()189     public void nullList() {
190         Parcel p = Parcel.obtain();
191         p.writeList(null);
192 
193         List<Object> map = new ArrayList<>();
194         throughBytes(p).readList(map, null);
195         assertTrue(map.isEmpty());
196     }
197 
198     @Test
nullArray()199     public void nullArray() {
200         Parcel p = Parcel.obtain();
201         p.writeArray(null);
202 
203         assertNull(throughBytes(p).readArray(null));
204     }
205 
206     @Test
nullSparseArray()207     public void nullSparseArray() {
208         Parcel p = Parcel.obtain();
209         p.writeSparseArray(null);
210 
211         assertNull(throughBytes(p).readSparseArray(null));
212     }
213 
214     @Test
nullSparseBooleanArray()215     public void nullSparseBooleanArray() {
216         Parcel p = Parcel.obtain();
217         p.writeSparseBooleanArray(null);
218 
219         assertNull(throughBytes(p).readSparseBooleanArray());
220     }
221 
222     @Test
nullSparseIntArray()223     public void nullSparseIntArray() {
224         Parcel p = Parcel.obtain();
225         p.writeSparseIntArray(null);
226 
227         assertNull(throughBytes(p).readSparseIntArray());
228     }
229 
230     @Test
nullBooleanArray()231     public void nullBooleanArray() {
232         Parcel p = Parcel.obtain();
233         p.writeBooleanArray(null);
234 
235         assertNull(throughBytes(p).createBooleanArray());
236     }
237 
238     @Test
nullCharArray()239     public void nullCharArray() {
240         Parcel p = Parcel.obtain();
241         p.writeCharArray(null);
242 
243         assertNull(throughBytes(p).createCharArray());
244     }
245 
246     @Test
nullIntArray()247     public void nullIntArray() {
248         Parcel p = Parcel.obtain();
249         p.writeIntArray(null);
250 
251         assertNull(throughBytes(p).createIntArray());
252     }
253 
254     @Test
nullLongArray()255     public void nullLongArray() {
256         Parcel p = Parcel.obtain();
257         p.writeLongArray(null);
258 
259         assertNull(throughBytes(p).createLongArray());
260     }
261 
262     @Test
nullFloatArray()263     public void nullFloatArray() {
264         Parcel p = Parcel.obtain();
265         p.writeFloatArray(null);
266 
267         assertNull(throughBytes(p).createFloatArray());
268     }
269 
270     @Test
nullDoubleArray()271     public void nullDoubleArray() {
272         Parcel p = Parcel.obtain();
273         p.writeDoubleArray(null);
274 
275         assertNull(throughBytes(p).createDoubleArray());
276     }
277 
278     @Test
nullStringArray()279     public void nullStringArray() {
280         Parcel p = Parcel.obtain();
281         p.writeStringArray(null);
282 
283         assertNull(throughBytes(p).createStringArray());
284     }
285 
286     @Test
nullCharSequenceArray()287     public void nullCharSequenceArray() {
288         Parcel p = Parcel.obtain();
289         p.writeCharSequenceArray(null);
290 
291         assertNull(throughBytes(p).readCharSequenceArray());
292     }
293 
294     @Test
nullCharSequenceList()295     public void nullCharSequenceList() {
296         Parcel p = Parcel.obtain();
297         p.writeCharSequenceList(null);
298 
299         assertNull(throughBytes(p).readCharSequenceList());
300     }
301 
302     @Test
nullBinderArray()303     public void nullBinderArray() {
304         Parcel p = Parcel.obtain();
305         p.writeBinderArray(null);
306 
307         assertNull(throughBytes(p).createBinderArray());
308     }
309 
310     @Test
nullTypedList()311     public void nullTypedList() {
312         Parcel p = Parcel.obtain();
313         p.writeTypedList(null);
314 
315         assertNull(throughBytes(p).createTypedArrayList(null));
316     }
317 
318     @Test
nullStringList()319     public void nullStringList() {
320         Parcel p = Parcel.obtain();
321         p.writeStringList(null);
322 
323         assertNull(throughBytes(p).createStringArrayList());
324     }
325 
326     @Test
nullBinderList()327     public void nullBinderList() {
328         Parcel p = Parcel.obtain();
329         p.writeBinderList(null);
330 
331         assertNull(throughBytes(p).createBinderArrayList());
332     }
333 
334     @Test
nullParcelableList()335     public void nullParcelableList() {
336         Parcel p = Parcel.obtain();
337         p.writeParcelableList(null, 0);
338 
339         List<Parcelable> list = new ArrayList<>();
340         throughBytes(p).readParcelableList(list, null);
341         assertTrue(list.isEmpty());
342     }
343 
344     @Test
nullTypedArray()345     public void nullTypedArray() {
346         Parcel p = Parcel.obtain();
347         p.writeTypedArray(null, 0);
348 
349         assertNull(throughBytes(p).createTypedArray(null));
350     }
351 
352     @Test
nullTypedObject()353     public void nullTypedObject() {
354         Parcel p = Parcel.obtain();
355         p.writeTypedObject(null, 0);
356 
357         assertNull(throughBytes(p).readTypedObject(null));
358     }
359 
360     @Test
nullValue()361     public void nullValue() {
362         Parcel p = Parcel.obtain();
363         p.writeValue(null);
364 
365         assertNull(throughBytes(p).readValue(null));
366     }
367 
368     @Test
nullParcelable()369     public void nullParcelable() {
370         Parcel p = Parcel.obtain();
371         p.writeParcelable(null, 0);
372 
373         assertNull(throughBytes(p).readParcelable(null));
374     }
375 
376     @Test
nullSerializable()377     public void nullSerializable() {
378         Parcel p = Parcel.obtain();
379         p.writeSerializable(null);
380 
381         assertNull(throughBytes(p).readSerializable());
382     }
383 
384     @Test
nullException()385     public void nullException() {
386         Parcel p = Parcel.obtain();
387         try {
388             p.writeException(null);
389             fail();
390         } catch (RuntimeException expected) {
391         }
392     }
393 
throughBytes(Parcel p)394     private static Parcel throughBytes(Parcel p) {
395         byte[] bytes = p.marshall();
396         p = Parcel.obtain();
397         p.unmarshall(bytes, 0, bytes.length);
398         p.setDataPosition(0);
399         return p;
400     }
401 }
402