1 /*
2  * Copyright (C) 2024 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.tests.deletekeepdata.app;
18 
19 import static android.content.Context.MODE_PRIVATE;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 
25 import android.content.Context;
26 import android.util.Log;
27 
28 import androidx.test.platform.app.InstrumentationRegistry;
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.OutputStreamWriter;
41 import java.nio.charset.StandardCharsets;
42 
43 @RunWith(AndroidJUnit4.class)
44 public class DeleteKeepDataDeviceTest {
45     private static final String TAG = DeleteKeepDataDeviceTest.class.getName();
46     private static final String TEST_FILE_NAME = "my_test_file";
47     private static final String TEST_FILE_CONTENT = "testing";
48     private final Context mContext =
49             InstrumentationRegistry.getInstrumentation().getTargetContext();
50 
51     @Test
testWriteData()52     public void testWriteData() {
53         try (OutputStreamWriter writer = new OutputStreamWriter(
54                 mContext.openFileOutput(TEST_FILE_NAME, MODE_PRIVATE))) {
55             // Write to /data/user/[user_id]/[package_name]/files/
56             writer.write(TEST_FILE_CONTENT);
57             final File internalFile = new File(mContext.getFilesDir(), TEST_FILE_NAME);
58             assertTrue(internalFile.exists());
59             Log.i(TAG, "Wrote file to " + internalFile.getAbsolutePath());
60         } catch (IOException e) {
61             Log.e(TAG, "Failed to write internal data", e);
62         }
63         // Verify that the file content was written successfully
64         assertEquals(TEST_FILE_CONTENT, readDataUserFile());
65         // Write to external storage
66         final File externalFile = new File(mContext.getExternalFilesDir(null), TEST_FILE_NAME);
67         try (FileOutputStream fos = new FileOutputStream(externalFile)) {
68             fos.write(TEST_FILE_CONTENT.getBytes());
69         } catch (IOException e) {
70             Log.e(TAG, "Failed to write external data", e);
71         }
72         // Verify that the file content was written successfully
73         assertEquals(TEST_FILE_CONTENT, readExternalDataFile());
74     }
75 
76     @Test
testReadData()77     public void testReadData() {
78         assertEquals(TEST_FILE_CONTENT, readDataUserFile());
79         assertEquals(TEST_FILE_CONTENT, readExternalDataFile());
80     }
81 
82     @Test
testReadDataFail()83     public void testReadDataFail() {
84         assertNull(readDataUserFile());
85         assertNull(readExternalDataFile());
86     }
87 
readDataUserFile()88     private String readDataUserFile() {
89         byte[] buffer = new byte[1024];
90         ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
91         try (InputStream is = mContext.openFileInput(TEST_FILE_NAME)) {
92             int len;
93             while ((len = is.read(buffer)) != -1) {
94                 byteBuffer.write(buffer, 0, len);
95             }
96         } catch (IOException e) {
97             Log.e(TAG, "Failed to read internal data", e);
98             return null;
99         }
100         return byteBuffer.toString(StandardCharsets.UTF_8);
101     }
102 
readExternalDataFile()103     private String readExternalDataFile() {
104         byte[] buffer = new byte[1024];
105         ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
106         try (FileInputStream fis = new FileInputStream(
107                 new File(mContext.getExternalFilesDir(null), TEST_FILE_NAME))) {
108             int len;
109             while ((len = fis.read(buffer)) != -1) {
110                 byteBuffer.write(buffer, 0, len);
111             }
112         } catch (IOException e) {
113             Log.e(TAG, "Failed to read external data", e);
114             return null;
115         }
116         return byteBuffer.toString(StandardCharsets.UTF_8);
117     }
118 }
119