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