-fix off-by-1
[oweals/gnunet.git] / src / dht / test_dht_tools.py.in
1 #!@PYTHON@
2 from __future__ import print_function
3 import os
4 import sys
5 import shutil
6 import re
7 import subprocess
8 import time
9 import tempfile
10
11 if os.name == "nt":
12   tmp = os.getenv ("TEMP")
13 else:
14   tmp = "/tmp"
15
16 if os.name == 'nt':
17   get = './gnunet-dht-get.exe'
18   put = './gnunet-dht-put.exe'
19   arm = 'gnunet-arm.exe'
20 else:
21   get = './gnunet-dht-get'
22   put = './gnunet-dht-put'
23   arm = 'gnunet-arm'
24
25 tf, tempcfg = tempfile.mkstemp (prefix='test_dht_api_peer1.')
26 os.close (tf)
27
28 run_get = [get, '-c', tempcfg]
29 run_put = [put, '-c', tempcfg]
30 run_arm = [arm, '-c', tempcfg]
31 debug = os.getenv ('DEBUG')
32 if debug:
33   run_arm += [debug.split (' ')]
34
35 def cleanup (exitcode):
36   os.remove (tempcfg)
37   sys.exit (exitcode)
38
39 def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
40   if want_stdo:
41     stdo = subprocess.PIPE
42   else:
43     stdo = None
44   if want_stde:
45     stde = subprocess.PIPE
46   else:
47     stde = None
48   p = subprocess.Popen (args, stdout = stdo, stderr = stde)
49   stdo, stde = p.communicate ()
50   if not nofail:
51     if p.returncode != 0:
52       sys.exit (p.returncode)
53   return (p.returncode, stdo, stde)
54
55 def fail (result):
56   print (result)
57   r_arm (['-e'], want_stdo = False)
58   cleanup (1)
59
60 def r_something (to_run, extra_args, failer = None, normal = True, **kw):
61   rc, stdo, stde = sub_run (to_run + extra_args, nofail = True, **kw)
62   if failer is not None:
63     failer (to_run + extra_args, rc, stdo, stde, normal)
64   return (rc, stdo, stde)
65
66 def r_arm (extra_args, **kw):
67   return r_something (run_arm, extra_args, **kw)
68
69 def r_get (extra_args, **kw):
70   return r_something (run_get, extra_args, **kw)
71
72 def r_put (extra_args, **kw):
73   return r_something (run_put, extra_args, **kw)
74
75 def end_arm_failer (command, rc, stdo, stde, normal):
76   if normal:
77     if rc != 0:
78       fail ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
79   else:
80     if rc == 0:
81       fail ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
82
83 def print_only_failer (command, rc, stdo, stde, normal):
84   if normal:
85     if rc != 0:
86       print ("FAIL: error running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
87       cleanup (1)
88   else:
89     if rc == 0:
90       print ("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format (command, stdo, stde))
91       cleanup (1)
92
93 shutil.copyfile ('test_dht_api_peer1.conf', tempcfg)
94
95 print ("TEST: Starting ARM...", end='')
96 r_arm (['-s'], failer = end_arm_failer, want_stdo = False, want_stde = False)
97 print ("PASS")
98 time.sleep (1)
99
100 print ("TEST: Testing put...", end='')
101 r_put (['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer = end_arm_failer)
102 print ("PASS")
103 time.sleep (1)
104
105 print ("TEST: Testing get...", end='')
106 rc, stdo, stde = r_get (['-k', 'testkey', '-T', '5 ms', '-t', '8'], want_stdo = True, failer = end_arm_failer)
107 stdo = stdo.replace ('\r', '').splitlines ()
108 expect = "Result 0, type 8:\ntestdata".splitlines()
109 if len (stdo) != 2 or len (expect) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
110   fail ("output `{}' differs from expected `{}'".format (stdo, expect))
111 print ("PASS")
112
113 r_arm (['-e', '-d'], failer = print_only_failer)