PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/coretests/src/android/app/backup/BackupDataTest.java

https://gitlab.com/AvayKumar/android_frameworks_base
Java | 291 lines | 234 code | 39 blank | 18 comment | 16 complexity | 52f5a72abbf132de73ed3f00e0856e57 MD5 | raw file
  1. /*
  2. * Copyright (C) 2013 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. package android.app.backup;
  17. import android.app.backup.BackupDataInput;
  18. import android.app.backup.BackupDataOutput;
  19. import android.content.res.AssetFileDescriptor;
  20. import android.content.res.AssetManager;
  21. import android.os.Bundle;
  22. import android.os.Environment;
  23. import android.os.ParcelFileDescriptor;
  24. import android.test.AndroidTestCase;
  25. import android.test.InstrumentationTestCase;
  26. import android.util.Base64;
  27. import android.util.Log;
  28. import org.json.JSONObject;
  29. import java.io.BufferedReader;
  30. import java.io.File;
  31. import java.io.FileInputStream;
  32. import java.io.FileNotFoundException;
  33. import java.io.FileOutputStream;
  34. import java.io.FileReader;
  35. import java.io.IOException;
  36. import java.io.InputStream;
  37. import java.io.InputStreamReader;
  38. import java.lang.Exception;
  39. import java.nio.ByteBuffer;
  40. public class BackupDataTest extends AndroidTestCase {
  41. private static final String KEY1 = "key1";
  42. private static final String KEY2 = "key2a";
  43. private static final String KEY3 = "key3bc";
  44. private static final String KEY4 = "key4dad"; // variable key lengths to test padding
  45. private static final String[] KEYS = {KEY1, KEY2, KEY3, KEY4};
  46. private static final String DATA1 = "abcdef";
  47. private static final String DATA2 = "abcdefg";
  48. private static final String DATA3 = "abcdefgh";
  49. private static final String DATA4 = "abcdeffhi"; //variable data lengths to test padding
  50. private static final String[] DATA = {DATA1, DATA2, DATA3, DATA4};
  51. private static final String TAG = "BackupDataTest";
  52. private File mFile;
  53. private ParcelFileDescriptor mDataFile;
  54. private File mDirectory;
  55. private Bundle mStatusBundle;
  56. private AssetManager mAssets;
  57. @Override
  58. protected void setUp() throws Exception {
  59. super.setUp();
  60. mDirectory = new File(Environment.getExternalStorageDirectory(), "test_data");
  61. mDirectory.mkdirs();
  62. mAssets = mContext.getAssets();
  63. }
  64. @Override
  65. protected void tearDown() throws Exception {
  66. super.tearDown();
  67. if (mDataFile != null) {
  68. mDataFile.close();
  69. }
  70. }
  71. public void testSingle() throws IOException {
  72. mFile = new File(mDirectory, "backup_mixed_sinlge.dat");
  73. openForWriting();
  74. BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
  75. writeEntity(bdo, KEY1, DATA1.getBytes());
  76. mDataFile.close();
  77. openForReading();
  78. BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
  79. int count = 0;
  80. while (bdi.readNextHeader()) {
  81. readAndVerifyEntity(bdi, KEY1, DATA1.getBytes());
  82. count++;
  83. }
  84. assertEquals("only one entity in this stream", 1, count);
  85. }
  86. public void testMultiple() throws IOException {
  87. mFile = new File(mDirectory, "backup_multiple_test.dat");
  88. openForWriting();
  89. BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
  90. for(int i = 0; i < KEYS.length; i++) {
  91. writeEntity(bdo, KEYS[i], DATA[i].getBytes());
  92. }
  93. mDataFile.close();
  94. openForReading();
  95. BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
  96. int count = 0;
  97. while (bdi.readNextHeader()) {
  98. readAndVerifyEntity(bdi, KEYS[count], DATA[count].getBytes());
  99. count++;
  100. }
  101. assertEquals("four entities in this stream", KEYS.length, count);
  102. }
  103. public void testDelete() throws IOException {
  104. mFile = new File(mDirectory, "backup_delete_test.dat");
  105. openForWriting();
  106. BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
  107. for(int i = 0; i < KEYS.length; i++) {
  108. deleteEntity(bdo, KEYS[i]);
  109. }
  110. mDataFile.close();
  111. openForReading();
  112. BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
  113. int count = 0;
  114. while (bdi.readNextHeader()) {
  115. readAndVerifyDeletedEntity(bdi, KEYS[count]);
  116. count++;
  117. }
  118. assertEquals("four deletes in this stream", KEYS.length, count);
  119. }
  120. public void testMixed() throws IOException {
  121. mFile = new File(mDirectory, "backup_mixed_test.dat");
  122. openForWriting();
  123. BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());
  124. int i = 0;
  125. deleteEntity(bdo, KEYS[i]); i++;
  126. writeEntity(bdo, KEYS[i], DATA[i].getBytes()); i++;
  127. writeEntity(bdo, KEYS[i], DATA[i].getBytes()); i++;
  128. deleteEntity(bdo, KEYS[i]); i++;
  129. mDataFile.close();
  130. openForReading();
  131. BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
  132. int out = 0;
  133. assertTrue(bdi.readNextHeader());
  134. readAndVerifyDeletedEntity(bdi, KEYS[out]); out++;
  135. assertTrue(bdi.readNextHeader());
  136. readAndVerifyEntity(bdi, KEYS[out], DATA[out].getBytes()); out++;
  137. assertTrue(bdi.readNextHeader());
  138. readAndVerifyEntity(bdi, KEYS[out], DATA[out].getBytes()); out++;
  139. assertTrue(bdi.readNextHeader());
  140. readAndVerifyDeletedEntity(bdi, KEYS[out]); out++;
  141. assertFalse("four items in this stream",
  142. bdi.readNextHeader());
  143. }
  144. public void testReadMockData() throws IOException {
  145. copyAssetToFile("backup_mock.dat", "backup_read_mock_test.dat");
  146. openForReading();
  147. BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
  148. BufferedReader truth = new BufferedReader(new InputStreamReader(
  149. mAssets.openFd("backup_mock.gld").createInputStream()));
  150. while( bdi.readNextHeader()) {
  151. String[] expected = truth.readLine().split(":");
  152. byte[] expectedBytes = null;
  153. if (expected.length > 1) {
  154. expectedBytes = Base64.decode(expected[1], Base64.DEFAULT);
  155. }
  156. String key = bdi.getKey();
  157. int dataSize = bdi.getDataSize();
  158. assertEquals("wrong key", expected[0], key);
  159. assertEquals("wrong length for key " + key,
  160. (expectedBytes == null ? -1: expectedBytes.length), dataSize);
  161. if (dataSize != -1) {
  162. byte[] buffer = new byte[dataSize];
  163. bdi.readEntityData(buffer, 0, dataSize);
  164. assertEquals("wrong data for key " + key, expected[1],
  165. Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
  166. }
  167. }
  168. assertNull("there are unused entries in the golden file", truth.readLine());
  169. }
  170. public void testReadRealData() throws IOException {
  171. copyAssetToFile("backup_real.dat", "backup_read_real_test.dat");
  172. openForReading();
  173. BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
  174. BufferedReader truth = new BufferedReader(new InputStreamReader(
  175. mAssets.openFd("backup_real.gld").createInputStream()));
  176. while(bdi.readNextHeader()) {
  177. String[] expected = truth.readLine().split(":");
  178. byte[] expectedBytes = null;
  179. if (expected.length > 1) {
  180. expectedBytes = Base64.decode(expected[1], Base64.DEFAULT);
  181. }
  182. String key = bdi.getKey();
  183. int dataSize = bdi.getDataSize();
  184. assertEquals("wrong key", expected[0], key);
  185. assertEquals("wrong length for key " + key,
  186. (expectedBytes == null ? -1: expectedBytes.length), dataSize);
  187. if (dataSize != -1) {
  188. byte[] buffer = new byte[dataSize];
  189. bdi.readEntityData(buffer, 0, dataSize);
  190. assertEquals("wrong data for key " + key, expected[1],
  191. Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
  192. }
  193. }
  194. assertNull("there are unused entries in the golden file", truth.readLine());
  195. }
  196. private void copyAssetToFile(String source, String destination) throws IOException {
  197. mFile = new File(mDirectory, destination);
  198. openForWriting();
  199. FileInputStream fileInputStream = mAssets.openFd(source).createInputStream();
  200. FileOutputStream fileOutputStream = new FileOutputStream(mDataFile.getFileDescriptor());
  201. byte[] copybuffer = new byte[1024];
  202. int numBytes = fileInputStream.read(copybuffer);
  203. fileOutputStream.write(copybuffer, 0, numBytes);
  204. fileOutputStream.close();
  205. }
  206. private void openForWriting() throws FileNotFoundException {
  207. mDataFile = ParcelFileDescriptor.open(mFile,
  208. ParcelFileDescriptor.MODE_WRITE_ONLY |
  209. ParcelFileDescriptor.MODE_CREATE |
  210. ParcelFileDescriptor.MODE_TRUNCATE); // Make an empty file if necessary
  211. }
  212. private void openForReading() throws FileNotFoundException {
  213. mDataFile = ParcelFileDescriptor.open(mFile,
  214. ParcelFileDescriptor.MODE_READ_ONLY |
  215. ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary
  216. }
  217. private void writeEntity(BackupDataOutput bdo, String key, byte[] data) throws IOException {
  218. int status = bdo.writeEntityHeader(key, data.length);
  219. // documentation says "number of bytes written" but that's not what we get:
  220. assertEquals(0, status);
  221. status = bdo.writeEntityData(data, data.length);
  222. // documentation says "number of bytes written" but that's not what we get:
  223. assertEquals(0, status);
  224. }
  225. private void deleteEntity(BackupDataOutput bdo, String key) throws IOException {
  226. int status = bdo.writeEntityHeader(key, -1);
  227. // documentation says "number of bytes written" but that's not what we get:
  228. assertEquals(0, status);
  229. }
  230. private void readAndVerifyEntity(BackupDataInput bdi, String expectedKey, byte[] expectedData)
  231. throws IOException {
  232. assertEquals("Key mismatch",
  233. expectedKey, bdi.getKey());
  234. assertEquals("data size mismatch",
  235. expectedData.length, bdi.getDataSize());
  236. byte[] data = new byte[bdi.getDataSize()];
  237. bdi.readEntityData(data, 0, bdi.getDataSize());
  238. assertEquals("payload size is wrong",
  239. expectedData.length, data.length);
  240. for (int i = 0; i < data.length; i++) {
  241. assertEquals("payload mismatch",
  242. expectedData[i], data[i]);
  243. }
  244. }
  245. private void readAndVerifyDeletedEntity(BackupDataInput bdi, String expectedKey)
  246. throws IOException {
  247. assertEquals("Key mismatch",
  248. expectedKey, bdi.getKey());
  249. assertEquals("deletion mis-reported",
  250. -1, bdi.getDataSize());
  251. }
  252. }