David Anderson | 28dea68 | 2019-02-20 13:37:51 -0800 | [diff] [blame] | 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 android.test.AndroidTestCase; |
| 20 | |
| 21 | import java.io.ByteArrayInputStream; |
| 22 | import java.io.ByteArrayOutputStream; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | public class PasswordSlotManagerTests extends AndroidTestCase { |
| 29 | |
| 30 | PasswordSlotManagerTestable mManager; |
| 31 | |
| 32 | @Override |
| 33 | protected void setUp() throws Exception { |
| 34 | super.setUp(); |
| 35 | |
| 36 | mManager = new PasswordSlotManagerTestable(); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | protected void tearDown() throws Exception { |
| 41 | super.tearDown(); |
| 42 | |
| 43 | mManager.cleanup(); |
| 44 | } |
| 45 | |
| 46 | public void testBasicSlotUse() throws Exception { |
| 47 | mManager.markSlotInUse(0); |
| 48 | mManager.markSlotInUse(1); |
| 49 | |
| 50 | Set<Integer> expected = new HashSet<Integer>(); |
| 51 | expected.add(0); |
| 52 | expected.add(1); |
| 53 | assertEquals(expected, mManager.getUsedSlots()); |
| 54 | |
| 55 | mManager.markSlotDeleted(1); |
| 56 | |
| 57 | expected = new HashSet<Integer>(); |
| 58 | expected.add(0); |
| 59 | assertEquals(expected, mManager.getUsedSlots()); |
| 60 | } |
| 61 | |
| 62 | public void testMergeSlots() throws Exception { |
| 63 | // Add some slots from a different OS image. |
| 64 | mManager.setGsiImageNumber(1); |
| 65 | mManager.markSlotInUse(4); |
| 66 | mManager.markSlotInUse(6); |
| 67 | |
| 68 | // Switch back to the host image. |
| 69 | mManager.setGsiImageNumber(0); |
| 70 | mManager.markSlotInUse(0); |
| 71 | mManager.markSlotInUse(3); |
| 72 | mManager.markSlotInUse(5); |
| 73 | |
| 74 | // Correct slot information for the host image. |
| 75 | Set<Integer> actual = new HashSet<Integer>(); |
| 76 | actual.add(1); |
| 77 | actual.add(3); |
| 78 | mManager.refreshActiveSlots(actual); |
| 79 | |
| 80 | Set<Integer> expected = new HashSet<Integer>(); |
| 81 | expected.add(1); |
| 82 | expected.add(3); |
| 83 | expected.add(4); |
| 84 | expected.add(6); |
| 85 | assertEquals(expected, mManager.getUsedSlots()); |
| 86 | } |
| 87 | |
| 88 | public void testSerialization() throws Exception { |
| 89 | mManager.markSlotInUse(0); |
| 90 | mManager.markSlotInUse(1); |
| 91 | mManager.setGsiImageNumber(1); |
| 92 | mManager.markSlotInUse(4); |
| 93 | |
| 94 | final ByteArrayOutputStream saved = new ByteArrayOutputStream(); |
| 95 | mManager.saveSlotMap(saved); |
| 96 | |
| 97 | final HashMap<Integer, String> expected = new HashMap<Integer, String>(); |
| 98 | expected.put(0, "host"); |
| 99 | expected.put(1, "host"); |
| 100 | expected.put(4, "gsi1"); |
| 101 | |
| 102 | final Map<Integer, String> map = mManager.loadSlotMap( |
| 103 | new ByteArrayInputStream(saved.toByteArray())); |
| 104 | assertEquals(expected, map); |
| 105 | } |
| 106 | |
| 107 | public void testSaving() throws Exception { |
| 108 | mManager.markSlotInUse(0); |
| 109 | mManager.markSlotInUse(1); |
| 110 | mManager.setGsiImageNumber(1); |
| 111 | mManager.markSlotInUse(4); |
| 112 | |
| 113 | // Make a new one. It should load the previous map. |
| 114 | mManager = new PasswordSlotManagerTestable(); |
| 115 | |
| 116 | Set<Integer> expected = new HashSet<Integer>(); |
| 117 | expected.add(0); |
| 118 | expected.add(1); |
| 119 | expected.add(4); |
| 120 | assertEquals(expected, mManager.getUsedSlots()); |
| 121 | } |
| 122 | } |