o
    6hI                     @   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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*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j0v rd e/d&< ed)i e/ee. d'd( Z1d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_implicitremove_octet_stringremove_sequenceencode_implicitc                   @   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   b/var/www/html/construction_image-detection-poc/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_set1   r   z7TestRemoveInteger.test_two_zero_bytes_with_high_bit_setc                 C   r   )Ns    r   r   r   r   r   test_zero_length_integer5   r   z*TestRemoveInteger.test_zero_length_integerc                 C   r   Nr#   r   r   r   r   r   test_empty_string9   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_127C   r*   z&TestRemoveInteger.test_encoding_of_127c                 C   r    r!   r$   r&   r   r   r   test_encoding_of_128I   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_tagO      
z TestRemoveInteger.test_wrong_tagc                 C   r3   )Ns    Length longerr4   r8   r   r   r   test_wrong_lengthU   r;   z#TestRemoveInteger.test_wrong_lengthN)__name__
__module____qualname__r   r   r)   r+   r,   r.   r/   r1   r2   r:   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_lengthc   r   z(TestReadLength.test_two_byte_zero_lengthc                 C   r   )Ns   rG   r   r   r   r   test_two_byte_small_lengthg   r   z)TestReadLength.test_two_byte_small_lengthc                 C   r   )N   rG   r   r   r   r   test_long_form_with_zero_lengthk   r   z.TestReadLength.test_long_form_with_zero_lengthc                 C   rB   )N)r"      s   rD   r   r   r   r   test_smallest_two_byte_lengtho   rF   z,TestReadLength.test_smallest_two_byte_lengthc                 C   r   )Ns    rG   r   r   r   r   test_zero_padded_lengthr   r   z&TestReadLength.test_zero_padded_lengthc                 C   rB   )N)      s    rD   r   r   r   r   test_two_three_byte_lengthv   rF   z)TestReadLength.test_two_three_byte_lengthc                 C   r   r-   rG   r   r   r   r   r.   y   r   z TestReadLength.test_empty_stringc                 C   r   )Ns    rG   r   r   r   r   test_length_overflow}   r   z#TestReadLength.test_length_overflowN)r>   r?   r@   rE   rH   rI   rK   rM   rN   rQ   r.   rR   r   r   r   r   rA   \   s    rA   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%   lenr5   messageargs)r   rZ   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   NrV   rW   catch_warningsrX   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.
        ra   rU   NrV   rc   re   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%   re   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   rl   )Nr#   r   rn   r   r   r   r   %test_invalid_unused_with_empty_string   rq   z9TestEncodeBitstring.test_invalid_unused_with_empty_stringc                 C   rl   )Nrb   rL   rn   r   r   r   r   test_non_zero_padding_bits   rq   z.TestEncodeBitstring.test_non_zero_padding_bitsN)r>   r?   r@   r`   rf   rg   rj   r.   rp   rr   rs   r   r   r   r   rS      s    	rS   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.rT   rV   Nr   z$expect_unused= needs to be specifiedr   rU   r#   )rW   rX   rY   rZ   r[   r   r%   r\   r5   r]   r^   )r   rZ   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 )Nra   rV   r   rb   r#   rW   rd   rX   r   r%   r   ru   rv   r   r   r   rf         

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 )Nra   rV   )rb   r   r#   rw   rx   r   r   r   test_implicit_unexpected_unused   ry   z3TestRemoveBitstring.test_implicit_unexpected_unusedc                 C   s*   t dd \}}| |d | |d d S )Ns   )   ri   r#   )r   r%   )r   retrv   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   rq   z(TestRemoveBitstring.test_not_a_bitstringc                 C   r~   )Ns    r   r   r   r   r   test_empty_encoding   rq   z'TestRemoveBitstring.test_empty_encodingc                 C   r~   r-   r   r   r   r   r   r.      rq   z%TestRemoveBitstring.test_empty_stringc                 C   r~   )N   r   r   r   r   r   test_no_length   rq   z"TestRemoveBitstring.test_no_lengthc                 C   rl   )NrV   r   r   r   r   r   r   %test_unexpected_number_of_unused_bits   rq   z9TestRemoveBitstring.test_unexpected_number_of_unused_bitsc                 C   r~   )Ns    r   r   r   r   r   $test_invalid_encoding_of_unused_bits   rq   z8TestRemoveBitstring.test_invalid_encoding_of_unused_bitsc                 C   r~   )Ns   r   r   r   r   r   %test_invalid_encoding_of_empty_string   rq   z9TestRemoveBitstring.test_invalid_encoding_of_empty_stringc                 C   r~   )Ns   r   r   r   r   r   test_invalid_padding_bits   rq   z-TestRemoveBitstring.test_invalid_padding_bitsN)r>   r?   r@   r`   rf   rz   r}   r   r   r.   r   r   r   r   r   r   r   r   r   rt      s    		rt   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   r6   r   r%   r   r   r   r   r   test_str  rk   zTestStrIdxAsInt.test_strc                 C   r   Nr   s   strr   r   r   r   r   r   
test_bytes  rk   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   rL   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  rk   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 )NrL     rP      7r   r%   r   oidr   r   r   test_large_second_subid  s   z%TestEncodeOid.test_large_second_subidc                 C   rh   )NrL   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   rl   )Nr   (   r   r   r   r   r   r   r   "test_with_small_first_large_second-  rq   z0TestEncodeOid.test_with_small_first_large_secondc                 C   rh   )Nr   '   s   Or   r   r   r   r   test_small_first_max_second1  r   z)TestEncodeOid.test_small_first_max_secondc                 C   rl   )NrP   r   r   r   r   r   r   test_with_invalid_first5  rq   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   rL   r   r   )r   r   clsr   r   r   
setUpClass;  s   zTestRemoveObject.setUpClassc                 C   s*   t | j\}}| |d | |d d S )Nr#   r   rL   r   r   rL   r   r   r   r%   r   r   rv   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_bytesD  s   z&TestRemoveObject.test_with_extra_bytesc                 C   (   t d\}}| |d | |d d S )Nr   r#   )rL   r   rP   r   r%   r   r   r   r   test_with_large_second_subidI  s   z-TestRemoveObject.test_with_large_second_subidc                 C   r   )Ns    r   r
   r   r   r   r   r   test_with_padded_first_subidO  r   z-TestRemoveObject.test_with_padded_first_subidc                 C   r   )Ns   7r   r   r   r   r   test_with_padded_second_subidS  r   z.TestRemoveObject.test_with_padded_second_subidc                 C   r   )Ns   7r   r   r   r   r   )test_with_missing_last_byte_of_multi_byteW  r   z:TestRemoveObject.test_with_missing_last_byte_of_multi_bytec                 C   r   )Nr   r#   )rL   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   `  r   zTestRemoveObject.test_zero_zeroc                 C   r   r-   r   r   r   r   r   r.   e  r   z"TestRemoveObject.test_empty_stringc                 C   r   )N   r   r   r   r   r   test_missing_lengthi  r   z$TestRemoveObject.test_missing_lengthc                 C   r   )Ns    r   r   r   r   r   test_empty_oidm  r   zTestRemoveObject.test_empty_oidc                 C   r   )Ns   r   r   r   r   r   test_empty_oid_overflowq  r   z(TestRemoveObject.test_empty_oid_overflowc                 C   r   )Ns   7r   r   r   r   r   test_with_wrong_typeu  r   z%TestRemoveObject.test_with_wrong_typec                 C   r   )Ns   7r   r   r   r   r   test_with_too_long_lengthy  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bodyrv   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   r5   r6   r7   r   r   r9   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                   @   sX   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S )TestRemoveImplicitc                 C   s"   d| _ d| _d| _d| _d| _d S )N      
   F
   
   
)exp_tagexp_datadata_applicationdata_context_specificdata_privater   r   r   r   r     s
   
zTestRemoveImplicit.setUpClassc                 C   s<   t | j\}}}| || j | || j | |d d S r-   r   r   r%   r   r   r   r   r   rv   r   r   r   r     s   zTestRemoveImplicit.test_simplec                 C   sL   |  t}t| jd W d    n1 sw   Y  | dt|j d S )Nfoobarzinvalid `exp_class` value)r   ro   r   r   r5   r6   r7   r8   r   r   r   test_wrong_expected_class     z,TestRemoveImplicit.test_wrong_expected_classc                 C   sJ   |  t}t| j W d    n1 sw   Y  | dt|j d S )Nz+wanted class context-specific, got 0x46 tag)r   r
   r   r   r5   r6   r7   r8   r   r   r   test_with_wrong_class  s   
z(TestRemoveImplicit.test_with_wrong_classc                 C   >   t | jd\}}}| || j | || j | |d d S )Napplicationr#   )r   r   r%   r   r   r   r   r   r   test_with_application_class     z.TestRemoveImplicit.test_with_application_classc                 C   r   )Nprivater#   )r   r   r%   r   r   r   r   r   r   test_with_private_class  r   z*TestRemoveImplicit.test_with_private_classc                 C   sD   d}t | j| \}}}| || j | || j | || d S )Ns    r   )r   
extra_datar   r   rv   r   r   r   test_with_data_following  s   
z+TestRemoveImplicit.test_with_data_followingc                 C   r   )Ns   
z#wanted type primitive, got 0xa6 tag)r   r
   r   r5   r6   r7   r   r   r   r   test_with_constructed  r   z(TestRemoveImplicit.test_with_constructedc                 C   sD   d}t td|dd\}}}| |d | || | |d d S )Ns   some longish stringr   r   r#   )r   r   r%   r   r   r   r   test_encode_decode  s   
z%TestRemoveImplicit.test_encode_decodeN)r>   r?   r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    

r   c                   @   sH   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S )TestEncodeImplicitc                 C   s   d| _ d| _d| _d| _d S )Nr   r   r   r   )r   r   r   r   r   r   r   r   r     s   
zTestEncodeImplicit.setUpClassc                 C   s   t d| j}| || j d S )Nr   r   r   r%   r   r   r|   r   r   r   test_encode_with_default_class  s   z1TestEncodeImplicit.test_encode_with_default_classc                 C       t d| jd}| || j d S )Nr   r   )r   r   r%   r   r   r   r   r   "test_encode_with_application_class     z5TestEncodeImplicit.test_encode_with_application_classc                 C   r   )Nr   zcontext-specificr   r   r   r   r   'test_encode_with_context_specific_class  r   z:TestEncodeImplicit.test_encode_with_context_specific_classc                 C   r   )Nr   r   )r   r   r%   r   r   r   r   r   test_encode_with_private_class  r   z1TestEncodeImplicit.test_encode_with_private_classc                 C   sN   |  t}td| jd W d    n1 sw   Y  | dt|j d S )Nr   r   zinvalid tag classr   ro   r   r   r5   r6   r7   r8   r   r   r   test_encode_with_invalid_class  s   z1TestEncodeImplicit.test_encode_with_invalid_classc                 C   sL   |  t}td| j W d    n1 sw   Y  | dt|j d S )N    zLong tags not supportedr   r8   r   r   r   test_encode_with_too_large_tag  r   z1TestEncodeImplicit.test_encode_with_too_large_tagN)r>   r?   r@   r   r   r   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   rv   r   r   r   r        z!TestRemoveOctetString.test_simplec                 C   r   )Ns   octetstring)r   r
   r   r5   r6   r7   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   r3   )Nr#   zEmpty stringr   r
   r   r5   r6   r7   r8   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   rL   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   rL   )	min_value	max_valuer   )max_size)stintegersliststuple)drawr  r  firstsecondrv   r   r   r   st_oid8  s   r  z--fastmax_examplesc                 C   s0   t |  }t|\}}|dksJ || ksJ d S r-   )r   r   )idsr   decoded_oidrv   r   r   r   	test_oidsT  s   r  r   )2rW   binasciir   	unittest2unittestImportErrorsyshypothesis.strategies
strategiesr  
hypothesisr   r   rY   _compatr   curvesr   r   r_   r	   r
   r   r   r   r   r   r   r   r   r   r   TestCaser   rA   rS   rt   r   r   r   r   r   r   r   r   	compositer  HYP_SETTINGSargvr  r   r   r   r   <module>   sD   8<&:H+DN,
