
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
yb                 @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlZddlmZ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	d
lmZ d	dlmZ G dd dZG dd dZdS )    N)patch   )SleepingBandwidthLimiterRepositoryCachecache_if_remote)
Repository)PlaintextKey)CompressionSpec)IntegrityError   )H)TestKeyc               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestSleepingBandwidthLimiterc             C   s   || _ || _d S )N)Zexpected_fdZexpected_data)selffddata r   /usr/lib64/python3.6/remote.pyexpect_write   s    z)TestSleepingBandwidthLimiter.expect_writec             C   s   t |S )N)len)r   r   r   r   r   r   check_write   s    z(TestSleepingBandwidthLimiter.check_writec             C   s4   |j td| j td}| jdd |jdd d S )Nwriter      s   test)setattrosr   r   r   r   )r   monkeypatchitr   r   r   test_write_unlimited   s    z1TestSleepingBandwidthLimiter.test_write_unlimitedc                s   |j td| j |j td fdd |j tddd  d td}| jdd	 |jdd	 | jdd
 |jdd | jdd
 |jdd
  d7  | jdd |jdd  d7  | jdd |jdd  d7  | jdd |jdd d S )Nr   Z	monotonicc                  s    S )Nr   r   )nowr   r   <lambda>%   s    z9TestSleepingBandwidthLimiter.test_write.<locals>.<lambda>Zsleepc             S   s   d S )Nr   )xr   r   r   r   &   s    d   r   s   tests   123456s
   1234567890
      1)r   r   r   timer   r   r   )r   r   r   r   )r   r   
test_write#   s(    z'TestSleepingBandwidthLimiter.test_writeN)__name__
__module____qualname__r   r   r   r%   r   r   r   r   r      s   r   c               @   s   e Zd Zejdd Zejdd ZedddZeddd	Z	edd
dZ
ejdd Zdd Zejdd Zejdd Zejdd Zejdd ZedddZdS )TestRepositoryCachec             c   sn   t jjt|d| _t| jddd@}|jtdd |jtdd |jtdtd	 |V  W d Q R X d S )
N
repositoryT)Z	exclusivecreater   s   1234r   s   5678   r!   )	r   pathjoinstrZrepository_locationr   putr   bytes)r   Ztmpdirr*   r   r   r   r*   I   s    zTestRepositoryCache.repositoryc             C   s   t |S )N)r   )r   r*   r   r   r   cacheR   s    zTestRepositoryCache.cache)r2   c             C   s   d S )Nr   )r   r2   r   r   r   test_simpleV   s    zTestRepositoryCache.test_simplec                sF    fdd} j tdtdtdg} j}| _ j  | _d S )Nc                  s
   d _ d S )Nr   )Z
size_limitr   )r2   r   r   query_size_limiti   s    z:TestRepositoryCache.test_backoff.<locals>.query_size_limitr   r,   r   )get_manyr   r4   Zbackoff)r   r2   r4   iteratorZqslr   )r2   r   test_backoffh   s    
z TestRepositoryCache.test_backoffc          	   C   sD   G dd d}|j tdtdtdg}td| W d Q R X d S )Nc               @   s6   e Zd Zdd Zdd Zdd Zdd Zdd
dZd	S )z4TestRepositoryCache.test_enospc.<locals>.enospc_openc             W   s   d S )Nr   )r   argsr   r   r   __init__   s    z=TestRepositoryCache.test_enospc.<locals>.enospc_open.__init__c             S   s   | S )Nr   )r   r   r   r   	__enter__   s    z>TestRepositoryCache.test_enospc.<locals>.enospc_open.__enter__c             S   s   d S )Nr   )r   exc_typeZexc_valZexc_tbr   r   r   __exit__   s    z=TestRepositoryCache.test_enospc.<locals>.enospc_open.__exit__c             S   s   t tjdd S )NZfoo)OSErrorerrnoZENOSPC)r   r   r   r   r   r      s    z:TestRepositoryCache.test_enospc.<locals>.enospc_open.writeNc             S   s   d S )Nr   )r   nr   r   r   truncate   s    z=TestRepositoryCache.test_enospc.<locals>.enospc_open.truncate)N)r&   r'   r(   r9   r:   r<   r   r@   r   r   r   r   enospc_open   s
   rA   r   r   r,   zbuiltins.open)r5   r   r   )r   r2   rA   r6   r   r   r   test_enospc   s
    
zTestRepositoryCache.test_enospcc             C   s,   |j dd tj|tj }tdj|_|S )NZBORG_PASSPHRASEZtestZnone)Zsetenvr   r+   r   ZMockArgsr	   Z
compressor)r   r*   r   keyr   r   r   rC      s    zTestRepositoryCache.keyc             C   s    |j |}|j||j| |S )N)Zid_hashr0   Zencrypt)r   rC   r*   r   Zid_r   r   r   _put_encrypted_object   s    
z)TestRepositoryCache._put_encrypted_objectc             C   s   | j ||dS )Ns   1234)rD   )r   rC   r*   r   r   r   H1   s    zTestRepositoryCache.H1c             C   s   | j ||dS )Ns   5678)rD   )r   rC   r*   r   r   r   H2   s    zTestRepositoryCache.H2c             C   s   | j ||tdS )Nr!   )rD   r1   )r   rC   r*   r   r   r   H3   s    zTestRepositoryCache.H3c             C   s   t ||ddS )NT)decrypted_cacheZforce_cache)r   )r   rC   r*   r   r   r   rH      s    z#TestRepositoryCache.decrypted_cache)rH   c             C   s   t |j|||g |j|||g}t|j|dP}|jdtj tj|j	 ddA j
dd}|jdtj |j| |j  W d Q R X tjt W d Q R X d S )Nza+br   littler   rJ   )listr5   openZkey_filenameseekioSEEK_ENDint
from_bytesreadto_bytesr   r@   pytestZraisesr
   )r   rH   rE   rF   rG   r6   r   Z	corruptedr   r   r   test_cache_corruption   s    
z)TestRepositoryCache.test_cache_corruptionN)r&   r'   r(   rT   Zfixturer*   r2   r   r3   r7   rB   rC   rD   rE   rF   rG   rH   rU   r   r   r   r   r)   H   s   	r)   )r>   r   rN   r$   Zunittest.mockr   rT   Zremoter   r   r   r*   r   Z
crypto.keyr   compressr	   Zhelpersr
   Z	hashindexr   rC   r   r   r)   r   r   r   r   <module>   s   6