3 import nepi.util.environ
7 # Unittest from Python 2.6 doesn't have these decorators
8 def _bannerwrap(f, text):
10 def banner(*args, **kwargs):
11 sys.stderr.write("*** WARNING: Skipping test %s: `%s'\n" %
16 return lambda f: _bannerwrap(f, text)
17 def skipUnless(cond, text):
18 return (lambda f: _bannerwrap(f, text)) if not cond else lambda f: f
19 def skipIf(cond, text):
20 return (lambda f: _bannerwrap(f, text)) if cond else lambda f: f
22 def ns3_bindings_path():
23 if "NEPI_NS3BINDINGS" in os.environ:
24 return os.environ["NEPI_NS3BINDINGS"]
27 def ns3_library_path():
28 if "NEPI_NS3LIBRARY" in os.environ:
29 return os.environ["NEPI_NS3LIBRARY"]
32 def autoconfig_ns3_backend(conf):
33 if ns3_bindings_path():
34 conf.set_attribute_value("Ns3Bindings", ns3_bindings_path())
35 if ns3_library_path():
36 conf.set_attribute_value("Ns3Library", ns3_library_path())
39 if ns3_library_path():
41 ctypes.CDLL(ns3_library_path(), ctypes.RTLD_GLOBAL)
44 if ns3_bindings_path():
45 sys.path.insert(0, ns3_bindings_path())
48 found = imp.find_module('ns3')
49 module = imp.load_module('ns3', *found)
53 if ns3_bindings_path():
59 def find_bin(name, extra_path = None):
61 if "PATH" in os.environ:
62 search += os.environ["PATH"].split(":")
63 for pref in ("/", "/usr/", "/usr/local/"):
64 for d in ("bin", "sbin"):
65 search.append(pref + d)
71 os.stat(d + "/" + name)
74 if e.errno != os.errno.ENOENT:
78 def find_bin_or_die(name, extra_path = None):
81 raise RuntimeError(("Cannot find `%s' command, impossible to " +
87 import os, os.path, re, signal, shutil, socket, subprocess, tempfile
88 def gen_ssh_keypair(filename):
89 ssh_keygen = nepi.util.environ.find_bin_or_die("ssh-keygen")
90 args = [ssh_keygen, '-q', '-N', '', '-f', filename]
91 assert subprocess.Popen(args).wait() == 0
92 return filename, "%s.pub" % filename
94 def add_key_to_agent(filename):
95 ssh_add = nepi.util.environ.find_bin_or_die("ssh-add")
96 args = [ssh_add, filename]
97 null = file("/dev/null", "w")
98 assert subprocess.Popen(args, stderr = null).wait() == 0
103 s.bind(("127.0.0.1", 0))
104 port = s.getsockname()[1]
107 _SSH_CONF = """ListenAddress 127.0.0.1:%d
110 UsePrivilegeSeparation no
111 PubkeyAuthentication yes
112 PasswordAuthentication no
113 AuthorizedKeysFile %s
115 AllowAgentForwarding yes
118 PermitUserEnvironment yes
121 def gen_sshd_config(filename, port, server_key, auth_keys):
122 conf = open(filename, "w")
123 text = _SSH_CONF % (port, server_key, auth_keys)
128 def gen_auth_keys(pubkey, output, environ):
129 #opts = ['from="127.0.0.1/32"'] # fails in stupid yans setup
131 for k, v in environ.items():
132 opts.append('environment="%s=%s"' % (k, v))
134 lines = file(pubkey).readlines()
135 pubkey = lines[0].split()[0:2]
136 out = file(output, "w")
137 out.write("%s %s %s\n" % (",".join(opts), pubkey[0], pubkey[1]))
141 def start_ssh_agent():
142 ssh_agent = nepi.util.environ.find_bin_or_die("ssh-agent")
143 proc = subprocess.Popen([ssh_agent], stdout = subprocess.PIPE)
144 (out, foo) = proc.communicate()
145 assert proc.returncode == 0
147 for l in out.split("\n"):
148 match = re.search("^(\w+)=([^ ;]+);.*", l)
151 k, v = match.groups()
156 def stop_ssh_agent(data):
157 # No need to gather the pid, ssh-agent knows how to kill itself; after we
158 # had set up the environment
159 ssh_agent = nepi.util.environ.find_bin_or_die("ssh-agent")
160 null = file("/dev/null", "w")
161 proc = subprocess.Popen([ssh_agent, "-k"], stdout = null)
163 assert proc.wait() == 0
167 class test_environment(object):
169 sshd = find_bin_or_die("sshd")
171 if 'PYTHONPATH' in os.environ:
172 environ['PYTHONPATH'] = ":".join(map(os.path.realpath,
173 os.environ['PYTHONPATH'].split(":")))
174 if 'NEPI_NS3BINDINGS' in os.environ:
175 environ['NEPI_NS3BINDINGS'] = \
176 os.path.realpath(os.environ['NEPI_NS3BINDINGS'])
177 if 'NEPI_NS3LIBRARY' in os.environ:
178 environ['NEPI_NS3LIBRARY'] = \
179 os.path.realpath(os.environ['NEPI_NS3LIBRARY'])
181 self.dir = tempfile.mkdtemp()
182 self.server_keypair = gen_ssh_keypair(
183 os.path.join(self.dir, "server_key"))
184 self.client_keypair = gen_ssh_keypair(
185 os.path.join(self.dir, "client_key"))
186 self.authorized_keys = gen_auth_keys(self.client_keypair[1],
187 os.path.join(self.dir, "authorized_keys"), environ)
188 self.port = get_free_port()
189 self.sshd_conf = gen_sshd_config(
190 os.path.join(self.dir, "sshd_config"),
191 self.port, self.server_keypair[0], self.authorized_keys)
193 self.sshd = subprocess.Popen([sshd, '-q', '-D', '-f', self.sshd_conf])
194 self.ssh_agent_vars = start_ssh_agent()
195 add_key_to_agent(self.client_keypair[0])
199 os.kill(self.sshd.pid, signal.SIGTERM)
201 if self.ssh_agent_vars:
202 stop_ssh_agent(self.ssh_agent_vars)
203 shutil.rmtree(self.dir)