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 17 package com.android.tradefed.device; 18 19 import com.android.tradefed.util.CommandResult; 20 import com.android.tradefed.util.CommandStatus; 21 import com.android.tradefed.util.IRunUtil; 22 23 import junit.framework.TestCase; 24 25 import org.easymock.EasyMock; 26 27 import java.util.Collection; 28 29 /** 30 * Unit tests for {@link FastbootHelper}. 31 */ 32 public class FastbootHelperTest extends TestCase { 33 34 private IRunUtil mMockRunUtil; 35 private FastbootHelper mFastbootHelper; 36 37 /** 38 * {@inheritDoc} 39 */ 40 @Override 41 protected void setUp() throws Exception { 42 super.setUp(); 43 mMockRunUtil = EasyMock.createMock(IRunUtil.class); 44 mFastbootHelper = new FastbootHelper(mMockRunUtil, "fastboot"); 45 } 46 47 /** 48 * Verify the 'fastboot devices' output parsing 49 */ 50 public void testParseDevicesOnFastboot() { 51 Collection<String> deviceSerials = mFastbootHelper.parseDevices( 52 "04035EEB0B01F01C fastboot\n" + 53 "HT99PP800024 fastboot\n" + 54 "???????????? fastboot"); 55 assertEquals(2, deviceSerials.size()); 56 assertTrue(deviceSerials.contains("04035EEB0B01F01C")); 57 assertTrue(deviceSerials.contains("HT99PP800024")); 58 } 59 60 /** 61 * Verify 'fastboot devices' parsing with hyphenated serial number. 62 */ 63 public void testParseDevicesOnFastboot_hyphen() { 64 Collection<String> deviceSerials = mFastbootHelper.parseDevices( 65 "Foo-Bar123 fastboot"); 66 assertEquals(1, deviceSerials.size()); 67 assertTrue(deviceSerials.contains("Foo-Bar123")); 68 } 69 70 /** 71 * Verify the 'fastboot devices' output parsing when empty 72 */ 73 public void testParseDevicesOnFastboot_empty() { 74 Collection<String> deviceSerials = mFastbootHelper.parseDevices(""); 75 assertEquals(0, deviceSerials.size()); 76 } 77 78 /** 79 * Ensure that FastbootHelper cannot be created with incorrect params 80 */ 81 public void testConstructor_noRunUtil() { 82 try { 83 new FastbootHelper(null, "/fakepath/"); 84 fail("Should have thrown an exception"); 85 } catch (IllegalArgumentException expected) { 86 assertEquals("runUtil cannot be null", expected.getMessage()); 87 } 88 } 89 90 /** 91 * Ensure that FastbootHelper cannot be created with incorrect path params 92 */ 93 public void testConstructor_badPath() { 94 try { 95 new FastbootHelper(mMockRunUtil, null); 96 fail("Should have thrown an exception"); 97 } catch (IllegalArgumentException expected) { 98 assertEquals("fastboot cannot be null or empty", expected.getMessage()); 99 } 100 try { 101 new FastbootHelper(mMockRunUtil, ""); 102 fail("Should have thrown an exception"); 103 } catch (IllegalArgumentException expected) { 104 assertEquals("fastboot cannot be null or empty", expected.getMessage()); 105 } 106 } 107 108 /** 109 * Test that an older version of fastboot is still accepted 110 */ 111 public void testIsFastbootAvailable_oldVersion() { 112 CommandResult fakeRes = new CommandResult(CommandStatus.FAILED); 113 fakeRes.setStderr("Help doesn't exists. usage: fastboot"); 114 EasyMock.expect(mMockRunUtil.runTimedCmdSilently(EasyMock.anyLong(), 115 EasyMock.eq("fastboot"), EasyMock.eq("help"))).andReturn(fakeRes); 116 EasyMock.replay(mMockRunUtil); 117 assertTrue(mFastbootHelper.isFastbootAvailable()); 118 EasyMock.verify(mMockRunUtil); 119 } 120 121 /** 122 * Test that an older version of fastboot is still accepted 123 */ 124 public void testIsFastbootAvailable_noBinary() { 125 CommandResult fakeRes = new CommandResult(CommandStatus.FAILED); 126 fakeRes.setStderr("No command 'fastboot' found"); 127 EasyMock.expect(mMockRunUtil.runTimedCmdSilently(EasyMock.anyLong(), 128 EasyMock.eq("fastboot"), EasyMock.eq("help"))).andReturn(fakeRes); 129 EasyMock.replay(mMockRunUtil); 130 assertFalse(mFastbootHelper.isFastbootAvailable()); 131 EasyMock.verify(mMockRunUtil); 132 } 133 134 /** 135 * Test that get device returns null if command fails. 136 */ 137 public void testGetDevice_fail() { 138 CommandResult fakeRes = new CommandResult(CommandStatus.FAILED); 139 fakeRes.setStdout(""); 140 fakeRes.setStderr(""); 141 EasyMock.expect( 142 mMockRunUtil.runTimedCmdSilently( 143 EasyMock.anyLong(), 144 EasyMock.eq("fastboot"), 145 EasyMock.eq("devices"))) 146 .andReturn(fakeRes); 147 EasyMock.replay(mMockRunUtil); 148 assertTrue(mFastbootHelper.getDevices().isEmpty()); 149 EasyMock.verify(mMockRunUtil); 150 } 151 152 /** 153 * Test {@link FastbootHelper#executeCommand(String, String)} return the stdout on success. 154 */ 155 public void testExecuteCommand() { 156 final String expectedStdout = "stdout"; 157 CommandResult fakeRes = new CommandResult(CommandStatus.SUCCESS); 158 fakeRes.setStdout(expectedStdout); 159 EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(), EasyMock.eq("fastboot"), 160 EasyMock.eq("-s"), EasyMock.eq("SERIAL"), EasyMock.eq("wipe"))).andReturn(fakeRes); 161 EasyMock.replay(mMockRunUtil); 162 assertEquals(expectedStdout, mFastbootHelper.executeCommand("SERIAL", "wipe")); 163 EasyMock.verify(mMockRunUtil); 164 } 165 166 /** 167 * Test {@link FastbootHelper#executeCommand(String, String)} return null on failure 168 */ 169 public void testExecuteCommand_fail() { 170 CommandResult fakeRes = new CommandResult(CommandStatus.FAILED); 171 fakeRes.setStderr("error"); 172 EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(), EasyMock.eq("fastboot"), 173 EasyMock.eq("-s"), EasyMock.eq("SERIAL"), EasyMock.eq("wipe"))).andReturn(fakeRes); 174 EasyMock.replay(mMockRunUtil); 175 assertNull(mFastbootHelper.executeCommand("SERIAL", "wipe")); 176 EasyMock.verify(mMockRunUtil); 177 } 178 } 179