media/webrtc/trunk/build/android/pylib/fake_dns.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/build/android/pylib/fake_dns.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,63 @@
     1.4 +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +# Use of this source code is governed by a BSD-style license that can be
     1.6 +# found in the LICENSE file.
     1.7 +
     1.8 +import android_commands
     1.9 +import constants
    1.10 +import logging
    1.11 +import os
    1.12 +import subprocess
    1.13 +import time
    1.14 +
    1.15 +
    1.16 +class FakeDns(object):
    1.17 +  """Wrapper class for the fake_dns tool."""
    1.18 +  _FAKE_DNS_PATH = constants.TEST_EXECUTABLE_DIR + '/fake_dns'
    1.19 +
    1.20 +  def __init__(self, adb, build_type):
    1.21 +    """
    1.22 +      Args:
    1.23 +        adb: the AndroidCommands to use.
    1.24 +        build_type: 'Release' or 'Debug'.
    1.25 +    """
    1.26 +    self._adb = adb
    1.27 +    self._build_type = build_type
    1.28 +    self._fake_dns = None
    1.29 +    self._original_dns = None
    1.30 +
    1.31 +  def _PushAndStartFakeDns(self):
    1.32 +    """Starts the fake_dns server that replies all name queries 127.0.0.1.
    1.33 +
    1.34 +    Returns:
    1.35 +      subprocess instance connected to the fake_dns process on the device.
    1.36 +    """
    1.37 +    self._adb.PushIfNeeded(
    1.38 +        os.path.join(constants.CHROME_DIR, 'out', self._build_type, 'fake_dns'),
    1.39 +        FakeDns._FAKE_DNS_PATH)
    1.40 +    return subprocess.Popen(
    1.41 +        ['adb', '-s', self._adb._adb.GetSerialNumber(),
    1.42 +         'shell', '%s -D' % FakeDns._FAKE_DNS_PATH])
    1.43 +
    1.44 +  def SetUp(self):
    1.45 +    """Configures the system to point to a DNS server that replies 127.0.0.1.
    1.46 +
    1.47 +    This can be used in combination with the forwarder to forward all web
    1.48 +    traffic to a replay server.
    1.49 +
    1.50 +    The TearDown() method will perform all cleanup.
    1.51 +    """
    1.52 +    self._adb.RunShellCommand('ip route add 8.8.8.0/24 via 127.0.0.1 dev lo')
    1.53 +    self._fake_dns = self._PushAndStartFakeDns()
    1.54 +    self._original_dns = self._adb.RunShellCommand('getprop net.dns1')[0]
    1.55 +    self._adb.RunShellCommand('setprop net.dns1 127.0.0.1')
    1.56 +    time.sleep(2)  # Time for server to start and the setprop to take effect.
    1.57 +
    1.58 +  def TearDown(self):
    1.59 +    """Shuts down the fake_dns."""
    1.60 +    if self._fake_dns:
    1.61 +      if not self._original_dns or self._original_dns == '127.0.0.1':
    1.62 +        logging.warning('Bad original DNS, falling back to Google DNS.')
    1.63 +        self._original_dns = '8.8.8.8'
    1.64 +      self._adb.RunShellCommand('setprop net.dns1 %s' % self._original_dns)
    1.65 +      self._fake_dns.kill()
    1.66 +      self._adb.RunShellCommand('ip route del 8.8.8.0/24 via 127.0.0.1 dev lo')

mercurial