979a55d830003e1772570c6e3ab964ef97a94468
[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 Affero 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 #    You should have received a copy of the GNU Affero General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #    SPDX-License-Identifier: AGPL3.0-or-later
19 #
20 # Testcase for ego revocation
21
22 from builtins import str
23 import sys
24 import os
25 import subprocess
26 import re
27 import shutil
28
29 if os.name == 'posix':
30     config = 'gnunet-config'
31     gnunetarm = 'gnunet-arm'
32     ident = 'gnunet-identity'
33     revoc = './gnunet-revocation'
34 elif os.name == 'nt':
35     config = 'gnunet-config.exe'
36     gnunetarm = 'gnunet-arm.exe'
37     ident = 'gnunet-identity.exe'
38     revoc = './gnunet-revocation.exe'
39
40 TEST_CONFIGURATION = "test_revocation.conf"
41 TEST_REVOCATION_EGO = "revoc_test"
42
43 get_clean = subprocess.Popen([config, '-c', TEST_CONFIGURATION, '-s', 'PATHS', '-o', 'GNUNET_HOME', '-f'], stdout=subprocess.PIPE)
44 cleandir, x = get_clean.communicate()
45 cleandir = cleandir.decode("utf-8")
46 cleandir = cleandir.rstrip('\n').rstrip('\r')
47
48 if os.path.isdir(cleandir):
49     shutil.rmtree(cleandir, True)
50
51 res = 0
52 arm = subprocess.Popen([gnunetarm, '-s', '-c', TEST_CONFIGURATION])
53 arm.communicate()
54
55 try:
56     print("Creating an ego " + TEST_REVOCATION_EGO)
57     sys.stdout.flush()
58     sys.stderr.flush()
59     idc = subprocess.Popen([ident, '-C', TEST_REVOCATION_EGO, '-c', TEST_CONFIGURATION])
60     idc.communicate()
61     if idc.returncode != 0:
62         raise Exception("gnunet-identity failed to create an ego `" + TEST_REVOCATION_EGO + "'")
63
64     sys.stdout.flush()
65     sys.stderr.flush()
66     idd = subprocess.Popen([ident, '-d'], stdout=subprocess.PIPE)
67     rev_key, x = idd.communicate()
68     rev_key = rev_key.decode("utf-8")
69     if len(rev_key.split()) < 3:
70         raise Exception("can't get revocation key out of `" + rev_key + "'")
71     rev_key = rev_key.split()[2]
72
73     print("Testing key " + rev_key)
74     sys.stdout.flush()
75     sys.stderr.flush()
76     tst = subprocess.Popen([revoc, '-t', rev_key, '-c', TEST_CONFIGURATION], stdout=subprocess.PIPE)
77     output_not_revoked, x = tst.communicate()
78     output_not_revoked = output_not_revoked.decode("utf-8")
79     if tst.returncode != 0:
80         raise Exception("gnunet-revocation failed to test a key - " + str(tst.returncode) + ": " + output_not_revoked)
81     if 'valid' not in output_not_revoked:
82         res = 1
83         print("Key was not valid")
84     else:
85         print("Key was valid")
86
87     print("Revoking key " + rev_key)
88     sys.stdout.flush()
89     sys.stderr.flush()
90     rev = subprocess.Popen([revoc, '-R', TEST_REVOCATION_EGO, '-p', '-c', TEST_CONFIGURATION])
91     rev.communicate()
92     if rev.returncode != 0:
93         raise Exception("gnunet-revocation failed to revoke a key")
94
95     print("Testing revoked key " + rev_key)
96     sys.stdout.flush()
97     sys.stderr.flush()
98     tst = subprocess.Popen([revoc, '-t', rev_key, '-c', TEST_CONFIGURATION], stdout=subprocess.PIPE)
99     output_revoked, x = tst.communicate()
100     output_revoked = output_revoked.decode("utf-8")
101     if tst.returncode != 0:
102         raise Exception("gnunet-revocation failed to test a revoked key")
103     if 'revoked' not in output_revoked:
104         res = 1
105         print("Key was not revoked")
106     else:
107         print("Key was revoked")
108
109 finally:
110     arm = subprocess.Popen([gnunetarm, '-e', '-c', TEST_CONFIGURATION])
111     arm.communicate()
112     if os.path.isdir(cleandir):
113         shutil.rmtree(cleandir, True)
114
115 sys.exit(res)