From 7d74a45637149f73a6b42e01a13c722609b5ccf1 Mon Sep 17 00:00:00 2001 From: Kevin O'Gorman Date: Fri, 13 Dec 2019 10:43:38 -0800 Subject: [PATCH] Added docs and test for check_url_file() --- securedrop/source_app/utils.py | 5 +++++ securedrop/tests/test_source_utils.py | 30 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 securedrop/tests/test_source_utils.py diff --git a/securedrop/source_app/utils.py b/securedrop/source_app/utils.py index a496ff753b..0a5475567f 100644 --- a/securedrop/source_app/utils.py +++ b/securedrop/source_app/utils.py @@ -116,6 +116,11 @@ def normalize_timestamps(filesystem_id): def check_url_file(path, regexp): + """ + Check that a file exists at the path given and contains a single line + matching the regexp. Used for checking the source interface address + files at /var/lib/securedrop/source_{v2,v3}_url. + """ try: f = open(path, "r") contents = f.readline().strip() diff --git a/securedrop/tests/test_source_utils.py b/securedrop/tests/test_source_utils.py new file mode 100644 index 0000000000..1a5b998b00 --- /dev/null +++ b/securedrop/tests/test_source_utils.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +import os + +from source_app.utils import check_url_file + + +def test_check_url_file(config): + + assert check_url_file("nosuchfile", "whatever") is None + + try: + def write_url_file(path, content): + url_file = open(path, "w") + url_file.write("{}\n".format(content)) + + url_path = "test_source_url" + + onion_test_url = "abcdabcdabcdabcd.onion" + write_url_file(url_path, onion_test_url) + assert check_url_file(url_path, r"^[a-z0-9]{16}\.onion$") == onion_test_url + + onion_test_url = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh.onion" + write_url_file(url_path, onion_test_url) + assert check_url_file(url_path, r"^[a-z0-9]{56}\.onion$") == onion_test_url + + write_url_file(url_path, "NO.onion") + assert check_url_file(url_path, r"^[a-z0-9]{56}\.onion$") is None + finally: + if os.path.exists(url_path): + os.unlink(url_path)