o
    6dl:                     @   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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   <   |  t ttd W d    d S 1 sw   Y  d S )Nz assertRaisesr
   r	   r   self r   eC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\ecdsa/test_der.pytest_non_minimal_encoding!      "z+TestRemoveInteger.test_non_minimal_encodingc                 C   r   )Nu   r   r   r   r   r   test_negative_with_high_bit_set%   r   z1TestRemoveInteger.test_negative_with_high_bit_setc                 C   ,   t td\}}| |d | |d d S Nu           r	   r   assertEqualr   valremr   r   r   test_minimal_with_high_bit_set)      z0TestRemoveInteger.test_minimal_with_high_bit_setc                 C   r   )Nu     ÿ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   )Nz r   r   r   r   r   test_zero_length_integer3   r   z*TestRemoveInteger.test_zero_length_integerc                 C   r   N r   r   r   r   r   test_empty_string7   r   z#TestRemoveInteger.test_empty_stringc                 C   r   )Nz r   r!   r"   r$   r   r   r   test_encoding_of_zero;   r(   z'TestRemoveInteger.test_encoding_of_zeroc                 C   r   )Nz   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   r2   )Ns    Length longerr3   r7   r   r   r   test_wrong_lengthS   r:   z#TestRemoveInteger.test_wrong_lengthN)__name__
__module____qualname__r   r   r'   r)   r*   r-   r.   r0   r1   r9   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td d S )N)r   r    r#   r   r   r   r   r   r   test_zero_length^      zTestReadLength.test_zero_lengthc                 C   r   )Nu    r   r
   r   r   r   r   r   r   test_two_byte_zero_lengtha   r   z(TestReadLength.test_two_byte_zero_lengthc                 C   r   )Nu   rF   r   r   r   r   test_two_byte_small_lengthe   r   z)TestReadLength.test_two_byte_small_lengthc                 C   r   )N   rF   r   r   r   r   test_long_form_with_zero_lengthi   r   z.TestReadLength.test_long_form_with_zero_lengthc                 C   rA   )N)r       u   rC   r   r   r   r   test_smallest_two_byte_lengthm   rE   z,TestReadLength.test_smallest_two_byte_lengthc                 C   r   )Nu    rF   r   r   r   r   test_zero_padded_lengthp   r   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   r   r+   rF   r   r   r   r   r-   w   r   z TestReadLength.test_empty_stringc                 C   r   )Nu    rF   r   r   r   r   test_length_overflow{   r   z#TestReadLength.test_length_overflowN)r=   r>   r?   rD   rG   rH   rJ   rL   rM   rP   r-   rQ   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#   lenr4   messageargsr   rY   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 sw   Y  | t|d | |d dS )z$This is how it should be called now.rS   N   r   rU   rV   rW   rX   rY   r   r#   r[   r^   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 sw   Y  | t|d | |d dS )zU
        Writing bit string with already included the number of unused bits.
        rS   NrT   r   rU   rb   r^   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#   )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-         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   ri   )Nr!   r   rk   r   r   r   r   %test_invalid_unused_with_empty_string   rn   z9TestEncodeBitstring.test_invalid_unused_with_empty_stringc                 C   ri   )Nra   rK   rk   r   r   r   r   test_non_zero_padding_bits   rn   z.TestEncodeBitstring.test_non_zero_padding_bitsN)r=   r>   r?   r`   rc   rd   rg   r-   rm   ro   rp   r   r   r   r   rR      s    rR   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.rS   rU   Nr   z$expect_unused= needs to be specifiedr   rT   r!   )rV   rW   rX   rY   rZ   r   r#   r[   r4   r\   r]   r   rY   bitsrestr   r   r   r`      s   
z,TestRemoveBitstring.test_old_call_conventionc                 C   sn   t d td }tdd\}}W d    n1 sw   Y  | t|d | |d | |d d S )NrS   rU   r   ra   r!   rV   rW   rX   rY   r   r#   r[   rr   r   r   r   rc         
z,TestRemoveBitstring.test_new_call_conventionc                 C   sn   t d td }tdd \}}W d    n1 sw   Y  | t|d | |d | |d d S )NrS   rU   r   )ra   r   r!   ru   rr   r   r   r   test_implicit_unexpected_unused   rv   z3TestRemoveBitstring.test_implicit_unexpected_unusedc                 C   s*   t dd \}}| |d | |d d S )Ns   )   rf   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   rn   z(TestRemoveBitstring.test_not_a_bitstringc                 C   r{   )Ns    r|   r   r   r   r   test_empty_encoding   rn   z'TestRemoveBitstring.test_empty_encodingc                 C   r{   Nr!   r|   r   r   r   r   r-      rn   z%TestRemoveBitstring.test_empty_stringc                 C   r{   )N   r|   r   r   r   r   test_no_length   rn   z"TestRemoveBitstring.test_no_lengthc                 C   ri   )NrU   r   r|   r   r   r   r   %test_unexpected_number_of_unused_bits   rn   z9TestRemoveBitstring.test_unexpected_number_of_unused_bitsc                 C   r{   )Ns    r|   r   r   r   r   $test_invalid_encoding_of_unused_bits   rn   z8TestRemoveBitstring.test_invalid_encoding_of_unused_bitsc                 C   r{   )Ns   r|   r   r   r   r   %test_invalid_encoding_of_empty_string   rn   z9TestRemoveBitstring.test_invalid_encoding_of_empty_stringc                 C   r{   )Ns   r|   r   r   r   r   test_invalid_padding_bits  rn   z-TestRemoveBitstring.test_invalid_padding_bitsN)r=   r>   r?   r`   rc   rw   rz   r}   r~   r-   r   r   r   r   r   r   r   r   r   rq      s    

rq   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   r5   r   r#   r   r   r   r   r   test_str	  rh   zTestStrIdxAsInt.test_strc                 C   r   Nr   s   strr   r   r   r   r   r   
test_bytes  rh   zTestStrIdxAsInt.test_bytesc                 C   s   |  dttdd d S r   )r#   r   	bytearrayr   r   r   r   test_bytearray     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|td d S )Nr   rK   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  r   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 )NrK     rO      7r   r#   r   oidr   r   r   test_large_second_subid   s   z%TestEncodeOid.test_large_second_subidc                 C   re   )NrK   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   ri   )Nr   (   r   r   r   r   r   r   r   "test_with_small_first_large_second1  rn   z0TestEncodeOid.test_with_small_first_large_secondc                 C   re   )Nr   '   s   Or   r   r   r   r   test_small_first_max_second5  r   z)TestEncodeOid.test_small_first_max_secondc                 C   ri   )NrO   r   r   r   r   r   r   test_with_invalid_first9  rn   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   rK   r   r   )r   r   )clsr   r   r   
setUpClass?  s   zTestRemoveObject.setUpClassc                 C   s*   t | j\}}| |d | |d d S )Nr!   r   rK   r   r   rK   r   r   r   r#   r   r   rt   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   (   t d\}}| |d | |d d S )Nr   r!   )rK   r   rO   r   r#   r   r   r   r   test_with_large_second_subidM  s   z-TestRemoveObject.test_with_large_second_subidc                 C   8   |  t td W d    d S 1 sw   Y  d S )Ns    r   r
   r   r   r   r   r   test_with_padded_first_subidS     
"z-TestRemoveObject.test_with_padded_first_subidc                 C   r   )Ns   7r   r   r   r   r   test_with_padded_second_subidW  r   z.TestRemoveObject.test_with_padded_second_subidc                 C   r   )Ns   7r   r   r   r   r   )test_with_missing_last_byte_of_multi_byte[  r   z:TestRemoveObject.test_with_missing_last_byte_of_multi_bytec                 C   r   )Nr   r!   )rK   r   r   r   r   r   r   r   _     z%TestRemoveObject.test_with_two_subidsc                 C   r   )Nr   r!   )r   r   r   r   r   r   r   r   d  r   zTestRemoveObject.test_zero_zeroc                 C   r   r   r   r   r   r   r   r-   i  r   z"TestRemoveObject.test_empty_stringc                 C   r   )N   r   r   r   r   r   test_missing_lengthm  r   z$TestRemoveObject.test_missing_lengthc                 C   r   )Ns    r   r   r   r   r   test_empty_oidq  r   zTestRemoveObject.test_empty_oidc                 C   r   )Ns   r   r   r   r   r   test_empty_oid_overflowu  r   z(TestRemoveObject.test_empty_oid_overflowc                 C   r   )Ns   7r   r   r   r   r   test_with_wrong_typey  r   z%TestRemoveObject.test_with_wrong_typec                 C   r   )Ns   7r   r   r   r   r   test_with_too_long_length}  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   >  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   r4   r5   r6   r   r   r8   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   Zoctetstring)r   r
   r   r4   r5   r6   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   r2   )Nr!   zEmpty stringr   r
   r   r4   r5   r6   r7   r   r   r   test_with_empty_string  r:   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   rK   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   rK   )Z	min_value	max_valuer   )max_size)stZintegersliststuple)Zdrawr   r   firstsecondrt   r   r   r   st_oid  s   r   c                 C   s0   t |  }t|\}}|dksJ || ksJ d S r   )r   r   )idsr   Zdecoded_oidrt   r   r   r   	test_oids  s   r   ),rV   binasciir   Z	unittest2ZunittestImportErrorsixr   Zhypothesis.strategies
strategiesr   Z
hypothesisr   rX   Z_compatr   Zcurvesr   r   r_   r	   r
   r   r   r   r   r   r   r   r   ZTestCaser   r@   rR   rq   r   r   r   r   r   r   Z	compositer   r   r   r   r   r   <module>   s8   0<&>J+D