1 /* 2 * Copyright (C) 2012 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 android.filesystem.cts; 18 19 import static android.support.test.InstrumentationRegistry.getContext; 20 import static android.support.test.InstrumentationRegistry.getInstrumentation; 21 22 import android.support.test.runner.AndroidJUnit4; 23 import android.util.Log; 24 25 import com.android.compatibility.common.util.DeviceReportLog; 26 import com.android.compatibility.common.util.SystemUtil; 27 import org.junit.After; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.util.concurrent.atomic.AtomicBoolean; 33 import java.util.concurrent.atomic.AtomicInteger; 34 35 @RunWith(AndroidJUnit4.class) 36 public class AlmostFullTest { 37 private static final String DIR_INITIAL_FILL = "INITIAL_FILL"; 38 private static final String DIR_SEQ_UPDATE = "SEQ_UPDATE"; 39 private static final String DIR_RANDOM_WR = "RANDOM_WR"; 40 private static final String DIR_RANDOM_RD = "RANDOM_RD"; 41 private static final String TAG = "AlmostFullTest"; 42 private static final String REPORT_LOG_NAME = "CtsFileSystemTestCases"; 43 44 private static final long FREE_SPACE_FINAL = 1000L * 1024 * 1024L; 45 46 // test runner creates multiple instances at the begging. 47 // use that to fill disk only once. 48 // set as final to initialize it only once 49 private static final AtomicInteger mRefCounter = new AtomicInteger(0); 50 private static final AtomicBoolean mDiskFilled = new AtomicBoolean(false); 51 52 public AlmostFullTest() { 53 int currentCounter = mRefCounter.incrementAndGet(); 54 Log.i(TAG, "++currentCounter: " + currentCounter); 55 } 56 57 @Before 58 public void setUp() throws Exception { 59 if (mDiskFilled.compareAndSet(false, true)) { 60 Log.i(TAG, "Filling disk"); 61 // initial fill done in two stage as disk can be filled by other 62 // components 63 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 64 long diskToFill = freeDisk - FREE_SPACE_FINAL; 65 if (diskToFill >= 0) { 66 Log.i(TAG, "free disk " + freeDisk + ", to fill " + diskToFill); 67 } else { 68 Log.i(TAG, "free disk " + freeDisk + " too small, needs " + FREE_SPACE_FINAL); 69 return; 70 } 71 // Ensure MAX_SIZE_TO_FILL is an integral multiple of FileUtil.BUFFER_SIZE to avoid 72 // rounding errors caused by FileUtil.createNewFilledFile. See b/63535343. 73 final long MAX_FILE_SIZE_TO_FILL = FileUtil.BUFFER_SIZE * 100L; 74 long filled = 0; 75 while (filled < diskToFill) { 76 long toFill = diskToFill - filled; 77 if (toFill > MAX_FILE_SIZE_TO_FILL) { 78 toFill = MAX_FILE_SIZE_TO_FILL; 79 } 80 Log.i(TAG, "Generating file " + toFill); 81 FileUtil.createNewFilledFile(getContext(), 82 DIR_INITIAL_FILL, toFill); 83 filled += toFill; 84 } 85 } 86 Log.i(TAG, "free disk " + SystemUtil.getFreeDiskSize(getContext())); 87 } 88 89 @After 90 public void tearDown() throws Exception { 91 Log.i(TAG, "tearDown free disk " + SystemUtil.getFreeDiskSize(getContext())); 92 int currentCounter = mRefCounter.decrementAndGet(); 93 Log.i(TAG, "--currentCounter: " + currentCounter); 94 if (currentCounter == 0) { 95 FileUtil.removeFileOrDir(getContext(), DIR_INITIAL_FILL); 96 } 97 FileUtil.removeFileOrDir(getContext(), DIR_SEQ_UPDATE); 98 FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_WR); 99 FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_RD); 100 Log.i(TAG, "tearDown free disk " + SystemUtil.getFreeDiskSize(getContext())); 101 } 102 103 @Test 104 public void testSequentialUpdate() throws Exception { 105 // now about freeSpaceToLeave should be left 106 // and try updating exceeding the free space size 107 final long FILE_SIZE = 400L * 1024L * 1024L; 108 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 109 Log.i(TAG, "Now free space is " + freeDisk); 110 if (freeDisk < FILE_SIZE) { 111 Log.w(TAG, "too little space: " + freeDisk); 112 return; 113 } 114 final int BUFFER_SIZE = 10 * 1024 * 1024; 115 final int NUMBER_REPETITION = 10; 116 String streamName = "test_sequential_update"; 117 FileUtil.doSequentialUpdateTest(getContext(), DIR_SEQ_UPDATE, FILE_SIZE, BUFFER_SIZE, 118 NUMBER_REPETITION, REPORT_LOG_NAME, streamName); 119 } 120 121 // TODO: file size too small and caching will give wrong better result. 122 // needs to flush cache by reading big files per each read. 123 @Test 124 public void testRandomRead() throws Exception { 125 final int BUFFER_SIZE = 4 * 1024; 126 final long fileSize = 400L * 1024L * 1024L; 127 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 128 if (freeDisk < fileSize) { 129 Log.w(TAG, "too little space: " + freeDisk); 130 return; 131 } 132 String streamName = "test_random_read"; 133 DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName); 134 FileUtil.doRandomReadTest(getContext(), DIR_RANDOM_RD, report, fileSize, BUFFER_SIZE); 135 report.submit(getInstrumentation()); 136 } 137 138 @Test 139 public void testRandomUpdate() throws Exception { 140 final int BUFFER_SIZE = 4 * 1024; 141 final long fileSize = 256L * 1024L * 1024L; 142 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 143 if (freeDisk < fileSize) { 144 Log.w(TAG, "too little space: " + freeDisk); 145 return; 146 } 147 String streamName = "test_random_update"; 148 DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName); 149 FileUtil.doRandomWriteTest(getContext(), DIR_RANDOM_WR, report, fileSize, BUFFER_SIZE); 150 report.submit(getInstrumentation()); 151 } 152 } 153