1/* 2 * Copyright (C) 2014 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 17package android.os; 18 19import android.os.FileBridge.FileBridgeOutputStream; 20import android.test.AndroidTestCase; 21import android.test.MoreAsserts; 22 23import libcore.io.Streams; 24 25import java.io.ByteArrayOutputStream; 26import java.io.File; 27import java.io.FileInputStream; 28import java.io.FileOutputStream; 29import java.io.IOException; 30import java.nio.charset.StandardCharsets; 31import java.util.Random; 32 33public class FileBridgeTest extends AndroidTestCase { 34 35 private File file; 36 private FileOutputStream fileOs; 37 private FileBridge bridge; 38 private FileBridgeOutputStream client; 39 40 @Override 41 protected void setUp() throws Exception { 42 super.setUp(); 43 44 file = getContext().getFileStreamPath("meow.dat"); 45 file.delete(); 46 47 fileOs = new FileOutputStream(file); 48 49 bridge = new FileBridge(); 50 bridge.setTargetFile(fileOs.getFD()); 51 bridge.start(); 52 client = new FileBridgeOutputStream(bridge.getClientSocket()); 53 } 54 55 @Override 56 protected void tearDown() throws Exception { 57 fileOs.close(); 58 file.delete(); 59 } 60 61 private void assertOpen() throws Exception { 62 assertFalse("expected open", bridge.isClosed()); 63 } 64 65 private void closeAndAssertClosed() throws Exception { 66 client.close(); 67 68 // Wait a beat for things to settle down 69 SystemClock.sleep(200); 70 assertTrue("expected closed", bridge.isClosed()); 71 } 72 73 private void assertContents(byte[] expected) throws Exception { 74 MoreAsserts.assertEquals(expected, Streams.readFully(new FileInputStream(file))); 75 } 76 77 public void testNoWriteNoSync() throws Exception { 78 assertOpen(); 79 closeAndAssertClosed(); 80 } 81 82 public void testNoWriteSync() throws Exception { 83 assertOpen(); 84 client.flush(); 85 closeAndAssertClosed(); 86 } 87 88 public void testWriteNoSync() throws Exception { 89 assertOpen(); 90 client.write("meow".getBytes(StandardCharsets.UTF_8)); 91 closeAndAssertClosed(); 92 assertContents("meow".getBytes(StandardCharsets.UTF_8)); 93 } 94 95 public void testWriteSync() throws Exception { 96 assertOpen(); 97 client.write("cake".getBytes(StandardCharsets.UTF_8)); 98 client.flush(); 99 closeAndAssertClosed(); 100 assertContents("cake".getBytes(StandardCharsets.UTF_8)); 101 } 102 103 public void testWriteSyncWrite() throws Exception { 104 assertOpen(); 105 client.write("meow".getBytes(StandardCharsets.UTF_8)); 106 client.flush(); 107 client.write("cake".getBytes(StandardCharsets.UTF_8)); 108 closeAndAssertClosed(); 109 assertContents("meowcake".getBytes(StandardCharsets.UTF_8)); 110 } 111 112 public void testEmptyWrite() throws Exception { 113 assertOpen(); 114 client.write(new byte[0]); 115 closeAndAssertClosed(); 116 assertContents(new byte[0]); 117 } 118 119 public void testWriteAfterClose() throws Exception { 120 assertOpen(); 121 client.write("meow".getBytes(StandardCharsets.UTF_8)); 122 closeAndAssertClosed(); 123 try { 124 client.write("cake".getBytes(StandardCharsets.UTF_8)); 125 fail("wrote after close!"); 126 } catch (IOException expected) { 127 } 128 assertContents("meow".getBytes(StandardCharsets.UTF_8)); 129 } 130 131 public void testRandomWrite() throws Exception { 132 final Random r = new Random(); 133 final ByteArrayOutputStream result = new ByteArrayOutputStream(); 134 135 for (int i = 0; i < 512; i++) { 136 final byte[] test = new byte[r.nextInt(24169)]; 137 r.nextBytes(test); 138 result.write(test); 139 client.write(test); 140 client.flush(); 141 } 142 143 closeAndAssertClosed(); 144 assertContents(result.toByteArray()); 145 } 146 147 public void testGiantWrite() throws Exception { 148 final byte[] test = new byte[263401]; 149 new Random().nextBytes(test); 150 151 assertOpen(); 152 client.write(test); 153 closeAndAssertClosed(); 154 assertContents(test); 155 } 156} 157