o
    +ke                     @   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)expected_fdexpected_dataselffddata r   9usr/lib/python3.10/site-packages/borg/testsuite/remote.pyexpect_write   s   
z)TestSleepingBandwidthLimiter.expect_writec                 C   s$   || j ksJ || jksJ t|S r   )r   r   lenr   r   r   r   check_write   s   z(TestSleepingBandwidthLimiter.check_writec                 C   s4   | td| j td}| dd |dd d S )Nwriter         test)setattrosr   r   r   r   r   monkeypatchitr   r   r   test_write_unlimited   s   z1TestSleepingBandwidthLimiter.test_write_unlimitedc                    s   | td| j | td fdd | tddd  d td}| dd	 |dd	 | dd
 |dd | dd
 |dd
  d7  | dd |dd  d7  | dd |dd  d7  | dd |dd d S )Nr   	monotonicc                      s    S r   r   r   nowr   r   <lambda>%       z9TestSleepingBandwidthLimiter.test_write.<locals>.<lambda>sleepc                 S      d S r   r   )xr   r   r   r'   &   r(   d   r   r   s   123456s
   1234567890
      1)r   r   r   timer   r   r   r    r   r%   r   
test_write#   s(   z'TestSleepingBandwidthLimiter.test_writeN)__name__
__module____qualname__r   r   r#   r0   r   r   r   r   r      s
    r   c                   @   s   e Zd Zejdd Zejdd ZdefddZdefdd	Z	def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defddZdS )TestRepositoryCachec                 c   s    t jt|d| _t| jddd&}|tdd |tdd |tdtd	 |V  W d    d S 1 s<w   Y  d S )
N
repositoryT)Z	exclusivecreater      1234r      5678   r,   )	r   pathjoinstrZrepository_locationr   putr   bytes)r   Ztmpdirr5   r   r   r   r5   I   s   "zTestRepositoryCache.repositoryc                 C   s   t |S r   )r   )r   r5   r   r   r   cacheR   s   zTestRepositoryCache.cacher?   c                 C   s   | tddksJ |jdksJ |jdksJ t|tdgdgks(J |jdks/J |jdks6J t|tdgdgksEJ |jdksLJ |jdksSJ | tddks^J |jdkseJ |jdkslJ d S )Nr   r7   r   r   )getr   misseshitslistget_many)r   r?   r   r   r   test_simpleV   s   zTestRepositoryCache.test_simplec                    s
   fdd}t  tdtdgddgksJ  jdks J  jdks'J  tdtdtdg}t|dks=J  j}| _   | _ jdksQJ td jvsZJ td jvscJ t|t	d	ksmJ  j
dkstJ t|dks|J  j
dksJ d S )
Nc                      s
   d _ d S )Nr   )Z
size_limitr   r?   r   r   query_size_limiti   s   
z:TestRepositoryCache.test_backoff.<locals>.query_size_limitr   r   r7   r8   r   r9   r,   )rC   rD   r   rA   	evictionsnextrG   Zbackoffr?   r>   Zslow_misses)r   r?   rG   iteratorZqslr   rF   r   test_backoffh   s"   &z TestRepositoryCache.test_backoffc                 C   s   G dd d}| tdtdtdg}t|dksJ td| t|dks+J |jdks2J |jd	ks9J W d    n1 sCw   Y  t|td
ksRJ 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   r*   r   r   )r   argsr   r   r   __init__      z=TestRepositoryCache.test_enospc.<locals>.enospc_open.__init__c                 S   s   | S r   r   )r   r   r   r   	__enter__   rN   z>TestRepositoryCache.test_enospc.<locals>.enospc_open.__enter__c                 S   r*   r   r   )r   exc_typeZexc_valZexc_tbr   r   r   __exit__   rN   z=TestRepositoryCache.test_enospc.<locals>.enospc_open.__exit__c                 S   s   t tjd)NZfoo)OSErrorerrnoENOSPC)r   r   r   r   r   r      s   z:TestRepositoryCache.test_enospc.<locals>.enospc_open.writeNc                 S   r*   r   r   )r   nr   r   r   truncate   rN   z=TestRepositoryCache.test_enospc.<locals>.enospc_open.truncater   )r1   r2   r3   rM   rO   rQ   r   rV   r   r   r   r   enospc_open   s    rW   r   r   r9   r7   zbuiltins.openr8   r   r,   )rD   r   rI   r   ZenospcrH   r>   )r   r?   rW   rJ   r   r   r   test_enospc   s   zTestRepositoryCache.test_enospcc                 C   s,   | dd t|t }tdj|_|S )NZBORG_PASSPHRASEtestnone)Zsetenvr   r6   r   ZMockArgsr	   Z
compressor)r   r5   r!   keyr   r   r   r[      s   zTestRepositoryCache.keyc                 C   s    | |}|||| |S r   )Zid_hashr=   Zencrypt)r   r[   r5   r   Zid_r   r   r   _put_encrypted_object   s   
z)TestRepositoryCache._put_encrypted_objectc                 C      |  ||dS )Nr7   r\   r   r[   r5   r   r   r   H1      zTestRepositoryCache.H1c                 C   r]   )Nr8   r^   r_   r   r   r   H2   ra   zTestRepositoryCache.H2c                 C   s   |  ||tdS )Nr,   )r\   r>   r_   r   r   r   H3   s   zTestRepositoryCache.H3c                 C   s   t ||ddS )NT)decrypted_cacheZforce_cache)r   r_   r   r   r   rd      ra   z#TestRepositoryCache.decrypted_cacherd   c                 C   s   t ||||g ||||g}t|dksJ t||d-}|dtj t	|
 ddA dd}|dtj || |  W d    n1 sRw   Y  tt t|dkseJ W d    d S 1 spw   Y  d S )N)   r7   za+blittler   r   )re   r8   )rC   rD   rI   openZkey_filenameseekioSEEK_ENDint
from_bytesreadto_bytesr   rV   pytestraisesr
   )r   rd   r`   rb   rc   rJ   r   Z	corruptedr   r   r   test_cache_corruption   s   

"z)TestRepositoryCache.test_cache_corruptionN)r1   r2   r3   rp   Zfixturer5   r?   r   rE   rK   rX   r[   r\   r`   rb   rc   rd   rr   r   r   r   r   r4   H   s(    






r4   )rS   r   rj   r/   Zunittest.mockr   rp   Zremoter   r   r   r5   r   Z
crypto.keyr   compressr	   helpersr
   Z	hashindexr   r[   r   r   r4   r   r   r   r   <module>   s    6