
    \dE                         d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ  G d de          Z G d dej                  Z G d	 d
e          ZdS )    N)skipIf)logfileruntime)TestCasec                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Z eej                                        d          d             Zd Zd Zd Zd ZdS )LogFileTestsz%
    Test the rotating log file.
    c                     |                                  | _        t          j        | j                   d| _        t          j                            | j        | j                  | _        d S )Nztest.logmktempdirosmakedirsnamepathjoinselfs    9lib/python3.11/site-packages/twisted/test/test_logfile.pysetUpzLogFileTests.setUp   sI    ;;==
DH	GLL4955			    c                     t          j        | j        d           t           j                            | j                  rt          j        | j        d           dS dS )z
        Restore back write rights on created paths: if tests modified the
        rights, that will allow the paths to be removed easily afterwards.
          N)r   chmodr   r   existsr   s    r   tearDownzLogFileTests.tearDown   sT    
 	5!!!7>>$)$$ 	'HTY&&&&&	' 	'r   c                     t          j        | j        | j                  }|                     |j                   |                     t          |j                   dS )zf
        L{BaseLogFile.shouldRotate} is abstract and must be implemented by
        subclass.
        N)	r   BaseLogFiler   r   
addCleanupcloseassertRaisesNotImplementedErrorshouldRotater   logs     r   test_abstractShouldRotatez&LogFileTests.test_abstractShouldRotate$   sN    
 !$)TX66	"""-s/?@@@@@r   c                    t          j        t          j        | j        | j                            5 }|                    d           |                    d           |                                 |                    d           ddd           n# 1 swxY w Y   t          | j	                  5 }| 
                    |                                d           ddd           dS # 1 swxY w Y   dS )zn
        Log files can be written to, flushed and closed. Closing a log file
        also flushes it.
        1234567890N
1234567890)
contextlibclosingr   LogFiler   r   writeflushopenr   assertEqualreadr   r$   fs      r   test_writingzLogFileTests.test_writing-   sI   
 	48 D DEE 	IIeIIeIIKKKIIf		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 $)__ 	5QVVXX|444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s$   ABBB1)C''C+.C+c                    t          j        t          j        | j        | j        d                    5 }|                    d           |                    d           |                    d           |                     t          j	        
                    | j	         d                     |                     t          j	        
                    | j	         d                     |                    d           |                     t          j	        
                    | j	         d                     |                     t          j	        
                    | j	         d                     |                     t          j	        
                    | j	         d	                     |                    d
           |                     t          j	        
                    | j	         d	                     |                                 |                     t          j	        
                    | j	         d	                     |                     t          j	        
                    | j	         d                     ddd           n# 1 swxY w Y   |                     |                                g d           dS )zq
        Rotating log files autorotate after a period of time, and can also be
        manually rotated.
        
   )rotateLengthr'   456789011111111111.1.2 .33.4N)         )r+   r,   r   r-   r   r   r.   
assertTruer   r   r   assertFalserotater1   listLogsr#   s     r   test_rotationzLogFileTests.test_rotation;   sq    ODItxbAAA
 
 	? IIeIIi   IIhOOBGNNdi+;+;+;<<===RW^^ty,<,<,<==>>>IIbMMMOOBGNNdi+;+;+;<<===OOBGNNdi+;+;+;<<===RW^^ty,<,<,<==>>>IIcNNNRW^^ty,<,<,<==>>> JJLLLOOBGNNdi+;+;+;<<===RW^^ty,<,<,<==>>>)	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?, 	33333s   IJJJc                    t          j        t          j        | j        | j                            5 }|                    d           ddd           n# 1 swxY w Y   t          j        | j        | j                  }|                     |j                   | 	                    |j
        d           | 	                    |j                                        |j
                   |                    d           | 	                    |j
        d           | 	                    |j                                        |j
                   |j        }|                    dd           | 	                    |                                d           dS )a  
        Log files can be written to, closed. Their size is the number of
        bytes written to them. Everything that was written to them can
        be read, even if the writing happened on separate occasions,
        and even if the log file was closed in between.
        
0123456789Nr7   abc   r   s   0123456789abc)r+   r,   r   r-   r   r   r.   r   r   r1   size_filetellseekr2   r3   s      r   test_appendzLogFileTests.test_appendY   s{    	48 D DEE 	$IIl###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ odi22	"""2&&&))38444		%2&&&))38444I	q!#344444s   AAAc                    t          j        | j        | j                  }|                     |j                   |                    d           |                    d           |                                 |                    d           |                                 | 	                    |
                                dg           t          j        |                                          5 }|j                            d           | 	                    |                                dg           | 	                    |                                g            ddd           n# 1 swxY w Y   t          j        |                    d                    5 }| 	                    |                                ddg           | 	                    |                                g            ddd           n# 1 swxY w Y   |                     t&          |j        d           |                     t(          |j        d           |                                 | 	                    |
                                ddg           t          j        |                    d                    5 }|j                            d           | 	                    |                                dg           | 	                    |                                g            ddd           n# 1 swxY w Y   t          j        |                    d                    5 }| 	                    |                                ddg           | 	                    |                                g            ddd           dS # 1 swxY w Y   dS )	a[  
        Various tests for log readers.

        First of all, log readers can get logs by number and read what
        was written to those log files. Getting nonexistent log files
        raises C{ValueError}. Using anything other than an integer
        index raises C{TypeError}. As logs get older, their log
        numbers increase.
        zabc
zdef
zghi
rA   r   NrB   1)r   r-   r   r   r   r   r.   rF   r/   r1   rG   r+   r,   getCurrentLogrN   rP   	readLinesgetLogr    
ValueError	TypeError)r   r$   readers      r   test_logReaderzLogFileTests.test_logReadern   s    odi22	"""		'		'

		'		 	!--- 1 1 3 344 	5La   V--//';;;V--//444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 

1.. 	5&V--//'71CDDDV--//444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5
 	*cj!444)SZ555 	

!Q000

1.. 	5&La   V--//';;;V--//444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 

1.. 	5&V--//'71CDDDV--//444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5sL   0A,E((E,/E,AG99G= G=+A,L##L'*L'AN55N9<N9c                    t          | j        d          5  	 ddd           n# 1 swxY w Y   t          j        | j                  }|                     |j                   |                     g |                    d                     dS )zB
        L{LogReader.readLines} supports reading no line.
        wNr   )r0   r   r   	LogReaderr   r   r1   rU   )r   rY   s     r   test_LogReaderReadsZeroLinez(LogFileTests.test_LogReaderReadsZeroLine   s    
 $)S!! 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 "49--%%%V--a0011111s   $((c                    t          | j        d                                           t          j        | j        d           t          j        | j                  t
          j                 }t          j        | j	        | j
                  }|                     |j                   |                    d           |                                 |                     |t          j        | j                  t
          j                            dS )zH
        Check rotated files have same permissions as original.
        r\     rK   N)r0   r   r   r   r   statST_MODEr   r-   r   r   r   r.   rF   r1   )r   moder$   s      r   test_modePreservationz"LogFileTests.test_modePreservation   s     	TY""$$$
E"""wty!!$,/odi22	"""		%

rwty11$,?@@@@@r   c                    t          j        | j        | j                  }|                     |j                   |                    d           t          j        | j        d           	 t          t          j
                            | j        d          d          }|                                 dS # t          $ r Y nw xY w|                                 |                    d           |                                 |j        }|                     |                                d           |                    dd           |                     |                                d	           dS )
zH
        Check it keeps working when permission on dir changes.
        rK   m  xxxr\   Ndef   r   s   abcdef)r   r-   r   r   r   r   r.   r   r   r0   r   r   OSErrorrF   r/   rN   r1   rO   rP   r2   r3   s      r   test_noPermissionzLogFileTests.test_noPermission   s9    odi22	"""		% 	5!!!	RW\\$(E22C88A GGIIIF	  	 	 	D	 	

		%		I1%%%	q!9-----s   *3B3 3
C ?C c                 |   t          j        | j        | j        dd          }|                     |j                   |                    d           |                    d           |                     t          j	        
                    | j	         d                     |                    d           |                     t          j	        
                    | j	         d                     |                    d	           |                     t          j	        
                    | j	         d
                     t          | j	         d
          5 }|                     |                                d           ddd           n# 1 swxY w Y   |                    d           t          | j	         d
          5 }|                     |                                d           ddd           n# 1 swxY w Y   |                     t          j	        
                    | j	         d                     dS )zl
        Test it respect the limit on the number of files when maxRotatedFiles
        is not None.
        r7   rC   )r8   maxRotatedFilesr:   22222222222r;   33333333333r<   44444444444r>   N55555555555r@   )r   r-   r   r   r   r   r.   rD   r   r   r   r0   r1   r2   rE   r   r$   fps      r   test_maxNumberOfLogz LogFileTests.test_maxNumberOfLog   sW   
 odiTUVVV	"""		(		($)'7'7'788999		($)'7'7'788999		($)'7'7'788999TY"""## 	2rRWWYY111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 			(TY"""## 	2rRWWYY111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	249(8(8(899:::::s$   )FFF)G77G;>G;c                 :   t          j        | j        | j        dd          }|                     |j                   t           j                            | j        dd          }|                     |j                   |                     |j        |j                   |                     t          j        
                    |j                  |j                   |                     |j        |j                   |                     |j        |j                   dS )z/
        Test the fromFullPath method.
        r7   r   defaultModeN)r   r-   r   r   r   r   fromFullPathr   r1   r   abspathr8   rw   )r   log1log2s      r   test_fromFullPathzLogFileTests.test_fromFullPath   s     ty$(BEJJJ
###++DIru+MM
###DI...33TY???*D,=>>>)4+;<<<<<r   c                    t          | j        d          5  t          j        | j        d           t	          j        t          j        | j                  t          j                           }ddd           n# 1 swxY w Y   t          j        | j	        | j
                  }|                     t	          j        t          j        | j                  t          j                           |           |                     |j                   dS )zx
        Test the default permission of the log file: if the file exist, it
        should keep the permission.
        wbr`   N)r0   r   r   r   ra   S_IMODErb   r   r-   r   r   r1   r   r   )r   currentModerz   s      r   test_defaultPermissionsz$LogFileTests.test_defaultPermissions   s   
 $)T"" 	I 	IHTY&&&,rwty'9'9$,'GHHK	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I ty$(33bgdi&8&8&FGGUUU
#####s   AA88A<?A<c                    t          j        | j        | j        d          }|                     |j                   t          j        t          j        | j	                  t          j
                           }t          j                                        r|                     |d           dS |                     |d           dS )zG
        Test specifying the permissions used on the log file.
        6   rv   $  N)r   r-   r   r   r   r   ra   r   r   r   rb   r   platform	isWindowsr1   )r   rz   rc   s      r   test_specifiedPermissionsz&LogFileTests.test_specifiedPermissions  s     ty$(FFF
###|BGDI..t|<==%%'' 	*T5)))))T5)))))r   zCan't test reopen on Windowsc                    t          j        t          j        | j        | j                            5 }|                    d           t          j        	                    | j        d          }t          j
        | j        |           |                                 |                    d           ddd           n# 1 swxY w Y   t          | j                  5 }|                     |                                d           ddd           n# 1 swxY w Y   t          |          5 }|                     |                                d           ddd           dS # 1 swxY w Y   dS )z
        L{logfile.LogFile.reopen} allows to rename the currently used file and
        make L{logfile.LogFile} create a new file.
        hello1zsave.loghello2N)r+   r,   r   r-   r   r   r.   r   r   r   renamereopenr0   r1   r2   )r   rz   savePathr4   s       r   test_reopenzLogFileTests.test_reopen  s    	48 D DEE 	!JJx   w||DHj99HIdi***KKMMMJJx   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! $)__ 	1QVVXXx000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1(^^ 	1qQVVXXx000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s6   A>B<<C C )DDD*)E  E$'E$c                     |                      t          t          j        | j        d          }|                     |j        t          j                   dS )zR
        Specifying an invalid directory to L{LogFile} raises C{IOError}.
        this_dir_does_not_existN)r    IOErrorr   r-   r   r1   errnoENOENT)r   es     r   test_nonExistentDirz LogFileTests.test_nonExistentDir!  sI     W_di1J
 
 	%,/////r   c                 .   t           j                                        rd\  }}d}nd\  }}d}t          j        ||d          }|                     |j                   |                     |j        |           |                     |j	        d           dS )z
        Opening a L{LogFile} which can be read and write but whose mode can't
        be changed doesn't trigger an error.
        )NULr=   r   )nullz/devz	/dev/nullrf   rv   N)
r   r   r   r   r-   r   r   r1   r   rw   )r   r   	directoryexpectedPathr$   s        r   test_cantChangeFileModez$LogFileTests.test_cantChangeFileMode*  s    
 %%'' 	''OD) LL,OD)&LodI5AAA	"""<000%00000r   c                    t          j        | j        | j                  }|                     |j                   t          |j         dd          5 }|                    d           ddd           n# 1 swxY w Y   t          |j         dd          5 }|                    d           ddd           n# 1 swxY w Y   | 	                    dg|
                                           dS )zl
        L{LogFile.listLogs} doesn't choke if it encounters a file with an
        unexpected name.
        r;   r\   r'   Nz	.bad-filerA   )r   r-   r   r   r   r   r0   r   r.   r1   rG   rr   s      r    test_listLogsWithBadlyNamedFilesz-LogFileTests.test_listLogsWithBadlyNamedFiles<  sJ   
 odi22	"""SX///3'' 	2HHUOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	SX(((#.. 	"HHUOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!cllnn-----s$   A44A8;A8B99B= B=c                    t          j        | j        | j                  }|                     |j                   t          dd          D ]I}t          |j         d| d          5 }|	                    d           ddd           n# 1 swxY w Y   J| 
                    ddg|                                           dS )	zR
        L{LogFile.listLogs} ignores log files which rotated suffix is 0.
        r   rC   .r\   r'   NrA   rB   )r   r-   r   r   r   r   ranger0   r   r.   r1   rG   )r   r$   irs   s       r   %test_listLogsIgnoresZeroSuffixedFilesz2LogFileTests.test_listLogsIgnoresZeroSuffixedFilesK  s     odi22	"""q! 	  	 A''A''--                                	!Q00000s   &BB	B	N)__name__
__module____qualname____doc__r   r   r%   r5   rH   rQ   rZ   r^   rd   rk   rt   r|   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r      sl        6 6 6' ' 'A A A5 5 54 4 4<5 5 5*)5 )5 )5V
2 
2 
2A A A. . .>; ; ;0= = =
$ 
$ 
$* * * VG&&((*HII1 1 JI1"0 0 01 1 1$. . .1 1 1 1 1r   r   c                       e Zd ZdZd Zd ZdS )RiggedDailyLogFile        c                 v    t           j                            |            |                                 | _        d S )N)r   DailyLogFile	_openFiletoDatelastDater   s    r   r   zRiggedDailyLogFile._openFile\  s,    &&t,,,r   c                 t    |rt          j        | d d         S t          j        | j                  d d         S )NrC   )timegmtime_clock)r   argss     r   r   zRiggedDailyLogFile.toDatea  s;     	*;%bqb)){4;''++r   N)r   r   r   r   r   r   r   r   r   r   r   Y  s7        F& & &
, , , , ,r   r   c                       e Zd ZdZd Zd Zd Zd Zd Z e	e
j                                        d          d             Zd	 Zd
 Zd Zd ZdS )DailyLogFileTestsz!
    Test rotating log file.
    c                     |                                  | _        t          j        | j                   d| _        t          j                            | j        | j                  | _        d S )Nztestdaily.logr
   r   s    r   r   zDailyLogFileTests.setUpl  sI    ;;==
DH#	GLL4955			r   c                    t          j        t          | j        | j                            5 }|                    d           |                    d           |                                 |                    d           ddd           n# 1 swxY w Y   t          | j                  5 }| 	                    |
                                d           ddd           dS # 1 swxY w Y   dS )zO
        A daily log file can be written to like an ordinary log file.
        r'   r(   r)   Nr*   )r+   r,   r   r   r   r.   r/   r0   r   r1   r2   r3   s      r   r5   zDailyLogFileTests.test_writingr  sH     249dh G GHH 	CIIeIIeIIKKKIIf		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 $)__ 	5QVVXX|444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s$   ABBB,)C""C&)C&c                 b    t           j         j                                       j                    fdt          d          D             }d_                            d           d_                            d           d_                            d                                t          j
                            |d	                                                   t          j
                            |d
                              d_                            d                                t          j
                            |d	                                                   t          j
                            |d
                                                   t          j
                            |d                              d_                            d                                t          j
                            |d                              dS )z/
        Daily log files rotate daily.
        c                 V    g | ]%}j         d z                       |dz            z   &S )r   Q )r   suffix).0dayr$   r   s     r   
<listcomp>z3DailyLogFileTests.test_rotation.<locals>.<listcomp>  s4    PPPS3::cEk#:#::PPPr   rC   r   r'   i  r9   r   r:   r   rA   i  r=   rB   i r?   N)r   r   r   r   r   r   r   r.   rD   r   r   r   rE   )r   daysr$   s   ` @r   rH   zDailyLogFileTests.test_rotation  s    !DH55	"""PPPPPuQxxPPP 
		%
		)
		(tAw//000Q00111
		"tAw//000tAw//000Q00111
		#Q0011111r   c                    g d}t          | j        | j                  }|                     |j                   |D ]}|                    |           |                                 |                    d          }|                     |j                   |                     ||	                                           | 
                    t          |j        d           d|_        |                                 |                                 |                    d          }|                     |j                   |                     ||	                                           dS )zH
        Test retrieving log files with L{DailyLogFile.getLog}.
        )z1
z2
z3
r   r   iQ r   N)r   r   r   r   r   r.   r/   rV   r1   rU   r    rW   r   rF   )r   datar$   drs        r   test_getLogzDailyLogFileTests.test_getLog  s*    %$$ DH55	""" 	 	AIIaLLLL		 JJsOO   q{{}}--- 	*cj%888
				

JJqMM   q{{}}-----r   c                    t          | j        | j                  }|                     |j                   |j         d|                    |j                   }t          |d          5 }|	                    d           ddd           n# 1 swxY w Y   |j
        }|                                 |                     ||j
                   dS )zu
        L{DailyLogFile.rotate} doesn't do anything if they new log file already
        exists on the disk.
        r   r\   r'   N)r   r   r   r   r   r   r   r   r0   r.   rN   rF   r1   )r   r$   newFilePathrs   previousFiles        r   test_rotateAlreadyExistsz*DailyLogFileTests.test_rotateAlreadyExists  s    
 !DH55	""" >>CJJs|$<$<>>+s## 	rHHUOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	y

sy11111s   )BBBzVMaking read-only directories on Windows is too complex for this test to reasonably do.c                 d   t          j        | j        | j                  }|                     |j                   t          j        |j        d           |                     t          j        |j        d           |j	        }|
                                 |                     ||j	                   dS )z
        L{DailyLogFile.rotate} doesn't do anything if the directory containing
        the log files can't be written to.
        r   i  N)r   r   r   r   r   r   r   r   r   rN   rF   r1   r   r$   r   s      r   #test_rotatePermissionDirectoryNotOkz5DailyLogFileTests.test_rotatePermissionDirectoryNotOk  s     "49dh77	"""
&&&#-777y

sy11111r   c                    t          j        | j        | j                  }|                     |j                   t          j        |j        d           |j	        }|
                                 |                     ||j	                   dS )zi
        L{DailyLogFile.rotate} doesn't do anything if the log file can't be
        written to.
        r   N)r   r   r   r   r   r   r   r   r   rN   rF   r1   r   s      r   test_rotatePermissionFileNotOkz0DailyLogFileTests.test_rotatePermissionFileNotOk  st    
 "49dh77	"""
5!!!y

sy11111r   c                     t          j        | j        | j                  }|                     |j                   t          j        d          }|                     d|	                    |                     dS )z~
        Test that L{DailyLogFile.toDate} converts its timestamp argument to a
        time tuple (year, month, day).
        )	  rA   rA   r   r   r   r   r   r   )r   rA   rA   N)
r   r   r   r   r   r   r   mktimer1   r   )r   r$   	timestamps      r   test_toDatezDailyLogFileTests.test_toDate  sf    
 "49dh77	"""K >??	szz)'<'<=====r   c                      fd}t          j         j         j                  }                     |j                                        t          d|           |                                } 	                    g d|           dS )a  
        Test that L{DailyLogFile.toDate} returns today's date by default.

        By mocking L{time.localtime}, we ensure that L{DailyLogFile.toDate}
        returns the first 3 values of L{time.localtime} which is the current
        date.

        Note that we don't compare the *real* result of L{DailyLogFile.toDate}
        to the *real* current date, as there's a slight possibility that the
        date changes between the 2 function calls.
        c                  j                         d|            t          t          dd                    S )Nr   r   	   )r1   listr   )r   r   s    r   mock_localtimezADailyLogFileTests.test_toDateDefaultToday.<locals>.mock_localtime  s/    R&&&a$$$r   	localtime)r   rA   rB   N)
r   r   r   r   r   r   patchr   r   r1   )r   r   r$   logDates   `   r   test_toDateDefaultTodayz)DailyLogFileTests.test_toDateDefaultToday  s    	% 	% 	% 	% 	% "49dh77	"""

4n555**,,G,,,,,r   c                     t          j        | j        | j                  }|                     |j                   d}t          j        |dz             }|                    |          }| 	                    ||           dS )zc
        Test that L{DailyLogFile.toDate} uses its arguments to create a new
        date.
        )i  r7      )r   r   r   r   r   r   N)
r   r   r   r   r   r   r   r   r   r1   )r   r$   datesecondsr   s        r   #test_toDateUsesArgumentsToMakeADatez5DailyLogFileTests.test_toDateUsesArgumentsToMakeADate	  ss    
 "49dh77	"""+dXo..**W%%w'''''r   N)r   r   r   r   r   r5   rH   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   g  s         6 6 65 5 52 2 24. . .62 2 2" V""$$	! 
2 2 
22 2 2	> 	> 	>- - -0( ( ( ( (r   r   )r+   r   r   ra   r   unittestr   twisted.pythonr   r   twisted.trial.unittestr   r   r   r   r   r   r   r   <module>r      s  
      				         + + + + + + + + + + + + + +F1 F1 F1 F1 F18 F1 F1 F1R
, , , , ,- , , ,n( n( n( n( n( n( n( n( n( n(r   