U
    •¢]Ë  ã                   @   s„   d Z ddlZddlZddlZddlmZ G dd„ dejƒZG dd„ dejƒZG dd	„ d	ejƒZ	G d
d„ dejƒZ
edkr€e ¡  dS )zTests for josepy.util.é    N)Ú	test_utilc                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚComparableX509Testz%Tests for josepy.util.ComparableX509.c                 C   sL   t  d¡| _t  d¡| _t  d¡| _t  d¡| _t  d¡| _t  d¡| _d S )Nzcsr.pemzcsr-san.pemzcert.pemzcert-san.pem)	r   Zload_comparable_csrÚreq1Úreq2Ú	req_otherZload_comparable_certÚcert1Úcert2Ú
cert_other©Úself© r   ú2/usr/lib/python3/dist-packages/josepy/util_test.pyÚsetUp   s    zComparableX509Test.setUpc                 C   s   |   | j ¡ ¡ d S ©N)Ú
assertTruer   Zhas_expiredr
   r   r   r   Útest_getattr_proxy   s    z%ComparableX509Test.test_getattr_proxyc                 C   s$   |   | j| j¡ |   | j| j¡ d S r   )ÚassertEqualr   r   r   r   r
   r   r   r   Útest_eq   s    zComparableX509Test.test_eqc                 C   s$   |   | j| j¡ |   | j| j¡ d S r   )ÚassertNotEqualr   r   r   r	   r
   r   r   r   Útest_ne   s    zComparableX509Test.test_nec                 C   s    |   | jd¡ |   | jd¡ d S ©Né   )r   r   r   r
   r   r   r   Útest_ne_wrong_types"   s    z&ComparableX509Test.test_ne_wrong_typesc                 C   sd   |   t| jƒt| jƒ¡ |  t| jƒt| jƒ¡ |   t| jƒt| jƒ¡ |  t| jƒt| jƒ¡ d S r   )	r   Úhashr   r   r   r   r   r   r	   r
   r   r   r   Ú	test_hash&   s    zComparableX509Test.test_hashc                 C   s.   | j | jfD ]}|  t|ƒd |j¡¡ qd S )Nz<ComparableX509({0!r})>)r   r   r   ÚreprÚformatÚwrapped)r   Zx509r   r   r   Ú	test_repr-   s    

ÿzComparableX509Test.test_reprN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r   r   
   s   
r   c                   @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚComparableRSAKeyTestz'Tests for josepy.util.ComparableRSAKey.c                 C   s(   t  d¡| _t  d¡| _t  d¡| _d S )Nzrsa256_key.pemzrsa512_key.pem)r   Zload_rsa_private_keyÚkeyÚkey_sameÚkey2r
   r   r   r   r   6   s    zComparableRSAKeyTest.setUpc                 C   s   |   d| jj¡ d S )Né   )r   r$   Zkey_sizer
   r   r   r   r   <   s    z'ComparableRSAKeyTest.test_getattr_proxyc                 C   s   |   | j| j¡ d S r   )r   r$   r%   r
   r   r   r   r   ?   s    zComparableRSAKeyTest.test_eqc                 C   s   |   | j| j¡ d S r   )r   r$   r&   r
   r   r   r   r   B   s    zComparableRSAKeyTest.test_nec                 C   s   |   | jd¡ d S r   )r   r$   r
   r   r   r   Útest_ne_different_typesE   s    z,ComparableRSAKeyTest.test_ne_different_typesc                 C   s   |   | j| jj¡ d S r   )r   r$   r%   Z_wrappedr
   r   r   r   Útest_ne_not_wrappedH   s    z(ComparableRSAKeyTest.test_ne_not_wrappedc                 C   s$   ddl m} |  |dƒ|dƒ¡ d S )Nr   ©ÚComparableRSAKeyr   )Újosepy.utilr+   r   ©r   r+   r   r   r   Útest_ne_no_serializationL   s    z-ComparableRSAKeyTest.test_ne_no_serializationc                 C   sJ   |   tt| jƒtƒ¡ |  t| jƒt| jƒ¡ |  t| jƒt| jƒ¡ d S r   )	r   Ú
isinstancer   r$   Úintr   r%   r   r&   r
   r   r   r   r   P   s    zComparableRSAKeyTest.test_hashc                 C   s   |   t| jƒ d¡¡ d S )Nz'<ComparableRSAKey(<cryptography.hazmat.)r   r   r$   Ú
startswithr
   r   r   r   r   U   s    ÿzComparableRSAKeyTest.test_reprc                 C   s&   ddl m} |  t| j ¡ |ƒ¡ d S )Nr   r*   )r,   r+   r   r/   r$   Z
public_keyr-   r   r   r   Útest_public_keyY   s    z$ComparableRSAKeyTest.test_public_keyN)r   r    r!   r"   r   r   r   r   r(   r)   r.   r   r   r2   r   r   r   r   r#   3   s   r#   c                   @   sp   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚImmutableMapTestz#Tests for josepy.util.ImmutableMap.c                 C   s|   ddl m} G dd„ d|ƒ}G dd„ d|ƒ}|| _|| _| jddd	| _| jddd
| _| jddd	| _| jddd	| _d S )Nr   )ÚImmutableMapc                   @   s   e Zd ZdZdS )z!ImmutableMapTest.setUp.<locals>.A©ÚxÚyN©r   r    r!   Ú	__slots__r   r   r   r   ÚAf   s   r:   c                   @   s   e Zd ZdZdS )z!ImmutableMapTest.setUp.<locals>.Br5   Nr8   r   r   r   r   ÚBi   s   r;   é   é   r5   )r7   r6   é   é   )r,   r4   r:   r;   Úa1Úa1_swapÚa2Úb)r   r4   r:   r;   r   r   r   r   a   s    zImmutableMapTest.setUpc                 C   s>   |   | jddd| jjdd¡ |   | j| jjddd¡ d S )Nr=   r5   ©r6   r>   r?   )r   r:   r@   ÚupdaterB   r
   r   r   r   Útest_updatet   s     zImmutableMapTest.test_updatec                 C   s   |   t| jjd¡ d S ©NÚz)ÚassertRaisesÚKeyErrorr@   Ú__getitem__r
   r   r   r   Ú&test_get_missing_item_raises_key_errorx   s    z7ImmutableMapTest.test_get_missing_item_raises_key_errorc                 C   s   |   | j| j¡ d S r   )r   r@   rA   r
   r   r   r   Ú"test_order_of_args_does_not_matter{   s    z3ImmutableMapTest.test_order_of_args_does_not_matterc                 C   s(   | j t| jdd | j t| jdd d S )Nr<   rD   r=   )r7   ©rI   Ú	TypeErrorr:   r
   r   r   r   Útest_type_error_on_missing~   s    z+ImmutableMapTest.test_type_error_on_missingc                 C   s.   | j t| jddd | j t| jdddd d S )Nr<   r=   )r6   rH   r>   )r6   r7   rH   rN   r
   r   r   r   Útest_type_error_on_unrecognized‚   s    z0ImmutableMapTest.test_type_error_on_unrecognizedc                 C   sD   |   d| jj¡ |   d| jj¡ |   d| jj¡ |   d| jj¡ d S )Nr<   r=   )r   r@   r6   r7   rA   r
   r   r   r   Útest_get_attr†   s    zImmutableMapTest.test_get_attrc                 C   s   |   tt | jjd¡d¡ d S )Nr6   é
   )rI   ÚAttributeErrorÚ	functoolsÚpartialr@   Ú__setattr__r
   r   r   r   Ú$test_set_attr_raises_attribute_errorŒ   s
      ÿz5ImmutableMapTest.test_set_attr_raises_attribute_errorc                 C   s4   |   | j| j¡ |   | j| j¡ |  | j| j¡ d S r   )r   r@   rB   r   r
   r   r   r   Ú
test_equal   s    zImmutableMapTest.test_equalc                 C   s   |   tdƒt| jƒ¡ d S )N)r<   r=   )r   r   r@   r
   r   r   r   r   •   s    zImmutableMapTest.test_hashc                 C   s   |   t| jdi dj¡ d S )Nr<   r5   )rI   rO   r:   Ú__hash__r
   r   r   r   Útest_unhashable˜   s    z ImmutableMapTest.test_unhashablec                 C   sT   |   dt| jƒ¡ |   dt| jƒ¡ |   dt| jƒ¡ |   dt| jdddƒ¡ d S )NzA(x=1, y=2)zB(x=1, y=2)zB(x='foo', y='bar')ZfooZbarr5   )r   r   r@   rA   rC   r;   r
   r   r   r   r   ›   s    zImmutableMapTest.test_reprN)r   r    r!   r"   r   rF   rL   rM   rP   rQ   rR   rX   rY   r   r[   r   r   r   r   r   r3   ^   s   r3   c                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚfrozendictTestz!Tests for josepy.util.frozendict.c                 C   s   ddl m} |ddd| _d S ©Nr   ©Ú
frozendictr<   Ú2r5   )r,   r_   Úfdict©r   r_   r   r   r   r   ¥   s    zfrozendictTest.setUpc                 C   s(   ddl m} |  | j|dddœƒ¡ d S r]   )r,   r_   r   ra   rb   r   r   r   Útest_init_dict©   s    zfrozendictTest.test_init_dictc                 C   s(   ddl m} |  t|t ddi¡¡ d S )Nr   r^   ÚarC   )r,   r_   rI   rO   ÚsixZ	iteritemsrb   r   r   r   Ú!test_init_other_raises_type_error­   s    z0frozendictTest.test_init_other_raises_type_errorc                 C   s   |   dt| jƒ¡ d S )Nr=   )r   Úlenra   r
   r   r   r   Útest_len²   s    zfrozendictTest.test_lenc                 C   s   |   tt| jƒtƒ¡ d S r   )r   r/   r   ra   r0   r
   r   r   r   r   µ   s    zfrozendictTest.test_hashc                 C   s$   |   d| jj¡ |   d| jj¡ d S )Nr<   r`   )r   ra   r6   r7   r
   r   r   r   r   ¸   s    z!frozendictTest.test_getattr_proxyc                 C   s   |   t| jjd¡ d S rG   )rI   rT   ra   Ú__getattr__r
   r   r   r   Ú#test_getattr_raises_attribute_error¼   s    z2frozendictTest.test_getattr_raises_attribute_errorc                 C   s   |   t| jjdd¡ d S )NrH   r>   )rI   rT   ra   rW   r
   r   r   r   Útest_setattr_immutable¿   s    z%frozendictTest.test_setattr_immutablec                 C   s   |   dt| jƒ¡ d S )Nzfrozendict(x=1, y='2'))r   r   ra   r
   r   r   r   r   Â   s    zfrozendictTest.test_reprN)r   r    r!   r"   r   rc   rf   rh   r   r   rj   rk   r   r   r   r   r   r\   ¢   s   r\   Ú__main__)r"   rU   Zunittestre   Zjosepyr   ZTestCaser   r#   r3   r\   r   Úmainr   r   r   r   Ú<module>   s   )+D$