From a131cd66d361c66e4dc83f8463feebf9792edbb9 Mon Sep 17 00:00:00 2001 From: "Piotr F. Mieszkowski" Date: Tue, 5 Jul 2022 22:32:13 +0200 Subject: [PATCH] Move different parts of Lacre tests to subdirectories In particular, move: - test utilities to test/utils; - unit tests to test/modules. Also: start implementing the Lacre daemon test (just a stub for now). --- Makefile | 4 +- test/daemon_test.py | 32 ++++++++++++ test/e2e.ini | 2 +- test/e2e_test.py | 73 ++++++++++++++------------- test/{ => modules}/test_contracts.py | 0 test/{ => modules}/test_gnupg.py | 0 test/{ => modules}/test_lacre_text.py | 0 test/modules/test_m2crypto.py | 15 ++++++ test/{ => utils}/relay.py | 0 test/{ => utils}/schema.py | 0 test/{ => utils}/sendmail.py | 17 ++++++- 11 files changed, 104 insertions(+), 39 deletions(-) create mode 100644 test/daemon_test.py rename test/{ => modules}/test_contracts.py (100%) rename test/{ => modules}/test_gnupg.py (100%) rename test/{ => modules}/test_lacre_text.py (100%) create mode 100644 test/modules/test_m2crypto.py rename test/{ => utils}/relay.py (100%) rename test/{ => utils}/schema.py (100%) rename test/{ => utils}/sendmail.py (54%) diff --git a/Makefile b/Makefile index bb332ff..1ba8e9a 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ crontest: clean-db $(TEST_DB) GPG_MAILGATE_CONFIG=test/gpg-mailgate-cron-test.conf PYTHONPATH=`pwd` $(PYTHON) gpg-mailgate-web/cron.py $(TEST_DB): - $(PYTHON) test/schema.py $(TEST_DB) + $(PYTHON) test/utils/schema.py $(TEST_DB) # # Run an e2e test of Advanced Content Filter. @@ -59,7 +59,7 @@ clean-db: # Run unit tests # unittest: - $(PYTHON) -m unittest discover -s test + $(PYTHON) -m unittest discover -s test/modules pre-clean: rm -fv test/gpg-mailgate.conf diff --git a/test/daemon_test.py b/test/daemon_test.py new file mode 100644 index 0000000..45c8e8e --- /dev/null +++ b/test/daemon_test.py @@ -0,0 +1,32 @@ +# +# gpg-mailgate +# +# This file is part of the gpg-mailgate source code. +# +# gpg-mailgate is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# gpg-mailgate source code is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with gpg-mailgate source code. If not, see . +# + +import logging + + +def _main(): + logging.basicConfig(filename="test/logs/daemon-test.log", + format="%(asctime)s %(pathname)s:%(lineno)d %(levelname)s [%(funcName)s] %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + level=logging.DEBUG) + logging.info("Starting Lacre Daemon tests...") + + +if __name__ == '__main__': + _main() diff --git a/test/e2e.ini b/test/e2e.ini index 9513ad8..a704dcc 100644 --- a/test/e2e.ini +++ b/test/e2e.ini @@ -22,7 +22,7 @@ [relay] port: 2500 -script: test/relay.py +script: test/utils/relay.py [dirs] keys: test/keyhome diff --git a/test/e2e_test.py b/test/e2e_test.py index 2ba8905..7515f21 100644 --- a/test/e2e_test.py +++ b/test/e2e_test.py @@ -1,38 +1,34 @@ # -# gpg-mailgate +# gpg-mailgate # -# This file is part of the gpg-mailgate source code. +# This file is part of the gpg-mailgate source code. # -# gpg-mailgate is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. +# gpg-mailgate is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. # -# gpg-mailgate source code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# gpg-mailgate source code is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with gpg-mailgate source code. If not, see . +# You should have received a copy of the GNU General Public License +# along with gpg-mailgate source code. If not, see . # import os -import sys - import subprocess -import difflib - import configparser import logging -from time import sleep -RELAY_SCRIPT = "test/relay.py" +RELAY_SCRIPT = "test/utils/relay.py" CONFIG_FILE = "test/gpg-mailgate.conf" -def build_config(config): + +def _build_config(config): cp = configparser.RawConfigParser() cp.add_section("logging") @@ -48,6 +44,10 @@ def build_config(config): cp.set("relay", "host", "localhost") cp.set("relay", "port", config["port"]) + cp.add_section("daemon") + cp.set("daemon", "host", "localhost") + cp.set("daemon", "port", "10025") + cp.add_section("enc_keymap") cp.set("enc_keymap", "alice@disposlab", "1CD245308F0963D038E88357973CF4D9387C44D7") cp.set("enc_keymap", "bob@disposlab", "19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67") @@ -61,24 +61,27 @@ def build_config(config): logging.debug(f"Created config with keyhome={config['gpg_keyhome']}, cert_path={config['smime_certpath']} and relay at port {config['port']}") return cp -def write_test_config(outfile, **config): + +def _write_test_config(outfile, **config): logging.debug(f"Generating configuration with {config!r}") out = open(outfile, "w+") - cp = build_config(config) + cp = _build_config(config) cp.write(out) out.close() logging.debug(f"Wrote configuration to {outfile}") -def load_file(name): - f = open(name, 'r') - contents = f.read() - f.close() - return bytes(contents, 'utf-8') +def _load_file(name): + f = open(name, 'r') + contents = f.read() + f.close() -def report_result(message_file, expected, test_output): + return bytes(contents, 'utf-8') + + +def _report_result(message_file, expected, test_output): status = None if expected in test_output: status = "Success" @@ -87,7 +90,8 @@ def report_result(message_file, expected, test_output): print(message_file.ljust(30), status) -def execute_e2e_test(case_name, config, config_path): + +def _execute_e2e_test(case_name, config, config_path): """Read test case configuration from config and run that test case. Parameter case_name should refer to a section in test @@ -114,7 +118,7 @@ def execute_e2e_test(case_name, config, config_path): # pass PATH because otherwise it would be dropped gpglacre_proc = subprocess.run(gpglacre_cmd, - input = load_file(config.get(case_name, "in")), + input = _load_file(config.get(case_name, "in")), capture_output = True, env = {"GPG_MAILGATE_CONFIG": config_path, "PATH": os.getenv("PATH")}) @@ -127,16 +131,17 @@ def execute_e2e_test(case_name, config, config_path): logging.debug(f"Read {len(testout)} characters of test output: '{testout}'") - report_result(config.get(case_name, "in"), config.get(case_name, "out"), testout) + _report_result(config.get(case_name, "in"), config.get(case_name, "out"), testout) -def load_test_config(): + +def _load_test_config(): cp = configparser.ConfigParser() cp.read("test/e2e.ini") return cp -config = load_test_config() +config = _load_test_config() logging.basicConfig(filename = config.get("tests", "e2e_log"), # Get raw values of log and date formats because they @@ -148,7 +153,7 @@ logging.basicConfig(filename = config.get("tests", "e2e_log"), config_path = os.getcwd() + "/" + CONFIG_FILE -write_test_config(config_path, +_write_test_config(config_path, port = config.get("relay", "port"), gpg_keyhome = config.get("dirs", "keys"), smime_certpath = config.get("dirs", "certs"), @@ -158,6 +163,6 @@ for case_no in range(1, config.getint("tests", "cases")+1): case_name = f"case-{case_no}" logging.info(f"Executing {case_name}: {config.get(case_name, 'descr')}") - execute_e2e_test(case_name, config, config_path) + _execute_e2e_test(case_name, config, config_path) print("See diagnostic output for details. Tests: '%s', Lacre: '%s'" % (config.get("tests", "e2e_log"), config.get("tests", "lacre_log"))) diff --git a/test/test_contracts.py b/test/modules/test_contracts.py similarity index 100% rename from test/test_contracts.py rename to test/modules/test_contracts.py diff --git a/test/test_gnupg.py b/test/modules/test_gnupg.py similarity index 100% rename from test/test_gnupg.py rename to test/modules/test_gnupg.py diff --git a/test/test_lacre_text.py b/test/modules/test_lacre_text.py similarity index 100% rename from test/test_lacre_text.py rename to test/modules/test_lacre_text.py diff --git a/test/modules/test_m2crypto.py b/test/modules/test_m2crypto.py new file mode 100644 index 0000000..2233c2d --- /dev/null +++ b/test/modules/test_m2crypto.py @@ -0,0 +1,15 @@ +from M2Crypto import BIO +import unittest + +class M2CryptoBioMemoryBufferTest(unittest.TestCase): + def test_memory_buffer_write_str(self): + mb = BIO.MemoryBuffer() + mb.write("Foo") + mb.close() + self.assertEqual(len(mb), 3) + + def test_memory_buffer_write_bytes(self): + mb = BIO.MemoryBuffer() + mb.write(b"Foo") + mb.close() + self.assertEqual(len(mb), 3) diff --git a/test/relay.py b/test/utils/relay.py similarity index 100% rename from test/relay.py rename to test/utils/relay.py diff --git a/test/schema.py b/test/utils/schema.py similarity index 100% rename from test/schema.py rename to test/utils/schema.py diff --git a/test/sendmail.py b/test/utils/sendmail.py similarity index 54% rename from test/sendmail.py rename to test/utils/sendmail.py index bc59c6e..edacf3b 100644 --- a/test/sendmail.py +++ b/test/utils/sendmail.py @@ -1,4 +1,6 @@ import smtplib +import sys +import getopt def _send(host, port, from_addr, recipients, message): @@ -11,7 +13,18 @@ def _send(host, port, from_addr, recipients, message): # print(f"Couldn't deliver message.\nGot error: {e}\n") -message = """\ +sender = recipient = message = None + +for opt, value in getopt.getopt(sys.argv[1:], "f:t:m:"): + if opt == "f": + sender = value + if opt == "t": + recipient = value + if opt == "m": + message = value + +if message is None: + message = """\ From: dave@disposlab To: alice@disposlab Subject: Test message @@ -19,4 +32,4 @@ Subject: Test message Lorem ipsum dolor sit amet. """ -_send('localhost', 10025, 'dave@disposlab', ['alice@disposlab'], message) +_send('localhost', 10025, sender, [recipient], message)