blob: 5bda22f84f74077aecdd0db2c6500b2c2a381e1e [file] [log] [blame]
Dan Albert8e1fdd72015-07-24 17:08:33 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2015 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#
17"""Tests for the adb program itself.
18
19This differs from things in test_device.py in that there is no API for these
20things. Most of these tests involve specific error messages or the help text.
21"""
22from __future__ import print_function
23
Spencer Low1ce06082015-09-16 20:45:53 -070024import os
Dan Albert8e1fdd72015-07-24 17:08:33 -070025import random
26import subprocess
Spencer Low1ce06082015-09-16 20:45:53 -070027import threading
Dan Albert8e1fdd72015-07-24 17:08:33 -070028import unittest
29
30import adb
31
32
33class NonApiTest(unittest.TestCase):
34 """Tests for ADB that aren't a part of the AndroidDevice API."""
35
36 def test_help(self):
37 """Make sure we get _something_ out of help."""
38 out = subprocess.check_output(
39 ['adb', 'help'], stderr=subprocess.STDOUT)
40 self.assertGreater(len(out), 0)
41
42 def test_version(self):
43 """Get a version number out of the output of adb."""
44 lines = subprocess.check_output(['adb', 'version']).splitlines()
45 version_line = lines[0]
46 self.assertRegexpMatches(
47 version_line, r'^Android Debug Bridge version \d+\.\d+\.\d+$')
48 if len(lines) == 2:
49 # Newer versions of ADB have a second line of output for the
50 # version that includes a specific revision (git SHA).
51 revision_line = lines[1]
52 self.assertRegexpMatches(
53 revision_line, r'^Revision [0-9a-f]{12}-android$')
54
55 def test_tcpip_error_messages(self):
56 p = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE,
57 stderr=subprocess.STDOUT)
58 out, _ = p.communicate()
59 self.assertEqual(1, p.returncode)
60 self.assertIn('help message', out)
61
62 p = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE,
63 stderr=subprocess.STDOUT)
64 out, _ = p.communicate()
65 self.assertEqual(1, p.returncode)
66 self.assertIn('error', out)
67
Spencer Low1ce06082015-09-16 20:45:53 -070068 # Helper method that reads a pipe until it is closed, then sets the event.
69 def _read_pipe_and_set_event(self, pipe, event):
70 x = pipe.read()
71 event.set()
72
73 # Test that launch_server() does not let the adb server inherit
74 # stdin/stdout/stderr handles which can cause callers of adb.exe to hang.
75 # This test also runs fine on unix even though the impetus is an issue
76 # unique to Windows.
77 def test_handle_inheritance(self):
78 # This test takes 5 seconds to run on Windows: if there is no adb server
79 # running on the the port used below, adb kill-server tries to make a
80 # TCP connection to a closed port and that takes 1 second on Windows;
81 # adb start-server does the same TCP connection which takes another
82 # second, and it waits 3 seconds after starting the server.
83
84 # Start adb client with redirected stdin/stdout/stderr to check if it
85 # passes those redirections to the adb server that it starts. To do
86 # this, run an instance of the adb server on a non-default port so we
87 # don't conflict with a pre-existing adb server that may already be
88 # setup with adb TCP/emulator connections. If there is a pre-existing
89 # adb server, this also tests whether multiple instances of the adb
90 # server conflict on adb.log.
91
92 port = 5038
93 # Kill any existing server on this non-default port.
94 subprocess.check_output(['adb', '-P', str(port), 'kill-server'],
95 stderr=subprocess.STDOUT)
96
97 try:
98 # Run the adb client and have it start the adb server.
99 p = subprocess.Popen(['adb', '-P', str(port), 'start-server'],
100 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
101 stderr=subprocess.PIPE)
102
103 # Start threads that set events when stdout/stderr are closed.
104 stdout_event = threading.Event()
105 stdout_thread = threading.Thread(
106 target=self._read_pipe_and_set_event,
107 args=(p.stdout, stdout_event))
108 stdout_thread.daemon = True
109 stdout_thread.start()
110
111 stderr_event = threading.Event()
112 stderr_thread = threading.Thread(
113 target=self._read_pipe_and_set_event,
114 args=(p.stderr, stderr_event))
115 stderr_thread.daemon = True
116 stderr_thread.start()
117
118 # Wait for the adb client to finish. Once that has occurred, if
119 # stdin/stderr/stdout are still open, it must be open in the adb
120 # server.
121 p.wait()
122
123 # Try to write to stdin which we expect is closed. If it isn't
124 # closed, we should get an IOError. If we don't get an IOError,
125 # stdin must still be open in the adb server. The adb client is
126 # probably letting the adb server inherit stdin which would be
127 # wrong.
128 with self.assertRaises(IOError):
129 p.stdin.write('x')
130
131 # Wait a few seconds for stdout/stderr to be closed (in the success
132 # case, this won't wait at all). If there is a timeout, that means
133 # stdout/stderr were not closed and and they must be open in the adb
134 # server, suggesting that the adb client is letting the adb server
135 # inherit stdout/stderr which would be wrong.
136 self.assertTrue(stdout_event.wait(5), "adb stdout not closed")
137 self.assertTrue(stderr_event.wait(5), "adb stderr not closed")
138 finally:
139 # If we started a server, kill it.
140 subprocess.check_output(['adb', '-P', str(port), 'kill-server'],
141 stderr=subprocess.STDOUT)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700142
143def main():
144 random.seed(0)
145 if len(adb.get_devices()) > 0:
146 suite = unittest.TestLoader().loadTestsFromName(__name__)
147 unittest.TextTestRunner(verbosity=3).run(suite)
148 else:
149 print('Test suite must be run with attached devices')
150
151
152if __name__ == '__main__':
153 main()