From 27d7481078a4dff419efa960734009f4817d0c7d Mon Sep 17 00:00:00 2001 From: "Piotr F. Mieszkowski" Date: Thu, 11 Nov 2021 10:57:00 +0100 Subject: [PATCH] Set up ground for E2E tests - Use an environment variable to point at the configuration file while strating gpg-mailgate.py. - Unify paths: store temporary config, logs and anything else under 'test' directory. - Configure more tests (RSA, Ed25519). - Add test descriptions to be shown before they're started. --- Makefile | 16 ++++++++++++++-- test/e2e.ini | 17 +++++++++++++++-- test/e2e_test.py | 27 +++++++++++++++++++-------- test/msgin/clear2ed.msg | 5 +++++ test/msgin/clear2rsa.msg | 5 +++++ test/msgout/clear2ed.msg | 5 +++++ test/msgout/clear2rsa.msg | 5 +++++ test/relay.py | 10 ++++++++-- 8 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 test/msgin/clear2ed.msg create mode 100644 test/msgin/clear2rsa.msg create mode 100644 test/msgout/clear2ed.msg create mode 100644 test/msgout/clear2rsa.msg diff --git a/Makefile b/Makefile index 1a4bc8a..5c50c1d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,18 @@ PYTHON = python2.7 -.PHONY: test +.PHONY: test pre-clean clean -test: +test: test/tmp test/logs pre-clean $(PYTHON) test/e2e_test.py + +pre-clean: + rm -fv test/gpg-mailgate.conf + +test/tmp: + mkdir test/tmp + +test/logs: + mkdir test/logs + +clean: pre-clean + rm -rfv test/tmp test/logs diff --git a/test/e2e.ini b/test/e2e.ini index 6f3574d..3e0899f 100644 --- a/test/e2e.ini +++ b/test/e2e.ini @@ -2,9 +2,22 @@ port = 2500 [tests] -cases = 1 +cases = 3 [case-1] -from = alice@localhost +descr = Clear text message to a user without a key +to = carlos@disposlab in = test/msgin/clear2clear.msg out = test/msgout/clear2clear.msg + +[case-2] +descr = Clear text message to a user with an RSA key +to = alice@disposlab +in = test/msgin/clear2rsa.msg +out = test/msgout/clear2rsa.msg + +[case-3] +descr = Clear text message to a user with an Ed25519 key +to = bob@disposlab +in = test/msgin/clear2ed.msg +out = test/msgout/clear2ed.msg diff --git a/test/e2e_test.py b/test/e2e_test.py index 40964ad..37ccbb0 100644 --- a/test/e2e_test.py +++ b/test/e2e_test.py @@ -8,8 +8,12 @@ import difflib import ConfigParser import logging +from time import sleep + EOL = "\n" +DELAY = 3 + RELAY_SCRIPT = "test/relay.py" CONFIG_FILE = "test/gpg-mailgate.conf" @@ -19,7 +23,7 @@ def build_config(config): cp = ConfigParser.ConfigParser() cp.add_section("logging") - cp.set("logging", "file", "/dev/stout") + cp.set("logging", "file", config["log_file"]) cp.set("logging", "verbose", "yes") cp.add_section("gpg") @@ -75,7 +79,7 @@ def report_result(message_file, expected_file, test_output): print diff_line def execute_e2e_test(message_file, expected_file, **kwargs): - test_command = "%s gpg-mailgate.py %s < %s" % (PYTHON_BIN, kwargs["from_addr"], message_file) + test_command = "GPG_MAILGATE_CONFIG=%s %s gpg-mailgate.py %s < %s" % (kwargs["config_path"], PYTHON_BIN, kwargs["to_addr"], message_file) result_command = "%s %s %d" % (PYTHON_BIN, RELAY_SCRIPT, kwargs["port"]) logging.debug("Spawning: '%s'" % (result_command)) @@ -102,19 +106,26 @@ def load_config(): config = load_config() -logging.basicConfig(filename = "e2e_test.log", - format = "%(pathname)s:%(lineno)d %(levelname)s [%(funcName)s] %(message)s", +logging.basicConfig(filename = "test/logs/e2e.log", + format = "%(asctime)s %(pathname)s:%(lineno)d %(levelname)s [%(funcName)s] %(message)s", datefmt = "%Y-%m-%d %H:%M:%S", level = logging.DEBUG) -write_test_config(os.getcwd() + "/" + CONFIG_FILE, +config_path = os.getcwd() + "/" + CONFIG_FILE + +write_test_config(config_path, port = config.getint("relay", "port"), gpg_keyhome = "test/keyhome", - smime_certpath = "test/certs") + smime_certpath = "test/certs", + log_file = "test/logs/gpg-mailgate.log") for case_no in range(1, config.getint("tests", "cases")+1): case_name = "case-%d" % (case_no) + print "Executing: %s" % (config.get(case_name, "descr")) execute_e2e_test(config.get(case_name, "in"), config.get(case_name, "out"), - from_addr = config.get(case_name, "from"), - port = config.getint("relay", "port")) + config_path = config_path, + to_addr = config.get(case_name, "to"), + port = config.getint("relay", "port")) + + sleep(DELAY) diff --git a/test/msgin/clear2ed.msg b/test/msgin/clear2ed.msg new file mode 100644 index 0000000..5fe7ff7 --- /dev/null +++ b/test/msgin/clear2ed.msg @@ -0,0 +1,5 @@ +From: Bob +To: Alice +Subject: Test + +Body of the message. diff --git a/test/msgin/clear2rsa.msg b/test/msgin/clear2rsa.msg new file mode 100644 index 0000000..5fe7ff7 --- /dev/null +++ b/test/msgin/clear2rsa.msg @@ -0,0 +1,5 @@ +From: Bob +To: Alice +Subject: Test + +Body of the message. diff --git a/test/msgout/clear2ed.msg b/test/msgout/clear2ed.msg new file mode 100644 index 0000000..5fe7ff7 --- /dev/null +++ b/test/msgout/clear2ed.msg @@ -0,0 +1,5 @@ +From: Bob +To: Alice +Subject: Test + +Body of the message. diff --git a/test/msgout/clear2rsa.msg b/test/msgout/clear2rsa.msg new file mode 100644 index 0000000..5fe7ff7 --- /dev/null +++ b/test/msgout/clear2rsa.msg @@ -0,0 +1,5 @@ +From: Bob +To: Alice +Subject: Test + +Body of the message. diff --git a/test/relay.py b/test/relay.py index cfbe02a..eca73ec 100644 --- a/test/relay.py +++ b/test/relay.py @@ -12,6 +12,8 @@ import sys import socket +EXIT_UNAVAILABLE = 1 + BUFFER_SIZE = 4096 EOM = "\r\n.\r\n" LAST_LINE = -3 @@ -32,8 +34,12 @@ def receive_and_confirm(session): def serve(port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.bind(('', port)) - s.listen(1) + try: + s.bind(('', port)) + s.listen(1) + except socket.error, e: + print "Cannot connect", e + sys.exit(EXIT_UNAVAILABLE) (conn, addr) = s.accept() conn.sendall(welcome("TEST SERVER"))