ó
©9Nc           @   s7  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z
 d d l	 m Z e j e j Z e j e j Z d e f d „  ƒ  YZ e j d „  ƒ Z d „  Z d „  Z e d	 „ Z d
 „  Z d „  Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e f d „  ƒ  YZ! d e j f d „  ƒ  YZ" d e j f d „  ƒ  YZ# d e f d „  ƒ  YZ$ d e$ f d „  ƒ  YZ% d  e$ f d! „  ƒ  YZ& d" e$ f d# „  ƒ  YZ' d$ „  Z( e) d% k r3e j* d& d' ƒ n  d S((   sY   
test_backend.py

Test case for keyring basic function

created by Kang Zhang 2009-07-14
iÿÿÿÿN(   t   PasswordSetErrort   ImportKillerc           B   s>   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z d „  Z RS(   s@   Context manager to make an import of a given name or names fail.c         G   s   | |  _  d  S(   N(   t   names(   t   selfR   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   __init__   s    c         C   s   | |  j  k r |  Sd  S(   N(   R   (   R   t   fullnamet   path(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   find_module   s    c         C   s%   | |  j  k s t ‚ t | ƒ ‚ d  S(   N(   R   t   AssertionErrort   ImportError(   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   load_module    s    c         C   sM   i  |  _  x- |  j D]" } t j j | d  ƒ |  j  | <q Wt j j |  ƒ d  S(   N(   t   originalR   t   syst   modulest   popt   Nonet	   meta_patht   append(   R   t   name(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt	   __enter__#   s    	 c         G   sP   t  j j |  ƒ x9 |  j j ƒ  D]( \ } } | d  k	 r  | t  j | <q  q  Wd  S(   N(   R   R   t   removeR   t   itemsR   R   (   R   t   argst   keyt   value(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   __exit__(   s    N(	   t   __name__t
   __module__t   __doc__R   R   R   R
   R   R   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR      s   			c         k   sº   i  } x\ | j  ƒ  D]N \ } } |  j | ƒ | | <| d k rW | |  k ra |  | =qa q | |  | <q Wd VxI | j  ƒ  D]; \ } } | d k r¨ | |  k r² |  | =q² qw | |  | <qw Wd S(   s³   Helper context manager to make and unmake changes to a dict.
    
    A None is not a valid value for the destination, and so means that the
    associated name should be removed.N(   R   t   getR   (   t   destinationt   changesR   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   NoNoneDictMutator/   s    c          K   s   t  t j |   S(   s6   A context manager to temporarily change the os.environ(   R    t   ost   environ(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   EnvironF   s    c          O   s4   x! |  D] } t  j | ƒ | | <q Wt t j |  S(   sD   A context manager to temporarily make it possible to import a module(   t   typest
   ModuleTypeR    R   R   (   R   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   ImportBlesserK   s    c         C   s7   d } x* t  d |  ƒ D] } | t j | ƒ 7} q W| S(   s2   Generate a random string with length <i>k</i>
    t    i    (   t   ranget   randomt   choice(   t   kt   sourcet   resultt   i(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   random_stringR   s    c         C   s   t  j d |  ƒ d S(   s    Backup the file as file.bak
    s   mv %s{,.bak}N(   t   commandst	   getoutput(   t   file(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   backupZ   s    c         C   s   t  j d |  ƒ d S(   s#   Restore the file from file.bak
    s   mv %s{.bak,}N(   R0   R1   (   R2   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   restore_   s    t   BackendBasicTestCasec           B   sJ   e  Z d  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sI   Test for the keyring's basic funtions. password_set and password_get
    c         C   s   |  j  ƒ  |  _ d  S(   N(   t   init_keyringt   keyring(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   setUpj   s    c         C   s   |  j  } |  j ƒ  d k r d  S|  j | j | | ƒ d  ƒ | j | | | ƒ |  j | j | | ƒ | ƒ | j | | d ƒ |  j | j | | ƒ d ƒ d  S(   NiÿÿÿÿR'   (   R7   t	   supportedt   assertEqualt   get_passwordR   t   set_password(   R   t   servicet   usernamet   passwordR7   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   check_set_getm   s    	c         C   s;   t  d ƒ } t  d ƒ } t  d ƒ } |  j | | | ƒ d  S(   Ni   (   R/   R@   (   R   R?   R>   R=   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_password_set_get~   s    c         C   sD   t  d t ƒ } t  d t ƒ } t  d t ƒ } |  j | | | ƒ d  S(   Ni   (   R/   t   DIFFICULT_CHARSR@   (   R   R?   R>   R=   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_difficult_chars„   s    c         C   s   d S(   s0   Return the correct value for supported.
        iÿÿÿÿ(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR9   Š   s    c         C   s#   |  j  |  j j ƒ  |  j ƒ  ƒ d S(   s,   Test the keyring's supported value.
        N(   R:   R7   R9   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_supported   s    (   R   R   R   t   Falset   __test__R8   R@   RA   RC   R9   RD   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR5   d   s   					t   OSXKeychainTestCasec           B   s    e  Z e Z d  „  Z d „  Z RS(   c         C   s   t  j d IJt j j ƒ  S(   NsD   Testing OSXKeychain, following password prompts are for this keyring(   R   t   stderrR7   t   backendt   OSXKeychain(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR6   —   s    c         C   s   t  j d k r d Sd S(   Nt   mact   darwini   iÿÿÿÿ(   RK   s   darwin(   R   t   platform(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR9   ›   s    (   R   R   t   TrueRF   R6   R9   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRG   ”   s   	t   GnomeKeyringTestCasec           B   sM   e  Z e Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   c         C   s   t  d d d d d d ƒ S(   Nt   GNOME_KEYRING_CONTROLt   1t   DISPLAYt   DBUS_SESSION_BUS_ADDRESS(   t   dict(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR"   £   s    c         C   s   t  j d IJt j j ƒ  S(   NsE   Testing GnomeKeyring, following password prompts are for this keyring(   R   RH   R7   RI   t   GnomeKeyring(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR6   ¨   s    c         C   sI   t  d ƒ 7 t |  j ƒ     |  j d |  j j ƒ  ƒ Wd  QXWd  QXd  S(   Nt   gnomekeyringi   (   R&   R#   R"   R:   R7   R9   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRD   ¬   s    c         C   sI   t  d ƒ 7 t |  j ƒ     |  j d |  j j ƒ  ƒ Wd  QXWd  QXd  S(   NRV   iÿÿÿÿ(   R   R#   R"   R:   R7   R9   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_supported_no_module±   s    c         C   sY   t  d ƒ G |  j ƒ  } d  | d <t |    |  j d |  j j ƒ  ƒ Wd  QXWd  QXd  S(   NRV   RP   i    (   R&   R"   R   R#   R:   R7   R9   (   R   R"   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_supported_no_keyring¶   s
    
c         C   sY   t  d ƒ G |  j ƒ  } d  | d <t |    |  j d |  j j ƒ  ƒ Wd  QXWd  QXd  S(   NRV   RR   i    (   R&   R"   R   R#   R:   R7   R9   (   R   R"   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_supported_no_display½   s
    
c         C   sY   t  d ƒ G |  j ƒ  } d  | d <t |    |  j d |  j j ƒ  ƒ Wd  QXWd  QXd  S(   NRV   RS   i    (   R&   R"   R   R#   R:   R7   R9   (   R   R"   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_supported_no_sessionÄ   s
    
(   R   R   RN   RF   R"   R6   RD   RW   RX   RY   RZ   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRO       s   						t   KDEKWalletTestCasec           B   s    e  Z e Z d  „  Z d „  Z RS(   c         C   s   t  j d IJt j j ƒ  S(   NsC   Testing KDEKWallet, following password prompts are for this keyring(   R   RH   R7   RI   t
   KDEKWallet(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR6   Ï   s    c         C   s   |  j  j ƒ  S(   N(   R7   R9   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR9   Ó   s    (   R   R   RN   RF   R6   R9   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR[   Ì   s   	t   UnOpenableKWalletc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s7   A module-like object used to test KDE wallet fall-back.c         G   s   d  S(   N(   R   (   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt
   openWalletÜ   s    c         C   s   d  S(   N(   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   NetworkWalletß   s    N(   R   R   R   R   t   SynchronousR^   R_   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR]   ×   s   	t	   FauxQtGuic           B   sM   e  Z d  Z d d d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ RS(   sD   A fake module-like object used in testing the open_kwallet function.t   qAppc           B   s   e  Z e d  „  ƒ Z RS(   c           C   s   d  S(   N(    (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   instanceç   s    (   R   R   t   staticmethodRc   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRb   æ   s   t   QApplicationc           B   s   e  Z d  „  Z d „  Z RS(   c         G   s   d  S(   N(    (   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR   ì   s    c         C   s   d  S(   N(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   exitï   s    (   R   R   R   Rf   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRe   ë   s   	t   QWidgetc           B   s   e  Z d  „  Z d „  Z RS(   c         G   s   d  S(   N(    (   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR   ó   s    c         C   s   d  S(   N(    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   winIdö   s    (   R   R   R   Rh   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRg   ò   s   	(    (   R   R   R   Rb   t   objectRe   Rg   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRa   ã   s   t   KDEWalletCanceledTestCasec           B   s   e  Z d  „  Z RS(   c         C   s)   |  j  t j j t ƒ  t ƒ  ƒ d  ƒ d  S(   N(   R:   R7   RI   t   open_kwalletR]   Ra   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_user_canceledû   s    (   R   R   Rl   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRj   ù   s   t   KDEKWalletInQApplicationc           B   s   e  Z d  „  Z RS(   c         C   s   y$ d d l  m } d d l m } Wn d  SX| g  ƒ } t j j ƒ  } |  j t | | j	 ƒ d d t
 | ƒ ƒ| j ƒ  d  S(   Niÿÿÿÿ(   t   KWallet(   Re   t   msgs>   The object wallet should be type<KWallet.Wallet> but it is: %s(   t   PyKDE4.kdeuiRn   t   PyQt4.QtGuiRe   R7   RI   Rk   t
   assertTruet
   isinstancet   Wallett   reprRf   (   R   Rn   Re   t   appt   wallet(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_QApplication	  s    )(   R   R   Rx   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRm     s   t   FileKeyringTestCasec           B   s)   e  Z e Z d  „  Z d „  Z d „  Z RS(   c         C   sM   |  j  ƒ  |  _ t j j t j j d ƒ |  j j ƒ  ƒ |  _ t |  j ƒ d S(   s(   Backup the file before the test
        t   ~N(	   R6   R7   R!   R   t   joint
   expandusert   filenamet	   file_pathR3   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR8     s    c         C   s   t  |  j ƒ d S(   s"   Restore the keyring file.
        N(   R4   R~   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   tearDown"  s    c         C   sT   |  j  ƒ  d k r d  St d ƒ } |  j j | ƒ } |  j | |  j j | ƒ ƒ d  S(   Niÿÿÿÿi   (   R9   R/   R7   t   encryptR:   t   decrypt(   R   R?   t	   encyrpted(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   test_encrypt_decrypt'  s
    (   R   R   RE   RF   R8   R   Rƒ   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRy     s   			t   UncryptedFileKeyringTestCasec           B   s    e  Z e Z d  „  Z d „  Z RS(   c         C   s   t  j d IJt j j ƒ  S(   NsG   Testing UnecryptedFile, following password prompts are for this keyring(   R   RH   R7   RI   t   UncryptedFileKeyring(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR6   3  s    c         C   s   d S(   Ni    (    (   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR9   7  s    (   R   R   RN   RF   R6   R9   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR„   0  s   	t   CryptedFileKeyringTestCasec           B   s    e  Z e Z d  „  Z d „  Z RS(   c         C   s   t  j d IJt j j ƒ  S(   NsD   Testing CryptedFile, following password prompts are for this keyring(   R   RH   R7   RI   t   CryptedFileKeyring(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR6   =  s    c         C   s0   y d d l  m } d SWn t k
 r+ n Xd S(   Niÿÿÿÿ(   t   AESi    (   t   Crypto.CipherRˆ   R	   (   R   Rˆ   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR9   A  s    (   R   R   RN   RF   R6   R9   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR†   :  s   	t   Win32CryptoKeyringTestCasec           B   s    e  Z e Z d  „  Z d „  Z RS(   c         C   s   t  j d IJt j j ƒ  S(   Ns>   Testing Win32, following password prompts are for this keyring(   R   RH   R7   RI   t   Win32CryptoKeyring(   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR6   L  s    c         C   sQ   y9 d d  l  } t j d k r8 t j ƒ  d d k r8 d SWn t k
 rL n Xd S(   Niÿÿÿÿt   win32iþÿÿÿi   i   (   s   win32(   t   win32_cryptoR   RM   t   getwindowsversionR	   (   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyR9   P  s    %(   R   R   RN   RF   R6   R9   (    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyRŠ   I  s   	c          C   sÀ   t  j ƒ  }  |  j t  j t ƒ ƒ |  j t  j t ƒ ƒ |  j t  j t ƒ ƒ |  j t  j t ƒ ƒ |  j t  j t ƒ ƒ |  j t  j t	 ƒ ƒ |  j t  j t
 ƒ ƒ |  j t  j t ƒ ƒ |  S(   N(   t   unittestt	   TestSuitet   addTestt	   makeSuiteRG   RO   Rj   R[   Rm   R„   R†   RŠ   (   t   suite(    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt
   test_suiteY  s    t   __main__t   defaultTestR”   (+   R   R0   t
   contextlibR!   R)   t   stringR   R$   R   t   keyring.backendR7   R    t   ascii_letterst   digitst   ALPHABETt
   whitespacet   punctuationRB   Ri   R   t   contextmanagerR    R#   R&   R/   R3   R4   t   TestCaseR5   RG   RO   R[   R]   Ra   Rj   Rm   Ry   R„   R†   RŠ   R”   R   t   main(    (    (    s>   /usr/lib/python2.7/dist-packages/keyring/tests/test_backend.pyt   <module>   sD   				0,
	