
    H                     |   d Z ddlZddlmZ ddlZddlmZmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ d
ZdZdZdZd Z eg d      Z eg d      Z G d d      Z ee      Z ed      Zd Z ee      Z ed      Zd Z ee      Z ed      Zd Z ee      Z ed      Zd Z  ee      Z! ed      Z"d Z# ee      Z$d Z% G d dejL                        Z' G d  d!ejL                        Z( G d" d#ejL                        Z) G d$ d%ejL                        Z* G d& d'ejL                        Z+d( Z,e-d)k(  r e,        yy)*z#Unit tests for crcmod functionality    N)array   )mkCrcFunCrc)_usingExtension)PredefinedCrc)mkPredefinedCrcFun)_crc_definitionsi  i! im]l   	 c                 (    d}| D ]
  }|d|z  z  } |S Nr   r    )bitspns      Fplatform/bundledpythonunix/lib/python3.12/site-packages/crcmod/test.pypolyFromBitsr   ?   s#    	AaL H    )!@   ?   =   ;   :   8   7   4   1   0   /   .   ,   )   %   $   "                                  
   	            r   )"r   >   9   r   6   5   r   r   r   -   (   '   &   r"   #   !   r%   r&            r)   r*      r+      r-   r.   r/   r0      r2   r   r   c                   v    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)polyz?Class implementing polynomials over the field of integers mod 2c                 H    t        |      }|dk  rt        d      || _        y )Nr   zinvalid polynomial)int
ValueErrorr   )selfr   s     r   __init__zpoly.__init__Y   s$    Fq5
#788r   c                     | j                   S Nr   rI   s    r   __int__zpoly.__int__^   s    vvr   c                 4    | j                   |j                   k(  S rL   rM   rI   others     r   __eq__zpoly.__eq__a       vv  r   c                 4    | j                   |j                   k7  S rL   rM   rQ   s     r   __ne__zpoly.__ne__d   rT   r   c                 B    t        | j                  |j                        S rL   )cmpr   rQ   s     r   __cmp__zpoly.__cmp__i   s    466577##r   c                      | j                   dk7  S Nr   rM   rN   s    r   __bool__zpoly.__bool__l   s    vv{r   c                     | S rL   r   rN   s    r   __neg__zpoly.__neg__o   s    r   c                 |    t        | j                         dz   d      }d|z  dz
  }t        | j                  |z        S Nr   )maxdegrE   r   )rI   r   xs      r   
__invert__zpoly.__invert__r   s8    
Q"!VqLDFFQJr   c                 F    t        | j                  |j                  z        S rL   rE   r   rQ   s     r   __add__zpoly.__add__w       DFFUWW$%%r   c                 F    t        | j                  |j                  z        S rL   rf   rQ   s     r   __sub__zpoly.__sub__z   rh   r   c                     | j                   }|j                   }|dk(  s|dk(  rt        d      S d}|r|dz  r||z  }|dz  }|dz	  }|rt        |      S r   )r   rE   )rI   rR   abrc   s        r   __mul__zpoly.__mul__}   sc    FFGG6Q!VDG^sE1A1A	 
 Awr   c                    | j                   }| j                         }|j                   }|j                         }|dk(  rt        d      |dk(  r| t        d      fS ||k  rt        d      | fS ||z
  }d|z  }||z  }d}|dkD  r)||z  r
||z  }|dz  }|dz  }|dz	  }|dz	  }|dz  }|dkD  r)||z  r
||z  }|dz  }t        |      t        |      fS )Nr   zpolynomial division by zeror   )r   rb   ZeroDivisionErrorrE   )	rI   rR   umvr   krl   qs	            r   
__divmod__zpoly.__divmod__   s    FFHHJGGIIK6*+HII64Q.(q5$q'$'aCFF!e1uEEQAQAQAFA !e q5AAAAQQ  r   c                 *    | j                  |      d   S r[   rv   rQ   s     r   __div__zpoly.__div__       u%a((r   c                 *    | j                  |      d   S r`   rx   rQ   s     r   __mod__zpoly.__mod__   rz   r   c                      d| j                   z  S )Nzpoly(0x%XL)rM   rN   s    r   __repr__zpoly.__repr__   s    tvv%%r   c                     | j                   }|dk(  ryg dgdgddgd|dz     }|dz	  }d}|r&|dz  r|j                  d	|z         |dz	  }|dz  }|r&|j                          d
j                  |      S )Nr   01rc   )r   r      r3   r3   r   r   zx^%d+)r   appendreversejoin)rI   r   lstr   s       r   __str__zpoly.__str__   s    FF6##3s)5ac:qDsCJJvz*1AFA  	xx}r   c                     | j                   }|dk(  ryd}|dk\  r|dz  }|dz	  }|dk\  rt        |      }|dkD  r|dz  }|dz	  }|dkD  r|S )z#return the degree of the polynomialr      r,   r   )r   rG   )rI   rl   r   s      r   rb   zpoly.deg   so    FF6"7lGARA 7l F!eFAQA !e r   N)__name__
__module____qualname____doc__rJ   rO   rS   rV   rY   r\   r^   rd   rg   rj   rn   rv   ry   r|   r~   r   rb   r   r   r   rE   rE   W   sZ    I
!!
$ 
&&
!2))&r   rE      c                 l    d}| D ]
  }|dz  |z   } t        |      }t        |t        z  t        z        S Nr   r   )rE   rG   x8pg8pdr   is      r   crc8pr      s9    	AcEAI QAquSy>r   r   c                 l    d}| D ]
  }|dz  |z   } t        |      }t        |t        z  t        z        S r   )rE   rG   x16pg16pr   s      r   crc16pr      :    	AcEAI QAqvd{r   i   c                 l    d}| D ]
  }|dz  |z   } t        |      }t        |t        z  t        z        S r   )rE   rG   x24pg24pr   s      r   crc24pr      r   r   l        c                 l    d}| D ]
  }|dz  |z   } t        |      }t        |t        z  t        z        S r   )rE   rG   x32pg32pr   s      r   crc32pr      r   r   l            c                 l    d}| D ]
  }|dz  |z   } t        |      }t        |t        z  t        z        S r   )rE   rG   x64pg64apr   s      r   crc64apr      :    	AcEAI QAqve|r   c                 l    d}| D ]
  }|dz  |z   } t        |      }t        |t        z  t        z        S r   )rE   rG   r   g64bpr   s      r   crc64bpr      r   r   c                       e Zd ZddgZeddfdgeddfdgeddfdgeddfd	geddfd
geddfdgeddfdgeddfdgeddfdgeddfdgeddfdgeddfdgedddfdggZd Z	y)KnownAnswerTests   T   CatMouse987654321r   )      r   r   )O      )r   b   )iq  iV  )i&  in  )i  i  )iļ i )iY i7
 )i+ i# )iݓki    )iA    ho )ill   H )   `z| XKc                    | j                   D ]  \  }}t        | }| j                   |dd      dd|d       t        | j                        D ]  \  }}| j                   ||      ||   d|d|d       | j                   ||dd   ||d d             ||   d|d|d       | j                   ||dd   ||d d             ||   d|d|d         y )	Nr   r   z Wrong answer for CRC parameters z
, input ''	, input ''r2   r   )known_answersr   assertEqual	enumeratetest_messages)rI   crcfun_paramsrs   crcfunr   msgs         r   test_known_answersz#KnownAnswerTests.test_known_answers  s     $ 2 2M1}-FVC]Aan/pq#D$6$673  adhuvy4z{  ABBQ!A1Q4  ~K  LO  JP  Q  BC&Sb2B!CQqT  @M  NQ  LR  S 8 !3r   N)
r   r   r   r   g8g16g24g32r   r   r   r   r   r   r     s    M a( 5Ab) 5Aa( 5Aq) 7Ar!* 7Aq) 7Aq) 9Ar!* 9Aq) 9Aq) ;Az!
 ;Aq) ;Aq:
 ;AM Sr   r   c                   ~    e Zd Zg dZeddfegeddfegeddfe	ge
ddfegeddfegeddfeggZedd       Zd Zd Zy)CompareReferenceCrcTest)r   r   	   123456789r   r   c                 z    |dkD  rt        |dz        }|dz  }t        j                  | |      }t        |      dz  S )zThis function modifies the return value of binascii.crc32
        to be an unsigned 32-bit value. I.e. in the range 0 to 2**32-1.ii   r   )rG   binasciicrc32)r   crcrc   s      r   reference_crc32z'CompareReferenceCrcTest.reference_crc322  sC    
 C*$%Ak/CNN1S!1v
""r   c                     t        t        ddd      }| j                  D ])  }| j                   ||      | j	                  |             + y)a  The binascii module has a 32-bit CRC function that is used in a wide range
        of applications including the checksum used in the ZIP file format.
        This test compares the CRC-32 implementation of this crcmod module to
        that of binascii.crc32.r   r   r   N)r   r   r   r   r   )rI   r   r   s      r   test_compare_crc32z*CompareReferenceCrcTest.test_compare_crc32=  sC     Qq,%%CU3Z)=)=c)BC &r   c                     | j                   D ]<  \  }}t        | }| j                  D ]   }| j                   ||       ||             " > y)z]Compare various CRCs of this crcmod module to a pure
        polynomial-based implementation.N)test_poly_crcsr   r   r   )rI   r   crc_poly_funr   r   s        r   test_compare_polyz)CompareReferenceCrcTest.test_compare_polyI  sN     ,0+>+>'M< }-F))  l3.?@ * ,?r   N)r   )r   r   r   r   r   r   r   r   r   r   r   r   g64ar   g64br   r   staticmethodr   r   r   r   r   r   r   r   !  s    M a("q)"q)"q)"!*"!*"N # #
D	Ar   r   c                        e Zd ZdZdZd Zd Zy)CrcClassTestzVerify the Crc classr   c                    t        t              }d}| j                  t        |      |       | j                  |j	                         d       | j                  |j                         d       |j                  | j                         | j                  |j                  d       | j                  |j	                         d       | j                  |j                         d       |j                         }| j                  ||u       d}| j                  t        |      |       | j                  t        |      |       y)	z*Verify the CRC class when not using xorOutzcpoly = 0x104C11DB7
reverse = True
initCrc  = 0xFFFFFFFF
xorOut   = 0x00000000
crcValue = 0xFFFFFFFFs   FFFFFFFFr   s    F7B400A7zcpoly = 0x104C11DB7
reverse = True
initCrc  = 0xFFFFFFFF
xorOut   = 0x00000000
crcValue = 0xF7B400A7N)r   r   r   strdigest	hexdigestupdater   crcValuecopy
assertTrue)rI   r   str_reprc   s       r   test_simple_crc32_classz$CrcClassTest.test_simple_crc32_classZ  s    #h 	 	S7+':;*5

488z2':;*5 HHJ% 	 	S7+Q)r   c                 ,   t        t        dd      }d}| j                  t        |      |       | j                  |j	                         d       | j                  |j                         d       |j                  | j                         | j                  |j                  d       | j                  |j	                         d       | j                  |j                         d	       |j                         }| j                  ||u       d
}| j                  t        |      |       | j                  t        |      |       |j                         }| j                  ||u       | j                  ||u       d}| j                  t        |      |       y)z&Verify the CRC class when using xorOutr   r   )initCrcxorOutzcpoly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x00000000s       00000000r   s   KX084BFF58zcpoly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x084BFF58N)r   r   r   r   r   r   r   r   r   r   r   new)rI   r   r   rc   ys        r   test_full_crc32_classz"CrcClassTest.test_full_crc32_classy  sN    #q"- 	 	S7+':;*5

488y1':;*5 HHJ% 	 	S7+Q) GGI%
# 	 	Q)r   N)r   r   r   r   r   r   r   r   r   r   r   r   U  s    
C*>**r   r   c                   F    e Zd ZdZg dZddgddgddggZd	 Zd
 Zd Zd Z	y)PredefinedCrcTestzVerify the predefined CRCs)r   r   r   crc-aug-ccitt)i  i  i7V  x-25)r   i  i
  crc-32)r   r   r   c                    | j                   D ]  \  }}t        |      }| j                   |dd      dd|z         t        | j                        D ]  \  }}| j                   ||      ||   d|d|d       | j                   ||dd   ||d d             ||   d|d|d       | j                   ||dd   ||d d             ||   d|d|d         y )	Nr   r   z#Wrong answer for CRC '%s', input ''zWrong answer for CRC r   r   r2   r   )r   r	   r   r   test_messages_for_known_answers)rI   crcfun_namers   r   r   r   s         r   r   z$PredefinedCrcTest.test_known_answers  s    "00NK'4FVC]A/TWb/bc#D$H$HI3  ad]hil4mn  ABBQ!A1Q4r}  B  JC  D  BC&Sb2B!CQqTt  AD  LE  F J 1r   c                 n   | j                   D ]%  \  }}t        | j                        D ]  \  }}t        |      }|j	                  |       | j                  |j                  ||   d|d|d       |j                         }| j                  |j                  ||   d|d|d       | j                  |j                  |d   d|d|d       |j	                  |       | j                  |j                  ||   d|d|d       | j                  |j                  ||   d|d|d       	 ( y )NzWrong answer for crc1 r   r   zWrong state for crc1 r   zWrong state for crc2 )r   r   r   r   r   r   r   r   )rI   r   rs   r   r   crc1crc2s          r   test_class_with_known_answersz/PredefinedCrcTest.test_class_with_known_answers  s   "00NK#D$H$HI3$[1C   !`klo6pqxxz  !_jkn6op   !_jkn6opC   !_jkn6op  !_jkn6op# J 1r   c                 ~    t         D ]4  }t        |d         } |d      }| j                  ||d   d|d   z         6 y Nnamer   checkzWrong answer for CRC '%s')_predefined_crc_definitionsr	   r   )rI   table_entrycrc_func
calc_values       r   test_function_predefined_tablez0PredefinedCrcTest.test_function_predefined_table  sI    6K)+f*=>H!,/JZW)=?Z]hio]p?pq	 7r   c                     t         D ]G  }t        |d         }|j                  d       | j                  |j                  |d   d|d   z         I y r   )r  r   r   r   r   )rI   r  r   s      r   test_class_predefined_tablez-PredefinedCrcTest.test_class_predefined_table  sO    6K V!45DKK%T]]K,@B]`klr`sBst	 7r   N)
r   r   r   r   r   r   r   r   r  r  r   r   r   r   r     sI    $'# BL
BL
FLMFq*rur   r   c                   6    e Zd ZdZdZg dZg dZd Zd Zd Z	y)	InputTypesTestz<Check the various input types that CRC functions can accept.r   )r   r   r   )BHILc                     | j                   D ]m  }t        |      }t        t        | j                        dz         D ]<  }| j                  d| } ||      } |t        |            }| j                  ||       > o y)zmTest that bytearray inputs are accepted, as an example
        of a type that implements the buffer protocol.r   N)check_crc_namesr	   rangelenr   	bytearrayr   )rI   crc_namer   r   test_msgbytes_answerbytearray_answers          r   test_bytearray_inputz#InputTypesTest.test_bytearray_input  st     ,,H'1F3txx=1,-88BQ<%h/#))H*=#>   /?@	 . -r   c                 \   | j                   D ]  }t        |      }t        t        | j                        dz         D ]l  }| j                  d| } ||      }| j
                  D ]D  }|t        |      j                  z  dk(  st        ||      } ||      }| j                  ||       F n  y)ziTest that array inputs are accepted, as an example
        of a type that implements the buffer protocol.r   Nr   )	r  r	   r  r  r   array_check_typesr   itemsizer   )	rI   r  r   r   r  r  
array_type
test_arrayarray_answers	            r   test_array_inputzInputTypesTest.test_array_input  s     ,,H'1F3txx=1,-88BQ<%h/"&"8"8J5,555:%*:x%@
'-j'9((|D	 #9 . -r   c                     | j                   D ]3  }t        |      }| j                  t              5   |d       ddd       5 y# 1 sw Y   @xY w)z(Test that Unicode input raises TypeError	123456789N)r  r	   assertRaises	TypeError)rI   r  r   s      r   test_unicode_inputz!InputTypesTest.test_unicode_input  sA    ,,H'1F""9-{# .- ---s   	AA	N)
r   r   r   r   r   r  r  r  r  r#  r   r   r   r	  r	    s*    F
CO	AE$r   r	  c                  `    t        dt               t                t        j                          y )NzUsing extension:)printr   unittestmainr   r   r   runtestsr(    s    	
o.	GMMOr   __main__).r   r&  r   r   crcmodr   r   r   
predefinedr   r	   r
   r  r   r   r   r   r   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   TestCaser   r   r   r   r	  r(  r   r   r   r   <module>r-     s  . *    ! # % * G 
  
       m mh 2h
4j CyE{ CyE{ CyE{ 	T
E{ 	T
Sx(( S@1Ah// 1AhN*8$$ N*b9u)) 9ux0$X&& 0$f zJ r   