1 /* Copyright 2019, The Android Open Source Project, Inc. 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.google.android.attestation; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static java.nio.charset.StandardCharsets.UTF_8; 20 21 import com.google.android.attestation.AttestationApplicationId.AttestationPackageInfo; 22 import com.google.common.collect.ImmutableList; 23 import org.bouncycastle.asn1.DEROctetString; 24 import org.bouncycastle.util.encoders.Base64; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Test for {@link AttestationApplicationId}. */ 30 @RunWith(JUnit4.class) 31 public class AttestationApplicationIdTest { 32 33 // Generated from certificate with RSA Algorithm and StrongBox Security Level 34 private static final DEROctetString ATTESTATION_APPLICATION_ID = 35 new DEROctetString( 36 Base64.decode( 37 "MIIBszGCAYswDAQHYW5kcm9pZAIBHTAZBBRjb20uYW5kcm9pZC5rZXljaGFpbgIBHTAZBBRjb20uYW5kcm9p" 38 + "ZC5zZXR0aW5ncwIBHTAZBBRjb20ucXRpLmRpYWdzZXJ2aWNlcwIBHTAaBBVjb20uYW5kcm9pZC5keW" 39 + "5zeXN0ZW0CAR0wHQQYY29tLmFuZHJvaWQuaW5wdXRkZXZpY2VzAgEdMB8EGmNvbS5hbmRyb2lkLmxv" 40 + "Y2FsdHJhbnNwb3J0AgEdMB8EGmNvbS5hbmRyb2lkLmxvY2F0aW9uLmZ1c2VkAgEdMB8EGmNvbS5hbm" 41 + "Ryb2lkLnNlcnZlci50ZWxlY29tAgEdMCAEG2NvbS5hbmRyb2lkLndhbGxwYXBlcmJhY2t1cAIBHTAh" 42 + "BBxjb20uZ29vZ2xlLlNTUmVzdGFydERldGVjdG9yAgEdMCIEHWNvbS5nb29nbGUuYW5kcm9pZC5oaW" 43 + "RkZW5tZW51AgEBMCMEHmNvbS5hbmRyb2lkLnByb3ZpZGVycy5zZXR0aW5ncwIBHTEiBCAwGqPLCBE0" 44 + "UBxF8UIqvGbCQiT9Xe1f3I8X5pcXb9hmqg==")); 45 46 private static final ImmutableList<AttestationPackageInfo> EXPECTED_PACKAGE_INFOS = 47 ImmutableList.of( 48 new AttestationPackageInfo("android", 29L), 49 new AttestationPackageInfo("com.android.keychain", 29L), 50 new AttestationPackageInfo("com.android.settings", 29L), 51 new AttestationPackageInfo("com.qti.diagservices", 29L), 52 new AttestationPackageInfo("com.android.dynsystem", 29L), 53 new AttestationPackageInfo("com.android.inputdevices", 29L), 54 new AttestationPackageInfo("com.android.localtransport", 29L), 55 new AttestationPackageInfo("com.android.location.fused", 29L), 56 new AttestationPackageInfo("com.android.server.telecom", 29L), 57 new AttestationPackageInfo("com.android.wallpaperbackup", 29L), 58 new AttestationPackageInfo("com.google.SSRestartDetector", 29L), 59 new AttestationPackageInfo("com.google.android.hiddenmenu", 1L), 60 new AttestationPackageInfo("com.android.providers.settings", 29L)); 61 private static final ImmutableList<byte[]> EXPECTED_SIGNATURE_DIGESTS = 62 ImmutableList.of(Base64.decode("MBqjywgRNFAcRfFCKrxmwkIk/V3tX9yPF+aXF2/YZqo=\n")); 63 64 private static final AttestationApplicationId EXPECTED_ATTESTATION_APPLICATION_ID = 65 new AttestationApplicationId(EXPECTED_PACKAGE_INFOS, EXPECTED_SIGNATURE_DIGESTS); 66 67 @Test testCreateAttestationApplicationId()68 public void testCreateAttestationApplicationId() { 69 AttestationApplicationId attestationApplicationId = 70 AttestationApplicationId.createAttestationApplicationId(ATTESTATION_APPLICATION_ID); 71 assertThat(attestationApplicationId).isEqualTo(EXPECTED_ATTESTATION_APPLICATION_ID); 72 } 73 74 @Test testCreateEmptyAttestationApplicationIdFromEmptyOrInvalidInput()75 public void testCreateEmptyAttestationApplicationIdFromEmptyOrInvalidInput() { 76 assertThat(AttestationApplicationId.createAttestationApplicationId(null)).isNull(); 77 assertThat( 78 AttestationApplicationId.createAttestationApplicationId( 79 new DEROctetString("Invalid DEROctet String".getBytes(UTF_8)))) 80 .isNull(); 81 } 82 83 @Test testEquals()84 public void testEquals() { 85 AttestationApplicationId attestationApplicationId = 86 AttestationApplicationId.createAttestationApplicationId(ATTESTATION_APPLICATION_ID); 87 AttestationApplicationId emptyAttestationApplicationId = 88 new AttestationApplicationId(ImmutableList.of(), ImmutableList.of()); 89 90 assertThat(attestationApplicationId.equals(EXPECTED_ATTESTATION_APPLICATION_ID)).isTrue(); 91 assertThat(EXPECTED_ATTESTATION_APPLICATION_ID.equals(attestationApplicationId)).isTrue(); 92 93 assertThat(attestationApplicationId.equals(emptyAttestationApplicationId)).isFalse(); 94 assertThat(emptyAttestationApplicationId.equals(attestationApplicationId)).isFalse(); 95 } 96 97 @Test testEqualObjectsHaveEqualHashCodes()98 public void testEqualObjectsHaveEqualHashCodes() { 99 AttestationApplicationId attestationApplicationId = 100 AttestationApplicationId.createAttestationApplicationId(ATTESTATION_APPLICATION_ID); 101 102 assertThat(attestationApplicationId.equals(EXPECTED_ATTESTATION_APPLICATION_ID)).isTrue(); 103 assertThat(attestationApplicationId.hashCode()) 104 .isEqualTo(EXPECTED_ATTESTATION_APPLICATION_ID.hashCode()); 105 } 106 } 107