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.security.keystore.recovery; 18 19 import static org.junit.Assert.assertArrayEquals; 20 import static org.junit.Assert.assertEquals; 21 22 import android.os.Parcel; 23 import android.support.test.filters.SmallTest; 24 import android.support.test.runner.AndroidJUnit4; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 @RunWith(AndroidJUnit4.class) 30 @SmallTest 31 public class KeyChainProtectionParamsTest { 32 33 private static final byte[] SALT = new byte[] { 0, 1, 2, 3, 4, 5 }; 34 private static final byte[] SECRET = new byte[] { 5, 4, 3, 2, 1, 0 }; 35 private static final int USER_SECRET_TYPE = KeyChainProtectionParams.TYPE_LOCKSCREEN; 36 private static final int LOCK_SCREEN_UI_FORMAT = KeyChainProtectionParams.UI_FORMAT_PATTERN; 37 38 @Test 39 public void build_setsSecret() { 40 assertArrayEquals(SECRET, createTestParams().getSecret()); 41 } 42 43 @Test 44 public void build_setsLockScreenUiFormat() { 45 assertEquals(LOCK_SCREEN_UI_FORMAT, createTestParams().getLockScreenUiFormat()); 46 } 47 48 @Test 49 public void build_setsUserSecretType() { 50 assertEquals(USER_SECRET_TYPE, createTestParams().getUserSecretType()); 51 } 52 53 @Test 54 public void build_setsKeyDerivationParams() { 55 KeyChainProtectionParams protParams = createTestParams(); 56 KeyDerivationParams keyDerivationParams = protParams.getKeyDerivationParams(); 57 58 assertEquals(KeyDerivationParams.ALGORITHM_SHA256, keyDerivationParams.getAlgorithm()); 59 assertArrayEquals(SALT, keyDerivationParams.getSalt()); 60 } 61 62 @Test 63 public void writeToParcel_writesSecret() { 64 KeyChainProtectionParams protParams = writeToThenReadFromParcel(createTestParams()); 65 66 assertArrayEquals(SECRET, protParams.getSecret()); 67 } 68 69 @Test 70 public void writeToParcel_writesUserSecretType() { 71 KeyChainProtectionParams protParams = writeToThenReadFromParcel(createTestParams()); 72 73 assertEquals(USER_SECRET_TYPE, protParams.getUserSecretType()); 74 } 75 76 @Test 77 public void writeToParcel_writesLockScreenUiFormat() { 78 KeyChainProtectionParams protParams = writeToThenReadFromParcel(createTestParams()); 79 80 assertEquals(LOCK_SCREEN_UI_FORMAT, protParams.getLockScreenUiFormat()); 81 } 82 83 @Test 84 public void writeToParcel_writesKeyDerivationParams() { 85 KeyChainProtectionParams protParams = writeToThenReadFromParcel(createTestParams()); 86 KeyDerivationParams keyDerivationParams = protParams.getKeyDerivationParams(); 87 88 assertEquals(KeyDerivationParams.ALGORITHM_SHA256, keyDerivationParams.getAlgorithm()); 89 assertArrayEquals(SALT, keyDerivationParams.getSalt()); 90 } 91 92 private KeyChainProtectionParams writeToThenReadFromParcel(KeyChainProtectionParams params) { 93 Parcel parcel = Parcel.obtain(); 94 params.writeToParcel(parcel, /*flags=*/ 0); 95 parcel.setDataPosition(0); 96 KeyChainProtectionParams fromParcel = 97 KeyChainProtectionParams.CREATOR.createFromParcel(parcel); 98 parcel.recycle(); 99 return fromParcel; 100 } 101 102 private KeyChainProtectionParams createTestParams() { 103 return new KeyChainProtectionParams.Builder() 104 .setKeyDerivationParams(KeyDerivationParams.createSha256Params(SALT)) 105 .setSecret(SECRET) 106 .setUserSecretType(USER_SECRET_TYPE) 107 .setLockScreenUiFormat(LOCK_SCREEN_UI_FORMAT) 108 .build(); 109 } 110 } 111