コード例 #1
0
    def test_load_generate(self, genkey_mock, open_mock):
        open_mock.side_effect = IOError()
        kf = KeyFile('asdf.txt')
        kf._KeyFile__load_key()

        open_mock.assert_called_once_with('asdf.txt', 'rb')
        genkey_mock.assert_called_once_with()
コード例 #2
0
 def test_get_provider_aes(self):
     kf = KeyFile('asdf.txt')
     kf._KeyFile__key = b'x' * 32
     provider, method = kf._get_provider('aes')
     assert isinstance(provider, AesProvider)
     assert provider._AesProvider__key == b'x' * 32
     assert method == 'aes'
コード例 #3
0
 def test_get_provider_xor(self):
     kf = KeyFile('asdf.txt')
     kf._KeyFile__key = b'x' * 32
     provider, method = kf._get_provider('xor')
     assert isinstance(provider, XorProvider)
     assert provider._XorProvider__key == b'x' * 32
     assert method == 'xor'
コード例 #4
0
    def test_load_key_exists(self, genkey_mock, validate_mock, open_mock):
        kf = KeyFile('asdf.txt')
        kf._KeyFile__load_key()

        assert kf._KeyFile__key == b'x' * 32
        open_mock.assert_called_once_with('asdf.txt', 'rb')
        validate_mock.assert_called_once_with()
        genkey_mock.assert_not_called()
コード例 #5
0
    def test_generate_key(self, urandom_mock, open_mock):
        urandom_mock.return_value = b'x' * 32
        kf = KeyFile('asdf.txt')
        kf.generate_key()

        open_mock.assert_called_once_with('asdf.txt', 'wb')
        handle = open_mock()
        handle.write.assert_called_once_with(b'x' * 32)
        urandom_mock.assert_called_once_with(32)
コード例 #6
0
    def test_decrypt(self):
        provider = StubProvider()
        kf = KeyFile('asdf.txt')
        kf._KeyFile__key = b'x' * 32
        kf._get_provider = MagicMock(return_value=provider)

        text = kf.decrypt(SecureValue('test', b'hello'))
        provider.decrypt.assert_called_once_with(b'hello')
        kf._get_provider.assert_called_once_with('test')
        assert text == b'plaintext'
コード例 #7
0
    def test_encrypt(self):
        provider = StubProvider()
        kf = KeyFile('asdf.txt')
        kf._KeyFile__key = b'x' * 32
        kf._get_provider = MagicMock(return_value=provider)

        secret = kf.encrypt('hello', 'test')
        provider.encrypt.assert_called_once_with(b'hello')
        kf._get_provider.assert_called_once_with('test')
        assert secret == SecureValue('test', b'ciphertext')
コード例 #8
0
    def test_with(self, load_key):
        kf = KeyFile('asdf.txt')
        with kf as kf2:
            kf._KeyFile__key = 'asdf'
            assert kf is kf2
            assert kf._KeyFile__refcount == 1
            load_key.assert_called_once_with()

            with kf as kf3:
                assert kf._KeyFile__refcount == 2
                assert kf3 is kf
                assert kf._KeyFile__key == 'asdf'

            assert kf._KeyFile__refcount == 1
            assert kf._KeyFile__key == 'asdf'

        assert kf._KeyFile__refcount == 0
        assert kf._KeyFile__key is None
コード例 #9
0
 def test_validate_short(self):
     kf = KeyFile('asdf')
     kf._KeyFile__key = b'x' * 31
     with pytest.raises(EncryptionError):
         kf._validate_key()
コード例 #10
0
 def test_validate_none(self):
     kf = KeyFile('asdf')
     with pytest.raises(EncryptionError):
         kf._validate_key()
コード例 #11
0
 def test_get_provider_xor(self):
     kf = KeyFile('asdf.txt')
     kf._KeyFile__key = b'x' * 32
     p = kf._get_provider('xor')
     assert isinstance(p, XorProvider)
     assert p._XorProvider__key == b'x' * 32
コード例 #12
0
 def test_encrypt_nokey(self):
     kf = KeyFile('asdf.txt')
     with pytest.raises(TypeError):
         kf.encrypt(b'hello')
コード例 #13
0
 def test_get_provider_no_key(self):
     kf = KeyFile('asdf.txt')
     with pytest.raises(TypeError):
         kf._get_provider('xor')
コード例 #14
0
 def test_validate_success(self):
     kf = KeyFile('asdf')
     kf._KeyFile__key = b'x' * 32
     kf._validate_key()
コード例 #15
0
 def test_decrypt_nokey(self):
     kf = KeyFile('asdf.txt')
     with pytest.raises(TypeError):
         kf.decrypt(b'asdf')
コード例 #16
0
 def test_get_provider_aes(self):
     kf = KeyFile('asdf.txt')
     kf._KeyFile__key = b'x' * 32
     p = kf._get_provider('aes')
     assert isinstance(p, AesProvider)
     assert p._AesProvider__key == b'x' * 32
コード例 #17
0
 def test_encrypt_best_xor(self):
     kf = KeyFile('asdf.txt')
     kf._KeyFile__key = b'x' * 32
     assert isinstance(kf._get_provider(method='best'), XorProvider)
コード例 #18
0
 def test_get_provider_unknown(self):
     kf = KeyFile('asdf.txt')
     kf._KeyFile__key = b'x' * 32
     with pytest.raises(TypeError):
         kf._get_provider('balh')
コード例 #19
0
 def test_encrypt_best_aes(self):
     kf = KeyFile('asdf.txt')
     kf._KeyFile__key = b'x' * 32
     provider, method = kf._get_provider(method='best')
     assert isinstance(provider, AesProvider)
     assert method == 'aes'