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
(c03148994acd89317915ea2f2d080d6dd127aa09@baee0894dba14b12085eacb204284b97e362f4f3e5a5807693cc90ef415c1b2d 70e2afd3fd7e336ae478b1e740a5f08e)sha1sha256md5f843815fe57948fa98d6f1a2 0267d220db258fffb0c567c0ecd1b689811e3f2a12aec53f)xxh64xxh32xxh128xxh3_128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)tmp_path r!   M/home/ubuntu/vllm_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   



r@   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,   r0   subdirzsubdir/tiny-data.txt)r   r1   r	   r2   r3   r4   REGISTRY_RECURSIVEr   r6   r7   r8   r   r9   r:   )r#   r;   r<   r/   r=   r>   r!   r!   r"   test_make_registry_recursiveX   s    

rC   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 algorithm	somethingblah)r?   algNz'blah')pytestraises
ValueErrorr
   r7   value)excr!   r!   r"    test_file_hash_invalid_algorithmn   s   rL   zalg,expected_hash)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.0r0   )
startswithxxhashrG   skipXXHASH_MAJOR_VERSIONr9   r-   joinr   r   r
   )rF   expected_hashr?   returned_hashr!   r!   r"   test_file_hashu   s   



rY   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 worksrN   NrO   rP   r   rQ   r0   ::blablablabla)rR   rS   rG   rT   rU   r9   r-   rV   r   r   r   )rF   rW   r?   
known_hashr!   r!   r"   test_hash_matches   s   



r]   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 strictr0   rZ   Tstrictr[   	Neverlandr_   sourceN)r9   r-   rV   r   r   r   rG   rH   rI   r7   rJ   )rF   rW   r?   r\   bad_hasherrorr!   r!   r"   test_hash_matches_strict   s   
re   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=Noner0   N)r\    )r?   r\   T)r\   r_   )r9   r-   rV   r   r   )r?   r!   r!   r"   test_hash_matches_none   s   rg   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 caser0   rZ   Tr^   Nr`   ra   )r9   r-   rV   r   r   upperr   rG   rH   rI   r7   rJ   )rF   rW   r?   r\   rd   r!   r!   r"   test_hash_matches_uppercase   s   rj   )0__doc__r9   pathlibr   tempfiler   rG   rS   intVERSIONsplitrU   ImportErrorcorer   hashesr	   r
   r   utilsr   r   r7   __file__parentr   r5   rB   TINY_DATA_HASHES_HASHLIBTINY_DATA_HASHES_XXHcopyTINY_DATA_HASHESupdatefixturer#   r@   rC   rL   markparametrizelistitemskeysrY   r]   re   rg   rj   r!   r!   r!   r"   <module>   s|   













