Skip to content

Commit

Permalink
Added docs and test for check_url_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
zenmonkeykstop committed Dec 13, 2019
1 parent 7f1c53f commit 7d74a45
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions securedrop/source_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions securedrop/tests/test_source_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7d74a45

Please sign in to comment.