|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 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.keyattestation.verifier |
| 18 | + |
| 19 | +import com.google.common.truth.Truth.assertThat |
| 20 | +import com.google.protobuf.ByteString |
| 21 | +import org.junit.Test |
| 22 | +import org.junit.runner.RunWith |
| 23 | +import org.junit.runners.JUnit4 |
| 24 | + |
| 25 | +@RunWith(JUnit4::class) |
| 26 | +class ExtensionConstraintConfigTest { |
| 27 | + |
| 28 | + private companion object { |
| 29 | + val authorizationList = |
| 30 | + AuthorizationList(purposes = setOf(1.toBigInteger()), algorithms = 1.toBigInteger()) |
| 31 | + |
| 32 | + fun createTestKeyDescription( |
| 33 | + attestationSecurityLevel: SecurityLevel, |
| 34 | + keyMintSecurityLevel: SecurityLevel, |
| 35 | + ) = |
| 36 | + KeyDescription( |
| 37 | + attestationVersion = 1.toBigInteger(), |
| 38 | + attestationSecurityLevel = attestationSecurityLevel, |
| 39 | + keyMintVersion = 1.toBigInteger(), |
| 40 | + keyMintSecurityLevel = keyMintSecurityLevel, |
| 41 | + attestationChallenge = ByteString.empty(), |
| 42 | + uniqueId = ByteString.empty(), |
| 43 | + softwareEnforced = authorizationList, |
| 44 | + hardwareEnforced = authorizationList, |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + val keyDescriptionWithStrongBoxSecurityLevels = |
| 49 | + createTestKeyDescription(SecurityLevel.STRONG_BOX, SecurityLevel.STRONG_BOX) |
| 50 | + val keyDescriptionWithTeeSecurityLevels = |
| 51 | + createTestKeyDescription(SecurityLevel.TRUSTED_ENVIRONMENT, SecurityLevel.TRUSTED_ENVIRONMENT) |
| 52 | + val keyDescriptionWithSoftwareSecurityLevels = |
| 53 | + createTestKeyDescription(SecurityLevel.SOFTWARE, SecurityLevel.SOFTWARE) |
| 54 | + val keyDescriptionWithMismatchedSecurityLevels = |
| 55 | + createTestKeyDescription(SecurityLevel.STRONG_BOX, SecurityLevel.TRUSTED_ENVIRONMENT) |
| 56 | + |
| 57 | + @Test |
| 58 | + fun ValidationLevelIsSatisfiedBy_strictWithExpectedValue() { |
| 59 | + val level = ValidationLevel.STRICT("foo") |
| 60 | + |
| 61 | + assertThat(level.isSatisfiedBy("foo")).isTrue() |
| 62 | + assertThat(level.isSatisfiedBy("bar")).isFalse() |
| 63 | + assertThat(level.isSatisfiedBy(null)).isFalse() |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun ValidationLevelIsSatisfiedBy_notNull_allowsAnyValue() { |
| 68 | + val level = ValidationLevel.NOT_NULL |
| 69 | + |
| 70 | + assertThat(level.isSatisfiedBy("foo")).isTrue() |
| 71 | + assertThat(level.isSatisfiedBy(null)).isFalse() |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun ValidationLevelIsSatisfiedBy_ignore_allowsAnyValue() { |
| 76 | + val level = ValidationLevel.IGNORE |
| 77 | + |
| 78 | + assertThat(level.isSatisfiedBy("foo")).isTrue() |
| 79 | + assertThat(level.isSatisfiedBy(null)).isTrue() |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun SecurityLevelValidationLevelIsSatisfiedBy_strictWithExpectedValue() { |
| 84 | + val level = SecurityLevelValidationLevel.STRICT(SecurityLevel.STRONG_BOX) |
| 85 | + |
| 86 | + assertThat(level.isSatisfiedBy(keyDescriptionWithStrongBoxSecurityLevels)).isTrue() |
| 87 | + assertThat(level.isSatisfiedBy(keyDescriptionWithTeeSecurityLevels)).isFalse() |
| 88 | + assertThat(level.isSatisfiedBy(keyDescriptionWithMismatchedSecurityLevels)).isFalse() |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun SecurityLevelValidationLevelIsSatisfiedBy_notSoftware_allowsAnyNonSoftwareMatchingLevels() { |
| 93 | + val level = SecurityLevelValidationLevel.NOT_SOFTWARE |
| 94 | + |
| 95 | + assertThat(level.isSatisfiedBy(keyDescriptionWithStrongBoxSecurityLevels)).isTrue() |
| 96 | + assertThat(level.isSatisfiedBy(keyDescriptionWithTeeSecurityLevels)).isTrue() |
| 97 | + assertThat(level.isSatisfiedBy(keyDescriptionWithSoftwareSecurityLevels)).isFalse() |
| 98 | + assertThat(level.isSatisfiedBy(keyDescriptionWithMismatchedSecurityLevels)).isFalse() |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun SecurityLevelValidationLevelIsSatisfiedBy_consistent_allowsAnyMatchingLevels() { |
| 103 | + val level = SecurityLevelValidationLevel.CONSISTENT |
| 104 | + |
| 105 | + assertThat(level.isSatisfiedBy(keyDescriptionWithStrongBoxSecurityLevels)).isTrue() |
| 106 | + assertThat(level.isSatisfiedBy(keyDescriptionWithTeeSecurityLevels)).isTrue() |
| 107 | + assertThat(level.isSatisfiedBy(keyDescriptionWithSoftwareSecurityLevels)).isTrue() |
| 108 | + assertThat(level.isSatisfiedBy(keyDescriptionWithMismatchedSecurityLevels)).isFalse() |
| 109 | + } |
| 110 | +} |
0 commit comments