a
    dbn:                     @   s  d dl Z d dlmZ zd dlZW n ey:   d dlZY n0 d dlmZ d dlm	Z
 d dlmZ d dlZddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZ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"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&G dd dejZ'G dd dejZ(e
j)dd dfd d!Z*ee* d"d# Z+dS )$    N)hexlify)b)given   )str_idx_as_int)NIST256pNIST224p)
remove_integerUnexpectedDERread_lengthencode_bitstringremove_bitstringremove_object
encode_oidremove_constructedremove_octet_stringremove_sequencec                   @   sd   e 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S )TestRemoveIntegerc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nz assertRaisesr
   r	   r   self r   _/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/ecdsa/test_der.pytest_non_minimal_encoding!   s    z+TestRemoveInteger.test_non_minimal_encodingc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nu   r   r   r   r   r   test_negative_with_high_bit_set%   s    z1TestRemoveInteger.test_negative_with_high_bit_setc                 C   s,   t td\}}| |d | |d d S Nu           r	   r   assertEqualr   valremr   r   r   test_minimal_with_high_bit_set)   s    z0TestRemoveInteger.test_minimal_with_high_bit_setc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nu     ÿr   r   r   r   r   %test_two_zero_bytes_with_high_bit_set/   s    z7TestRemoveInteger.test_two_zero_bytes_with_high_bit_setc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nz r   r   r   r   r   test_zero_length_integer3   s    z*TestRemoveInteger.test_zero_length_integerc                 C   s:   |  t ttd W d    n1 s,0    Y  d S N r   r   r   r   r   test_empty_string7   s    z#TestRemoveInteger.test_empty_stringc                 C   s,   t td\}}| |d | |d d S )Nz r   r   r   r!   r   r   r   test_encoding_of_zero;   s    z'TestRemoveInteger.test_encoding_of_zeroc                 C   s,   t td\}}| |d | |d d S )Nz   r   r   r!   r   r   r   test_encoding_of_127A   s    z&TestRemoveInteger.test_encoding_of_127c                 C   s,   t td\}}| |d | |d d S r   r   r!   r   r   r   test_encoding_of_128G   s    z&TestRemoveInteger.test_encoding_of_128c                 C   sH   |  t}td W d    n1 s(0    Y  | dt|j d S )Ns    zwanted type 'integer'r   r
   r	   assertInstr	exceptionr   er   r   r   test_wrong_tagM   s    &z TestRemoveInteger.test_wrong_tagc                 C   sH   |  t}td W d    n1 s(0    Y  | dt|j d S )Ns    Length longerr.   r2   r   r   r   test_wrong_lengthS   s    &z#TestRemoveInteger.test_wrong_lengthN)__name__
__module____qualname__r   r   r$   r%   r&   r)   r*   r,   r-   r4   r6   r   r   r   r   r      s   r   c                   @   sT   e 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 )TestReadLengthc                 C   s   |  dttd d S )N)r   r    r    r   r   r   r   r   r   test_zero_length^   s    zTestReadLength.test_zero_lengthc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nu    r   r
   r   r   r   r   r   r   test_two_byte_zero_lengtha   s    z(TestReadLength.test_two_byte_zero_lengthc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nu   r>   r   r   r   r   test_two_byte_small_lengthe   s    z)TestReadLength.test_two_byte_small_lengthc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )N   r>   r   r   r   r   test_long_form_with_zero_lengthi   s    z.TestReadLength.test_long_form_with_zero_lengthc                 C   s   |  dttd d S )N)r      u   r<   r   r   r   r   test_smallest_two_byte_lengthm   s    z,TestReadLength.test_smallest_two_byte_lengthc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nu    r>   r   r   r   r   test_zero_padded_lengthp   s    z&TestReadLength.test_zero_padded_lengthc                 C   s   |  dtd d S )N)      s    )r    r   r   r   r   r   test_two_three_byte_lengtht   s    z)TestReadLength.test_two_three_byte_lengthc                 C   s:   |  t ttd W d    n1 s,0    Y  d S r'   r>   r   r   r   r   r)   w   s    z TestReadLength.test_empty_stringc                 C   s:   |  t ttd W d    n1 s,0    Y  d S )Nu    r>   r   r   r   r   test_length_overflow{   s    z#TestReadLength.test_length_overflowN)r7   r8   r9   r=   r?   r@   rB   rD   rE   rH   r)   rI   r   r   r   r   r:   Z   s   r:   c                   @   sL   e 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S )TestEncodeBitstringc                 C   st   t d tt}td}W d   n1 s20    Y  | t|d | d|d j	j
d  | |d dS )z(This is the old way to use the function.always    Nr   zunused= needs to be specifiedr       )warningssimplefilterpytestwarnsDeprecationWarningr   r    lenr/   messageargsr   rQ   derr   r   r   test_old_call_convention   s    
&z,TestEncodeBitstring.test_old_call_conventionc                 C   s^   t d td}tdd}W d   n1 s40    Y  | t|d | |d dS )z$This is how it should be called now.rK   N   r   rM   rN   rO   rP   rQ   r   r    rS   rV   r   r   r   test_new_call_convention   s
    
(z,TestEncodeBitstring.test_new_call_conventionc                 C   s^   t d td}tdd}W d   n1 s40    Y  | t|d | |d dS )zU
        Writing bit string with already included the number of unused bits.
        rK   NrL   r   rM   rZ   rV   r   r   r   test_implicit_unused_bits   s
    
(z-TestEncodeBitstring.test_implicit_unused_bitsc                 C   s   t dd}| |d d S )Ns      s   )r   r    )r   rW   r   r   r   test_explicit_unused_bits   s    
z-TestEncodeBitstring.test_explicit_unused_bitsc                 C   s   |  tddd d S )Nr   r   s    )r    r   r   r   r   r   r)      s    z%TestEncodeBitstring.test_empty_stringc                 C   s8   |  t tdd W d    n1 s*0    Y  d S )Ns       r   
ValueErrorr   r   r   r   r   test_invalid_unused_count   s    z-TestEncodeBitstring.test_invalid_unused_countc                 C   s8   |  t tdd W d    n1 s*0    Y  d S )Nr   r   r`   r   r   r   r   %test_invalid_unused_with_empty_string   s    z9TestEncodeBitstring.test_invalid_unused_with_empty_stringc                 C   s8   |  t tdd W d    n1 s*0    Y  d S )NrY   rC   r`   r   r   r   r   test_non_zero_padding_bits   s    z.TestEncodeBitstring.test_non_zero_padding_bitsN)r7   r8   r9   rX   r[   r\   r^   r)   rb   rc   rd   r   r   r   r   rJ      s   rJ   c                   @   sl   e 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 )TestRemoveBitstringc                 C   s   t d tt}td\}}W d   n1 s60    Y  | t|d | d|d j	j
d  | |d | |d dS )	z)This is the old way to call the function.rK   rM   Nr   z$expect_unused= needs to be specifiedr   rL   r   )rN   rO   rP   rQ   rR   r   r    rS   r/   rT   rU   r   rQ   bitsrestr   r   r   rX      s    
*z,TestRemoveBitstring.test_old_call_conventionc                 C   sn   t d td }tdd\}}W d    n1 s80    Y  | t|d | |d | |d d S )NrK   rM   r   rY   r   rN   rO   rP   rQ   r   r    rS   rf   r   r   r   r[      s    
,z,TestRemoveBitstring.test_new_call_conventionc                 C   sn   t d td }tdd \}}W d    n1 s80    Y  | t|d | |d | |d d S )NrK   rM   r   )rY   r   r   ri   rf   r   r   r   test_implicit_unexpected_unused   s    
,z3TestRemoveBitstring.test_implicit_unexpected_unusedc                 C   s*   t dd \}}| |d | |d d S )Ns   )   r]   r   )r   r    )r   retrh   r   r   r   test_with_padding   s    z%TestRemoveBitstring.test_with_paddingc                 C   s8   |  t tdd  W d    n1 s*0    Y  d S )Ns    r   r
   r   r   r   r   r   test_not_a_bitstring   s    z(TestRemoveBitstring.test_not_a_bitstringc                 C   s8   |  t tdd  W d    n1 s*0    Y  d S )Ns    rn   r   r   r   r   test_empty_encoding   s    z'TestRemoveBitstring.test_empty_encodingc                 C   s8   |  t tdd  W d    n1 s*0    Y  d S Nr   rn   r   r   r   r   r)      s    z%TestRemoveBitstring.test_empty_stringc                 C   s8   |  t tdd  W d    n1 s*0    Y  d S )N   rn   r   r   r   r   test_no_length   s    z"TestRemoveBitstring.test_no_lengthc                 C   s8   |  t tdd W d    n1 s*0    Y  d S )NrM   r   rn   r   r   r   r   %test_unexpected_number_of_unused_bits   s    z9TestRemoveBitstring.test_unexpected_number_of_unused_bitsc                 C   s8   |  t tdd  W d    n1 s*0    Y  d S )Ns    rn   r   r   r   r   $test_invalid_encoding_of_unused_bits   s    z8TestRemoveBitstring.test_invalid_encoding_of_unused_bitsc                 C   s8   |  t tdd  W d    n1 s*0    Y  d S )Ns   rn   r   r   r   r   %test_invalid_encoding_of_empty_string   s    z9TestRemoveBitstring.test_invalid_encoding_of_empty_stringc                 C   s8   |  t tdd  W d    n1 s*0    Y  d S )Ns   rn   r   r   r   r   test_invalid_padding_bits  s    z-TestRemoveBitstring.test_invalid_padding_bitsN)r7   r8   r9   rX   r[   rj   rm   ro   rp   r)   rs   rt   ru   rv   rw   r   r   r   r   re      s   

re   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestStrIdxAsIntc                 C   s   |  dtdd d S )Ns   r0   r   r    r   r   r   r   r   test_str	  s    zTestStrIdxAsInt.test_strc                 C   s   |  dtdd d S Nry   s   strr   rz   r   r   r   r   
test_bytes  s    zTestStrIdxAsInt.test_bytesc                 C   s   |  dttdd d S r|   )r    r   	bytearrayr   r   r   r   test_bytearray  s    zTestStrIdxAsInt.test_bytearrayN)r7   r8   r9   r{   r}   r   r   r   r   r   rx     s   rx   c                   @   s\   e 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 )TestEncodeOidc                 C   s*   t dddddd}| t|td d S )Nr   rC   H  ='  Z06072a8648ce3d0201)r   r    r   r   )r   oid_ecPublicKeyr   r   r   test_pub_key_oid  s    zTestEncodeOid.test_pub_key_oidc                 C   s   |  ttjtd d S )NZ06052b81040021)r    r   r   encoded_oidr   r   r   r   r   test_nist224p_oid  s    zTestEncodeOid.test_nist224p_oidc                 C   s   |  ttjd d S )Ns   06082a8648ce3d030107)r    r   r   r   r   r   r   r   test_nist256p_oid  s    
zTestEncodeOid.test_nist256p_oidc                 C   s   t ddd}| |d d S )NrC     rG      7r   r    r   oidr   r   r   test_large_second_subid   s    z%TestEncodeOid.test_large_second_subidc                 C   s   t dd}| |d d S )NrC   r      7r   r   r   r   r   test_with_two_subids%  s    
z"TestEncodeOid.test_with_two_subidsc                 C   s   t dd}| |d d S )Nr       r   r   r   r   r   test_zero_zero)  s    
zTestEncodeOid.test_zero_zeroc                 C   s<   |  ttf tdd  W d    n1 s.0    Y  d S )Nr   )r   	TypeErrorAssertionErrorr   r   r   r   r   test_with_wrong_types-  s    z#TestEncodeOid.test_with_wrong_typesc                 C   s8   |  t tdd W d    n1 s*0    Y  d S )Nr   (   r   r   r   r   r   r   r   "test_with_small_first_large_second1  s    z0TestEncodeOid.test_with_small_first_large_secondc                 C   s   t dd}| |d d S )Nr   '   s   Or   r   r   r   r   test_small_first_max_second5  s    
z)TestEncodeOid.test_small_first_max_secondc                 C   s8   |  t tdd W d    n1 s*0    Y  d S )NrG   r   r   r   r   r   r   test_with_invalid_first9  s    z%TestEncodeOid.test_with_invalid_firstN)r7   r8   r9   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd Ze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d Zdd Zdd ZdS ) TestRemoveObjectc                 C   s   t dddddd| _d S )Nr   rC   r   r   )r   r   )clsr   r   r   
setUpClass?  s    zTestRemoveObject.setUpClassc                 C   s*   t | j\}}| |d | |d d S )Nr   r   rC   r   r   rC   r   r   r   r    r   r   rh   r   r   r   r   C  s    z!TestRemoveObject.test_pub_key_oidc                 C   s.   t | jd \}}| |d | |d d S )Ns   morer   r   r   r   r   r   test_with_extra_bytesH  s    z&TestRemoveObject.test_with_extra_bytesc                 C   s(   t d\}}| |d | |d d S )Nr   r   )rC   r   rG   r   r    r   r   r   r   test_with_large_second_subidM  s    z-TestRemoveObject.test_with_large_second_subidc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns    r   r
   r   r   r   r   r   test_with_padded_first_subidS  s    z-TestRemoveObject.test_with_padded_first_subidc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns   7r   r   r   r   r   test_with_padded_second_subidW  s    z.TestRemoveObject.test_with_padded_second_subidc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns   7r   r   r   r   r   )test_with_missing_last_byte_of_multi_byte[  s    z:TestRemoveObject.test_with_missing_last_byte_of_multi_bytec                 C   s(   t d\}}| |d | |d d S )Nr   r   )rC   r   r   r   r   r   r   r   _  s    z%TestRemoveObject.test_with_two_subidsc                 C   s(   t d\}}| |d | |d d S )Nr   r   )r   r   r   r   r   r   r   r   d  s    zTestRemoveObject.test_zero_zeroc                 C   s6   |  t td W d    n1 s(0    Y  d S rq   r   r   r   r   r   r)   i  s    z"TestRemoveObject.test_empty_stringc                 C   s6   |  t td W d    n1 s(0    Y  d S )N   r   r   r   r   r   test_missing_lengthm  s    z$TestRemoveObject.test_missing_lengthc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns    r   r   r   r   r   test_empty_oidq  s    zTestRemoveObject.test_empty_oidc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns   r   r   r   r   r   test_empty_oid_overflowu  s    z(TestRemoveObject.test_empty_oid_overflowc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns   7r   r   r   r   r   test_with_wrong_typey  s    z%TestRemoveObject.test_with_wrong_typec                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns   7r   r   r   r   r   test_with_too_long_length}  s    z*TestRemoveObject.test_with_too_long_lengthN)r7   r8   r9   classmethodr   r   r   r   r   r   r   r   r   r)   r   r   r   r   r   r   r   r   r   r   >  s    
r   c                   @   s   e Zd Zdd Zdd ZdS )TestRemoveConstructedc                 C   s:   d}t |\}}}| |d | |d | |d d S )Ns   r      r   )r   r    )r   datatagbodyrh   r   r   r   test_simple  s
    z!TestRemoveConstructed.test_simplec                 C   sL   d}|  t}t| W d    n1 s,0    Y  | dt|j d S )Ns   zconstructed tag)r   r
   r   r/   r0   r1   r   r   r3   r   r   r   test_with_malformed_tag  s    &z-TestRemoveConstructed.test_with_malformed_tagNr7   r8   r9   r   r   r   r   r   r   r     s   	r   c                   @   s   e Zd Zdd Zdd ZdS )TestRemoveOctetStringc                 C   s,   d}t |\}}| |d | |d d S )Ns   s   r   )r   r    r   r   r   rh   r   r   r   r     s    z!TestRemoveOctetString.test_simplec                 C   sL   d}|  t}t| W d    n1 s,0    Y  | dt|j d S )Ns   Zoctetstring)r   r
   r   r/   r0   r1   r   r   r   r   r     s    &z-TestRemoveOctetString.test_with_malformed_tagNr   r   r   r   r   r     s   r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestRemoveSequencec                 C   s,   d}t |\}}| |d | |d d S )Ns   0r   r   )r   r    r   r   r   r   r     s    zTestRemoveSequence.test_simplec                 C   sH   |  t}td W d    n1 s(0    Y  | dt|j d S )Nr   zEmpty stringr   r
   r   r/   r0   r1   r2   r   r   r   test_with_empty_string  s    &z)TestRemoveSequence.test_with_empty_stringc                 C   sL   d}|  t}t| W d    n1 s,0    Y  | dt|j d S )Ns    zwanted type 'sequence'r   r   r   r   r   test_with_wrong_tag  s    &z&TestRemoveSequence.test_with_wrong_tagc                 C   sL   d}|  t}t| W d    n1 s,0    Y  | dt|j d S )Ns   0r5   r   r   r   r   r   test_with_wrong_length  s    &z)TestRemoveSequence.test_with_wrong_lengthN)r7   r8   r9   r   r   r   r   r   r   r   r   r     s   r   rC   i   2   c                 C   sl   | t jddd}|dk r.| t jddd}n| t jd|d}| t jt jd|d|d}||ft| S )z
    Hypothesis strategy that returns valid OBJECT IDENTIFIERs as tuples

    :param max_value: maximum value of any single sub-identifier
    :param max_size: maximum length of the generated OID
    r   rC   )Z	min_value	max_valuer   )max_size)stZintegersliststuple)Zdrawr   r   firstsecondrh   r   r   r   st_oid  s    r   c                 C   s0   t |  }t|\}}|dks J || ks,J d S rq   )r   r   )idsr   Zdecoded_oidrh   r   r   r   	test_oids  s    r   ),rN   binasciir   Z	unittest2ZunittestImportErrorsixr   Zhypothesis.strategies
strategiesr   Z
hypothesisr   rP   Z_compatr   Zcurvesr   r   rW   r	   r
   r   r   r   r   r   r   r   r   ZTestCaser   r:   rJ   re   rx   r   r   r   r   r   Z	compositer   r   r   r   r   r   <module>   s4   0<&>J+D