OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import android_commands | 5 import android_commands |
6 import constants | 6 import constants |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import subprocess | 9 import subprocess |
10 import time | 10 import time |
11 | 11 |
12 | 12 |
13 class FakeDns(object): | 13 class FakeDns(object): |
14 """Wrapper class for the fake_dns tool.""" | 14 """Wrapper class for the fake_dns tool.""" |
15 _FAKE_DNS_PATH = constants.TEST_EXECUTABLE_DIR + '/fake_dns' | 15 _FAKE_DNS_PATH = constants.TEST_EXECUTABLE_DIR + '/fake_dns' |
16 | 16 |
17 def __init__(self, adb, build_type): | 17 def __init__(self, adb): |
18 """ | 18 """ |
19 Args: | 19 Args: |
20 adb: the AndroidCommands to use. | 20 adb: the AndroidCommands to use. |
21 build_type: 'Release' or 'Debug'. | |
22 """ | 21 """ |
23 self._adb = adb | 22 self._adb = adb |
24 self._build_type = build_type | |
25 self._fake_dns = None | 23 self._fake_dns = None |
26 self._original_dns = None | 24 self._original_dns = None |
27 | 25 |
28 def _PushAndStartFakeDns(self): | 26 def _PushAndStartFakeDns(self): |
29 """Starts the fake_dns server that replies all name queries 127.0.0.1. | 27 """Starts the fake_dns server that replies all name queries 127.0.0.1. |
30 | 28 |
31 Returns: | 29 Returns: |
32 subprocess instance connected to the fake_dns process on the device. | 30 subprocess instance connected to the fake_dns process on the device. |
33 """ | 31 """ |
34 self._adb.PushIfNeeded( | 32 self._adb.PushIfNeeded( |
35 os.path.join(constants.DIR_SOURCE_ROOT, 'out', self._build_type, | 33 os.path.join(constants.DIR_SOURCE_ROOT, 'out', constants.GetBuildType(), |
36 'fake_dns'), | 34 'fake_dns'), |
37 FakeDns._FAKE_DNS_PATH) | 35 FakeDns._FAKE_DNS_PATH) |
38 return subprocess.Popen( | 36 return subprocess.Popen( |
39 ['adb', '-s', self._adb._adb.GetSerialNumber(), | 37 ['adb', '-s', self._adb._adb.GetSerialNumber(), |
40 'shell', '%s -D' % FakeDns._FAKE_DNS_PATH]) | 38 'shell', '%s -D' % FakeDns._FAKE_DNS_PATH]) |
41 | 39 |
42 def SetUp(self): | 40 def SetUp(self): |
43 """Configures the system to point to a DNS server that replies 127.0.0.1. | 41 """Configures the system to point to a DNS server that replies 127.0.0.1. |
44 | 42 |
45 This can be used in combination with the forwarder to forward all web | 43 This can be used in combination with the forwarder to forward all web |
46 traffic to a replay server. | 44 traffic to a replay server. |
47 | 45 |
48 The TearDown() method will perform all cleanup. | 46 The TearDown() method will perform all cleanup. |
49 """ | 47 """ |
50 self._adb.RunShellCommand('ip route add 8.8.8.0/24 via 127.0.0.1 dev lo') | 48 self._adb.RunShellCommand('ip route add 8.8.8.0/24 via 127.0.0.1 dev lo') |
51 self._fake_dns = self._PushAndStartFakeDns() | 49 self._fake_dns = self._PushAndStartFakeDns() |
52 self._original_dns = self._adb.RunShellCommand('getprop net.dns1')[0] | 50 self._original_dns = self._adb.RunShellCommand('getprop net.dns1')[0] |
53 self._adb.RunShellCommand('setprop net.dns1 127.0.0.1') | 51 self._adb.RunShellCommand('setprop net.dns1 127.0.0.1') |
54 time.sleep(2) # Time for server to start and the setprop to take effect. | 52 time.sleep(2) # Time for server to start and the setprop to take effect. |
55 | 53 |
56 def TearDown(self): | 54 def TearDown(self): |
57 """Shuts down the fake_dns.""" | 55 """Shuts down the fake_dns.""" |
58 if self._fake_dns: | 56 if self._fake_dns: |
59 if not self._original_dns or self._original_dns == '127.0.0.1': | 57 if not self._original_dns or self._original_dns == '127.0.0.1': |
60 logging.warning('Bad original DNS, falling back to Google DNS.') | 58 logging.warning('Bad original DNS, falling back to Google DNS.') |
61 self._original_dns = '8.8.8.8' | 59 self._original_dns = '8.8.8.8' |
62 self._adb.RunShellCommand('setprop net.dns1 %s' % self._original_dns) | 60 self._adb.RunShellCommand('setprop net.dns1 %s' % self._original_dns) |
63 self._fake_dns.kill() | 61 self._fake_dns.kill() |
64 self._adb.RunShellCommand('ip route del 8.8.8.0/24 via 127.0.0.1 dev lo') | 62 self._adb.RunShellCommand('ip route del 8.8.8.0/24 via 127.0.0.1 dev lo') |
OLD | NEW |