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