Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c46760b authored by Kenny Root's avatar Kenny Root
Browse files

Make sure AppCacheTest casts to long

AppCacheTest was multiplying two integers that were too large for
devices that had more free space than 32-bits could represent.

Hopefully no one releases devices with 1TB soon.

The testAppCacheClear test doesn't work currently because it has an
"imax" attribute that doesn't allow the disk to be filled up entirely.
It only writes a couple hundred MB before it gives up.

Bug: 3216419
Change-Id: Ie371f42211bab65f2c767b36d9837ce76922501e
parent 02e852f9
Loading
Loading
Loading
Loading
+35 −18
Original line number Diff line number Diff line
@@ -35,14 +35,17 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AppCacheTest extends AndroidTestCase {
    private static final boolean localLOGV = false;
    public static final String TAG="AppCacheTest";
    public final long MAX_WAIT_TIME=60*1000;
    public final long WAIT_TIME_INCR=10*1000;
    private static final int THRESHOLD=5;
    private static final int ACTUAL_THRESHOLD=10;
    private static final long THRESHOLD=5;
    private static final long ACTUAL_THRESHOLD=10;
    
    @Override
    protected void setUp() throws Exception {
@@ -99,10 +102,12 @@ public class AppCacheTest extends AndroidTestCase {
        Log.w(TAG, "errMsg="+errMsg);
        fail(errMsg);
    }

    void failStr(Exception e) {
        Log.w(TAG, "e.getMessage="+e.getMessage());
        Log.w(TAG, "e="+e);
    }

    long getFreeStorageBlks(StatFs st) {
        st.restat("/data");
        return st.getFreeBlocks();
@@ -110,8 +115,9 @@ public class AppCacheTest extends AndroidTestCase {

    long getFreeStorageSize(StatFs st) {
        st.restat("/data");
        return (st.getFreeBlocks()*st.getBlockSize());
        return (long) st.getFreeBlocks() * (long) st.getBlockSize();
    }

    @LargeTest
    public void testFreeApplicationCacheAllFiles() throws Exception {
        boolean TRACKING = true;
@@ -124,7 +130,9 @@ public class AppCacheTest extends AndroidTestCase {
        long blks2 = getFreeStorageBlks(st);
        if(localLOGV || TRACKING) Log.i(TAG, "blk1="+blks1+", blks2="+blks2);
        //this should free up the test files that were created earlier
        invokePMFreeApplicationCache(availableMem);
        if (!invokePMFreeApplicationCache(availableMem)) {
            fail("Could not successfully invoke PackageManager free app cache API");
        }
        long blks3 = getFreeStorageBlks(st);
        if(localLOGV || TRACKING) Log.i(TAG, "blks3="+blks3);
        verifyTestFiles1(cacheDir, "testtmpdir", 5);
@@ -139,7 +147,9 @@ public class AppCacheTest extends AndroidTestCase {
        long blks2 = getFreeStorageBlks(st);
        Log.i(TAG, "blk1="+blks1+", blks2="+blks2);
        long diff = (blks1-blks2-2);
        assertTrue(invokePMFreeApplicationCache(diff*st.getBlockSize()));    
        if (!invokePMFreeApplicationCache(diff * st.getBlockSize())) {
            fail("Could not successfully invoke PackageManager free app cache API");
        }
        long blks3 = getFreeStorageBlks(st);
        //blks3 should be greater than blks2 and less than blks1
        if(!((blks3 <= blks1) && (blks3 >= blks2))) {
@@ -257,7 +267,7 @@ public class AppCacheTest extends AndroidTestCase {
        return sbuffer.getBytes();
    }

    long getFileNumBlocks(long fileSize, int blkSize) {
    long getFileNumBlocks(long fileSize, long blkSize) {
        long ret = fileSize/blkSize;
        if(ret*blkSize < fileSize) {
            ret++;
@@ -269,10 +279,10 @@ public class AppCacheTest extends AndroidTestCase {
    public void testAppCacheClear() {
        String dataDir="/data/data";
        StatFs st = new StatFs(dataDir);
        int blkSize = st.getBlockSize();
        int totBlks = st.getBlockCount();
        long blkSize = st.getBlockSize();
        long totBlks = st.getBlockCount();
        long availableBlks = st.getFreeBlocks();
        long thresholdBlks = (totBlks*THRESHOLD)/100;
        long thresholdBlks = (totBlks * THRESHOLD) / 100L;
        String testDirName = "testdir";
        //create directory in cache
        File testDir = new File(mContext.getCacheDir(),  testDirName);
@@ -345,7 +355,7 @@ public class AppCacheTest extends AndroidTestCase {
        if((fileSize > (shouldFree-blkSize) && (fileSize < (shouldFree+blkSize)))) {
            Log.i(TAG, "passed");
        }
        assertTrue(removedFlag);
        assertTrue("Files should have been removed", removedFlag);
    }
    
    //createTestFiles(new File(super.getContext().getCacheDir(), "testtmp", "dir", 3)
@@ -377,11 +387,16 @@ public class AppCacheTest extends AndroidTestCase {
    }

    void verifyTestFiles1(File cacheDir, String testFilePrefix, int numTestFiles) {
        List<String> files = new ArrayList<String>();
        for(int i = 0; i < numTestFiles; i++) {
            File file1 = new File(cacheDir, testFilePrefix+i+".txt");
            if(file1.exists()) {
                fail("file:"+file1+" should not exist");
                files.add(file1.getName());
            }
        }
        if (files.size() > 0) {
            fail("Files should have been deleted: "
                    + Arrays.toString(files.toArray(new String[files.size()])));
        }
    }

@@ -639,7 +654,9 @@ public class AppCacheTest extends AndroidTestCase {
        PendingIntent pi = PendingIntent.getBroadcast(mContext,
                0,  new Intent(FreeStorageReceiver.ACTION_FREE), 0);
        // Invoke PackageManager api
        invokePMFreeStorage(availableMem, receiver, pi);
        if (!invokePMFreeStorage(availableMem, receiver, pi)) {
            fail("Could not invoke PackageManager free storage API");
        }
        long blks3 = getFreeStorageBlks(st);
        if(localLOGV || TRACKING) Log.i(TAG, "Available blocks after freeing cache"+blks3);
        assertEquals(receiver.getResultCode(), 1);