1 /* 2 * Copyright (C) 2019 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.server.locksettings; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.platform.test.annotations.Presubmit; 22 23 import androidx.test.filters.SmallTest; 24 import androidx.test.runner.AndroidJUnit4; 25 26 import org.junit.After; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.io.ByteArrayInputStream; 32 import java.io.ByteArrayOutputStream; 33 import java.util.HashMap; 34 import java.util.HashSet; 35 import java.util.Map; 36 import java.util.Set; 37 38 @SmallTest 39 @Presubmit 40 @RunWith(AndroidJUnit4.class) 41 public class PasswordSlotManagerTests { 42 43 PasswordSlotManagerTestable mManager; 44 45 @Before setUp()46 public void setUp() throws Exception { 47 mManager = new PasswordSlotManagerTestable(); 48 } 49 50 @After tearDown()51 public void tearDown() throws Exception { 52 mManager.cleanup(); 53 } 54 55 @Test testBasicSlotUse()56 public void testBasicSlotUse() throws Exception { 57 mManager.markSlotInUse(0); 58 mManager.markSlotInUse(1); 59 60 Set<Integer> expected = new HashSet<Integer>(); 61 expected.add(0); 62 expected.add(1); 63 assertEquals(expected, mManager.getUsedSlots()); 64 65 mManager.markSlotDeleted(1); 66 67 expected = new HashSet<Integer>(); 68 expected.add(0); 69 assertEquals(expected, mManager.getUsedSlots()); 70 } 71 72 @Test testMergeSlots()73 public void testMergeSlots() throws Exception { 74 // Add some slots from a different OS image. 75 mManager.setGsiImageNumber(1); 76 mManager.markSlotInUse(4); 77 mManager.markSlotInUse(6); 78 79 // Switch back to the host image. 80 mManager.setGsiImageNumber(0); 81 mManager.markSlotInUse(0); 82 mManager.markSlotInUse(3); 83 mManager.markSlotInUse(5); 84 85 // Correct slot information for the host image. 86 Set<Integer> actual = new HashSet<Integer>(); 87 actual.add(1); 88 actual.add(3); 89 mManager.refreshActiveSlots(actual); 90 91 Set<Integer> expected = new HashSet<Integer>(); 92 expected.add(1); 93 expected.add(3); 94 expected.add(4); 95 expected.add(6); 96 assertEquals(expected, mManager.getUsedSlots()); 97 } 98 99 @Test testSerialization()100 public void testSerialization() throws Exception { 101 mManager.markSlotInUse(0); 102 mManager.markSlotInUse(1); 103 mManager.setGsiImageNumber(1); 104 mManager.markSlotInUse(4); 105 106 final ByteArrayOutputStream saved = new ByteArrayOutputStream(); 107 mManager.saveSlotMap(saved); 108 109 final HashMap<Integer, String> expected = new HashMap<Integer, String>(); 110 expected.put(0, "host"); 111 expected.put(1, "host"); 112 expected.put(4, "gsi1"); 113 114 final Map<Integer, String> map = mManager.loadSlotMap( 115 new ByteArrayInputStream(saved.toByteArray())); 116 assertEquals(expected, map); 117 } 118 119 @Test testSaving()120 public void testSaving() throws Exception { 121 mManager.markSlotInUse(0); 122 mManager.markSlotInUse(1); 123 mManager.setGsiImageNumber(1); 124 mManager.markSlotInUse(4); 125 126 // Make a new one. It should load the previous map. 127 mManager = new PasswordSlotManagerTestable(); 128 129 Set<Integer> expected = new HashSet<Integer>(); 130 expected.add(0); 131 expected.add(1); 132 expected.add(4); 133 assertEquals(expected, mManager.getUsedSlots()); 134 } 135 } 136