blob: 573d1187455ec40eb48cb946e89704b8b893ab39 [file] [log] [blame]
Alex Deymoa7b7c2b2013-11-06 11:58:55 -08001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7import shutil
8
9from autotest_lib.client.common_lib import error, utils
Alex Deymoa25ea0d2013-12-27 12:42:34 -080010from autotest_lib.client.cros import service_stopper, avahi_utils, tcpdump
Alex Deymoa7b7c2b2013-11-06 11:58:55 -080011
12
13P2P_SHARE_PATH = '/var/cache/p2p'
14
15# A path used to store the existing p2p files during the test and restore them
16# once the test finishes.
17P2P_SHARE_BACKUP_PATH = '/var/cache/p2p-backup'
18
19
20def p2p_backup_files(backup_path=P2P_SHARE_BACKUP_PATH):
21 """Backup the P2P shared files and create an empty shared directory.
22
23 p2p-server shall not be running during backup or restore.
24
25 @param backup_path: The path where the files will be moved to.
26 @raise error.TestError
27 """
28 try:
29 if os.path.exists(backup_path):
30 shutil.rmtree(backup_path)
31 if os.path.exists(P2P_SHARE_PATH):
32 os.rename(P2P_SHARE_PATH, backup_path)
33 except OSError, e:
34 raise error.TestError("Error on P2P files backup: %s" % (e.message))
35
36
37def p2p_restore_files(backup_path=P2P_SHARE_BACKUP_PATH):
38 """Restore the P2P shared files from a backup and *delete* the backup.
39
40 p2p-server shall not be running during backup or restore.
41
42 @param backup_path: The path where the files will be moved from.
43 """
44 if os.path.exists(P2P_SHARE_PATH):
45 shutil.rmtree(P2P_SHARE_PATH, ignore_errors=True)
46 if os.path.exists(backup_path):
47 os.rename(backup_path, P2P_SHARE_PATH)
48
49
50class P2PServerOverTap(object):
51 """Manage a p2p-server instance running over a TAP interface.
52
53 This class manages a p2p-server instance configured to run over a TAP
54 interface, useful for any test that needs to interact with the p2p-server
55 (and its p2p-http-server instance) on a controled network environment.
56 """
57 def __init__(self, tap_ip='169.254.10.1', tap_mask=24, tap_name='faketap'):
58 """Initialize the configuration.
59
60 @param tap_ip: IPv4 address for the TAP interface on the DUT's end.
61 @param tap_mask: Network mask fot the tap_ip address.
62 @param tap_name: The name prefix for the TAP interface.
63 """
64 # The network 169.254/16 shouldn't clash with other real services and we
65 # use a /24 subnet of it as the default safe value here.
66 self._tap_ip = tap_ip
67 self._tap_mask = tap_mask
68 self._tap_name = tap_name
69 self._services = None
70 self.tap = None
Alex Deymoa25ea0d2013-12-27 12:42:34 -080071 self._tcpdump = None
Alex Deymoa7b7c2b2013-11-06 11:58:55 -080072
73
Alex Deymoa25ea0d2013-12-27 12:42:34 -080074 def setup(self, dumpdir=None):
75 """Initializes avahi daemon on a new tap interface.
76
77 @param dumpdir: Directory where the traffic on the new tap interface
78 is recorded. A value of None disables traffic dumping.
79 """
Alex Deymoa7b7c2b2013-11-06 11:58:55 -080080 try:
81 from lansim import tuntap
82 except ImportError:
83 logging.exception('Failed to import lansim.')
84 raise error.TestError('Error importing lansim. Did you setup_dep '
85 'and install_pkg lansim on your test?')
86
87 # Ensure p2p and avahi aren't running.
88 self._services = service_stopper.ServiceStopper(['p2p', 'avahi'])
89 self._services.stop_services()
90
91 # Backup p2p files.
92 p2p_backup_files()
93
94 # Initialize the TAP interface.
95 self.tap = tuntap.TunTap(tuntap.IFF_TAP, name=self._tap_name)
96 self.tap.set_addr(self._tap_ip, self._tap_mask)
97 self.tap.up()
98
Alex Deymoa25ea0d2013-12-27 12:42:34 -080099 # Enable traffic dump.
100 if not dumpdir is None:
101 dumpfile = os.path.join(dumpdir, 'dump-%s.pcap' % self.tap.name)
102 self._tcpdump = tcpdump.Tcpdump(self.tap.name, dumpfile)
103
Alex Deymoa7b7c2b2013-11-06 11:58:55 -0800104 # Re-launch avahi-daemon on the TAP interface only.
105 avahi_utils.avahi_start_on_iface(self.tap.name)
106 utils.system("start p2p")
107
108
109 def cleanup(self):
110 """Restore the original environment as before the call to setup().
111
112 This method makes a best-effort attempt to restore the environment and
113 logs all the errors encountered but doesn't fail.
114 """
115 try:
116 utils.system('stop p2p')
117 avahi_utils.avahi_stop()
118 except:
119 logging.exception('Failed to stop tested services.')
120
Alex Deymoa25ea0d2013-12-27 12:42:34 -0800121 if self._tcpdump:
122 self._tcpdump.stop()
123
Alex Deymoa7b7c2b2013-11-06 11:58:55 -0800124 if self.tap:
125 self.tap.down()
126
127 # Restore p2p files.
128 try:
129 p2p_restore_files()
130 except OSError:
131 logging.exception('Failed to restore the P2P backup.')
132
133 if self._services:
134 self._services.restore_services()