o
    i                     @   s  d Z ddlZddlmZ ddlmZ ddlZzddlZeej	j
dddd ZW n ey5   dZdZY nw dd	lmZ dd
lmZmZmZ ddlmZmZ eeejd d ZdZdZddddZddddddZe Ze e ej!dd Z"dd Z#dd Z$dd  Z%ej&j'd!e(e) e(e* d"d#d$ Z+ej&j'd!e(e) e(e* d"d%d& Z,ej&j'd!e(e) e(e* d"d'd( Z-d)d* Z.ej&j'd!e(e) e(e* d"d+d, Z/dS )-z3
Test the hash calculation and checking functions.
    N)Path)NamedTemporaryFile.   )maxsplit   )Pooch)make_registry	file_hashhash_matches)check_tiny_datamirror_directorydatastorezOtiny-data.txt baee0894dba14b12085eacb204284b97e362f4f3e5a5807693cc90ef415c1b2d
zsubdir/tiny-data.txt baee0894dba14b12085eacb204284b97e362f4f3e5a5807693cc90ef415c1b2d
tiny-data.txt baee0894dba14b12085eacb204284b97e362f4f3e5a5807693cc90ef415c1b2d
Z(c03148994acd89317915ea2f2d080d6dd127aa09Z@baee0894dba14b12085eacb204284b97e362f4f3e5a5807693cc90ef415c1b2dZ 70e2afd3fd7e336ae478b1e740a5f08e)sha1sha256md5Zf843815fe57948faZ98d6f1a2Z 0267d220db258fffb0c567c0ecd1b689Z811e3f2a12aec53f)xxh64xxh32Zxxh128Zxxh3_128Zxxh3_64c                 C   s
   t t| S )z
    Mirror the test data folder on a temporary directory. Needed to avoid
    permission errors when pooch is installed on a non-writable path.
    )r   DATA_DIR)Ztmp_path r   f/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/pooch/tests/test_hashes.pydata_dir_mirror:   s   
r   c              	   C   s   t dd}|  zTt| |jdd t|jdd}| }W d   n1 s(w   Y  |tks3J t| di d}||j t	| d	 }|
d	}||ksQJ t| W t|j dS t|j w )
zGCheck that the registry builder creates the right file names and hashesFdelete	recursiveutf-8encodingNsome bogus URLpathbase_urlregistrytiny-data.txt)r   closer	   nameopenreadREGISTRYr   load_registrystrfetchr   osremove)r   outfilefoutr$   puptruefnamer   r   r   test_make_registryC   s   



r5   c              	   C   s   t dd}|  zkt| |jdd t|jdd}| }W d   n1 s(w   Y  |tks3J t| di d	}||j t	| d
 |
d
ksMJ t|
d
 t	| d d
 }||
dkseJ t|
d W t|j dS t|j w )z7Check that the registry builder works in recursive modeFr   Tr   r   r   Nr    r!   r%   subdirzsubdir/tiny-data.txt)r   r&   r	   r'   r(   r)   REGISTRY_RECURSIVEr   r+   r,   r-   r   r.   r/   )r   r0   r1   r$   r2   r3   r   r   r   test_make_registry_recursiveX   s    

r8   c                  C   sL   t t} tddd W d   n1 sw   Y  dt| jv s$J dS )z!Test an invalid hashing algorithmZ	somethingZblah)r4   algNz'blah')pytestraises
ValueErrorr
   r,   value)excr   r   r    test_file_hash_invalid_algorithmn   s   r?   zalg,expected_hash)Zidsc                 C   sf   |  drtdu rtd | dvrtdk rtd tjtd}t	| t
|| }||ks1J dS )z2Test the hash calculation using hashlib and xxhashxxhNrequires xxhashr   r   r   requires xxhash > 2.0r%   )
startswithxxhashr:   skipXXHASH_MAJOR_VERSIONr.   r"   joinr   r   r
   )r9   expected_hashr4   Zreturned_hashr   r   r   test_file_hashu   s   



rJ   c                 C   s   |  drtdu rtd | dvrtdk rtd tjtd}t	| |  d| }t
||s4J |  d	}t
||r@J dS )
z*Make sure the hash checking function worksr@   NrA   rB   r   rC   r%   ::blablablabla)rD   rE   r:   rF   rG   r.   r"   rH   r   r   r   )r9   rI   r4   
known_hashr   r   r   test_hash_matches   s   



rN   c                 C   s   t jtd}t| |  d| }t||ddsJ |  d}tt}t||ddd W d   n1 s8w   Y  dt	|j
v sFJ tt}t||ddd W d   n1 s^w   Y  |t	|j
v slJ dS )	zBMake sure the hash checking function raises an exception if strictr%   rK   TstrictrL   	NeverlandrP   sourceN)r.   r"   rH   r   r   r   r:   r;   r<   r,   r=   )r9   rI   r4   rM   Zbad_hasherrorr   r   r   test_hash_matches_strict   s   
rU   c                  C   sD   t jtd} t| ddsJ tdddsJ t| ddds J dS )zHThe hash checking function should always returns True if known_hash=Noner%   N)rM    )r4   rM   T)rM   rP   )r.   r"   rH   r   r   )r4   r   r   r   test_hash_matches_none   s   rW   c                 C   s   t jtd}t| |  d|  }t||ddsJ tt	}t||dd ddd W d   n1 s9w   Y  dt
|jv sGJ dS )	z:Hash matching should be independent of upper or lower caser%   rK   TrO   NrQ   rR   )r.   r"   rH   r   r   upperr   r:   r;   r<   r,   r=   )r9   rI   r4   rM   rT   r   r   r   test_hash_matches_uppercase   s   rZ   )0__doc__r.   pathlibr   tempfiler   r:   rE   intVERSIONsplitrG   ImportErrorcorer   hashesr	   r
   r   utilsr   r   r,   __file__parentr   r*   r7   ZTINY_DATA_HASHES_HASHLIBZTINY_DATA_HASHES_XXHcopyZTINY_DATA_HASHESupdateZfixturer   r5   r8   r?   markZparametrizelistitemskeysrJ   rN   rU   rW   rZ   r   r   r   r   <module>   s|   













