Commit 3e22c678 authored by Carlos Garcia Campos's avatar Carlos Garcia Campos

regtest: Add support for testing password protected documents

Similar to how skipped file works, you can pass a passwords file from
the command line or add a Passwords file to your docs directory. This
file should be a python file containing a "passwords" dictionary where
the key is the test document and the value is the password required to
open that document.
parent 50ba37c7
......@@ -21,7 +21,7 @@ import errno
from backends import get_backend, get_all_backends
from Config import Config
from Printer import get_printer
from Utils import get_document_paths_from_dir, get_skipped_tests
from Utils import get_document_paths_from_dir, get_skipped_tests, get_passwords
from Queue import Queue
from threading import Thread, RLock
......@@ -32,6 +32,7 @@ class TestReferences:
self._docsdir = docsdir
self._refsdir = refsdir
self._skipped = get_skipped_tests(docsdir)
self._passwords = get_passwords(docsdir)
self.config = Config()
self.printer = get_printer()
self._total_tests = 1
......@@ -73,6 +74,8 @@ class TestReferences:
raise
doc_path = os.path.join(self._docsdir, filename)
password = self._passwords.get(filename)
for backend in backends:
if not self.config.force and backend.has_results(refs_path):
with self._lock:
......@@ -80,7 +83,7 @@ class TestReferences:
self.printer.print_default("Results found, skipping '%s' for %s backend" % (doc_path, backend.get_name()))
continue
if backend.create_refs(doc_path, refs_path):
if backend.create_refs(doc_path, refs_path, password):
backend.create_checksums(refs_path, self.config.checksums_only)
with self._lock:
self._n_tests += 1
......
......@@ -18,7 +18,7 @@
from backends import get_backend, get_all_backends
from Config import Config
from Utils import get_document_paths_from_dir, get_skipped_tests
from Utils import get_document_paths_from_dir, get_skipped_tests, get_passwords
from Printer import get_printer
import sys
import os
......@@ -34,6 +34,7 @@ class TestRun:
self._refsdir = refsdir
self._outdir = outdir
self._skip = get_skipped_tests(docsdir)
self._passwords = get_passwords(docsdir)
self.config = Config()
self.printer = get_printer()
self._total_tests = 1
......@@ -68,7 +69,7 @@ class TestRun:
return get_all_backends()
def test(self, refs_path, doc_path, test_path, backend):
def test(self, refs_path, doc_path, test_path, backend, password):
# First check whether there are test results for the backend
ref_has_md5 = backend.has_md5(refs_path)
ref_is_crashed = backend.is_crashed(refs_path)
......@@ -80,7 +81,7 @@ class TestRun:
self.printer.print_default("Reference files not found, skipping '%s' for %s backend" % (doc_path, backend.get_name()))
return
test_has_md5 = backend.create_refs(doc_path, test_path)
test_has_md5 = backend.create_refs(doc_path, test_path, password)
test_passed = False
if ref_has_md5 and test_has_md5:
test_passed = backend.compare_checksums(refs_path, test_path, not self.config.keep_results, self.config.create_diffs, self.config.update_refs)
......@@ -165,8 +166,10 @@ class TestRun:
self.printer.print_default("Reference dir not found for %s, skipping" % (doc_path))
return
password = self._passwords.get(filename)
for backend in backends:
self.test(refs_path, doc_path, out_path, backend)
self.test(refs_path, doc_path, out_path, backend, password)
def _worker_thread(self):
while True:
......
......@@ -55,4 +55,17 @@ def get_skipped_tests(docsdir):
f.close()
return skipped
def get_passwords(docsdir):
from Config import Config
config = Config()
if config.passwords_file:
passwords_file = config.passwords_file
elif os.path.exists(os.path.join(docsdir, 'Passwords')):
passwords_file = os.path.join(docsdir, 'Passwords')
else:
return {}
passwords = {}
execfile(passwords_file, passwords)
return passwords['passwords']
......@@ -287,7 +287,7 @@ class Backend:
def _create_diff(self, ref_path, result_path):
raise NotImplementedError
def create_refs(self, doc_path, refs_path):
def create_refs(self, doc_path, refs_path, password = None):
raise NotImplementedError
_backends = {}
......
......@@ -26,9 +26,12 @@ class Cairo(Backend):
Backend.__init__(self, name, '.diff.png')
self._pdftocairo = os.path.join(self._utilsdir, 'pdftocairo');
def create_refs(self, doc_path, refs_path):
def create_refs(self, doc_path, refs_path, password = None):
out_path = os.path.join(refs_path, 'cairo')
p = subprocess.Popen([self._pdftocairo, '-cropbox', '-r', '72', '-png', doc_path, out_path], stderr = subprocess.PIPE)
cmd = [self._pdftocairo, '-cropbox', '-r', '72', '-png', doc_path, out_path]
if password is not None:
cmd.extend(['-opw', password])
p = subprocess.Popen(cmd, stderr = subprocess.PIPE)
return self._check_exit_status(p, out_path)
def _create_diff(self, ref_path, result_path):
......
......@@ -26,9 +26,12 @@ class PostScript(Backend):
Backend.__init__(self, name)
self._pdftops = os.path.join(self._utilsdir, 'pdftops');
def create_refs(self, doc_path, refs_path):
def create_refs(self, doc_path, refs_path, password = None):
out_path = os.path.join(refs_path, 'postscript')
p = subprocess.Popen([self._pdftops, doc_path, out_path + '.ps'], stderr = subprocess.PIPE)
cmd = [self._pdftops, doc_path, out_path + '.ps']
if password is not None:
cmd.extend(['-opw', password])
p = subprocess.Popen(cmd, stderr = subprocess.PIPE)
return self._check_exit_status(p, out_path)
register_backend('postscript', PostScript)
......
......@@ -26,9 +26,12 @@ class Splash(Backend):
Backend.__init__(self, name, '.diff.png')
self._pdftoppm = os.path.join(self._utilsdir, 'pdftoppm');
def create_refs(self, doc_path, refs_path):
def create_refs(self, doc_path, refs_path, password = None):
out_path = os.path.join(refs_path, 'splash')
p = subprocess.Popen([self._pdftoppm, '-cropbox', '-r', '72', '-png', doc_path, out_path], stderr = subprocess.PIPE)
cmd = [self._pdftoppm, '-cropbox', '-r', '72', '-png', doc_path, out_path]
if password is not None:
cmd.extend(['-opw', password])
p = subprocess.Popen(cmd, stderr = subprocess.PIPE)
return self._check_exit_status(p, out_path)
def _create_diff(self, ref_path, result_path):
......
......@@ -26,9 +26,12 @@ class Text(Backend):
Backend.__init__(self, name, '.diff')
self._pdftotext = os.path.join(self._utilsdir, 'pdftotext');
def create_refs(self, doc_path, refs_path):
def create_refs(self, doc_path, refs_path, password = None):
out_path = os.path.join(refs_path, 'text')
p = subprocess.Popen([self._pdftotext, doc_path, out_path + '.txt'], stderr = subprocess.PIPE)
cmd = [self._pdftotext, doc_path, out_path + '.txt']
if password is not None:
cmd.extend(['-opw', password])
p = subprocess.Popen([cmd, stderr = subprocess.PIPE)
return self._check_exit_status(p, out_path)
def _create_diff(self, ref_path, result_path):
......
......@@ -65,6 +65,9 @@ def main(args):
parser.add_argument('--skip', metavar = 'FILE',
action = 'store', dest = 'skipped_file',
help = 'File containing tests to skip')
parser.add_argument('-p', '--passwords', metavar = 'FILE',
action = 'store', dest = 'passwords_file',
help = 'File containing the documents passwords')
parser.add_argument('-t', '--threads',
action = 'store', dest = 'threads', type = int, default = n_cpus,
help = 'Number of worker threads (Default: %d)' % n_cpus)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment