diff options
Diffstat (limited to 'src/peerinfo-tool/test_gnunet_peerinfo.py.in')
-rwxr-xr-x | src/peerinfo-tool/test_gnunet_peerinfo.py.in | 70 |
1 files changed, 9 insertions, 61 deletions
diff --git a/src/peerinfo-tool/test_gnunet_peerinfo.py.in b/src/peerinfo-tool/test_gnunet_peerinfo.py.in index 7751d533c..7a4d41c11 100755 --- a/src/peerinfo-tool/test_gnunet_peerinfo.py.in +++ b/src/peerinfo-tool/test_gnunet_peerinfo.py.in | |||
@@ -18,66 +18,19 @@ | |||
18 | # Boston, MA 02111-1307, USA. | 18 | # Boston, MA 02111-1307, USA. |
19 | # | 19 | # |
20 | # Testcase for gnunet-peerinfo | 20 | # Testcase for gnunet-peerinfo |
21 | #import pexpect | 21 | import sys |
22 | from __future__ import print_function | ||
23 | import os | 22 | import os |
24 | #import signal | ||
25 | import re | ||
26 | import subprocess | 23 | import subprocess |
27 | import sys | 24 | import re |
28 | import shutil | 25 | import shutil |
29 | import time | 26 | import time |
30 | 27 | ||
31 | class pexpect (object): | 28 | srcdir = "../.." |
32 | def __init__ (self): | 29 | gnunet_pyexpect_dir = os.path.join (srcdir, "contrib", "gnunet_pyexpect") |
33 | super (pexpect, self).__init__ () | 30 | if gnunet_pyexpect_dir not in sys.path: |
34 | 31 | sys.path.append (gnunet_pyexpect_dir) | |
35 | def spawn (self, stdin, arglist, *pargs, **kwargs): | ||
36 | self.proc = subprocess.Popen (arglist, *pargs, **kwargs) | ||
37 | if self.proc is None: | ||
38 | print ("Failed to spawn a process {0}".format (arglist)) | ||
39 | sys.exit (1) | ||
40 | if stdin is not None: | ||
41 | self.stdo, self.stde = self.proc.communicate (stdin) | ||
42 | else: | ||
43 | self.stdo, self.stde = self.proc.communicate () | ||
44 | return self.proc | ||
45 | |||
46 | def expect (self, s, r, flags=0): | ||
47 | stream = self.stdo if s == 'stdout' else self.stde | ||
48 | if isinstance (r, str): | ||
49 | if r == "EOF": | ||
50 | if len (stream) == 0: | ||
51 | return True | ||
52 | else: | ||
53 | print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r, stream)) | ||
54 | sys.exit (2) | ||
55 | raise ValueError ("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format (r)) | ||
56 | m = r.match (stream, flags) | ||
57 | if not m: | ||
58 | print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r.pattern, stream)) | ||
59 | sys.exit (2) | ||
60 | stream = stream[m.end ():] | ||
61 | if s == 'stdout': | ||
62 | self.stdo = stream | ||
63 | else: | ||
64 | self.stde = stream | ||
65 | return m | ||
66 | 32 | ||
67 | def read (self, s, size=-1): | 33 | from gnunet_pyexpect import pexpect |
68 | stream = self.stdo if s == 'stdout' else self.stde | ||
69 | result = "" | ||
70 | if size < 0: | ||
71 | result = stream | ||
72 | new_stream = "" | ||
73 | else: | ||
74 | result = stream[0:size] | ||
75 | new_stream = stream[size:] | ||
76 | if s == 'stdout': | ||
77 | self.stdo = new_stream | ||
78 | else: | ||
79 | self.stde = new_stream | ||
80 | return result | ||
81 | 34 | ||
82 | if os.name == 'posix': | 35 | if os.name == 'posix': |
83 | peerinfo = 'gnunet-peerinfo' | 36 | peerinfo = 'gnunet-peerinfo' |
@@ -94,7 +47,7 @@ pinfo.expect ("stdout", re.compile (r'Error in communication with PEERINFO servi | |||
94 | pinfo.expect ("stdout", "EOF") | 47 | pinfo.expect ("stdout", "EOF") |
95 | 48 | ||
96 | if os.name == "nt": | 49 | if os.name == "nt": |
97 | shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True) | 50 | shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-peerinfo"), True) |
98 | else: | 51 | else: |
99 | shutil.rmtree ("/tmp/gnunet-test-peerinfo", True) | 52 | shutil.rmtree ("/tmp/gnunet-test-peerinfo", True) |
100 | arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf']) | 53 | arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf']) |
@@ -103,15 +56,12 @@ arm.communicate () | |||
103 | try: | 56 | try: |
104 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 57 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
105 | pinfo.expect ("stdout", re.compile (r'I am peer `.*\'.\r?\n')) | 58 | pinfo.expect ("stdout", re.compile (r'I am peer `.*\'.\r?\n')) |
106 | pinfo.expect ("stdout", "EOF") | ||
107 | 59 | ||
108 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 60 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
109 | pinfo.expect ("stdout", re.compile (r'.......................................................................................................\r?\n')) | 61 | pinfo.expect ("stdout", re.compile (r'.......................................................................................................\r?\n')) |
110 | pinfo.expect ("stdout", "EOF") | ||
111 | 62 | ||
112 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 63 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
113 | pinfo.expect ("stdout", re.compile (r'Invalid command line argument `invalid\'\r?\n')) | 64 | pinfo.expect ("stdout", re.compile (r'Invalid command line argument `invalid\'\r?\n')) |
114 | pinfo.expect ("stdout", "EOF") | ||
115 | 65 | ||
116 | arm = subprocess.Popen ([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf']) | 66 | arm = subprocess.Popen ([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf']) |
117 | arm.communicate () | 67 | arm.communicate () |
@@ -122,14 +72,12 @@ try: | |||
122 | m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n")) | 72 | m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n")) |
123 | while len (m.group (0)) > 0: | 73 | while len (m.group (0)) > 0: |
124 | m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)")) | 74 | m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)")) |
125 | pinfo.expect ("stdout", "EOF") | ||
126 | 75 | ||
127 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-n'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 76 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-n'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
128 | pinfo.expect ("stdout", re.compile ("Peer `.*'\r?\n")) | 77 | pinfo.expect ("stdout", re.compile ("Peer `.*'\r?\n")) |
129 | m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n")) | 78 | m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r?\n")) |
130 | while len (m.group (0)) > 0: | 79 | while len (m.group (0)) > 0: |
131 | m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)")) | 80 | m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r?\n|\r?\n|)")) |
132 | pinfo.expect ("stdout", "EOF") | ||
133 | 81 | ||
134 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 82 | pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
135 | pid = pinfo.read ("stdout") | 83 | pid = pinfo.read ("stdout") |
@@ -139,7 +87,7 @@ finally: | |||
139 | arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf']) | 87 | arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf']) |
140 | arm.communicate () | 88 | arm.communicate () |
141 | if os.name == "nt": | 89 | if os.name == "nt": |
142 | shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True) | 90 | shutil.rmtree (os.path.join (os.getenv ("TEMP"), "gnunet-test-peerinfo"), True) |
143 | else: | 91 | else: |
144 | shutil.rmtree ("/tmp/gnunet-test-peerinfo", True) | 92 | shutil.rmtree ("/tmp/gnunet-test-peerinfo", True) |
145 | 93 | ||