first batch of license fixes (boring)
[oweals/gnunet.git] / src / revocation / test_local_revocation.py.in
1 #!@PYTHON@
2 #    This file is part of GNUnet.
3 #    (C) 2010, 2018 Christian Grothoff (and other contributing authors)
4 #
5 #    GNUnet is free software: you can redistribute it and/or modify it
6 #    under the terms of the GNU General Public License as published
7 #    by the Free Software Foundation, either version 3 of the License,
8 #    or (at your option) any later version.
9 #
10 #    GNUnet is distributed in the hope that it will be useful, but
11 #    WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #    Affero General Public License for more details.
14 #
15 # Testcase for ego revocation
16 from __future__ import print_function
17 import sys
18 import os
19 import subprocess
20 import re
21 import shutil
22
23 if os.name == 'posix':
24     config = 'gnunet-config'
25     gnunetarm = 'gnunet-arm'
26     ident = 'gnunet-identity'
27     revoc = './gnunet-revocation'
28 elif os.name == 'nt':
29     config = 'gnunet-config.exe'
30     gnunetarm = 'gnunet-arm.exe'
31     ident = 'gnunet-identity.exe'
32     revoc = './gnunet-revocation.exe'
33
34 TEST_CONFIGURATION = "test_revocation.conf"
35 TEST_REVOCATION_EGO = "revoc_test"
36
37 get_clean = subprocess.Popen([config, '-c', TEST_CONFIGURATION, '-s', 'PATHS', '-o', 'GNUNET_HOME', '-f'], stdout=subprocess.PIPE)
38 cleandir, x = get_clean.communicate()
39 cleandir = cleandir.decode("utf-8")
40 cleandir = cleandir.rstrip('\n').rstrip('\r')
41
42 if os.path.isdir(cleandir):
43     shutil.rmtree(cleandir, True)
44
45 res = 0
46 arm = subprocess.Popen([gnunetarm, '-s', '-c', TEST_CONFIGURATION])
47 arm.communicate()
48
49 try:
50     print("Creating an ego " + TEST_REVOCATION_EGO)
51     sys.stdout.flush()
52     sys.stderr.flush()
53     idc = subprocess.Popen([ident, '-C', TEST_REVOCATION_EGO, '-c', TEST_CONFIGURATION])
54     idc.communicate()
55     if idc.returncode != 0:
56         raise Exception("gnunet-identity failed to create an ego `" + TEST_REVOCATION_EGO + "'")
57
58     sys.stdout.flush()
59     sys.stderr.flush()
60     idd = subprocess.Popen([ident, '-d'], stdout=subprocess.PIPE)
61     rev_key, x = idd.communicate()
62     rev_key = rev_key.decode("utf-8")
63     if len(rev_key.split()) < 3:
64         raise Exception("can't get revocation key out of `" + rev_key + "'")
65     rev_key = rev_key.split()[2]
66
67     print("Testing key " + rev_key)
68     sys.stdout.flush()
69     sys.stderr.flush()
70     tst = subprocess.Popen([revoc, '-t', rev_key, '-c', TEST_CONFIGURATION], stdout=subprocess.PIPE)
71     output_not_revoked, x = tst.communicate()
72     output_not_revoked = output_not_revoked.decode("utf-8")
73     if tst.returncode != 0:
74         raise Exception("gnunet-revocation failed to test a key - " + str(tst.returncode) + ": " + output_not_revoked)
75     if 'valid' not in output_not_revoked:
76         res = 1
77         print("Key was not valid")
78     else:
79         print("Key was valid")
80
81     print("Revoking key " + rev_key)
82     sys.stdout.flush()
83     sys.stderr.flush()
84     rev = subprocess.Popen([revoc, '-R', TEST_REVOCATION_EGO, '-p', '-c', TEST_CONFIGURATION])
85     rev.communicate()
86     if rev.returncode != 0:
87         raise Exception("gnunet-revocation failed to revoke a key")
88
89     print("Testing revoked key " + rev_key)
90     sys.stdout.flush()
91     sys.stderr.flush()
92     tst = subprocess.Popen([revoc, '-t', rev_key, '-c', TEST_CONFIGURATION], stdout=subprocess.PIPE)
93     output_revoked, x = tst.communicate()
94     output_revoked = output_revoked.decode("utf-8")
95     if tst.returncode != 0:
96         raise Exception("gnunet-revocation failed to test a revoked key")
97     if 'revoked' not in output_revoked:
98         res = 1
99         print("Key was not revoked")
100     else:
101         print("Key was revoked")
102
103 finally:
104     arm = subprocess.Popen([gnunetarm, '-e', '-c', TEST_CONFIGURATION])
105     arm.communicate()
106     if os.path.isdir(cleandir):
107         shutil.rmtree(cleandir, True)
108
109 sys.exit(res)