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