From 12cbe63bfd2700a11b582622e4756ec2984921d1 Mon Sep 17 00:00:00 2001 From: Dan Sandler Date: Wed, 31 Mar 2021 22:59:29 -0400 Subject: [PATCH] Defend against null intents in CameraIntents. Kotlin makes this easy, but not easy enough for me to have done it the first time. Bug: 184185977 Test: atest SystemUITests; manual (see bug repro) Change-Id: Iec007bd9f47005bd3a2642ebcdc717fdb5c90b1c --- .../android/systemui/camera/CameraIntents.kt | 14 ++--- .../systemui/camera/CameraIntentsTest.kt | 56 +++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 packages/SystemUI/tests/src/com/android/systemui/camera/CameraIntentsTest.kt diff --git a/packages/SystemUI/src/com/android/systemui/camera/CameraIntents.kt b/packages/SystemUI/src/com/android/systemui/camera/CameraIntents.kt index 464bee18f0307..f8a20023e47ac 100644 --- a/packages/SystemUI/src/com/android/systemui/camera/CameraIntents.kt +++ b/packages/SystemUI/src/com/android/systemui/camera/CameraIntents.kt @@ -23,11 +23,11 @@ import android.text.TextUtils import com.android.systemui.R -interface CameraIntents { +class CameraIntents { companion object { - const val DEFAULT_SECURE_CAMERA_INTENT_ACTION = + val DEFAULT_SECURE_CAMERA_INTENT_ACTION = MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE - const val DEFAULT_INSECURE_CAMERA_INTENT_ACTION = + val DEFAULT_INSECURE_CAMERA_INTENT_ACTION = MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA @JvmStatic @@ -59,13 +59,13 @@ interface CameraIntents { } @JvmStatic - fun isSecureCameraIntent(intent: Intent): Boolean { - return intent.getAction().equals(DEFAULT_SECURE_CAMERA_INTENT_ACTION) + fun isSecureCameraIntent(intent: Intent?): Boolean { + return intent?.getAction()?.equals(DEFAULT_SECURE_CAMERA_INTENT_ACTION) ?: false } @JvmStatic - fun isInsecureCameraIntent(intent: Intent): Boolean { - return intent.getAction().equals(DEFAULT_INSECURE_CAMERA_INTENT_ACTION) + fun isInsecureCameraIntent(intent: Intent?): Boolean { + return intent?.getAction()?.equals(DEFAULT_INSECURE_CAMERA_INTENT_ACTION) ?: false } } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/camera/CameraIntentsTest.kt b/packages/SystemUI/tests/src/com/android/systemui/camera/CameraIntentsTest.kt new file mode 100644 index 0000000000000..feaedc53fbc11 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/camera/CameraIntentsTest.kt @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2021 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 com.android.systemui.camera + +import android.content.Intent +import android.test.suitebuilder.annotation.SmallTest +import android.testing.AndroidTestingRunner +import com.android.systemui.SysuiTestCase +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith + +@SmallTest +@RunWith(AndroidTestingRunner::class) +class CameraIntentsTest : SysuiTestCase() { + companion object { + val VALID_SECURE_INTENT = Intent(CameraIntents.DEFAULT_SECURE_CAMERA_INTENT_ACTION) + val VALID_INSECURE_INTENT = Intent(CameraIntents.DEFAULT_INSECURE_CAMERA_INTENT_ACTION) + } + + @Test + fun testIsSecureCameraIntent() { + assertTrue(CameraIntents.isSecureCameraIntent(VALID_SECURE_INTENT)) + // an intent with a specific package is still a valid secure camera intent + assertTrue(CameraIntents.isSecureCameraIntent( + Intent(VALID_SECURE_INTENT).setPackage("com.example"))) + assertFalse(CameraIntents.isSecureCameraIntent(null)) + assertFalse(CameraIntents.isSecureCameraIntent(Intent())) + assertFalse(CameraIntents.isSecureCameraIntent(Intent(Intent.ACTION_MAIN))) + } + @Test + fun testIsInsecureCameraIntent() { + assertTrue(CameraIntents.isInsecureCameraIntent(VALID_INSECURE_INTENT)) + // an intent with a specific package is still a valid secure camera intent + assertTrue(CameraIntents.isInsecureCameraIntent( + Intent(VALID_INSECURE_INTENT).setPackage("com.example"))) + assertFalse(CameraIntents.isInsecureCameraIntent(null)) + assertFalse(CameraIntents.isInsecureCameraIntent(Intent())) + assertFalse(CameraIntents.isInsecureCameraIntent(Intent(Intent.ACTION_MAIN))) + } +}