1 /* 2 * Copyright (C) 2022 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 com.android.permission.safetylabel 18 19 import androidx.test.ext.junit.runners.AndroidJUnit4 20 import com.android.permission.safetylabel.SafetyLabel.KEY_SAFETY_LABEL 21 import com.android.permission.safetylabel.SafetyLabel.KEY_VERSION 22 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createInvalidMetadataPersistableBundle 23 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createMetadataPersistableBundle 24 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createMetadataPersistableBundleInvalidVersion 25 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createMetadataPersistableBundleWithInvalidSafetyLabel 26 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createMetadataPersistableBundleWithoutVersion 27 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createNonVersionedEmptyMetadataPersistableBundle 28 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createSafetyLabelPersistableBundleWithInvalidVersion 29 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createSafetyLabelPersistableBundleWithoutVersion 30 import com.android.permission.safetylabel.SafetyLabelTestPersistableBundles.createVersionedEmptyMetadataPersistableBundle 31 import com.google.common.truth.Truth.assertThat 32 import org.junit.Test 33 import org.junit.runner.RunWith 34 35 /** CTS tests for [SafetyLabel]. */ 36 @RunWith(AndroidJUnit4::class) 37 class SafetyLabelTest { 38 39 @Test getSafetyLabelFromMetadata_nullMetadataBundle_nullSafetyLabelnull40 fun getSafetyLabelFromMetadata_nullMetadataBundle_nullSafetyLabel() { 41 val safetyLabel = SafetyLabel.getSafetyLabelFromMetadata(null) 42 43 assertThat(safetyLabel).isNull() 44 } 45 46 @Test getSafetyLabelFromMetadata_emptyMetadataBundle_nullSafetyLabelnull47 fun getSafetyLabelFromMetadata_emptyMetadataBundle_nullSafetyLabel() { 48 val safetyLabel = 49 SafetyLabel.getSafetyLabelFromMetadata(createNonVersionedEmptyMetadataPersistableBundle()) 50 51 assertThat(safetyLabel).isNull() 52 } 53 54 @Test getSafetyLabelFromMetadata_emptyVersionedMetadataBundle_nullSafetyLabelnull55 fun getSafetyLabelFromMetadata_emptyVersionedMetadataBundle_nullSafetyLabel() { 56 val safetyLabel = 57 SafetyLabel.getSafetyLabelFromMetadata(createVersionedEmptyMetadataPersistableBundle()) 58 59 assertThat(safetyLabel).isNull() 60 } 61 62 @Test getSafetyLabelFromMetadata_invalidMetadataBundle_nullSafetyLabelnull63 fun getSafetyLabelFromMetadata_invalidMetadataBundle_nullSafetyLabel() { 64 val safetyLabel = 65 SafetyLabel.getSafetyLabelFromMetadata(createInvalidMetadataPersistableBundle()) 66 67 assertThat(safetyLabel).isNull() 68 } 69 70 @Test getSafetyLabelFromMetadata_invalidSafetyLabelBundle_dataSharedEmptynull71 fun getSafetyLabelFromMetadata_invalidSafetyLabelBundle_dataSharedEmpty() { 72 val safetyLabel = 73 SafetyLabel.getSafetyLabelFromMetadata( 74 createMetadataPersistableBundleWithInvalidSafetyLabel()) 75 76 assertThat(safetyLabel).isNull() 77 } 78 79 @Test getSafetyLabelFromMetadata_validBundle_hasDataSharednull80 fun getSafetyLabelFromMetadata_validBundle_hasDataShared() { 81 val bundle = createMetadataPersistableBundle(TOP_LEVEL_VERSION, SAFETY_LABELS_VERSION) 82 val topLevelVersion = bundle.getLong(KEY_VERSION) 83 val safetyLabelBundle = bundle.getPersistableBundle(KEY_SAFETY_LABEL) 84 val safetyLabelsVersion = safetyLabelBundle?.getLong(KEY_VERSION) 85 val safetyLabel = SafetyLabel.getSafetyLabelFromMetadata(bundle) 86 87 assertThat(topLevelVersion).isEqualTo(TOP_LEVEL_VERSION) 88 assertThat(safetyLabelsVersion).isEqualTo(SAFETY_LABELS_VERSION) 89 assertThat(safetyLabel).isNotNull() 90 assertThat(safetyLabel?.dataLabel).isNotNull() 91 } 92 93 @Test getSafetyLabelFromMetadata_invalidBundle_noTopLevelBundleVersionnull94 fun getSafetyLabelFromMetadata_invalidBundle_noTopLevelBundleVersion() { 95 val safetyLabel = 96 SafetyLabel.getSafetyLabelFromMetadata(createMetadataPersistableBundleWithoutVersion()) 97 assertThat(safetyLabel).isNull() 98 } 99 100 @Test getSafetyLabelFromMetadata_invalidBundle_InvalidTopLevelBundleVersionnull101 fun getSafetyLabelFromMetadata_invalidBundle_InvalidTopLevelBundleVersion() { 102 val safetyLabel = 103 SafetyLabel.getSafetyLabelFromMetadata(createMetadataPersistableBundleInvalidVersion()) 104 assertThat(safetyLabel).isNull() 105 } 106 107 @Test getSafetyLabelFromMetadata_invalidBundle_NoSafetyLabelBundleVersionnull108 fun getSafetyLabelFromMetadata_invalidBundle_NoSafetyLabelBundleVersion() { 109 val bundle = createVersionedEmptyMetadataPersistableBundle() 110 bundle.putPersistableBundle( 111 SafetyLabel.KEY_SAFETY_LABEL, createSafetyLabelPersistableBundleWithoutVersion()) 112 val safetyLabel = SafetyLabel.getSafetyLabelFromMetadata(bundle) 113 assertThat(safetyLabel).isNull() 114 } 115 116 @Test getSafetyLabelFromMetadata_invalidBundle_InvalidSafetyLabelBundleVersionnull117 fun getSafetyLabelFromMetadata_invalidBundle_InvalidSafetyLabelBundleVersion() { 118 val bundle = createVersionedEmptyMetadataPersistableBundle() 119 bundle.putPersistableBundle( 120 SafetyLabel.KEY_SAFETY_LABEL, createSafetyLabelPersistableBundleWithInvalidVersion()) 121 val safetyLabel = SafetyLabel.getSafetyLabelFromMetadata(bundle) 122 assertThat(safetyLabel).isNull() 123 } 124 125 companion object { 126 private const val TOP_LEVEL_VERSION = 3L 127 private const val SAFETY_LABELS_VERSION = 2L 128 } 129 }