Home | History | Annotate | Download | only in restore
      1 /*
      2  * Copyright (C) 2017 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.backup.restore;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import android.content.Context;
     22 import android.platform.test.annotations.Presubmit;
     23 import android.support.test.InstrumentationRegistry;
     24 import android.support.test.filters.SmallTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 
     27 import com.android.frameworks.servicestests.R;
     28 
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.mockito.MockitoAnnotations;
     33 
     34 import java.io.InputStream;
     35 
     36 @SmallTest
     37 @Presubmit
     38 @RunWith(AndroidJUnit4.class)
     39 public class PerformAdbRestoreTaskTest {
     40     private Context mContext;
     41 
     42     @Before
     43     public void setUp() throws Exception {
     44         MockitoAnnotations.initMocks(this);
     45 
     46         mContext = InstrumentationRegistry.getContext();
     47     }
     48 
     49     @Test
     50     public void parseBackupFileAndReturnTarStream_backupNotEncrypted_returnsNonNull()
     51             throws Exception {
     52         InputStream inputStream = mContext.getResources().openRawResource(
     53                 R.raw.backup_telephony_no_password);
     54         InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream(
     55                 inputStream, null);
     56 
     57         assertThat(tarInputStream).isNotNull();
     58     }
     59 
     60     @Test
     61     public void
     62     parseBackupFileAndReturnTarStream_backupEncryptedAndPasswordProvided_returnsNonNull()
     63             throws Exception {
     64         InputStream inputStream = mContext.getResources().openRawResource(
     65                 R.raw.backup_telephony_with_password);
     66         InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream(
     67                 inputStream, "123");
     68 
     69         assertThat(tarInputStream).isNotNull();
     70     }
     71 
     72     @Test
     73     public void
     74     parseBackupFileAndReturnTarStream_backupEncryptedAndPasswordNotProvided_returnsNull()
     75             throws Exception {
     76         InputStream inputStream = mContext.getResources().openRawResource(
     77                 R.raw.backup_telephony_with_password);
     78         InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream(
     79                 inputStream, null);
     80 
     81         assertThat(tarInputStream).isNull();
     82     }
     83 
     84     @Test
     85     public void
     86     parseBackupFileAndReturnTarStream_backupEncryptedAndIncorrectPassword_returnsNull()
     87             throws Exception {
     88         InputStream inputStream = mContext.getResources().openRawResource(
     89                 R.raw.backup_telephony_with_password);
     90         InputStream tarInputStream = PerformAdbRestoreTask.parseBackupFileHeaderAndReturnTarStream(
     91                 inputStream, "1234");
     92 
     93         assertThat(tarInputStream).isNull();
     94     }
     95 }
     96