a
    _mgU:                     @   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Zd dlmZ	 d dl
mZ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*i Z+d!ej,v rde+d"< ef i e+ee* d#d$ Z-dS )%    N)hexlify)givensettings   )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   s6   |  t td W d    n1 s(0    Y  d S )Ns    assertRaisesr
   r	   self r   E/var/www/html/idle/venv/lib/python3.9/site-packages/ecdsa/test_der.pytest_non_minimal_encoding!   s    z+TestRemoveInteger.test_non_minimal_encodingc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns   r   r   r   r   r   test_negative_with_high_bit_set%   s    z1TestRemoveInteger.test_negative_with_high_bit_setc                 C   s(   t d\}}| |d | |d d S Ns           r	   assertEqualr   valremr   r   r   test_minimal_with_high_bit_set)   s    z0TestRemoveInteger.test_minimal_with_high_bit_setc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns     r   r   r   r   r   %test_two_zero_bytes_with_high_bit_set/   s    z7TestRemoveInteger.test_two_zero_bytes_with_high_bit_setc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns    r   r   r   r   r   test_zero_length_integer3   s    z*TestRemoveInteger.test_zero_length_integerc                 C   s6   |  t td W d    n1 s(0    Y  d S Nr   r   r   r   r   r   test_empty_string7   s    z#TestRemoveInteger.test_empty_stringc                 C   s(   t d\}}| |d | |d d S )Ns    r   r   r   r!   r   r   r   test_encoding_of_zero;   s    z'TestRemoveInteger.test_encoding_of_zeroc                 C   s(   t d\}}| |d | |d d S )Ns      r   r   r!   r   r   r   test_encoding_of_127A   s    z&TestRemoveInteger.test_encoding_of_127c                 C   s(   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-   r1   r   r   r   test_wrong_lengthS   s    &z#TestRemoveInteger.test_wrong_lengthN)__name__
__module____qualname__r   r   r$   r%   r&   r(   r)   r+   r,   r3   r5   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d d S )N)r   r       r    r   r   r   r   r   test_zero_length^   s    zTestReadLength.test_zero_lengthc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns    r   r
   r   r   r   r   r   test_two_byte_zero_lengtha   s    z(TestReadLength.test_two_byte_zero_lengthc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns   r=   r   r   r   r   test_two_byte_small_lengthe   s    z)TestReadLength.test_two_byte_small_lengthc                 C   s6   |  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d d S )N)r      s   r;   r   r   r   r   test_smallest_two_byte_lengthm   s    z,TestReadLength.test_smallest_two_byte_lengthc                 C   s6   |  t td W d    n1 s(0    Y  d S )Ns    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   test_two_three_byte_lengtht   s    z)TestReadLength.test_two_three_byte_lengthc                 C   s6   |  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   s6   |  t td W d    n1 s(0    Y  d S )Ns    r=   r   r   r   r   test_length_overflow{   s    z#TestReadLength.test_length_overflowN)r6   r7   r8   r<   r>   r?   rA   rC   rD   rG   r(   rH   r   r   r   r   r9   Z   s   r9   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   rP   derr   r   r   test_old_call_convention   s    
&z,TestEncodeBitstring.test_old_call_conventionc                 C   sL   t  $ t d tdd}W d   n1 s20    Y  | |d dS )z$This is how it should be called now.error   r   NrL   rM   catch_warningsrN   r   r    r   rU   r   r   r   test_new_call_convention   s    

(z,TestEncodeBitstring.test_new_call_conventionc                 C   sL   t  $ t d tdd}W d   n1 s20    Y  | |d dS )zU
        Writing bit string with already included the number of unused bits.
        rW   rK   NrL   rY   r[   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[   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   ra   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 )NrX   rB   ra   r   r   r   r   test_non_zero_padding_bits   s    z.TestEncodeBitstring.test_non_zero_padding_bitsN)r6   r7   r8   rV   r\   r]   r_   r(   rc   rd   re   r   r   r   r   rI      s   	rI   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.rJ   rL   Nr   z$expect_unused= needs to be specifiedr   rK   r   )rM   rN   rO   rP   rQ   r   r    rR   r.   rS   rT   )r   rP   bitsrestr   r   r   rV      s    
*z,TestRemoveBitstring.test_old_call_conventionc                 C   s\   t  ( t d tdd\}}W d    n1 s60    Y  | |d | |d d S )NrW   rL   r   rX   r   rM   rZ   rN   r   r    r   rg   rh   r   r   r   r\      s
    

,z,TestRemoveBitstring.test_new_call_conventionc                 C   s\   t  ( t d tdd \}}W d    n1 s60    Y  | |d | |d d S )NrW   rL   )rX   r   r   ri   rj   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    ro   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 r'   ro   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   ro   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 )NrL   r   ro   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    ro   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   ro   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   ro   r   r   r   r   test_invalid_padding_bits   s    z-TestRemoveBitstring.test_invalid_padding_bitsN)r6   r7   r8   rV   r\   rk   rn   rp   rq   r(   rs   rt   ru   rv   rw   r   r   r   r   rf      s   		rf   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   r/   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)r6   r7   r8   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|d d S )Nr   rB   H  ='  s   06072a8648ce3d0201)r   r    r   )r   oid_ecPublicKeyr   r   r   test_pub_key_oid  s    zTestEncodeOid.test_pub_key_oidc                 C   s   |  ttjd d S )Ns   06052b81040021)r    r   r   encoded_oidr   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 )NrB     rF      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 )NrB   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_second+  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_second/  s    
z)TestEncodeOid.test_small_first_max_secondc                 C   s8   |  t tdd W d    n1 s*0    Y  d S )NrF   r   r   r   r   r   r   test_with_invalid_first3  s    z%TestEncodeOid.test_with_invalid_firstN)r6   r7   r8   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   rB   r   r   )r   r   )clsr   r   r   
setUpClass9  s    zTestRemoveObject.setUpClassc                 C   s*   t | j\}}| |d | |d d S )Nr   r   rB   r   r   rB   r   r   r   r    r   r   rh   r   r   r   r   =  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_bytesB  s    z&TestRemoveObject.test_with_extra_bytesc                 C   s(   t d\}}| |d | |d d S )Nr   r   )rB   r   rF   r   r    r   r   r   r   test_with_large_second_subidG  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_subidM  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_subidQ  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_byteU  s    z:TestRemoveObject.test_with_missing_last_byte_of_multi_bytec                 C   s(   t d\}}| |d | |d d S )Nr   r   )rB   r   r   r   r   r   r   r   Y  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   ^  s    zTestRemoveObject.test_zero_zeroc                 C   s6   |  t td W d    n1 s(0    Y  d S r'   r   r   r   r   r   r(   c  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_lengthg  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_oidk  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_overflowo  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_types  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_lengthw  s    z*TestRemoveObject.test_with_too_long_lengthN)r6   r7   r8   classmethodr   r   r   r   r   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   r   8  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.   r/   r0   r   r   r2   r   r   r   test_with_malformed_tag  s    &z-TestRemoveConstructed.test_with_malformed_tagNr6   r7   r8   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.   r/   r0   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.   r/   r0   r1   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r4   r   r   r   r   r   test_with_wrong_length  s    &z)TestRemoveSequence.test_with_wrong_lengthN)r6   r7   r8   r   r   r   r   r   r   r   r   r     s   r   rB   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   rB   )Z	min_value	max_valuer   )max_size)stZintegersliststuple)Zdrawr   r   firstsecondrh   r   r   r   st_oid  s    r   z--fastZmax_examplesc                 C   s0   t |  }t|\}}|dks J || ks,J d S r'   )r   r   )idsr   Zdecoded_oidrh   r   r   r   	test_oids  s    r   ).rM   binasciir   Z	unittest2ZunittestImportErrorsysZhypothesis.strategies
strategiesr   Z
hypothesisr   r   rO   Z_compatr   Zcurvesr   r   rU   r	   r
   r   r   r   r   r   r   r   r   ZTestCaser   r9   rI   rf   rx   r   r   r   r   r   Z	compositer   ZHYP_SETTINGSargvr   r   r   r   r   <module>   s<   0<&:H+D