~ubuntu-branches/ubuntu/precise/python-keyring/precise-updates

« back to all changes in this revision

Viewing changes to keyring/tests/test_backend.py

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-11-19 12:50:49 UTC
  • mfrom: (8.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121119125049-mryxwft3qi7fz3pg
Tags: 0.9.2-0ubuntu0.12.04.2
* SECURITY UPDATE: CryptedFileKeyring format is insecure (LP: #1004845)
  - Rebuild python-keyring 0.9.2 from Ubuntu 12.10 as a security update
    for Ubuntu 12.04.
  - debian/patches/crypto_compat.patch: include PBKDF2() directly to be
    compatible with the older version of python-crypto in Ubuntu 12.04.
  - CVE-2012-4571
* SECURITY UPDATE: insecure default file permissions (LP: #1031465)
  - debian/patches/file_permissions.patch: set appropriate permissions on
    database directory.
  - CVE number pending
* debian/patches/fix_migration.patch: fix migration code so old
  databases get upgraded when a key is read. (LP: #1042754)
* debian/patches/fix_unlock.patch: fix unlocking an existing keyring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
import sys
14
14
import tempfile
15
15
import types
 
16
import getpass
16
17
 
17
18
try:
18
19
    # Python < 2.7 annd Python >= 3.0 < 3.1
21
22
    import unittest
22
23
 
23
24
import keyring.backend
24
 
from keyring.backend import PasswordSetError
25
25
 
26
26
ALPHABET = string.ascii_letters + string.digits
27
27
DIFFICULT_CHARS = string.whitespace + string.punctuation
94
94
 
95
95
def is_win32_crypto_supported():
96
96
    try:
97
 
        from keyring.backends import win32_crypto
98
 
        if sys.platform in ['win32'] and sys.getwindowsversion()[-2] == 2:
99
 
            return True
 
97
        __import__('keyring.backends.win32_crypto')
100
98
    except ImportError:
101
 
        pass
102
 
    return False
 
99
        return False
 
100
    return sys.platform in ['win32'] and sys.getwindowsversion()[-2] == 2
103
101
 
104
102
def is_osx_keychain_supported():
105
103
    return sys.platform in ('mac','darwin')
112
110
 
113
111
def is_crypto_supported():
114
112
    try:
115
 
        from Crypto.Cipher import AES
116
 
        import crypt
 
113
        __import__('Crypto.Cipher.AES')
 
114
        __import__('Crypto.Protocol.KDF')
 
115
        __import__('Crypto.Random')
117
116
    except ImportError:
118
117
        return False
119
118
    return True
126
125
 
127
126
def is_qt4_supported():
128
127
    try:
129
 
        from PyQt4.QtGui import QApplication
 
128
        __import__('PyQt4.QtGui')
130
129
    except ImportError:
131
130
        return False
132
131
    return True
133
132
 
134
133
def is_winvault_supported():
135
134
    try:
136
 
        from keyring.backend import WinVaultKeyring
137
 
        if sys.platform in ['win32'] and sys.getwindowsversion().major >= 6:
138
 
            return True
 
135
        __import__('win32cred')
 
136
        has_pywin32 = True
139
137
    except ImportError:
140
 
        pass
141
 
    return False
 
138
        has_pywin32 = False
 
139
    return (
 
140
        sys.platform in ['win32'] and sys.getwindowsversion().major >= 6
 
141
        and has_pywin32
 
142
    )
142
143
 
 
144
def is_dbus_supported():
 
145
    try:
 
146
        __import__('dbus')
 
147
    except ImportError:
 
148
        return False
 
149
    return True
143
150
 
144
151
class BackendBasicTests(object):
145
152
    """Test for the keyring's basic funtions. password_set and password_get
336
343
    def setUp(self):
337
344
        super(FileKeyringTests, self).setUp()
338
345
        self.keyring = self.init_keyring()
339
 
        self.keyring.file_path = self.tmp_keyring_file = tempfile.mktemp()
 
346
        self.keyring.file_path = self.tmp_keyring_file = os.path.join(
 
347
            tempfile.mkdtemp(), "test_pass.cfg")
340
348
 
341
349
    def tearDown(self):
342
350
        try:
356
364
 
357
365
class UncryptedFileKeyringTestCase(FileKeyringTests, unittest.TestCase):
358
366
 
359
 
 
360
367
    def init_keyring(self):
361
368
        return keyring.backend.UncryptedFileKeyring()
362
369
 
367
374
 
368
375
    def setUp(self):
369
376
        super(self.__class__, self).setUp()
370
 
        self.keyring._getpass = lambda *args, **kwargs: "abcdef"
 
377
        # patch the getpass module to bypass user input
 
378
        self.getpass_orig = getpass.getpass
 
379
        getpass.getpass = lambda *args, **kwargs: "abcdef"
 
380
 
 
381
    def tearDown(self):
 
382
        getpass.getpass = self.getpass_orig
 
383
        del self.getpass_orig
371
384
 
372
385
    def init_keyring(self):
373
386
        return keyring.backend.CryptedFileKeyring()
395
408
    def init_keyring(self):
396
409
        return keyring.backend.WinVaultKeyring()
397
410
 
 
411
@unittest.skipUnless(is_dbus_supported(),
 
412
    "DBus needed for SecretServiceKeyring")
 
413
class SecretServiceKeyringTestCase(BackendBasicTests, unittest.TestCase):
 
414
    __test__ = True
 
415
 
 
416
    def environ(self):
 
417
        return dict(DISPLAY='1',
 
418
                    DBUS_SESSION_BUS_ADDRESS='1')
 
419
 
 
420
    def init_keyring(self):
 
421
        print >> sys.stderr, "Testing SecretServiceKeyring, following password prompts are for this keyring"
 
422
        return keyring.backend.SecretServiceKeyring()
 
423
 
 
424
    def test_supported_no_module(self):
 
425
        with ImportKiller('dbus'):
 
426
            with Environ(**self.environ()):
 
427
                self.assertEqual(-1, self.keyring.supported())
 
428
 
 
429
 
398
430
def test_suite():
399
431
    suite = unittest.TestSuite()
400
432
    suite.addTest(unittest.makeSuite(OSXKeychainTestCase))
401
433
    suite.addTest(unittest.makeSuite(GnomeKeyringTestCase))
 
434
    suite.addTest(unittest.makeSuite(SecretServiceKeyringTestCase))
402
435
    suite.addTest(unittest.makeSuite(KDEWalletCanceledTestCase))
403
436
    suite.addTest(unittest.makeSuite(KDEKWalletTestCase))
404
437
    suite.addTest(unittest.makeSuite(KDEKWalletInQApplication))