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