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).
This commit is contained in:
parent
ad94385f9b
commit
a8bfe3bdaf
4
Makefile
4
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
|
GPG_MAILGATE_CONFIG=test/gpg-mailgate-cron-test.conf PYTHONPATH=`pwd` $(PYTHON) gpg-mailgate-web/cron.py
|
||||||
|
|
||||||
$(TEST_DB):
|
$(TEST_DB):
|
||||||
$(PYTHON) test/schema.py $(TEST_DB)
|
$(PYTHON) test/utils/schema.py $(TEST_DB)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Run an e2e test of Advanced Content Filter.
|
# Run an e2e test of Advanced Content Filter.
|
||||||
|
@ -59,7 +59,7 @@ clean-db:
|
||||||
# Run unit tests
|
# Run unit tests
|
||||||
#
|
#
|
||||||
unittest:
|
unittest:
|
||||||
$(PYTHON) -m unittest discover -s test
|
$(PYTHON) -m unittest discover -s test/modules
|
||||||
|
|
||||||
pre-clean:
|
pre-clean:
|
||||||
rm -fv test/gpg-mailgate.conf
|
rm -fv test/gpg-mailgate.conf
|
||||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
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()
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
[relay]
|
[relay]
|
||||||
port: 2500
|
port: 2500
|
||||||
script: test/relay.py
|
script: test/utils/relay.py
|
||||||
|
|
||||||
[dirs]
|
[dirs]
|
||||||
keys: test/keyhome
|
keys: test/keyhome
|
||||||
|
|
|
@ -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
|
# 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
|
# it under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# gpg-mailgate source code is distributed in the hope that it will be useful,
|
# gpg-mailgate source code is distributed in the hope that it will be useful,
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with gpg-mailgate source code. If not, see <http://www.gnu.org/licenses/>.
|
# along with gpg-mailgate source code. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
import difflib
|
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
RELAY_SCRIPT = "test/relay.py"
|
RELAY_SCRIPT = "test/utils/relay.py"
|
||||||
CONFIG_FILE = "test/gpg-mailgate.conf"
|
CONFIG_FILE = "test/gpg-mailgate.conf"
|
||||||
|
|
||||||
def build_config(config):
|
|
||||||
|
def _build_config(config):
|
||||||
cp = configparser.RawConfigParser()
|
cp = configparser.RawConfigParser()
|
||||||
|
|
||||||
cp.add_section("logging")
|
cp.add_section("logging")
|
||||||
|
@ -48,6 +44,10 @@ def build_config(config):
|
||||||
cp.set("relay", "host", "localhost")
|
cp.set("relay", "host", "localhost")
|
||||||
cp.set("relay", "port", config["port"])
|
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.add_section("enc_keymap")
|
||||||
cp.set("enc_keymap", "alice@disposlab", "1CD245308F0963D038E88357973CF4D9387C44D7")
|
cp.set("enc_keymap", "alice@disposlab", "1CD245308F0963D038E88357973CF4D9387C44D7")
|
||||||
cp.set("enc_keymap", "bob@disposlab", "19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67")
|
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']}")
|
logging.debug(f"Created config with keyhome={config['gpg_keyhome']}, cert_path={config['smime_certpath']} and relay at port {config['port']}")
|
||||||
return cp
|
return cp
|
||||||
|
|
||||||
def write_test_config(outfile, **config):
|
|
||||||
|
def _write_test_config(outfile, **config):
|
||||||
logging.debug(f"Generating configuration with {config!r}")
|
logging.debug(f"Generating configuration with {config!r}")
|
||||||
|
|
||||||
out = open(outfile, "w+")
|
out = open(outfile, "w+")
|
||||||
cp = build_config(config)
|
cp = _build_config(config)
|
||||||
cp.write(out)
|
cp.write(out)
|
||||||
out.close()
|
out.close()
|
||||||
|
|
||||||
logging.debug(f"Wrote configuration to {outfile}")
|
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
|
status = None
|
||||||
if expected in test_output:
|
if expected in test_output:
|
||||||
status = "Success"
|
status = "Success"
|
||||||
|
@ -87,7 +90,8 @@ def report_result(message_file, expected, test_output):
|
||||||
|
|
||||||
print(message_file.ljust(30), status)
|
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.
|
"""Read test case configuration from config and run that test case.
|
||||||
|
|
||||||
Parameter case_name should refer to a section in test
|
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
|
# pass PATH because otherwise it would be dropped
|
||||||
gpglacre_proc = subprocess.run(gpglacre_cmd,
|
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,
|
capture_output = True,
|
||||||
env = {"GPG_MAILGATE_CONFIG": config_path,
|
env = {"GPG_MAILGATE_CONFIG": config_path,
|
||||||
"PATH": os.getenv("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}'")
|
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 = configparser.ConfigParser()
|
||||||
cp.read("test/e2e.ini")
|
cp.read("test/e2e.ini")
|
||||||
|
|
||||||
return cp
|
return cp
|
||||||
|
|
||||||
|
|
||||||
config = load_test_config()
|
config = _load_test_config()
|
||||||
|
|
||||||
logging.basicConfig(filename = config.get("tests", "e2e_log"),
|
logging.basicConfig(filename = config.get("tests", "e2e_log"),
|
||||||
# Get raw values of log and date formats because they
|
# 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
|
config_path = os.getcwd() + "/" + CONFIG_FILE
|
||||||
|
|
||||||
write_test_config(config_path,
|
_write_test_config(config_path,
|
||||||
port = config.get("relay", "port"),
|
port = config.get("relay", "port"),
|
||||||
gpg_keyhome = config.get("dirs", "keys"),
|
gpg_keyhome = config.get("dirs", "keys"),
|
||||||
smime_certpath = config.get("dirs", "certs"),
|
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}"
|
case_name = f"case-{case_no}"
|
||||||
logging.info(f"Executing {case_name}: {config.get(case_name, 'descr')}")
|
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")))
|
print("See diagnostic output for details. Tests: '%s', Lacre: '%s'" % (config.get("tests", "e2e_log"), config.get("tests", "lacre_log")))
|
||||||
|
|
|
@ -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)
|
|
@ -1,4 +1,6 @@
|
||||||
import smtplib
|
import smtplib
|
||||||
|
import sys
|
||||||
|
import getopt
|
||||||
|
|
||||||
|
|
||||||
def _send(host, port, from_addr, recipients, message):
|
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")
|
# 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
|
From: dave@disposlab
|
||||||
To: alice@disposlab
|
To: alice@disposlab
|
||||||
Subject: Test message
|
Subject: Test message
|
||||||
|
@ -19,4 +32,4 @@ Subject: Test message
|
||||||
Lorem ipsum dolor sit amet.
|
Lorem ipsum dolor sit amet.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_send('localhost', 10025, 'dave@disposlab', ['alice@disposlab'], message)
|
_send('localhost', 10025, sender, [recipient], message)
|
Loading…
Reference in New Issue