File size: 2,337 Bytes
065fee7 |
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 64 65 66 67 68 69 70 71 72 |
import sys
import pytest
import keyring.backends.Windows
from keyring.testing.backend import UNICODE_CHARS, BackendBasicTests
@pytest.mark.skipif(
not keyring.backends.Windows.WinVaultKeyring.viable, reason="Needs Windows"
)
class TestWinVaultKeyring(BackendBasicTests):
def tearDown(self):
# clean up any credentials created
for cred in self.credentials_created:
try:
self.keyring.delete_password(*cred)
except Exception as e:
print(e, file=sys.stderr)
def init_keyring(self):
return keyring.backends.Windows.WinVaultKeyring()
def set_utf8_password(self, service, username, password):
"""
Write a UTF-8 encoded password using win32ctypes primitives
"""
from ctypes import c_char, cast, create_string_buffer, sizeof
from win32ctypes.core import _authentication as auth
from win32ctypes.core.ctypes._common import LPBYTE
credential = dict(
Type=1,
TargetName=service,
UserName=username,
CredentialBlob=password,
Comment="Stored using python-keyring",
Persist=3,
)
c_cred = auth.CREDENTIAL.fromdict(credential, 0)
blob_data = create_string_buffer(password.encode("utf-8"))
c_cred.CredentialBlobSize = sizeof(blob_data) - sizeof(c_char)
c_cred.CredentialBlob = cast(blob_data, LPBYTE)
c_cred_pointer = auth.PCREDENTIAL(c_cred)
auth._CredWrite(c_cred_pointer, 0)
self.credentials_created.add((service, username))
def test_long_password_nice_error(self):
self.keyring.set_password('system', 'user', 'x' * 512 * 2)
def test_read_utf8_password(self):
"""
Write a UTF-8 encoded credential and make sure it can be read back correctly.
"""
service = "keyring-utf8-test"
username = "keyring"
password = "utf8-test" + UNICODE_CHARS
self.set_utf8_password(service, username, password)
assert self.keyring.get_password(service, username) == password
@pytest.mark.skipif('sys.platform != "win32"')
def test_winvault_always_viable():
"""
The WinVault backend should always be viable on Windows.
"""
assert keyring.backends.Windows.WinVaultKeyring.viable
|