aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/toolkit.py
blob: 1a5859bcd6cce9cb1dfecb82cb540fd40544b098 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.

import os.path
import subprocess
import sys

class Tools:
    def __init__(self, root_dir_path, use_sde=False):
        self._root_dir_path = root_dir_path
        self._use_sde = use_sde

    def _get_tool_path(self, tool_name):
        return os.path.join(self._root_dir_path, tool_name)

    def _get_encrypt_tool_path(self, prefix):
        return self._get_tool_path('{0}_encrypt.exe'.format(prefix))

    def _get_decrypt_tool_path(self, prefix):
        return self._get_tool_path('{0}_decrypt.exe'.format(prefix))

    def _capture_tool_output(self, tool_path, args):
        with_sde = ['sde', '--', tool_path] if self._use_sde else [tool_path]
        return subprocess.check_output(with_sde + args, universal_newlines=True).split()

    def encrypt(self, prefix, key, args):
        print('Encrypting using \'{0}\'...'.format(prefix))
        print('\tKey:', key)
        return self._capture_tool_output(self._get_encrypt_tool_path(prefix), [key] + args)

    def decrypt(self, prefix, key, args):
        print('Decrypting using \'{0}\'...'.format(prefix))
        print('\tKey:', key)
        return self._capture_tool_output(self._get_decrypt_tool_path(prefix), [key] + args)

    def encrypt_with_iv(self, prefix, key, iv, args):
        print('Encrypting using \'{0}\'...'.format(prefix))
        print('\tKey:', key)
        print('\tInitialization vector:', iv)
        return self._capture_tool_output(self._get_encrypt_tool_path(prefix), [key, iv] + args)

    def decrypt_with_iv(self, prefix, key, iv, args):
        print('Decrypting using \'{0}\'...'.format(prefix))
        print('\tKey:', key)
        print('\tInitialization vector:', iv)
        return self._capture_tool_output(self._get_decrypt_tool_path(prefix), [key, iv] + args)

    def detect_mismatches(self, input, actual_output, expected_output):
        if len(actual_output) != len(expected_output):
            print('Unexpected output length!', file=sys.stderr)
            print('\tExpected length:', len(expected_output), file=sys.stderr)
            print('\tActual length:', len(actual_output), file=sys.stderr)
            return False
        no_mismatches = True
        for i in range(len(input)):
            if actual_output[i] != expected_output[i]:
                print('A mismatch detected!', file=sys.stderr)
                print('\tInput:', input[i], file=sys.stderr)
                print('\tExpected:', expected_output[i], file=sys.stderr)
                print('\tActual:', actual_output[i], file=sys.stderr)
                no_mismatches = False
        return no_mismatches