Kenny Root | 96690bc | 2019-11-15 10:20:59 -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.recoverysystem; |
| 18 | |
| 19 | import static org.hamcrest.CoreMatchers.is; |
| 20 | import static org.junit.Assert.assertThat; |
| 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
| 24 | import static org.mockito.Mockito.doNothing; |
| 25 | import static org.mockito.Mockito.doThrow; |
| 26 | import static org.mockito.Mockito.mock; |
| 27 | import static org.mockito.Mockito.times; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | |
| 32 | import android.content.Context; |
| 33 | import android.os.Handler; |
| 34 | import android.os.IPowerManager; |
| 35 | import android.os.IRecoverySystemProgressListener; |
| 36 | import android.os.Looper; |
| 37 | import android.os.PowerManager; |
| 38 | |
| 39 | import androidx.test.InstrumentationRegistry; |
| 40 | import androidx.test.filters.SmallTest; |
| 41 | import androidx.test.runner.AndroidJUnit4; |
| 42 | |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | |
| 47 | import java.io.FileWriter; |
| 48 | |
| 49 | /** |
| 50 | * atest FrameworksServicesTests:RecoverySystemServiceTest |
| 51 | */ |
| 52 | @SmallTest |
| 53 | @RunWith(AndroidJUnit4.class) |
| 54 | public class RecoverySystemServiceTest { |
| 55 | private RecoverySystemService mRecoverySystemService; |
| 56 | private RecoverySystemServiceTestable.FakeSystemProperties mSystemProperties; |
| 57 | private RecoverySystemService.UncryptSocket mUncryptSocket; |
| 58 | private Context mContext; |
| 59 | private IPowerManager mIPowerManager; |
| 60 | private FileWriter mUncryptUpdateFileWriter; |
| 61 | |
| 62 | @Before |
| 63 | public void setup() { |
| 64 | mContext = mock(Context.class); |
| 65 | mSystemProperties = new RecoverySystemServiceTestable.FakeSystemProperties(); |
| 66 | mUncryptSocket = mock(RecoverySystemService.UncryptSocket.class); |
| 67 | mUncryptUpdateFileWriter = mock(FileWriter.class); |
| 68 | |
| 69 | Looper looper = InstrumentationRegistry.getContext().getMainLooper(); |
| 70 | mIPowerManager = mock(IPowerManager.class); |
| 71 | PowerManager powerManager = new PowerManager(mock(Context.class), mIPowerManager, |
| 72 | new Handler(looper)); |
| 73 | |
| 74 | mRecoverySystemService = new RecoverySystemServiceTestable(mContext, mSystemProperties, |
| 75 | powerManager, mUncryptUpdateFileWriter, mUncryptSocket); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void clearBcb_success() throws Exception { |
| 80 | doNothing().when(mContext).enforceCallingOrSelfPermission( |
| 81 | eq(android.Manifest.permission.RECOVERY), any()); |
| 82 | when(mUncryptSocket.getPercentageUncrypted()).thenReturn(100); |
| 83 | |
| 84 | assertThat(mRecoverySystemService.clearBcb(), is(true)); |
| 85 | |
| 86 | assertThat(mSystemProperties.getCtlStart(), is("clear-bcb")); |
| 87 | verify(mUncryptSocket).sendAck(); |
| 88 | verify(mUncryptSocket).close(); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void clearBcb_uncrypt_failure() throws Exception { |
| 93 | doNothing().when(mContext).enforceCallingOrSelfPermission( |
| 94 | eq(android.Manifest.permission.RECOVERY), any()); |
| 95 | when(mUncryptSocket.getPercentageUncrypted()).thenReturn(0); |
| 96 | |
| 97 | assertThat(mRecoverySystemService.clearBcb(), is(false)); |
| 98 | |
| 99 | assertThat(mSystemProperties.getCtlStart(), is("clear-bcb")); |
| 100 | verify(mUncryptSocket).sendAck(); |
| 101 | verify(mUncryptSocket).close(); |
| 102 | } |
| 103 | |
| 104 | @Test(expected = SecurityException.class) |
| 105 | public void clearBcb_noPerm() { |
| 106 | doThrow(SecurityException.class).when(mContext).enforceCallingOrSelfPermission( |
| 107 | eq(android.Manifest.permission.RECOVERY), any()); |
| 108 | mRecoverySystemService.clearBcb(); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void setupBcb_success() throws Exception { |
| 113 | doNothing().when(mContext).enforceCallingOrSelfPermission( |
| 114 | eq(android.Manifest.permission.RECOVERY), any()); |
| 115 | when(mUncryptSocket.getPercentageUncrypted()).thenReturn(100); |
| 116 | |
| 117 | assertThat(mRecoverySystemService.setupBcb("foo"), is(true)); |
| 118 | |
| 119 | assertThat(mSystemProperties.getCtlStart(), is("setup-bcb")); |
| 120 | verify(mUncryptSocket).sendCommand("foo"); |
| 121 | verify(mUncryptSocket).sendAck(); |
| 122 | verify(mUncryptSocket).close(); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void setupBcb_uncrypt_failure() throws Exception { |
| 127 | doNothing().when(mContext).enforceCallingOrSelfPermission( |
| 128 | eq(android.Manifest.permission.RECOVERY), any()); |
| 129 | when(mUncryptSocket.getPercentageUncrypted()).thenReturn(0); |
| 130 | |
| 131 | assertThat(mRecoverySystemService.setupBcb("foo"), is(false)); |
| 132 | |
| 133 | assertThat(mSystemProperties.getCtlStart(), is("setup-bcb")); |
| 134 | verify(mUncryptSocket).sendCommand("foo"); |
| 135 | verify(mUncryptSocket).sendAck(); |
| 136 | verify(mUncryptSocket).close(); |
| 137 | } |
| 138 | |
| 139 | @Test(expected = SecurityException.class) |
| 140 | public void setupBcb_noPerm() { |
| 141 | doThrow(SecurityException.class).when(mContext).enforceCallingOrSelfPermission( |
| 142 | eq(android.Manifest.permission.RECOVERY), any()); |
| 143 | mRecoverySystemService.setupBcb("foo"); |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void rebootRecoveryWithCommand_success() throws Exception { |
| 148 | doNothing().when(mContext).enforceCallingOrSelfPermission( |
| 149 | eq(android.Manifest.permission.RECOVERY), any()); |
| 150 | when(mUncryptSocket.getPercentageUncrypted()).thenReturn(100); |
| 151 | |
| 152 | mRecoverySystemService.rebootRecoveryWithCommand("foo"); |
| 153 | |
| 154 | assertThat(mSystemProperties.getCtlStart(), is("setup-bcb")); |
| 155 | verify(mUncryptSocket).sendCommand("foo"); |
| 156 | verify(mUncryptSocket).sendAck(); |
| 157 | verify(mUncryptSocket).close(); |
| 158 | verify(mIPowerManager).reboot(anyBoolean(), eq("recovery"), anyBoolean()); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void rebootRecoveryWithCommand_failure() throws Exception { |
| 163 | doNothing().when(mContext).enforceCallingOrSelfPermission( |
| 164 | eq(android.Manifest.permission.RECOVERY), any()); |
| 165 | when(mUncryptSocket.getPercentageUncrypted()).thenReturn(0); |
| 166 | |
| 167 | mRecoverySystemService.rebootRecoveryWithCommand("foo"); |
| 168 | |
| 169 | assertThat(mSystemProperties.getCtlStart(), is("setup-bcb")); |
| 170 | verify(mUncryptSocket).sendCommand("foo"); |
| 171 | verify(mUncryptSocket).sendAck(); |
| 172 | verify(mUncryptSocket).close(); |
| 173 | verifyNoMoreInteractions(mIPowerManager); |
| 174 | } |
| 175 | |
| 176 | @Test(expected = SecurityException.class) |
| 177 | public void rebootRecoveryWithCommand_noPerm() { |
| 178 | doThrow(SecurityException.class).when(mContext).enforceCallingOrSelfPermission( |
| 179 | eq(android.Manifest.permission.RECOVERY), any()); |
| 180 | mRecoverySystemService.rebootRecoveryWithCommand("foo"); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void uncrypt_success() throws Exception { |
| 185 | doNothing().when(mContext).enforceCallingOrSelfPermission( |
| 186 | eq(android.Manifest.permission.RECOVERY), any()); |
| 187 | when(mUncryptSocket.getPercentageUncrypted()).thenReturn(0, 5, 25, 50, 90, 99, 100); |
| 188 | |
| 189 | IRecoverySystemProgressListener listener = mock(IRecoverySystemProgressListener.class); |
| 190 | assertThat(mRecoverySystemService.uncrypt("foo.zip", listener), is(true)); |
| 191 | |
| 192 | assertThat(mSystemProperties.getCtlStart(), is("uncrypt")); |
| 193 | verify(mUncryptSocket, times(7)).getPercentageUncrypted(); |
| 194 | verify(mUncryptSocket).sendAck(); |
| 195 | verify(mUncryptSocket).close(); |
| 196 | } |
| 197 | } |