1 /*
2  * Copyright (C) 2018 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.testing.shadows;
18 
19 import android.app.backup.BackupDataInput;
20 
21 import org.robolectric.annotation.Implementation;
22 import org.robolectric.annotation.Implements;
23 import org.robolectric.annotation.Resetter;
24 
25 import java.io.EOFException;
26 import java.io.FileDescriptor;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.ObjectInputStream;
30 
31 /**
32  * Shadow for {@link BackupDataInput}. Format read does NOT match implementation. To write data to
33  * be read by this shadow, you should also declare shadow {@link ShadowBackupDataOutput}.
34  */
35 @Implements(BackupDataInput.class)
36 public class ShadowBackupDataInput {
37     private static boolean sReadNextHeaderThrow = false;
38 
throwInNextHeaderRead()39     public static void throwInNextHeaderRead() {
40         sReadNextHeaderThrow = true;
41     }
42 
43     @Resetter
reset()44     public static void reset() {
45         sReadNextHeaderThrow = false;
46     }
47 
48     private FileDescriptor mFileDescriptor;
49     private ObjectInputStream mInput;
50     private int mSize;
51     private String mKey;
52     private boolean mHeaderReady;
53 
54     @Implementation
__constructor__(FileDescriptor fd)55     protected void __constructor__(FileDescriptor fd) {
56         mFileDescriptor = fd;
57     }
58 
59     @Implementation
readNextHeader()60     protected boolean readNextHeader() throws IOException {
61         if (sReadNextHeaderThrow) {
62             sReadNextHeaderThrow = false;
63             throw new IOException("Fake exception");
64         }
65         mHeaderReady = false;
66         try {
67             ensureInput();
68             mSize = mInput.readInt();
69         } catch (EOFException e) {
70             return false;
71         }
72         mKey = mInput.readUTF();
73         mHeaderReady = true;
74         return true;
75     }
76 
77     @Implementation
getKey()78     protected String getKey() {
79         checkHeaderReady();
80         return mKey;
81     }
82 
83     @Implementation
getDataSize()84     protected int getDataSize() {
85         checkHeaderReady();
86         return mSize;
87     }
88 
89     @Implementation
readEntityData(byte[] data, int offset, int size)90     protected int readEntityData(byte[] data, int offset, int size) throws IOException {
91         checkHeaderReady();
92         int result = mInput.read(data, offset, size);
93         if (result < 0) {
94             throw new IOException("result=0x" + Integer.toHexString(result));
95         }
96         return result;
97     }
98 
99     @Implementation
skipEntityData()100     protected void skipEntityData() throws IOException {
101         checkHeaderReady();
102         mInput.read(new byte[mSize], 0, mSize);
103     }
104 
checkHeaderReady()105     private void checkHeaderReady() {
106         if (!mHeaderReady) {
107             throw new IllegalStateException("Entity header not read");
108         }
109     }
110 
111     /**
112      * Lazily initializing input to avoid throwing exception when stream is completely empty in
113      * constructor (Java Object IO writes/reads some header data).
114      *
115      * @throws EOFException When the input is empty.
116      */
ensureInput()117     private void ensureInput() throws EOFException {
118         if (mInput == null) {
119             try {
120                 mInput = new ObjectInputStream(new FileInputStream(mFileDescriptor));
121             } catch (EOFException e) {
122                 throw e;
123             } catch (IOException e) {
124                 throw new AssertionError(e);
125             }
126         }
127     }
128 }
129