Home | History | Annotate | Download | only in testtype
      1 /*
      2  * Copyright (C) 2010 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 com.android.tradefed.testtype;
     17 
     18 import static org.junit.Assert.*;
     19 
     20 import com.android.ddmlib.FileListingService;
     21 import com.android.ddmlib.IShellOutputReceiver;
     22 import com.android.ddmlib.testrunner.ITestRunListener;
     23 import com.android.tradefed.config.OptionSetter;
     24 import com.android.tradefed.device.CollectingOutputReceiver;
     25 import com.android.tradefed.device.DeviceNotAvailableException;
     26 import com.android.tradefed.device.ITestDevice;
     27 import com.android.tradefed.device.MockFileUtil;
     28 import com.android.tradefed.result.ITestInvocationListener;
     29 
     30 import org.easymock.EasyMock;
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.junit.runners.JUnit4;
     35 
     36 import java.io.File;
     37 import java.util.concurrent.TimeUnit;
     38 
     39 
     40 /** Unit tests for {@link GTest}. */
     41 @RunWith(JUnit4.class)
     42 public class GTestTest {
     43     private static final String GTEST_FLAG_FILTER = "--gtest_filter";
     44     private ITestInvocationListener mMockInvocationListener = null;
     45     private IShellOutputReceiver mMockReceiver = null;
     46     private ITestDevice mMockITestDevice = null;
     47     private GTest mGTest;
     48     private OptionSetter mSetter;
     49 
     50     /** Helper to initialize the various EasyMocks we'll need. */
     51     @Before
     52     public void setUp() throws Exception {
     53         mMockInvocationListener = EasyMock.createMock(ITestInvocationListener.class);
     54         mMockReceiver = EasyMock.createMock(IShellOutputReceiver.class);
     55         mMockITestDevice = EasyMock.createMock(ITestDevice.class);
     56         mMockReceiver.flush();
     57         EasyMock.expectLastCall().anyTimes();
     58         EasyMock.expect(mMockITestDevice.getSerialNumber()).andStubReturn("serial");
     59         mGTest = new GTest() {
     60             @Override
     61             IShellOutputReceiver createResultParser(String runName, ITestRunListener listener) {
     62                 return mMockReceiver;
     63             }
     64             @Override
     65             GTestXmlResultParser createXmlParser(String testRunName, ITestRunListener listener) {
     66                 return new GTestXmlResultParser(testRunName, listener) {
     67                     @Override
     68                     public void parseResult(File f, CollectingOutputReceiver output) {
     69                         return;
     70                     }
     71                 };
     72             }
     73         };
     74         mGTest.setDevice(mMockITestDevice);
     75         mSetter = new OptionSetter(mGTest);
     76     }
     77 
     78     /**
     79      * Helper that replays all mocks.
     80      */
     81     private void replayMocks() {
     82       EasyMock.replay(mMockInvocationListener, mMockITestDevice, mMockReceiver);
     83     }
     84 
     85     /**
     86      * Helper that verifies all mocks.
     87      */
     88     private void verifyMocks() {
     89       EasyMock.verify(mMockInvocationListener, mMockITestDevice, mMockReceiver);
     90     }
     91 
     92     /** Test run when the test dir is not found on the device. */
     93     @Test
     94     public void testRun_noTestDir() throws DeviceNotAvailableException {
     95         EasyMock.expect(mMockITestDevice.doesFileExist(GTest.DEFAULT_NATIVETEST_PATH))
     96                 .andReturn(false);
     97         replayMocks();
     98         mGTest.run(mMockInvocationListener);
     99         verifyMocks();
    100     }
    101 
    102     /** Test run when no device is set should throw an exception. */
    103     @Test
    104     public void testRun_noDevice() throws DeviceNotAvailableException {
    105         mGTest.setDevice(null);
    106         replayMocks();
    107         try {
    108             mGTest.run(mMockInvocationListener);
    109             fail("an exception should have been thrown");
    110         } catch (IllegalArgumentException e) {
    111             // expected
    112         }
    113         verifyMocks();
    114     }
    115 
    116     /** Test the run method for a couple tests */
    117     @Test
    118     public void testRun() throws DeviceNotAvailableException {
    119         final String nativeTestPath = GTest.DEFAULT_NATIVETEST_PATH;
    120         final String test1 = "test1";
    121         final String test2 = "test2";
    122         final String testPath1 = String.format("%s/%s", nativeTestPath, test1);
    123         final String testPath2 = String.format("%s/%s", nativeTestPath, test2);
    124 
    125         MockFileUtil.setMockDirContents(mMockITestDevice, nativeTestPath, test1, test2);
    126         EasyMock.expect(mMockITestDevice.doesFileExist(nativeTestPath)).andReturn(true);
    127         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath)).andReturn(true);
    128         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath + "/test1")).andReturn(false);
    129         // report the file as executable
    130         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + testPath1))
    131                 .andReturn("-rwxr-xr-x 1 root shell 1000 2009-01-01 00:00 " + testPath1);
    132         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath + "/test2")).andReturn(false);
    133         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + testPath2))
    134                 .andReturn("-rwxr-xr-x 1 root shell 1000 2009-01-01 00:00 " + testPath2);
    135         String[] files = new String[] {"test1", "test2"};
    136         EasyMock.expect(mMockITestDevice.getChildren(nativeTestPath)).andReturn(files);
    137         mMockITestDevice.executeShellCommand(EasyMock.contains(test1),
    138                 EasyMock.same(mMockReceiver), EasyMock.anyLong(),
    139                 (TimeUnit)EasyMock.anyObject(), EasyMock.anyInt());
    140         mMockITestDevice.executeShellCommand(EasyMock.contains(test2),
    141                 EasyMock.same(mMockReceiver), EasyMock.anyLong(),
    142                 (TimeUnit)EasyMock.anyObject(), EasyMock.anyInt());
    143 
    144         replayMocks();
    145 
    146         mGTest.run(mMockInvocationListener);
    147         verifyMocks();
    148     }
    149 
    150     /** Test the run method when module name is specified */
    151     @Test
    152     public void testRun_moduleName() throws DeviceNotAvailableException {
    153         final String module = "test1";
    154         final String modulePath = String.format("%s%s%s",
    155                 GTest.DEFAULT_NATIVETEST_PATH, FileListingService.FILE_SEPARATOR, module);
    156         MockFileUtil.setMockDirContents(mMockITestDevice, modulePath, new String[] {});
    157 
    158         mGTest.setModuleName(module);
    159 
    160         EasyMock.expect(mMockITestDevice.doesFileExist(modulePath)).andReturn(true);
    161         EasyMock.expect(mMockITestDevice.isDirectory(modulePath)).andReturn(false);
    162         mMockITestDevice.executeShellCommand(EasyMock.contains(modulePath),
    163                 EasyMock.same(mMockReceiver),
    164                 EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), EasyMock.anyInt());
    165         // report the file as executable
    166         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + modulePath))
    167                 .andReturn("-rwxr-xr-x 1 root shell 1000 2009-01-01 00:00 " + modulePath);
    168 
    169         replayMocks();
    170 
    171         mGTest.run(mMockInvocationListener);
    172         verifyMocks();
    173     }
    174 
    175     /** Test the run method for a test in a subdirectory */
    176     @Test
    177     public void testRun_nested() throws DeviceNotAvailableException {
    178         final String nativeTestPath = GTest.DEFAULT_NATIVETEST_PATH;
    179         final String subFolderName = "subFolder";
    180         final String test1 = "test1";
    181         final String test1Path = String.format("%s%s%s%s%s", nativeTestPath,
    182                 FileListingService.FILE_SEPARATOR,
    183                 subFolderName,
    184                 FileListingService.FILE_SEPARATOR, test1);
    185         MockFileUtil.setMockDirPath(mMockITestDevice, nativeTestPath, subFolderName, test1);
    186         EasyMock.expect(mMockITestDevice.doesFileExist(nativeTestPath)).andReturn(true);
    187         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath)).andReturn(true);
    188         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath + "/" + subFolderName))
    189                 .andReturn(true);
    190         EasyMock.expect(mMockITestDevice.isDirectory(test1Path)).andReturn(false);
    191         // report the file as executable
    192         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + test1Path))
    193                 .andReturn("-rwxr-xr-x 1 root shell 1000 2009-01-01 00:00 " + test1Path);
    194         String[] files = new String[] {subFolderName};
    195         EasyMock.expect(mMockITestDevice.getChildren(nativeTestPath)).andReturn(files);
    196         String[] files2 = new String[] {"test1"};
    197         EasyMock.expect(mMockITestDevice.getChildren(nativeTestPath + "/" + subFolderName))
    198                 .andReturn(files2);
    199         mMockITestDevice.executeShellCommand(EasyMock.contains(test1Path),
    200                 EasyMock.same(mMockReceiver),
    201                 EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), EasyMock.anyInt());
    202 
    203         replayMocks();
    204 
    205         mGTest.run(mMockInvocationListener);
    206         verifyMocks();
    207     }
    208 
    209     /**
    210      * Helper function to do the actual filtering test.
    211      *
    212      * @param filterString The string to search for in the Mock, to verify filtering was called
    213      * @throws DeviceNotAvailableException
    214      */
    215     private void doTestFilter(String filterString) throws DeviceNotAvailableException {
    216         String nativeTestPath = GTest.DEFAULT_NATIVETEST_PATH;
    217         String testPath = nativeTestPath + "/test1";
    218         // configure the mock file system to have a single test
    219         MockFileUtil.setMockDirContents(mMockITestDevice, nativeTestPath, "test1");
    220         EasyMock.expect(mMockITestDevice.doesFileExist(nativeTestPath)).andReturn(true);
    221         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath)).andReturn(true);
    222         EasyMock.expect(mMockITestDevice.isDirectory(testPath)).andReturn(false);
    223         // report the file as executable
    224         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + testPath))
    225                 .andReturn("-rwxr-xr-x 1 root shell 1000 2009-01-01 00:00 " + testPath);
    226         String[] files = new String[] {"test1"};
    227         EasyMock.expect(mMockITestDevice.getChildren(nativeTestPath)).andReturn(files);
    228         mMockITestDevice.executeShellCommand(EasyMock.contains(filterString),
    229                 EasyMock.same(mMockReceiver), EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(),
    230                 EasyMock.anyInt());
    231         replayMocks();
    232         mGTest.run(mMockInvocationListener);
    233 
    234         verifyMocks();
    235     }
    236 
    237     /** Test the include filtering of test methods. */
    238     @Test
    239     public void testIncludeFilter() throws DeviceNotAvailableException {
    240         String includeFilter1 = "abc";
    241         String includeFilter2 = "def";
    242         mGTest.addIncludeFilter(includeFilter1);
    243         mGTest.addIncludeFilter(includeFilter2);
    244         doTestFilter(String.format("%s=%s:%s", GTEST_FLAG_FILTER, includeFilter1, includeFilter2));
    245     }
    246 
    247     /** Test the exclude filtering of test methods. */
    248     @Test
    249     public void testExcludeFilter() throws DeviceNotAvailableException {
    250         String excludeFilter1 = "*don?tRunMe*";
    251         mGTest.addExcludeFilter(excludeFilter1);
    252 
    253         doTestFilter(String.format(
    254                 "%s=-%s", GTEST_FLAG_FILTER, excludeFilter1));
    255     }
    256 
    257     /** Test simultaneous include and exclude filtering of test methods. */
    258     @Test
    259     public void testIncludeAndExcludeFilters() throws DeviceNotAvailableException {
    260         String includeFilter1 = "pleaseRunMe";
    261         String includeFilter2 = "andMe";
    262         String excludeFilter1 = "dontRunMe";
    263         String excludeFilter2 = "orMe";
    264         mGTest.addIncludeFilter(includeFilter1);
    265         mGTest.addExcludeFilter(excludeFilter1);
    266         mGTest.addIncludeFilter(includeFilter2);
    267         mGTest.addExcludeFilter(excludeFilter2);
    268 
    269         doTestFilter(String.format("%s=%s:%s-%s:%s", GTEST_FLAG_FILTER,
    270               includeFilter1, includeFilter2, excludeFilter1, excludeFilter2));
    271     }
    272 
    273     /** Test behavior for command lines too long to be run by ADB */
    274     @Test
    275     public void testCommandTooLong() throws DeviceNotAvailableException {
    276         String deviceScriptPath = "/data/local/tmp/gtest_script.sh";
    277         StringBuilder filterString = new StringBuilder(GTEST_FLAG_FILTER);
    278         filterString.append("=-");
    279         for (int i = 0; i < 100; i++) {
    280             if (i != 0) {
    281                 filterString.append(":");
    282             }
    283             String filter = String.format("ExcludeClass%d", i);
    284             filterString.append(filter);
    285             mGTest.addExcludeFilter(filter);
    286         }
    287         // filter string will be longer than GTest.GTEST_CMD_CHAR_LIMIT
    288 
    289         String nativeTestPath = GTest.DEFAULT_NATIVETEST_PATH;
    290         String testPath = nativeTestPath + "/test1";
    291         // configure the mock file system to have a single test
    292         MockFileUtil.setMockDirContents(mMockITestDevice, nativeTestPath, "test1");
    293         EasyMock.expect(mMockITestDevice.doesFileExist(nativeTestPath)).andReturn(true);
    294         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath)).andReturn(true);
    295         EasyMock.expect(mMockITestDevice.isDirectory(testPath)).andReturn(false);
    296         // report the file as executable
    297         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + testPath))
    298                 .andReturn("-rwxr-xr-x 1 root shell 1000 2009-01-01 00:00 " + testPath);
    299         String[] files = new String[] {"test1"};
    300         EasyMock.expect(mMockITestDevice.getChildren(nativeTestPath)).andReturn(files);
    301         // Expect push of script file
    302         EasyMock.expect(mMockITestDevice.pushString(EasyMock.<String>anyObject(),
    303                 EasyMock.eq(deviceScriptPath))).andReturn(Boolean.TRUE);
    304         // chmod 755 for the shell script
    305         EasyMock.expect(mMockITestDevice.executeShellCommand(EasyMock.contains("chmod")))
    306                 .andReturn("")
    307                 .times(1);
    308         // Expect command to run shell script, rather than direct adb command
    309         mMockITestDevice.executeShellCommand(EasyMock.eq(String.format("sh %s", deviceScriptPath)),
    310                 EasyMock.same(mMockReceiver), EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(),
    311                 EasyMock.anyInt());
    312         // Expect deletion of file on device
    313         EasyMock.expect(mMockITestDevice.executeShellCommand(
    314                 EasyMock.eq(String.format("rm %s", deviceScriptPath)))).andReturn("");
    315         replayMocks();
    316         mGTest.run(mMockInvocationListener);
    317 
    318         verifyMocks();
    319     }
    320 
    321     /** Empty file exclusion regex filter should not skip any files */
    322     @Test
    323     public void testFileExclusionRegexFilter_emptyfilters() throws Exception {
    324         // report /test_file as executable
    325         ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class);
    326         EasyMock.expect(mockDevice.executeShellCommand("ls -l /test_file"))
    327                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 /test_file");
    328         EasyMock.replay(mockDevice);
    329         mGTest.setDevice(mockDevice);
    330         assertFalse(mGTest.shouldSkipFile("/test_file"));
    331         EasyMock.verify(mockDevice);
    332     }
    333 
    334     /** File exclusion regex filter should skip invalid filepath. */
    335     @Test
    336     public void testFileExclusionRegexFilter_invalidInputString() throws Exception {
    337         assertTrue(mGTest.shouldSkipFile(null));
    338         assertTrue(mGTest.shouldSkipFile(""));
    339     }
    340 
    341     /** File exclusion regex filter should skip matched filepaths. */
    342     @Test
    343     public void testFileExclusionRegexFilter_skipMatched() throws Exception {
    344         // report all files as executable
    345         ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class);
    346         EasyMock.expect(mockDevice.executeShellCommand("ls -l /some/path/file/run_me"))
    347                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 /some/path/file/run_me");
    348         EasyMock.expect(mockDevice.executeShellCommand("ls -l /some/path/file/run_me2"))
    349                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 /some/path/file/run_me2");
    350         EasyMock.expect(mockDevice.executeShellCommand("ls -l /some/path/file/run_me.not"))
    351                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 /some/path/file/run_me.not");
    352         EasyMock.replay(mockDevice);
    353         mGTest.setDevice(mockDevice);
    354         // Skip files ending in .not
    355         mGTest.addFileExclusionFilterRegex(".*\\.not");
    356         assertFalse(mGTest.shouldSkipFile("/some/path/file/run_me"));
    357         assertFalse(mGTest.shouldSkipFile("/some/path/file/run_me2"));
    358         assertTrue(mGTest.shouldSkipFile("/some/path/file/run_me.not"));
    359         EasyMock.verify(mockDevice);
    360     }
    361 
    362     /** File exclusion regex filter for multi filters. */
    363     @Test
    364     public void testFileExclusionRegexFilter_skipMultiMatched() throws Exception {
    365         // report all files as executable
    366         ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class);
    367         EasyMock.expect(mockDevice.executeShellCommand("ls -l /some/path/file/run_me"))
    368                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 /some/path/file/run_me");
    369         EasyMock.expect(mockDevice.executeShellCommand("ls -l /some/path/file/run_me.not"))
    370                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 /some/path/file/run_me.not");
    371         EasyMock.expect(mockDevice.executeShellCommand("ls -l /some/path/file/run_me.not2"))
    372                 .andReturn(
    373                         "-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 /some/path/file/run_me.not2");
    374         EasyMock.replay(mockDevice);
    375         mGTest.setDevice(mockDevice);
    376         // Skip files ending in .not
    377         mGTest.addFileExclusionFilterRegex(".*\\.not");
    378         // Also skip files ending in .not2
    379         mGTest.addFileExclusionFilterRegex(".*\\.not2");
    380         assertFalse(mGTest.shouldSkipFile("/some/path/file/run_me"));
    381         assertTrue(mGTest.shouldSkipFile("/some/path/file/run_me.not"));
    382         assertTrue(mGTest.shouldSkipFile("/some/path/file/run_me.not2"));
    383     }
    384 
    385     /** Test the run method for a couple tests */
    386     @Test
    387     public void testRunXml() throws Exception {
    388         mSetter.setOptionValue("xml-output", "true");
    389 
    390         final String nativeTestPath = GTest.DEFAULT_NATIVETEST_PATH;
    391         final String test1 = "test1";
    392         final String test2 = "test2";
    393         final String testPath1 = String.format("%s/%s", nativeTestPath, test1);
    394         final String testPath2 = String.format("%s/%s", nativeTestPath, test2);
    395 
    396         MockFileUtil.setMockDirContents(mMockITestDevice, nativeTestPath, test1, test2);
    397         EasyMock.expect(mMockITestDevice.doesFileExist(nativeTestPath)).andReturn(true);
    398         EasyMock.expect(mMockITestDevice.isDirectory(nativeTestPath)).andReturn(true);
    399         EasyMock.expect(mMockITestDevice.isDirectory(testPath1)).andReturn(false);
    400         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + testPath1))
    401                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 " + testPath1);
    402         EasyMock.expect(mMockITestDevice.isDirectory(testPath2)).andReturn(false);
    403         EasyMock.expect(mMockITestDevice.executeShellCommand("ls -l " + testPath2))
    404                 .andReturn("-rwxr-xr-x 1 root shell 7 2009-01-01 00:00 " + testPath2);
    405         String[] files = new String[] {"test1", "test2"};
    406         EasyMock.expect(mMockITestDevice.getChildren(nativeTestPath)).andReturn(files);
    407         EasyMock.expect(mMockITestDevice.executeShellCommand(EasyMock.contains("rm")))
    408                 .andReturn("")
    409                 .times(2);
    410         EasyMock.expect(mMockITestDevice.pullFile((String)EasyMock.anyObject(),
    411                 (File)EasyMock.anyObject())).andStubReturn(true);
    412         mMockITestDevice.executeShellCommand(EasyMock.contains(test1),
    413                 (CollectingOutputReceiver) EasyMock.anyObject(),
    414                 EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), EasyMock.anyInt());
    415         mMockITestDevice.executeShellCommand(EasyMock.contains(test2),
    416                 (CollectingOutputReceiver) EasyMock.anyObject(),
    417                 EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), EasyMock.anyInt());
    418         replayMocks();
    419 
    420         mGTest.run(mMockInvocationListener);
    421         verifyMocks();
    422     }
    423 
    424     @Test
    425     public void testGetFileName() {
    426         String expected = "bar";
    427         String s1 = "/foo/" + expected;
    428         String s2 = expected;
    429         String s3 = "/foo/";
    430         assertEquals(expected, mGTest.getFileName(s1));
    431         assertEquals(expected, mGTest.getFileName(s2));
    432         try {
    433             mGTest.getFileName(s3);
    434             fail("Expected IllegalArgumentException not thrown");
    435         } catch (IllegalArgumentException iae) {
    436             // expected
    437         }
    438     }
    439 
    440     /** Test the include filtering by file of test methods. */
    441     @Test
    442     public void testFileFilter() throws Exception {
    443         String fileFilter = "presubmit";
    444         mSetter.setOptionValue("test-filter-key", fileFilter);
    445         String expectedFilterFile = String.format("%s/test1%s",
    446                 GTest.DEFAULT_NATIVETEST_PATH, GTest.FILTER_EXTENSION);
    447         String fakeContent = "{\n" +
    448                              "    \"presubmit\": {\n" +
    449                              "        \"filter\": \"Foo1.*:Foo2.*\"\n" +
    450                              "    },\n" +
    451                              "    \"continuous\": {\n" +
    452                              "        \"filter\": \"Foo1.*:Foo2.*:Bar.*\"\n" +
    453                              "    }\n" +
    454                              "}\n";
    455         EasyMock.expect(mMockITestDevice.doesFileExist(expectedFilterFile)).andReturn(true);
    456         EasyMock.expect(mMockITestDevice.executeShellCommand("cat \"" + expectedFilterFile + "\""))
    457                 .andReturn(fakeContent);
    458         doTestFilter(String.format("%s=%s", GTEST_FLAG_FILTER, "Foo1.*:Foo2.*"));
    459     }
    460 
    461     /**
    462      * Test the include filtering by providing a non existing filter. No filter will be applied in
    463      * this case.
    464      */
    465     @Test
    466     public void testFileFilter_notfound() throws Exception {
    467         String fileFilter = "garbage";
    468         mSetter.setOptionValue("test-filter-key", fileFilter);
    469         String expectedFilterFile = String.format("%s/test1%s",
    470                 GTest.DEFAULT_NATIVETEST_PATH, GTest.FILTER_EXTENSION);
    471         String fakeContent = "{\n" +
    472                              "    \"presubmit\": {\n" +
    473                              "        \"filter\": \"Foo1.*:Foo2.*\"\n" +
    474                              "    },\n" +
    475                              "    \"continuous\": {\n" +
    476                              "        \"filter\": \"Foo1.*:Foo2.*:Bar.*\"\n" +
    477                              "    }\n" +
    478                              "}\n";
    479         EasyMock.expect(mMockITestDevice.doesFileExist(expectedFilterFile)).andReturn(true);
    480         EasyMock.expect(mMockITestDevice.executeShellCommand("cat \"" + expectedFilterFile + "\""))
    481                 .andReturn(fakeContent);
    482         doTestFilter("");
    483     }
    484 
    485     /**
    486      * Test {@link GTest#getGTestCmdLine(String, String) with default options.
    487      */
    488     @Test
    489     public void testGetGTestCmdLine_defaults() {
    490         String cmd_line = mGTest.getGTestCmdLine("test_path", "flags");
    491         assertEquals("test_path flags", cmd_line);
    492     }
    493 
    494     /**
    495      * Test {@link GTest#getGTestCmdLine(String, String) with non-default user.
    496      */
    497     @Test
    498     public void testGetGTestCmdLine_runAs() throws Exception {
    499         mSetter.setOptionValue("run-test-as", "shell");
    500 
    501         String cmd_line = mGTest.getGTestCmdLine("test_path", "flags");
    502         assertEquals("su shell test_path flags", cmd_line);
    503     }
    504 
    505     /** Test GTest command line string for sharded tests. */
    506     @Test
    507     public void testGetGTestCmdLine_testShard() {
    508         mGTest.setShardIndex(1);
    509         mGTest.setShardCount(3);
    510 
    511         String cmd_line = mGTest.getGTestCmdLine("test_path", "flags");
    512         assertEquals("GTEST_SHARD_INDEX=1 GTEST_TOTAL_SHARDS=3 test_path flags", cmd_line);
    513     }
    514 
    515     /**
    516      * Verifies that {@link GTest#isDeviceFileExecutable(String)} recognizes regular executable file
    517      *
    518      * @throws Exception
    519      */
    520     @Test
    521     public void testIsDeviceFileExecutable_executable_rwx() throws Exception {
    522         ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class);
    523         EasyMock.expect(mockDevice.executeShellCommand("ls -l /system/bin/ping"))
    524                 .andReturn("-rwxr-xr-x 1 root shell 42824 2009-01-01 00:00 /system/bin/ping");
    525         EasyMock.replay(mockDevice);
    526         mGTest.setDevice(mockDevice);
    527         assertTrue(mGTest.isDeviceFileExecutable("/system/bin/ping"));
    528         EasyMock.verify(mockDevice);
    529     }
    530 
    531     /**
    532      * Verifies that {@link GTest#isDeviceFileExecutable(String)} recognizes symlink'd executable
    533      * file
    534      *
    535      * @throws Exception
    536      */
    537     @Test
    538     public void testIsDeviceFileExecutable_executable_lrwx() throws Exception {
    539         ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class);
    540         EasyMock.expect(mockDevice.executeShellCommand("ls -l /system/bin/start"))
    541                 .andReturn(
    542                         "lrwxr-xr-x 1 root shell 7 2009-01-01 00:00 /system/bin/start -> toolbox");
    543         EasyMock.replay(mockDevice);
    544         mGTest.setDevice(mockDevice);
    545         assertTrue(mGTest.isDeviceFileExecutable("/system/bin/start"));
    546         EasyMock.verify(mockDevice);
    547     }
    548 
    549     /**
    550      * Verifies that {@link GTest#isDeviceFileExecutable(String)} recognizes non-executable file
    551      *
    552      * @throws Exception
    553      */
    554     @Test
    555     public void testIsDeviceFileExecutable_notExecutable() throws Exception {
    556         ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class);
    557         EasyMock.expect(mockDevice.executeShellCommand("ls -l /system/build.prop"))
    558                 .andReturn("-rw-r--r-- 1 root root 5020 2009-01-01 00:00 /system/build.prop");
    559         EasyMock.replay(mockDevice);
    560         mGTest.setDevice(mockDevice);
    561         assertFalse(mGTest.isDeviceFileExecutable("/system/build.prop"));
    562         EasyMock.verify(mockDevice);
    563     }
    564 
    565     /**
    566      * Verifies that {@link GTest#isDeviceFileExecutable(String)} recognizes a directory listing
    567      *
    568      * @throws Exception
    569      */
    570     @Test
    571     public void testIsDeviceFileExecutable_directory() throws Exception {
    572         ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class);
    573         EasyMock.expect(mockDevice.executeShellCommand("ls -l /system"))
    574                 .andReturn(
    575                         "total 416\n"
    576                                 + "drwxr-xr-x 74 root root    4096 2009-01-01 00:00 app\n"
    577                                 + "drwxr-xr-x  3 root shell   8192 2009-01-01 00:00 bin\n"
    578                                 + "-rw-r--r--  1 root root    5020 2009-01-01 00:00 build.prop\n"
    579                                 + "drwxr-xr-x 15 root root    4096 2009-01-01 00:00 etc\n"
    580                                 + "drwxr-xr-x  2 root root    4096 2009-01-01 00:00 fake-libs\n"
    581                                 + "drwxr-xr-x  2 root root    8192 2009-01-01 00:00 fonts\n"
    582                                 + "drwxr-xr-x  4 root root    4096 2009-01-01 00:00 framework\n"
    583                                 + "drwxr-xr-x  6 root root    8192 2009-01-01 00:00 lib\n"
    584                                 + "drwx------  2 root root    4096 1970-01-01 00:00 lost+found\n"
    585                                 + "drwxr-xr-x  3 root root    4096 2009-01-01 00:00 media\n"
    586                                 + "drwxr-xr-x 68 root root    4096 2009-01-01 00:00 priv-app\n"
    587                                 + "-rw-r--r--  1 root root  137093 2009-01-01 00:00 recovery-from-boot.p\n"
    588                                 + "drwxr-xr-x  9 root root    4096 2009-01-01 00:00 usr\n"
    589                                 + "drwxr-xr-x  8 root shell   4096 2009-01-01 00:00 vendor\n"
    590                                 + "drwxr-xr-x  2 root shell   4096 2009-01-01 00:00 xbin\n");
    591         EasyMock.replay(mockDevice);
    592         mGTest.setDevice(mockDevice);
    593         assertFalse(mGTest.isDeviceFileExecutable("/system"));
    594         EasyMock.verify(mockDevice);
    595     }
    596 }
    597