<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from os import urandom
from base64 import b64decode
from nose.tools import eq_, ok_, raises
from itertools import product
from unittest import TestCase

from serverauditor_sshconfig.cloud.client.cryptor import RNCryptor


def test_dual_encrypt_and_decrypt():
    configs = [
        config_factory(i)
        for i in ('password', '<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
    ]
    texts = [
        'test', 'text', '',
    ]
    for config, text in product(configs, texts):
        cryptor = generate_cryptor(**config)
        yield dual_encrypt_decrypt_text, cryptor, text


def dual_encrypt_decrypt_text(cryptor, original_text):
    ciphertext = cryptor.encrypt(original_text)
    text = cryptor.decrypt(ciphertext)
    eq_(text, original_text)


def test_encrypt_and_decrypt():
    cryptor = generate_cryptor(
        '1', b64decode('wenOgffhaJ8='), b64decode('8VbldsORPa4=')
    )
    text__ciphertexts = [
        ('localhost',
         '<KEY>'
         'fEvUCObqsbPwOaD3kaj6L7W+uK03ayY6+mveto9yQqg=='),
        ('localhost',
         '<KEY>'
         'skP2XVvHAR9xolNtfWwUDLQqgO1q5hiH3bukiCLJ1cw=='),
        ('localhost',
         'AgHB6c6B9+Fon/FW5XbDkT2ub25WJP3rVv1e4yHAljHPbH1xn9IIqw24in73Dm'
         'Aihe0fEvUCObqsbPwOaD3kaj6L7W+uK03ayY6+mveto9yQqg=='),
    ]
    for text, ciphertext in text__ciphertexts:
        yield encrypt_decrypt_text, cryptor, text, ciphertext


def encrypt_decrypt_text(cryptor, original_text, original_ciphertext):
    text = cryptor.decrypt(original_ciphertext)
    ciphertext = cryptor.encrypt(text)
    eq_(text, original_text)
    ok_(ciphertext != original_ciphertext)


@raises(TypeError)
def test_encrypt_none():
    cryptor = generate_cryptor(**config_factory('pass'))
    cryptor.encrypt(None)


@raises(TypeError)
def test_decrypt_none():
    cryptor = generate_cryptor(**config_factory('pa$$'))
    cryptor.decrypt(None)


def generate_cryptor(password, encryption_salt, hmac_salt):
    cryptor = RNCryptor()
    cryptor.password = password
    cryptor.encryption_salt = encryption_salt
    cryptor.hmac_salt = hmac_salt
    return cryptor


def config_factory(password):
    return {
        'password': password,
        'encryption_salt': urandom(8),
        'hmac_salt': urandom(8),
    }


class CryptorV2Test(TestCase):

    def setUp(self):
        self.encryption_salt = b64decode('2ddhjTxwLmw=')
        self.hmac_salt = b64decode('Yx6z87fkXzo=')

    def test_decrypt_empty_value(self):
        password = 'a'
        decrypted_value = ''
        encrypted_by_ios_value = '<KEY>
        encrypted_by_web_value = '<KEY>NEgwgvIIX2Q0btG44er8AphIff/2VkXYIHCsi7FiQyucMqNyzFzIJKPu8iJciQPsq/g=='
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_decrypt_one_byte_value(self):
        password = '<PASSWORD>'
        decrypted_value = '01'
        encrypted_by_ios_value = '<KEY>
        encrypted_by_web_value = '<KEY>
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_decrypt_value(self):
        password = '<PASSWORD>'
        decrypted_value = '0123456789abcdef'
        encrypted_by_ios_value = '<KEY>
        encrypted_by_web_value = '<KEY>1ErmdfXjXSxH/<KEY>QDaVOMXDn/L2TbrmLt61mZSjoc2LPewVqfVNCQu3BoOA52iQc='
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_decrypt_value_with_space(self):
        password = '<PASSWORD>'
        decrypted_value = '0123456789abcdef 01234567'
        encrypted_by_ios_value = '<KEY>
        encrypted_by_web_value = '<KEY>
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_fail_to_decrypt_value_with_space_by_multi_byte_password(self):
        password = '<PASSWORD>'
        decrypted_value = '23456789abcdef 0123456701'
        encrypted_by_ios_value = 'AgHZ12GNPHAubGMes/O35F86KSMM0xuVMS3b/RQI/FeOFjpBQHxEKPfA+T2AIJ3QU15cbJcRC9mXagSQ0jdKh+HnUkalis3c6+WT/lPvjFLz0fQoWi6m4FSIe/rdtzvoknQ='
        encrypted_by_web_value = 'AgHZ12GNPHAubGMes/O35F86MNtvS3Wg6QPegGSBZbDac2CFPpGG48x29zQgX6fukIe+MBtI1lxqaL4fPal2crmOXpGLBCnqPyOFeJKzwhyFriyK8gZ71hPlLaOO4XvONAc='
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, True)

    def test_decrypt_long_value_with_space_by_long_password(self):
        password = '<PASSWORD>;'
        decrypted_value = '69 74 20 77 61 73 20 74 68 65 20 65 70 6f 63 68 20 6f 66 20 62 65 6c 69 65 66 2c 20 69 74 20 77 61 73 20 74 68 65 20 65 70 6f 63 68 20 6f 66 20 69 6e 63 72 65 64 75 6c 69 74 79 3b 20 69 74 20 77 61 73 20 74 68 65 20 73 65 61 73 6f 6e 20 6f 66 20 4c 69 67 68 74 2c 20 69 74 20 77 61 73 20 74 68 65 20 73 65 61 73 6f 6e 20 6f 66 20 44 61 72 6b 6e 65 73 73 3b 20 69 74 20 77 61 73 20 74 68 65 20 73 70 72 69 6e 67 20 6f 66 20 68 6f 70 65 2c 20 69 74 20 77 61 73 20 74 68 65 20 77 69 6e 74 65 72 20 6f 66 20 64 65 73 70 61 69 72 3b 20 77 65 20 68 61 64 20 65 76 65 72 79 74 68 69 6e 67 20 62 65 66 6f 72 65 20 75 73 2c 20 77 65 20 68 61 64 20 6e 6f 74 68 69 6e 67 20 62 65 66 6f 72 65 20 75 73 3b 20 77 65 20 77 65 72 65 20 61 6c 6c 20 67 6f 69 6e 67 20 64 69 72 65 63 74 6c 79 20 74 6f 20 48 65 61 76 65 6e 2c 20 77 65 20 77 65 72 65 20 61 6c 6c 20 67 6f 69 6e 67 20 74 68 65 20 6f 74 68 65 72 20 77 61 79 2e 0a 0a'
        encrypted_by_ios_value = '<KEY>'
        encrypted_by_web_value = '<KEY>'
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def perform_encryption_actions(self, encrypted_by_ios_value, encrypted_by_web_value, password,
                                   expected_result, should_faild):

        cryptor = self.generate_cryptor(password)

        val = cryptor.encrypt(expected_result)

        res_web = cryptor.decrypt(encrypted_by_web_value)
        self.assertEquals(expected_result, res_web)

        resIos = cryptor.decrypt(encrypted_by_ios_value)
        if should_faild:
            self.assertNotEquals(expected_result, resIos)
        else:
            self.assertEquals(expected_result, resIos)

    def generate_cryptor(self, password):<commit_msg>Fix test for failed decrypting with from ios.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from os import urandom
from base64 import b64decode
from nose.tools import eq_, ok_, raises
from itertools import product
from unittest import TestCase

from serverauditor_sshconfig.cloud.client.cryptor import RNCryptor, CryptorException


def test_dual_encrypt_and_decrypt():
    configs = [
        config_factory(i)
        for i in ('password', '<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
    ]
    texts = [
        'test', 'text', '',
    ]
    for config, text in product(configs, texts):
        cryptor = generate_cryptor(**config)
        yield dual_encrypt_decrypt_text, cryptor, text


def dual_encrypt_decrypt_text(cryptor, original_text):
    ciphertext = cryptor.encrypt(original_text)
    text = cryptor.decrypt(ciphertext)
    eq_(text, original_text)


def test_encrypt_and_decrypt():
    cryptor = generate_cryptor(
        '1', b64decode('wenOgffhaJ8='), b64decode('8VbldsORPa4=')
    )
    text__ciphertexts = [
        ('localhost',
         '<KEY>'
         'fEvUCObqsbPwOaD3kaj6L7W+uK03ayY6+mveto9yQqg=='),
        ('localhost',
         '<KEY>2FFm'
         'skP2XVvHAR9xolNtfWwUDLQqgO1q5hiH3bukiCLJ1cw=='),
        ('localhost',
         'AgHB6c6B9+Fon/FW5XbDkT2ub25WJP3rVv1e4yHAljHPbH1xn9IIqw24in73Dm'
         'Aihe0fEvUCObqsbPwOaD3kaj6L7W+uK03ayY6+mveto9yQqg=='),
    ]
    for text, ciphertext in text__ciphertexts:
        yield encrypt_decrypt_text, cryptor, text, ciphertext


def encrypt_decrypt_text(cryptor, original_text, original_ciphertext):
    text = cryptor.decrypt(original_ciphertext)
    ciphertext = cryptor.encrypt(text)
    eq_(text, original_text)
    ok_(ciphertext != original_ciphertext)


@raises(TypeError)
def test_encrypt_none():
    cryptor = generate_cryptor(**config_factory('pass'))
    cryptor.encrypt(None)


@raises(TypeError)
def test_decrypt_none():
    cryptor = generate_cryptor(**config_factory('pa$$'))
    cryptor.decrypt(None)


def generate_cryptor(password, encryption_salt, hmac_salt):
    cryptor = RNCryptor()
    cryptor.password = password
    cryptor.encryption_salt = encryption_salt
    cryptor.hmac_salt = hmac_salt
    return cryptor


def config_factory(password):
    return {
        'password': password,
        'encryption_salt': urandom(8),
        'hmac_salt': urandom(8),
    }


class CryptorV2Test(TestCase):

    def setUp(self):
        self.encryption_salt = b64decode('2ddhjTxwLmw=')
        self.hmac_salt = b64decode('Yx6z87fkXzo=')

    def test_decrypt_empty_value(self):
        password = 'a'
        decrypted_value = ''
        encrypted_by_ios_value = 'AgHZ12GNPHAubGMes/O35F86ZRB4uTy4AyCGVEax8dUXBQZe50jMkRckoUGrHFjkjc5jnfR2bg5aN6LWHKaXNmxJOHKi1Ok8PVEIrFEDKFKm2Q=='
        encrypted_by_web_value = 'AgHZ12GNPHAubGMes/O35F86rVUPKkH9SmPuMrcVfkrNEgwgvIIX2Q0btG44er8AphIff/2VkXYIHCsi7FiQyucMqNyzFzIJKPu8iJciQPsq/g=='
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_decrypt_one_byte_value(self):
        password = '<PASSWORD>'
        decrypted_value = '01'
        encrypted_by_ios_value = '<KEY>
        encrypted_by_web_value = '<KEY>
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_decrypt_value(self):
        password = '<PASSWORD>'
        decrypted_value = '0123456789abcdef'
        encrypted_by_ios_value = 'AgHZ12GNPHAubGMes/O35F86s0oM0Q2GPKMIq/pAgXT1qFFcjJ6JBQEnHFjTsofC9zNSdv+iBuJgXYV3V66zBHPgNtaxv1qfpip68dWVOKjJ9roPlvwtRi8m+4uhyp00Jew='
        encrypted_by_web_value = 'AgHZ12GNPHAubGMes/O35F86S7NBmtS8Ctdv0kr2jFYfN9+fMOui7s1iuZ1ErmdfXjXSxH/D4Rw3TWKVJKQDaVOMXDn/L2TbrmLt61mZSjoc2LPewVqfVNCQu3BoOA52iQc='
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_decrypt_value_with_space(self):
        password = '<PASSWORD>'
        decrypted_value = '0123456789abcdef 01234567'
        encrypted_by_ios_value = 'AgHZ12GNPHAubGMes/O35F86Jrimi5HAzmODZkuLzR1veMlAqoZF6ptWYC+keaJBOi8z9ektY+0m+uSb6DiRBdbkmr6QNp9usLaokrf3roSrLpOufF6Fb4lHc8Y65jZ/8Q0='
        encrypted_by_web_value = '<KEY>/<KEY>
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def test_fail_to_decrypt_value_with_space_by_multi_byte_password(self):
        password = '<PASSWORD>'
        decrypted_value = '23456789abcdef 0123456701'
        encrypted_by_ios_value = '<KEY>
        encrypted_by_web_value = '<KEY>
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, True)

    def test_decrypt_long_value_with_space_by_long_password(self):
        password = '<PASSWORD>;'
        decrypted_value = '69 74 20 77 61 73 20 74 68 65 20 65 70 6f 63 68 20 6f 66 20 62 65 6c 69 65 66 2c 20 69 74 20 77 61 73 20 74 68 65 20 65 70 6f 63 68 20 6f 66 20 69 6e 63 72 65 64 75 6c 69 74 79 3b 20 69 74 20 77 61 73 20 74 68 65 20 73 65 61 73 6f 6e 20 6f 66 20 4c 69 67 68 74 2c 20 69 74 20 77 61 73 20 74 68 65 20 73 65 61 73 6f 6e 20 6f 66 20 44 61 72 6b 6e 65 73 73 3b 20 69 74 20 77 61 73 20 74 68 65 20 73 70 72 69 6e 67 20 6f 66 20 68 6f 70 65 2c 20 69 74 20 77 61 73 20 74 68 65 20 77 69 6e 74 65 72 20 6f 66 20 64 65 73 70 61 69 72 3b 20 77 65 20 68 61 64 20 65 76 65 72 79 74 68 69 6e 67 20 62 65 66 6f 72 65 20 75 73 2c 20 77 65 20 68 61 64 20 6e 6f 74 68 69 6e 67 20 62 65 66 6f 72 65 20 75 73 3b 20 77 65 20 77 65 72 65 20 61 6c 6c 20 67 6f 69 6e 67 20 64 69 72 65 63 74 6c 79 20 74 6f 20 48 65 61 76 65 6e 2c 20 77 65 20 77 65 72 65 20 61 6c 6c 20 67 6f 69 6e 67 20 74 68 65 20 6f 74 68 65 72 20 77 61 79 2e 0a 0a'
        encrypted_by_ios_value = '<KEY>'
        encrypted_by_web_value = '<KEY>'
        self.perform_encryption_actions(encrypted_by_ios_value, encrypted_by_web_value, password, decrypted_value, False)

    def perform_encryption_actions(self, encrypted_by_ios_value, encrypted_by_web_value, password,
                                   expected_result, should_faild):

        cryptor = self.generate_cryptor(password)

        val = cryptor.encrypt(expected_result)

        res_web = cryptor.decrypt(encrypted_by_web_value)
        self.assertEquals(expected_result, res_web)

        if should_faild:
            with self.assertRaises(CryptorException):
                resIos = cryptor.decrypt(encrypted_by_ios_value)
        else:
            resIos = cryptor.decrypt(encrypted_by_ios_value)
            self.assertEquals(expected_result, resIos)

    def generate_cryptor(self, password):
{'fraction_non_alphanumeric': 0.07549618320610688, 'fraction_numerical': 0.11381679389312978, 'mean_word_length': 3.6789285714285715, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7570049', 'n_tokens_mistral': 6309, 'n_tokens_neox': 4880, 'n_words': 1151}