o
    RhU:                     @   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w 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d%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   8   |  t td W d    d S 1 sw   Y  d S )Ns    assertRaisesr
   r	   self r   P/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/ecdsa/test_der.pytest_non_minimal_encoding!      
"z+TestRemoveInteger.test_non_minimal_encodingc                 C   r   )Ns   r   r   r   r   r   test_negative_with_high_bit_set%   r   z1TestRemoveInteger.test_negative_with_high_bit_setc                 C   (   t d\}}| |d | |d d S Ns           r	   assertEqualr   valremr   r   r   test_minimal_with_high_bit_set)      z0TestRemoveInteger.test_minimal_with_high_bit_setc                 C   r   )Ns     r   r   r   r   r   %test_two_zero_bytes_with_high_bit_set/   r   z7TestRemoveInteger.test_two_zero_bytes_with_high_bit_setc                 C   r   )Ns    r   r   r   r   r   test_zero_length_integer3   r   z*TestRemoveInteger.test_zero_length_integerc                 C   r   Nr!   r   r   r   r   r   test_empty_string7   r   z#TestRemoveInteger.test_empty_stringc                 C   r   )Ns    r   r!   r"   r$   r   r   r   test_encoding_of_zero;   r(   z'TestRemoveInteger.test_encoding_of_zeroc                 C   r   )Ns      r!   r"   r$   r   r   r   test_encoding_of_127A   r(   z&TestRemoveInteger.test_encoding_of_127c                 C   r   r   r"   r$   r   r   r   test_encoding_of_128G   r(   z&TestRemoveInteger.test_encoding_of_128c                 C   H   |  t}td W d    n1 sw   Y  | dt|j d S )Ns    zwanted type 'integer'r   r
   r	   assertInstr	exceptionr   er   r   r   test_wrong_tagM      
z TestRemoveInteger.test_wrong_tagc                 C   r1   )Ns    Length longerr2   r6   r   r   r   test_wrong_lengthS   r9   z#TestRemoveInteger.test_wrong_lengthN)__name__
__module____qualname__r   r   r'   r)   r*   r,   r-   r/   r0   r8   r;   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      |  dtd d S )N)r   r       r#   r   r   r   r   r   test_zero_length^      zTestReadLength.test_zero_lengthc                 C   r   )Ns    r   r
   r   r   r   r   r   test_two_byte_zero_lengtha   r   z(TestReadLength.test_two_byte_zero_lengthc                 C   r   )Ns   rE   r   r   r   r   test_two_byte_small_lengthe   r   z)TestReadLength.test_two_byte_small_lengthc                 C   r   )N   rE   r   r   r   r   test_long_form_with_zero_lengthi   r   z.TestReadLength.test_long_form_with_zero_lengthc                 C   r@   )N)r       s   rB   r   r   r   r   test_smallest_two_byte_lengthm   rD   z,TestReadLength.test_smallest_two_byte_lengthc                 C   r   )Ns    rE   r   r   r   r   test_zero_padded_lengthp   r   z&TestReadLength.test_zero_padded_lengthc                 C   r@   )N)      s    rB   r   r   r   r   test_two_three_byte_lengtht   rD   z)TestReadLength.test_two_three_byte_lengthc                 C   r   r+   rE   r   r   r   r   r,   w   r   z TestReadLength.test_empty_stringc                 C   r   )Ns    rE   r   r   r   r   test_length_overflow{   r   z#TestReadLength.test_length_overflowN)r<   r=   r>   rC   rF   rG   rI   rK   rL   rO   r,   rP   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 sw   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#   lenr3   messageargs)r   rX   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 sw   Y  | |d dS )z$This is how it should be called now.error   r   NrT   rU   catch_warningsrV   r   r#   r   r]   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 sw   Y  | |d dS )zU
        Writing bit string with already included the number of unused bits.
        r_   rS   NrT   ra   rc   r   r   r   test_implicit_unused_bits   s
   

z-TestEncodeBitstring.test_implicit_unused_bitsc                 C      t dd}| |d d S )Ns      s   )r   r#   rc   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,         z%TestEncodeBitstring.test_empty_stringc                 C   :   |  t tdd W d    d S 1 sw   Y  d S )Ns       r   
ValueErrorr   r   r   r   r   test_invalid_unused_count      "z-TestEncodeBitstring.test_invalid_unused_countc                 C   rj   )Nr!   r   rl   r   r   r   r   %test_invalid_unused_with_empty_string   ro   z9TestEncodeBitstring.test_invalid_unused_with_empty_stringc                 C   rj   )Nr`   rJ   rl   r   r   r   r   test_non_zero_padding_bits   ro   z.TestEncodeBitstring.test_non_zero_padding_bitsN)r<   r=   r>   r^   rd   re   rh   r,   rn   rp   rq   r   r   r   r   rQ      s    	rQ   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 sw   Y  | t|d | d|d j	j
d  | |d | |d dS )	z)This is the old way to call the function.rR   rT   Nr   z$expect_unused= needs to be specifiedr   rS   r!   )rU   rV   rW   rX   rY   r   r#   rZ   r3   r[   r\   )r   rX   bitsrestr   r   r   r^      s   
z,TestRemoveBitstring.test_old_call_conventionc                 C   s\   t   t d tdd\}}W d    n1 sw   Y  | |d | |d d S )Nr_   rT   r   r`   r!   rU   rb   rV   r   r#   r   rs   rt   r   r   r   rd         

z,TestRemoveBitstring.test_new_call_conventionc                 C   s\   t   t d tdd \}}W d    n1 sw   Y  | |d | |d d S )Nr_   rT   )r`   r   r!   ru   rv   r   r   r   test_implicit_unexpected_unused   rw   z3TestRemoveBitstring.test_implicit_unexpected_unusedc                 C   s*   t dd \}}| |d | |d d S )Ns   )   rg   r!   )r   r#   )r   retrt   r   r   r   test_with_padding   s   z%TestRemoveBitstring.test_with_paddingc                 C   :   |  t tdd  W d    d S 1 sw   Y  d S )Ns    r   r
   r   r   r   r   r   test_not_a_bitstring   ro   z(TestRemoveBitstring.test_not_a_bitstringc                 C   r|   )Ns    r}   r   r   r   r   test_empty_encoding   ro   z'TestRemoveBitstring.test_empty_encodingc                 C   r|   r+   r}   r   r   r   r   r,      ro   z%TestRemoveBitstring.test_empty_stringc                 C   r|   )N   r}   r   r   r   r   test_no_length   ro   z"TestRemoveBitstring.test_no_lengthc                 C   rj   )NrT   r   r}   r   r   r   r   %test_unexpected_number_of_unused_bits   ro   z9TestRemoveBitstring.test_unexpected_number_of_unused_bitsc                 C   r|   )Ns    r}   r   r   r   r   $test_invalid_encoding_of_unused_bits   ro   z8TestRemoveBitstring.test_invalid_encoding_of_unused_bitsc                 C   r|   )Ns   r}   r   r   r   r   %test_invalid_encoding_of_empty_string   ro   z9TestRemoveBitstring.test_invalid_encoding_of_empty_stringc                 C   r|   )Ns   r}   r   r   r   r   test_invalid_padding_bits   ro   z-TestRemoveBitstring.test_invalid_padding_bitsN)r<   r=   r>   r^   rd   rx   r{   r~   r   r,   r   r   r   r   r   r   r   r   r   rr      s    		rr   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestStrIdxAsIntc                 C      |  dtdd d S )Ns   r4   r   r#   r   r   r   r   r   test_str  ri   zTestStrIdxAsInt.test_strc                 C   r   Nr   s   strr   r   r   r   r   r   
test_bytes  ri   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)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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   rJ   H  ='  s   06072a8648ce3d0201)r   r#   r   )r   oid_ecPublicKeyr   r   r   test_pub_key_oid  s   zTestEncodeOid.test_pub_key_oidc                 C      |  ttjd d S )Ns   06052b81040021)r#   r   r   encoded_oidr   r   r   r   test_nist224p_oid  ri   zTestEncodeOid.test_nist224p_oidc                 C   r   )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 )NrJ     rN      7r   r#   r   oidr   r   r   test_large_second_subid  s   z%TestEncodeOid.test_large_second_subidc                 C   rf   )NrJ   r      7r   r   r   r   r   test_with_two_subids     
z"TestEncodeOid.test_with_two_subidsc                 C   s   t dd}| |d d S )Nr       r   r   r   r   r   test_zero_zero#  r   zTestEncodeOid.test_zero_zeroc                 C   s>   |  ttf tdd  W d    d S 1 sw   Y  d S )Nr   )r   	TypeErrorAssertionErrorr   r   r   r   r   test_with_wrong_types'  s   "z#TestEncodeOid.test_with_wrong_typesc                 C   rj   )Nr   (   r   r   r   r   r   r   r   "test_with_small_first_large_second+  ro   z0TestEncodeOid.test_with_small_first_large_secondc                 C   rf   )Nr   '   s   Or   r   r   r   r   test_small_first_max_second/  r   z)TestEncodeOid.test_small_first_max_secondc                 C   rj   )NrN   r   r   r   r   r   r   test_with_invalid_first3  ro   z%TestEncodeOid.test_with_invalid_firstN)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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   rJ   r   r   )r   r   )clsr   r   r   
setUpClass9  s   zTestRemoveObject.setUpClassc                 C   s*   t | j\}}| |d | |d d S )Nr!   r   rJ   r   r   rJ   r   r   r   r#   r   r   rt   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   (   t d\}}| |d | |d d S )Nr   r!   )rJ   r   rN   r   r#   r   r   r   r   test_with_large_second_subidG  s   z-TestRemoveObject.test_with_large_second_subidc                 C   r   )Ns    r   r
   r   r   r   r   r   test_with_padded_first_subidM  r   z-TestRemoveObject.test_with_padded_first_subidc                 C   r   )Ns   7r   r   r   r   r   test_with_padded_second_subidQ  r   z.TestRemoveObject.test_with_padded_second_subidc                 C   r   )Ns   7r   r   r   r   r   )test_with_missing_last_byte_of_multi_byteU  r   z:TestRemoveObject.test_with_missing_last_byte_of_multi_bytec                 C   r   )Nr   r!   )rJ   r   r   r   r   r   r   r   Y     z%TestRemoveObject.test_with_two_subidsc                 C   r   )Nr   r!   )r   r   r   r   r   r   r   r   ^  r   zTestRemoveObject.test_zero_zeroc                 C   r   r+   r   r   r   r   r   r,   c  r   z"TestRemoveObject.test_empty_stringc                 C   r   )N   r   r   r   r   r   test_missing_lengthg  r   z$TestRemoveObject.test_missing_lengthc                 C   r   )Ns    r   r   r   r   r   test_empty_oidk  r   zTestRemoveObject.test_empty_oidc                 C   r   )Ns   r   r   r   r   r   test_empty_oid_overflowo  r   z(TestRemoveObject.test_empty_oid_overflowc                 C   r   )Ns   7r   r   r   r   r   test_with_wrong_types  r   z%TestRemoveObject.test_with_wrong_typec                 C   r   )Ns   7r   r   r   r   r   test_with_too_long_lengthw  r   z*TestRemoveObject.test_with_too_long_lengthN)r<   r=   r>   classmethodr   r   r   r   r   r   r   r   r   r,   r   r   r   r   r   r   r   r   r   r   8  s"    
r   c                   @      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bodyrt   r   r   r   test_simple}  s
   z!TestRemoveConstructed.test_simplec                 C   L   d}|  t}t| W d    n1 sw   Y  | dt|j d S )Ns   zconstructed tag)r   r
   r   r3   r4   r5   r   r   r7   r   r   r   test_with_malformed_tag  
   
z-TestRemoveConstructed.test_with_malformed_tagNr<   r=   r>   r   r   r   r   r   r   r   |  s    	r   c                   @   r   )TestRemoveOctetStringc                 C   ,   d}t |\}}| |d | |d d S )Ns   s   r!   )r   r#   r   r   r   rt   r   r   r   r        z!TestRemoveOctetString.test_simplec                 C   r   )Ns   octetstring)r   r
   r   r3   r4   r5   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   r   )Ns   0r   r!   )r   r#   r   r   r   r   r     r   zTestRemoveSequence.test_simplec                 C   r1   )Nr!   zEmpty stringr   r
   r   r3   r4   r5   r6   r   r   r   test_with_empty_string  r9   z)TestRemoveSequence.test_with_empty_stringc                 C   r   )Ns    zwanted type 'sequence'r   r   r   r   r   test_with_wrong_tag  r   z&TestRemoveSequence.test_with_wrong_tagc                 C   r   )Ns   0r:   r   r   r   r   r   test_with_wrong_length  r   z)TestRemoveSequence.test_with_wrong_lengthN)r<   r=   r>   r   r   r   r   r   r   r   r   r     s
    r   rJ   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   rJ   )	min_value	max_valuer   )max_size)stintegersliststuple)drawr   r   firstsecondrt   r   r   r   st_oid  s   r   z--fastmax_examplesc                 C   s0   t |  }t|\}}|dksJ || ksJ d S r+   )r   r   )idsr   decoded_oidrt   r   r   r   	test_oids  s   r   r   ).rU   binasciir   	unittest2unittestImportErrorsyshypothesis.strategies
strategiesr   
hypothesisr   r   rW   _compatr   curvesr   r   r]   r	   r
   r   r   r   r   r   r   r   r   TestCaser   r?   rQ   rr   r   r   r   r   r   r   	compositer   HYP_SETTINGSargvr   r   r   r   r   <module>   s@   0<&:H+D
