U
    ?h                     @   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k
rl   dZdZY nX 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   I/var/www/html/venv/lib/python3.8/site-packages/pooch/tests/test_hashes.pydata_dir_mirror:   s    r   c              	   C   s   t dd}|  zt| |jdd t|jdd}| }W 5 Q R X |tksRt	t
| di d}||j t| d }|d}||kst	t| W 5 t|j X d	S )
zGCheck that the registry builder creates the right file names and hashesFdelete	recursiveutf-8encodingsome bogus URLpathbase_urlregistrytiny-data.txtN)r   closeosremovenamer	   openreadREGISTRYAssertionErrorr   load_registrystrfetchr   )r   outfilefoutr$   puptruefnamer   r   r   test_make_registryC   s    

r6   c              	   C   s   t dd}|  zt| |jdd t|jdd}| }W 5 Q R X |tksRt	t
| di d}||j t| d	 |d	kst	t|d	 t| d
 d	 }||dkst	t|d W 5 t|j X dS )z7Check that the registry builder works in recursive modeFr   Tr   r   r   r    r!   r%   subdirzsubdir/tiny-data.txtN)r   r&   r'   r(   r)   r	   r*   r+   REGISTRY_RECURSIVEr-   r   r.   r/   r0   r   )r   r1   r2   r$   r3   r4   r   r   r   test_make_registry_recursiveX   s    
r9   c               	   C   s8   t t} tddd W 5 Q R X dt| jks4tdS )z!Test an invalid hashing algorithmZ	somethingZblah)r5   algz'blah'N)pytestraises
ValueErrorr
   r/   valuer-   )excr   r   r    test_file_hash_invalid_algorithmn   s    r@   zalg,expected_hash)Zidsc                 C   sf   |  dr6tdkrtd | dkr6tdk r6td tjtd}t	| t
|| }||ksbt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
   r-   )r:   expected_hashr5   Zreturned_hashr   r   r   test_file_hashu   s    



rK   c                 C   s   |  dr6tdkrtd | dkr6tdk r6td tjtd}t	| |  d| }t
||sht|  d	}t
||rtdS )
z*Make sure the hash checking function worksrA   NrB   rC   r   rD   r%   ::blablablabla)rE   rF   r;   rG   rH   r'   r"   rI   r   r   r   r-   )r:   rJ   r5   
known_hashr   r   r   test_hash_matches   s    



rO   c              	   C   s   t jtd}t| |  d| }t||dds6t|  d}tt	}t||ddd W 5 Q R X dt
|jksxttt	}t||ddd W 5 Q R X |t
|jkstdS )	zBMake sure the hash checking function raises an exception if strictr%   rL   TstrictrM   	NeverlandrQ   sourceN)r'   r"   rI   r   r   r   r-   r;   r<   r=   r/   r>   )r:   rJ   r5   rN   Zbad_hasherrorr   r   r   test_hash_matches_strict   s    
rV   c                  C   sD   t jtd} t| ddsttddds.tt| ddds@tdS )zHThe hash checking function should always returns True if known_hash=Noner%   N)rN    )r5   rN   T)rN   rQ   )r'   r"   rI   r   r   r-   )r5   r   r   r   test_hash_matches_none   s    rX   c              	   C   s~   t jtd}t| |  d|  }t||dds:tt	t
}t||dd ddd W 5 Q R X dt|jksztdS )	z:Hash matching should be independent of upper or lower caser%   rL   TrP   NrR   rS   )r'   r"   rI   r   r   upperr   r-   r;   r<   r=   r/   r>   )r:   rJ   r5   rN   rU   r   r   r   test_hash_matches_uppercase   s    "r[   )0__doc__r'   pathlibr   tempfiler   r;   rF   intVERSIONsplitrH   ImportErrorcorer   hashesr	   r
   r   utilsr   r   r/   __file__parentr   r,   r8   ZTINY_DATA_HASHES_HASHLIBZTINY_DATA_HASHES_XXHcopyZTINY_DATA_HASHESupdateZfixturer   r6   r9   r@   markZparametrizelistitemskeysrK   rO   rV   rX   r[   r   r   r   r   <module>   sx   














