1#!/usr/bin/env python3 2# 3# Copyright 2017 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import unittest 18import mock 19from acts.controllers import adb 20from acts.controllers.adb_lib.error import AdbCommandError 21from acts.controllers.adb_lib.error import AdbError 22 23 24class MockJob(object): 25 def __init__(self, exit_status=0, stderr='', stdout=''): 26 self.exit_status = exit_status 27 self.stderr = stderr 28 self.stdout = stdout 29 30 31class MockAdbProxy(adb.AdbProxy): 32 def __init__(self): 33 pass 34 35 36class ADBTest(unittest.TestCase): 37 """A class for testing acts/controllers/adb.py""" 38 39 def test__exec_cmd_failure_old_adb(self): 40 mock_job = MockJob(exit_status=1, stderr='error: device not found') 41 cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"'] 42 with mock.patch('acts.libs.proc.job.run', return_value=mock_job): 43 with self.assertRaises(AdbError): 44 MockAdbProxy()._exec_cmd(cmd) 45 46 def test__exec_cmd_failure_new_adb(self): 47 mock_job = MockJob( 48 exit_status=1, stderr='error: device \'DEADBEEF\' not found') 49 cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"'] 50 with mock.patch('acts.libs.proc.job.run', return_value=mock_job): 51 with self.assertRaises(AdbError): 52 MockAdbProxy()._exec_cmd(cmd) 53 54 def test__exec_cmd_pass_basic(self): 55 mock_job = MockJob(exit_status=0, stderr='DEADBEEF', stdout='FEEDACAB') 56 cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"'] 57 with mock.patch('acts.libs.proc.job.run', return_value=mock_job): 58 result = MockAdbProxy()._exec_cmd(cmd) 59 self.assertEqual(result, 'FEEDACAB') 60 61 def test__exec_cmd_ignore_status(self): 62 mock_job = MockJob(exit_status=0, stderr='DEADBEEF', stdout='') 63 cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"'] 64 with mock.patch('acts.libs.proc.job.run', return_value=mock_job): 65 result = MockAdbProxy()._exec_cmd(cmd, ignore_status=True) 66 self.assertEqual(result, 'DEADBEEF') 67 68 def test__exec_cmd_pass_grep(self): 69 mock_job = MockJob(exit_status=1, stderr='', stdout='foo') 70 cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"grep foo"'] 71 with mock.patch('acts.libs.proc.job.run', return_value=mock_job): 72 result = MockAdbProxy()._exec_cmd(cmd) 73 self.assertEqual(result, 'foo') 74 75 def test__exec_cmd_failure_ret_nonzero(self): 76 mock_job = MockJob(exit_status=1, stderr='error not related to adb') 77 cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"'] 78 with mock.patch('acts.libs.proc.job.run', return_value=mock_job): 79 with self.assertRaises(AdbCommandError): 80 MockAdbProxy()._exec_cmd(cmd) 81 82 def test__exec_cmd_raises_on_bind_error(self): 83 """Tests _exec_cmd raises an AdbError on port forwarding failure.""" 84 mock_job = MockJob(exit_status=1, 85 stderr='error: cannot bind listener: ' 86 'Address already in use', 87 stdout='') 88 cmd = ['adb', '-s', '"SOME_SERIAL"', 'shell', '"SOME_SHELL_CMD"'] 89 with mock.patch('acts.libs.proc.job.run', return_value=mock_job): 90 with self.assertRaises(AdbError): 91 MockAdbProxy()._exec_cmd(cmd) 92 93 def test__get_version_number_gets_version_number(self): 94 """Tests the positive case for AdbProxy.get_version_number().""" 95 proxy = MockAdbProxy() 96 expected_version_number = 39 97 proxy.version = lambda: ('Android Debug Bridge version 1.0.%s\nblah' % 98 expected_version_number) 99 self.assertEqual(expected_version_number, proxy.get_version_number()) 100 101 def test__get_version_number_raises_upon_parse_failure(self): 102 """Tests the failure case for AdbProxy.get_version_number().""" 103 proxy = MockAdbProxy() 104 proxy.version = lambda: 'Bad format' 105 with self.assertRaises(AdbError): 106 proxy.get_version_number() 107 108 109if __name__ == "__main__": 110 unittest.main() 111