
    YfbG                     R   d Z ddlmZmZ ddlmZmZmZ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 d	dlmZ d	dlmZ d	dlmZmZ d	dlmZm Z m!Z!m"Z"m#Z$ d	dl%m&Z& d	dl'm(Z( d	dl)m*Z* dede+de+fdZ, G d de          Z- G d de          Z. G d de          Z/dS )z$
Tests for L{twisted.logger._json}.
    )BytesIOStringIO)IOAnyListOptionalSequencecast)implementer)BrokenMethodImplementation)verifyObject)Failure)TestCase   )extractField)formatEvent)globalLogPublisher)ILogObserverLogEvent)eventAsJSONeventFromJSONeventsFromJSONLogFilejsonFileLogObserverlog)LogLevel)Logger)LogPublishertestCase	savedJSONreturnc                     |                      |t                     |                     |                    d          d           |S )a*  
    Assert a few things about the result of L{eventAsJSON}, then return it.

    @param testCase: The L{TestCase} with which to perform the assertions.
    @param savedJSON: The result of L{eventAsJSON}.

    @return: C{savedJSON}

    @raise AssertionError: If any of the preconditions fail.
    
r   )assertIsInstancestrassertEqualcount)r   r   s     O/var/www/html/env/lib/python3.11/site-packages/twisted/logger/test/test_json.pysavedJSONInvariantsr(   !   sB     i---..222    c                   r    e Zd ZdZdedefdZddZddZddZ	dd	Z
dd
ZddZddZddZddZddZdS )SaveLoadTestsz2
    Tests for loading and saving log events.
    eventr    c                 <    t          | t          |                    S )z
        Serialize some an events, assert some things about it, and return the
        JSON.

        @param event: An event.

        @return: JSON.
        )r(   r   )selfr,   s     r'   savedEventJSONzSaveLoadTests.savedEventJSON6   s     #4U););<<<r)   Nc                 r    |                      t          |                     i                     i            dS )zX
        Saving and loading an empty dictionary results in an empty dictionary.
        Nr%   r   r/   r.   s    r'   test_simpleSaveLoadz!SaveLoadTests.test_simpleSaveLoadA   s6     	t':':2'>'>??DDDDDr)   c           	      ~    |                      t          |                     ddd                    ddd           dS )a  
        Saving and loading a dictionary with some simple values in it results
        in those same simple values in the output; according to JSON's rules,
        though, all dictionary keys must be L{str} and any non-L{str}
        keys will be converted.
        r   4)   31r7   Nr1   r2   s    r'   test_saveLoadzSaveLoadTests.test_saveLoadG   sR     	$--!#.>.>??@@#	
 	
 	
 	
 	
r)   c           	          |                      t          |                     dt                      d                    dddid           dS )zx
        Saving and loading an object which cannot be represented in JSON will
        result in a placeholder.
        r   r8   unpersistableTNr%   r   r/   objectr2   s    r'   test_saveUnPersistablez$SaveLoadTests.test_saveUnPersistableS   s[    
 	$--AFHH.E.EFFGG?D122	
 	
 	
 	
 	
r)   c           	          |                      t          |                     dt                      d                    dddid           dS )zD
        Non-ASCII keys and values can be saved and loaded.
        u   䌡)u   ሴr7   r<   TNr=   r2   s    r'   test_saveNonASCIIzSaveLoadTests.test_saveNonASCII]   s[     	$--.Q.QRRSS&=>>	
 	
 	
 	
 	
r)   c           	      :   dt          t          d                    i}|                    ddi           |                     t	          |                     |                    dt          t          d                                        d          i           dS )zx
        Any L{bytes} objects will be saved as if they are latin-1 so they can
        be faithfully re-loaded.
        hello   s   skippedokaycharmapN)bytesrangeupdater%   r   r/   decode)r.   
inputEvents     r'   test_saveByteszSaveLoadTests.test_saveBytesf   s    
 uU3ZZ001
 	:v.///$--j99::eE#JJ''..y99:	
 	
 	
 	
 	
r)   c                      G d d          }d |d          d}t          |                     |                    }|                     t          |          d           dS )a
  
        Saving and loading an object which cannot be represented in JSON, but
        has a string representation which I{can} be saved as JSON, will result
        in the same string formatting; any extractable fields will retain their
        data types.
        c                   *    e Zd ZdeddfdZdefdZdS )@SaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprablevaluer    Nc                     || _         d S NrP   )r.   rP   s     r'   __init__zISaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprable.__init__~   s    "


r)   c                     dS )Nreprable r2   s    r'   __repr__zISaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprable.__repr__   s    !zr)   )__name__
__module____qualname__r>   rT   r$   rX   rW   r)   r'   ReprablerO   }   sQ        #f # # # # #"# " " " " " "r)   r\   z{object} {object.value}   
log_formatr>   z
reprable 7N)r   r/   r%   r   )r.   r\   rK   outputEvents       r'    test_saveUnPersistableThenFormatz.SaveLoadTests.test_saveUnPersistableThenFormatu   s    	" 	" 	" 	" 	" 	" 	" 	" %>RSUU
#D$7$7
$C$CDD[11<@@@@@r)   c                 f    G d d          }t          d |                      }t          |                     |                    }|                     t	          d|          d           |                     t          t          d|           |                     t          t          d|           dS )	zs
        L{extractField} can extract fields from an object that's been saved and
        loaded from JSON.
        c                       e Zd ZddZdS )8SaveLoadTests.test_extractingFieldsPostLoad.<locals>.Objr    Nc                     d| _         d S )NY  rS   r2   s    r'   rT   zASaveLoadTests.test_extractingFieldsPostLoad.<locals>.Obj.__init__   s     


r)   r    N)rY   rZ   r[   rT   rW   r)   r'   Objrd      s(        ! ! ! ! ! !r)   rh   z{object.value}r^   zobject.valuerf   r>   N)dictr   r/   r%   r   assertRaisesKeyError)r.   rh   rK   loadedEvents       r'   test_extractingFieldsPostLoadz+SaveLoadTests.test_extractingFieldsPostLoad   s    	! 	! 	! 	! 	! 	! 	! 	! %5cceeDDD
#D$7$7
$C$CDDnkBBCHHH
 	(L(KHHH(L(JGGGGGr)   c                 \   g }t          t          t          |j                            }	 ddz   n4# t          $ r' t                      }|                    d|           Y nw xY w|                     t          |          d           t          | 
                    |d                             d         }|                     |t
                     |                     |                    t                               |                     |                                t                     dS )zl
        Round-tripping a failure through L{eventAsJSON} preserves its class and
        structure.
        observerr6   r   za message about failurelog_failureN)r   r
   r   appendZeroDivisionErrorr   failurer%   lenr   r/   r#   
assertTruecheckgetTracebackr$   )r.   eventsr   floadeds        r'   test_failureStructurePreservedz,SaveLoadTests.test_failureStructurePreserved   s   
 "$d<??@@@	6EEE  	6 	6 	6		AKK1155555	6 	Va(((t226!9==>>}Mfg...%677888f1133S99999s   2 .A#"A#c                     t          t          j                  }t          |                     |                    }|                     |d         t          j                   dS )zo
        It's important that the C{log_level} key remain a
        L{constantly.NamedConstant} object.
        	log_levelr   N)ri   r   warnr   r/   assertIs)r.   rK   rl   s      r'   test_saveLoadLevelz SaveLoadTests.test_saveLoadLevel   sR    
 HM222
#D$7$7
$C$CDDk+.>>>>>r)   c                 l    t          d          }|                     |t          d                     dS )z
        If a saved bit of JSON (let's say, from a future version of Twisted)
        were to persist a different log_level, it will resolve as None.
        zZ{"log_level": {"name": "other", "__class_uuid__": "02E59486-F24D-46AD-8224-3ACDF2A5732A"}}Nr~   )r   r%   ri   )r.   rl   s     r'   test_saveLoadUnknownLevelz'SaveLoadTests.test_saveLoadUnknownLevel   sB    
 $I
 
 	dT&:&:&:;;;;;r)   rg   )rY   rZ   r[   __doc__r   r$   r/   r3   r:   r?   rA   rL   ra   rm   r|   r   r   rW   r)   r'   r+   r+   1   s        	=H 	= 	= 	= 	= 	=E E E E

 

 

 


 
 
 

 
 
 

 
 
 
A A A A&H H H H(: : : :$? ? ? ?	< 	< 	< 	< 	< 	<r)   r+   c                   D    e Zd ZdZddZddeddfdZddZdd	Zdd
Z	dS )FileLogObserverTestsz+
    Tests for L{jsonFileLogObserver}.
    r    Nc                     t                      5 }t          |          }	 t          t          |           n,# t          $ r}|                     |           Y d}~nd}~ww xY wddd           dS # 1 swxY w Y   dS )zh
        A L{FileLogObserver} returned by L{jsonFileLogObserver} is an
        L{ILogObserver}.
        N)r   r   r   r   r   fail)r.   
fileHandlerp   es       r'   test_interfacez#FileLogObserverTests.test_interface   s    
 ZZ 	:*:66H\84444-   		!		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s7   A/6A/
A AA/AA//A36A3recordSeparatorc                     t                      5 }t          ||          }t          d          } ||           |                     |                                | d           ddd           dS # 1 swxY w Y   dS )a!  
        Asserts that an observer created by L{jsonFileLogObserver} with the
        given arguments writes events serialized as JSON text, using the given
        record separator.

        @param recordSeparator: C{recordSeparator} argument to
            L{jsonFileLogObserver}
        r6   xz	{"x": 1}
N)r   r   ri   r%   getvalue)r.   r   r   rp   r,   s        r'   assertObserverWritesJSONz-FileLogObserverTests.assertObserverWritesJSON   s     ZZ 	V:*:GGH1IIIEHUOOOZ00224T4T4TUUU		V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	Vs   AA33A7:A7c                 .    |                                   dS )z
        A L{FileLogObserver} created by L{jsonFileLogObserver} writes events
        serialzed as JSON text to a file when it observes events.
        By default, the record separator is C{"\x1e"}.
        Nr   r2   s    r'   (test_observeWritesDefaultRecordSeparatorz=FileLogObserverTests.test_observeWritesDefaultRecordSeparator   s     	%%'''''r)   c                 2    |                      d           dS )z
        A L{FileLogObserver} created by L{jsonFileLogObserver} writes events
        serialzed as JSON text to a file when it observes events.
        This test sets the record separator to C{""}.
         r   Nr   r2   s    r'   &test_observeWritesEmptyRecordSeparatorz;FileLogObserverTests.test_observeWritesEmptyRecordSeparator   s!     	%%b%99999r)   c                 4    t                      }t                      }g }|                    t          t          |j                             |                    t          |                     t          |          }	 ddz   n%# t          $ r |	                    d           Y nw xY wt          |
                                          }t          t          |                    }dt          t                   ddf fd} ||            ||           dS )	z
        A L{FileLogObserver} created by L{jsonFileLogObserver} writes failures
        serialized as JSON text to a file when it observes events.
        ro   r6   r   zfailed as expected	logEventsr    Nc                                         t          |           d           | \  }                    d|           |d         }                    |t                     |                                }                     |j        j        j        	                    d          t          	                    d                     d S )Nr6   rq   co)r%   ru   assertInr#   r   getTracebackObjecttb_framef_codeco_filenamerstrip__file__)r   failureEventfailureObjecttracebackObjectr.   s       r'   checkEventsz@FileLogObserverTests.test_failureFormatting.<locals>.checkEvents  s    S^^Q///&N\MM-666(7M!!-999+>>@@O(/;BB4HH%%    r)   )r   r   addObserverr
   r   rr   r   r   BaseExceptionrt   r   listr   r	   r   )r.   io	publisherloggedloggerreaderdeserializedr   s   `       r'   test_failureFormattingz+FileLogObserverTests.test_failureFormatting   s1   
 ZZ NN	!#d<??@@@1"55666+++	1EEE 	1 	1 	1NN/00000	1"++--((1&99::
	8H#5 
	$ 
	 
	 
	 
	 
	 
	 	FL!!!!!s    B B('B(rg   )r   )
rY   rZ   r[   r   r   r$   r   r   r   r   rW   r)   r'   r   r      s         
 
 
 
V V V V V V V( ( ( (: : : :" " " " " "r)   r   c            	           e Zd ZdZddZddZ	 	 ddee         dee	         d	e
ddfd
ZddZddZddZddZddZddZddZddZddZddZddZddZdS )LogFileReaderTestsz-
    Tests for L{eventsFromJSONLogFile}.
    r    Nc                      g  _         t          t                    dt          dd f fd            }| _        t          j        |           d S )Nr,   r    c                 x    | d         t           j        k    r d| v rj                            |            d S d S d S )Nlog_namespacerecord)jsonLog	namespaceerrorEventsrr   )r,   r.   s    r'   rp   z*LogFileReaderTests.setUp.<locals>.observer  sK    _%):::x5?P?P ''..... ;:?P?Pr)   )r   r   r   r   logObserverr   r   )r.   rp   s   ` r'   setUpzLogFileReaderTests.setUp  sk    +-	\	"	"	/H 	/ 	/ 	/ 	/ 	/ 	/ 
#	"	/ $&x00000r)   c                 8    t          j        | j                   d S rR   )r   removeObserverr   r2   s    r'   tearDownzLogFileReaderTests.tearDown&  s    )$*:;;;;;r)      inFiler   
bufferSizec                    t          t          |||                    }|                     t          |          ddi           |                     t          |          ddi           |                     t
          t          |           dS )a|  
        Test that L{eventsFromJSONLogFile} reads two pre-defined events from a
        file: C{{"x": 1}} and C{{"y": 2}}.

        @param inFile: C{inFile} argument to L{eventsFromJSONLogFile}
        @param recordSeparator: C{recordSeparator} argument to
            L{eventsFromJSONLogFile}
        @param bufferSize: C{bufferSize} argument to L{eventsFromJSONLogFile}
        r   r6   yr   N)iterr   r%   nextrj   StopIteration)r.   r   r   r   ry   s        r'   _readEventszLogFileReaderTests._readEvents)  s|     +FOZPPQQfQx000fQx000-v66666r)   c                     t          d          5 }|                     |           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )z
        L{eventsFromJSONLogFile} reads events from a file and automatically
        detects use of C{"\x1e"} as the record separator.
        {"x": 1}
{"y": 2}
r   Nr   r   r%   ru   r   r.   r   s     r'   &test_readEventsAutoWithRecordSeparatorz9LogFileReaderTests.test_readEventsAutoWithRecordSeparator>  s    
 788 	7JZ(((S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7   >AA"Ac                     t          d          5 }|                     |           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )z
        L{eventsFromJSONLogFile} reads events from a file and automatically
        detects use of C{""} as the record separator.
        z{"x": 1}
{"y": 2}
r   Nr   r   s     r'   'test_readEventsAutoEmptyRecordSeparatorz:LogFileReaderTests.test_readEventsAutoEmptyRecordSeparatorG  s    
 /00 	7JZ(((S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7r   c                     t          d          5 }|                     |d           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )z{
        L{eventsFromJSONLogFile} reads events from a file and is told to use
        a specific record separator.
        z{"x": 1}
{"y": 2}
r   r   Nr   r   s     r'   &test_readEventsExplicitRecordSeparatorz9LogFileReaderTests.test_readEventsExplicitRecordSeparatorP  s     788 	7JZ@@@S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7   A AA!$A!c                     t          d          5 }|                     |d           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )zM
        L{eventsFromJSONLogFile} handles buffering a partial event.
        r   r6   r   r   Nr   r   s     r'   test_readEventsPartialBufferz/LogFileReaderTests.test_readEventsPartialBufferZ  s     788 	7JZA666S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7r   c                    t          d          5 }t          t          |                    }|                     t	          |          ddi           |                     t          t          |           |                     t          | j                  d           |                     | j        d         d         d           |                     | j        d         d         d	           d
d
d
           d
S # 1 swxY w Y   d
S )zF
        If the JSON text for a record is truncated, skip it.
        z{"x": 1{"y": 2}
r   r   r6   r   r_   z0Unable to read truncated JSON record: {record!r}r   s   {"x": 1N	r   r   r   r%   r   rj   r   ru   r   r.   r   ry   s      r'   test_readTruncatedz%LogFileReaderTests.test_readTruncatedc  sH    455 	H/
;;<<FT&\\C8444mT6::: S!122A666 #L1B   T-a0:JGGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H   CC66C:=C:c                 j   t          d          5 }t          t          |                    }|                     t	          |          ddi           |                     t          t          |           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )zc
        If the file being read from vends L{str}, strings decode from JSON
        as-is.
        u   {"currency": "€"}
currency   €r   Nr   r   s      r'   test_readUnicodez#LogFileReaderTests.test_readUnicodeu  s     455 	7/
;;<<FT&\\J+ABBBmT6:::S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7   BB((B,/B,c                 j   t          d          5 }t          t          |                    }|                     t	          |          ddi           |                     t          t          |           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )zh
        If the file being read from vends L{bytes}, strings decode from JSON as
        UTF-8.
        s   {"currency": "€"}
r   r   r   N	r   r   r   r%   r   rj   r   ru   r   r   s      r'   test_readUTF8Bytesz%LogFileReaderTests.test_readUTF8Bytes  s     :;; 	7z/
;;<<F T&\\J+ABBBmT6:::S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7r   c                 n   t          d          5 }t          t          |d                    }|                     t	          |          ddi           |                     t          t          |           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )z
        If the JSON text for a record is truncated in the middle of a two-byte
        Unicode codepoint, we don't want to see a codec exception and the
        stream is read properly when the additional data arrives.
        s   {"x": "€"}
   r   r   r   r   Nr   r   s      r'   test_readTruncatedUTF8Bytesz.LogFileReaderTests.test_readTruncatedUTF8Bytes  s     344 	7
/
qIIIJJFT&\\C?;;;mT6:::S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   BB**B.1B.c                    t          d          5 }t          t          |                    }|                     t	          |          ddi           |                     t          t          |           |                     t          | j                  d           |                     | j        d         d         d           |                     | j        d         d         d	           d
d
d
           d
S # 1 swxY w Y   d
S )zh
        If the JSON text for a record contains invalid UTF-8 text, ignore that
        record.
        s   {"x": ""}
{"y": 2}
r   r   r6   r   r_   z2Unable to decode UTF-8 for JSON record: {record!r}r   s   {"x": ""}
Nr   r   s      r'   test_readInvalidUTF8Bytesz,LogFileReaderTests.test_readInvalidUTF8Bytes  sI    ABB 	Tj/
;;<<FT&\\C8444mT6::: S!122A666 #L1D   T-a0:<RSSS	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	Tr   c                    t          d          5 }t          t          |                    }|                     t	          |          ddi           |                     t          t          |           |                     t          | j                  d           |                     | j        d         d         d           |                     | j        d         d         d	           d
d
d
           d
S # 1 swxY w Y   d
S )zD
        If the JSON text for a record is invalid, skip it.
        z{"x": }
{"y": 2}
r   r   r6   r   r_   &Unable to read JSON record: {record!r}r   s   {"x": }
Nr   r   s      r'   test_readInvalidJSONz'LogFileReaderTests.test_readInvalidJSON  sH    677 	J:/
;;<<FT&\\C8444mT6::: S!122A666 #L18   T-a0:LIII	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	Jr   c                    t          d          5 }t          |          }|                     t          t          |           |                     t          | j                  d           |                     | j        d         d         d           |                     | j        d         d         d           ddd           dS # 1 swxY w Y   dS )	zI
        Multiple events without a record separator are skipped.
        z{"x": 1}
{"y": 2}
r6   r   r_   r   r   s   {"x": 1}
{"y": 2}
N)r   r   rj   r   r   r%   ru   r   r   s      r'   test_readUnseparatedz'LogFileReaderTests.test_readUnseparated  s     344 	U
*:66FmT6::: S!122A666 #L18   T-a0:<STTT	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	Us   B'CCCc                    t          d          }t                      5 }t          |          } ||           |                    d           t	          |          }|                     t          |          |f           |                     t          | j                  d           ddd           dS # 1 swxY w Y   dS )z
        Data written by a L{FileLogObserver} returned by L{jsonFileLogObserver}
        and read by L{eventsFromJSONLogFile} is reconstructed properly.
        r6   r   r   N)	ri   r   r   seekr   r%   tupleru   r   )r.   r,   r   rp   ry   s        r'   test_roundTripz!LogFileReaderTests.test_roundTrip  s    
 q			ZZ 	7:*:66HHUOOOOOA*:66FU6]]UH555S!122A666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   BB77B;>B;rg   )Nr   )rY   rZ   r[   r   r   r   r   r   r   r$   intr   r   r   r   r   r   r   r   r   r   r   r   r   rW   r)   r'   r   r     s        
1 
1 
1 
1< < < < *.	7 737 "#7 	7
 
7 7 7 7*7 7 7 77 7 7 77 7 7 77 7 7 7H H H H$7 7 7 77 7 7 77 7 7 7T T T T(J J J J$U U U U"7 7 7 7 7 7r)   r   N)0r   r   r   r   typingr   r   r   r   r	   r
   zope.interfacer   zope.interface.exceptionsr   zope.interface.verifyr   twisted.python.failurer   twisted.trial.unittestr   _flattenr   _formatr   _globalr   _interfacesr   r   _jsonr   r   r   r   r   r   _levelsr   _loggerr   	_observerr   r$   r(   r+   r   r   rW   r)   r'   <module>r     sL    !               : : : : : : : : : : : : : : : : & & & & & & @ @ @ @ @ @ . . . . . . * * * * * * + + + + + + # # # # # # ! ! ! ! ! ! ( ( ( ( ( ( 0 0 0 0 0 0 0 0                          $ $ $ $ $ $( s s     O< O< O< O< O<H O< O< O<dO" O" O" O" O"8 O" O" O"dP7 P7 P7 P7 P7 P7 P7 P7 P7 P7r)   