2 # vim:ts=4:sw=4:et:ai:sts=4
5 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
24 import os, os.path, re, signal, shutil, socket, subprocess, tempfile
25 def gen_ssh_keypair(filename):
26 ssh_keygen = nepi.util.environ.find_bin_or_die("ssh-keygen")
27 args = [ssh_keygen, '-q', '-N', '', '-f', filename]
28 assert subprocess.Popen(args).wait() == 0
29 return filename, "%s.pub" % filename
31 def add_key_to_agent(filename):
32 ssh_add = nepi.util.environ.find_bin_or_die("ssh-add")
33 args = [ssh_add, filename]
34 null = file("/dev/null", "w")
35 assert subprocess.Popen(args, stderr = null).wait() == 0
40 s.bind(("127.0.0.1", 0))
41 port = s.getsockname()[1]
44 _SSH_CONF = """ListenAddress 127.0.0.1:%d
47 UsePrivilegeSeparation no
48 PubkeyAuthentication yes
49 PasswordAuthentication no
52 AllowAgentForwarding yes
55 PermitUserEnvironment yes
58 def gen_sshd_config(filename, port, server_key, auth_keys):
59 conf = open(filename, "w")
60 text = _SSH_CONF % (port, server_key, auth_keys)
65 def gen_auth_keys(pubkey, output, environ):
66 #opts = ['from="127.0.0.1/32"'] # fails in stupid yans setup
68 for k, v in environ.items():
69 opts.append('environment="%s=%s"' % (k, v))
71 lines = file(pubkey).readlines()
72 pubkey = lines[0].split()[0:2]
73 out = file(output, "w")
74 out.write("%s %s %s\n" % (",".join(opts), pubkey[0], pubkey[1]))
78 def start_ssh_agent():
79 ssh_agent = nepi.util.environ.find_bin_or_die("ssh-agent")
80 proc = subprocess.Popen([ssh_agent], stdout = subprocess.PIPE)
81 (out, foo) = proc.communicate()
82 assert proc.returncode == 0
84 for l in out.split("\n"):
85 match = re.search("^(\w+)=([^ ;]+);.*", l)
93 def stop_ssh_agent(data):
94 # No need to gather the pid, ssh-agent knows how to kill itself; after we
95 # had set up the environment
96 ssh_agent = nepi.util.environ.find_bin_or_die("ssh-agent")
97 null = file("/dev/null", "w")
98 proc = subprocess.Popen([ssh_agent, "-k"], stdout = null)
100 assert proc.wait() == 0