AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /lib/python3/dist-packages/twisted/logger/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /lib/python3/dist-packages/twisted/logger/test/__pycache__/test_json.cpython-310.pyc
o

�bbG�@s.dZddlmZmZddlmZmZmZmZm	Z	m
Z
ddlmZddl
mZddlmZddlmZddlmZd	d
lmZd	dlmZd	dlmZd	d
lmZmZd	dlmZm Z m!Z!m"Z"m#Z$d	dl%m&Z&d	dl'm(Z(d	dl)m*Z*dede+de+fdd�Z,Gdd�de�Z-Gdd�de�Z.Gdd�de�Z/dS)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�returncCs"|�|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)rr�r%�?/usr/lib/python3/dist-packages/twisted/logger/test/test_json.py�savedJSONInvariants!sr'c@s�eZdZdZdedefdd�Zddd�Zdd	d
�Zddd�Z	dd
d�Z
ddd�Zddd�Zddd�Z
ddd�Zddd�Zddd�ZdS)�
SaveLoadTestsz2
    Tests for loading and saving log events.
    �eventrcCst|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)r%r%r&�savedEventJSON6s	zSaveLoadTests.savedEventJSONNcCs|�t|�i��i�dS)zX
        Saving and loading an empty dictionary results in an empty dictionary.
        N�r#rr+�r*r%r%r&�test_simpleSaveLoadAsz!SaveLoadTests.test_simpleSaveLoadcCs&|�t|�ddd���ddd��dS)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��1r1Nr,r-r%r%r&�
test_saveLoadGs�zSaveLoadTests.test_saveLoadcC�,|�t|�dt�d���dddid��dS)zx
        Saving and loading an object which cannot be represented in JSON will
        result in a placeholder.
        rr2�
unpersistableTN�r#rr+�objectr-r%r%r&�test_saveUnPersistableSs�z$SaveLoadTests.test_saveUnPersistablecCr5)zD
        Non-ASCII keys and values can be saved and loaded.
        u䌡)uሴr1r6TNr7r-r%r%r&�test_saveNonASCII]s�zSaveLoadTests.test_saveNonASCIIcCsJdttd��i}|�ddi�|�t|�|��dttd���d�i�dS)zx
        Any L{bytes} objects will be saved as if they are latin-1 so they can
        be faithfully re-loaded.
        �hello�sskipped�okay�charmapN)�bytes�range�updater#rr+�decode)r*�
inputEventr%r%r&�test_saveBytesfs�zSaveLoadTests.test_saveBytescCs>Gdd�d�}d|d�d�}t|�|��}|�t|�d�dS)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@s,eZdZdeddfdd�Zdefdd�ZdS)z@SaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprable�valuerNcSs
||_dS�N�rE)r*rEr%r%r&�__init__~�
zISaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprable.__init__cSsdS)N�reprabler%r-r%r%r&�__repr__�szISaveLoadTests.test_saveUnPersistableThenFormat.<locals>.Reprable.__repr__)�__name__�
__module__�__qualname__r8rHr"rKr%r%r%r&�Reprable}srOz{object} {object.value}���
log_formatr8z
reprable 7N)rr+r#r)r*rOrC�outputEventr%r%r&� test_saveUnPersistableThenFormatusz.SaveLoadTests.test_saveUnPersistableThenFormatcCs`Gdd�d�}td|�d�}t|�|��}|�td|�d�|�ttd|�|�ttd|�dS)	zs
        L{extractField} can extract fields from an object that's been saved and
        loaded from JSON.
        c@seZdZddd�ZdS)z8SaveLoadTests.test_extractingFieldsPostLoad.<locals>.ObjrNcSs
d|_dS)N�YrGr-r%r%r&rH�rIzASaveLoadTests.test_extractingFieldsPostLoad.<locals>.Obj.__init__�rN)rLrMrNrHr%r%r%r&�Obj�srWz{object.value}rQzobject.valuerUr8N)�dictrr+r#r�assertRaises�KeyError)r*rWrC�loadedEventr%r%r&�test_extractingFieldsPostLoad�sz+SaveLoadTests.test_extractingFieldsPostLoadcCs�g}ttt|j�d�}zddWnty#t�}|�d|�Ynw|�t|�d�t	|�
|d��d}|�|t�|�|�
t��|�|��t�dS)zl
        Round-tripping a failure through L{eventAsJSON} preserves its class and
        structure.
        ��observerr0rza message about failure�log_failureN)rr	r�append�ZeroDivisionErrorr
�failurer#�lenrr+r!�
assertTrue�check�getTracebackr")r*�eventsr�f�loadedr%r%r&�test_failureStructurePreserved�s�z,SaveLoadTests.test_failureStructurePreservedcCs0ttjd�}t|�|��}|�|dtj�dS)zo
        It's important that the C{log_level} key remain a
        L{constantly.NamedConstant} object.
        ��	log_levelrlN)rXr�warnrr+�assertIs)r*rCr[r%r%r&�test_saveLoadLevel�sz SaveLoadTests.test_saveLoadLevelcCstd�}|�|tdd��dS)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"}}Nrk)rr#rX)r*r[r%r%r&�test_saveLoadUnknownLevel�s�z'SaveLoadTests.test_saveLoadUnknownLevelrV)rLrMrN�__doc__rr"r+r.r4r9r:rDrTr\rjrorpr%r%r%r&r(1s





	



	r(c@sLeZdZdZddd�Zddeddfdd	�Zdd
d�Zddd
�Zddd�Z	dS)�FileLogObserverTestsz+
    Tests for L{jsonFileLogObserver}.
    rNcCs�t��4}t|�}ztt|�Wnty&}z|�|�WYd}~n
d}~wwWd�dSWd�dS1s:wYdS)zh
        A L{FileLogObserver} returned by L{jsonFileLogObserver} is an
        L{ILogObserver}.
        N)rrrrr�fail)r*�
fileHandler^�er%r%r&�test_interface�s����"�z#FileLogObserverTests.test_interface��recordSeparatorcCs^t��"}t||�}tdd�}||�|�|��|�d��Wd�dS1s(wYdS)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}
        r0��xz	{"x": 1}
N)rrrXr#�getvalue)r*rxrtr^r)r%r%r&�assertObserverWritesJSON�s	

"�z-FileLogObserverTests.assertObserverWritesJSONcCs|��dS)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|r-r%r%r&�(test_observeWritesDefaultRecordSeparator�sz=FileLogObserverTests.test_observeWritesDefaultRecordSeparatorcCs|jdd�dS)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{""}.
        ��rxNr}r-r%r%r&�&test_observeWritesEmptyRecordSeparator�sz;FileLogObserverTests.test_observeWritesEmptyRecordSeparatorcs�t�}t�}g}|�tt|j��|�t|��t|d�}zddWnty1|�	d�Ynwt|�
��}tt|��}dt
tddf�fdd	�}||�||�dS)
z�
        A L{FileLogObserver} created by L{jsonFileLogObserver} writes failures
        serialized as JSON text to a file when it observes events.
        r]r0rzfailed as expected�	logEventsrNcs`��t|�d�|\}��d|�|d}��|t�|��}��|jjj�	d�t
�	d��dS)Nr0r_�co)r#rc�assertInr!r
�getTracebackObject�tb_frame�f_code�co_filename�rstrip�__file__)r��failureEvent�
failureObject�tracebackObjectr-r%r&�checkEventss�z@FileLogObserverTests.test_failureFormatting.<locals>.checkEvents)rr�addObserverr	rr`rr�
BaseExceptionrbr{�listrrr)r*�io�	publisher�logged�logger�reader�deserializedr�r%r-r&�test_failureFormatting�s 
�z+FileLogObserverTests.test_failureFormattingrV)rw)
rLrMrNrqrvr"r|r~r�r�r%r%r%r&rr�s


rrc	@s�eZdZdZd&dd�Zd&dd�Z		d'd	eed
ee	de
ddfdd
�Zd&dd�Zd&dd�Z
d&dd�Zd&dd�Zd&dd�Zd&dd�Zd&dd�Zd&dd�Zd&dd�Zd&d d!�Zd&d"d#�Zd&d$d%�ZdS)(�LogFileReaderTestsz-
    Tests for L{eventsFromJSONLogFile}.
    rNcs8g�_tt�dtddf�fdd��}|�_t�|�dS)Nr)rcs.|dtjkrd|vr�j�|�dSdSdS)N�
log_namespace�record)�jsonLog�	namespace�errorEventsr`)r)r-r%r&r^s�z*LogFileReaderTests.setUp.<locals>.observer)r�r
rr�logObserverrr�)r*r^r%r-r&�setUps
zLogFileReaderTests.setUpcCst�|j�dSrF)r�removeObserverr�r-r%r%r&�tearDown&szLogFileReaderTests.tearDown��inFilerx�
bufferSizecCsJtt|||��}|�t|�ddi�|�t|�ddi�|�tt|�dS)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}
        rzr0�yrN)�iterrr#�nextrY�
StopIteration)r*r�rxr�rgr%r%r&�_readEvents)szLogFileReaderTests._readEventscC�Jtd��}|�|�|�t|j�d�Wd�dS1swYdS)z�
        L{eventsFromJSONLogFile} reads events from a file and automatically
        detects use of C{"\x1e"} as the record separator.
        �{"x": 1}
{"y": 2}
rN�rr�r#rcr��r*rtr%r%r&�&test_readEventsAutoWithRecordSeparator>�

"�z9LogFileReaderTests.test_readEventsAutoWithRecordSeparatorcCr�)z�
        L{eventsFromJSONLogFile} reads events from a file and automatically
        detects use of C{""} as the record separator.
        z{"x": 1}
{"y": 2}
rNr�r�r%r%r&�'test_readEventsAutoEmptyRecordSeparatorGr�z:LogFileReaderTests.test_readEventsAutoEmptyRecordSeparatorcC�Ntd��}|j|dd�|�t|j�d�Wd�dS1s wYdS)z{
        L{eventsFromJSONLogFile} reads events from a file and is told to use
        a specific record separator.
        z{"x": 1}
{"y": 2}
�r�rNr�r�r%r%r&�&test_readEventsExplicitRecordSeparatorPs
"�z9LogFileReaderTests.test_readEventsExplicitRecordSeparatorcCr�)zM
        L{eventsFromJSONLogFile} handles buffering a partial event.
        r�r0�r�rNr�r�r%r%r&�test_readEventsPartialBufferZs
"�z/LogFileReaderTests.test_readEventsPartialBuffercC�td��?}tt|��}|�t|�ddi�|�tt|�|�t|j�d�|�|jddd�|�|jddd	�Wd
�d
S1sFwYd
S)zF
        If the JSON text for a record is truncated, skip it.
        z{"x": 1{"y": 2}
r�rr0rrRz0Unable to read truncated JSON record: {record!r}r�s{"x": 1N�	rr�rr#r�rYr�rcr��r*rtrgr%r%r&�test_readTruncatedc�
�"�z%LogFileReaderTests.test_readTruncatedcC�ntd��)}tt|��}|�t|�ddi�|�tt|�|�t|j�d�Wd�dS1s0wYdS)zc
        If the file being read from vends L{str}, strings decode from JSON
        as-is.
        u{"currency": "€"}
�currency�€rNr�r�r%r%r&�test_readUnicodeus
"�z#LogFileReaderTests.test_readUnicodecCr�)zh
        If the file being read from vends L{bytes}, strings decode from JSON as
        UTF-8.
        s{"currency": "€"}
r�r�rN�	rr�rr#r�rYr�rcr�r�r%r%r&�test_readUTF8Bytes�s
"�z%LogFileReaderTests.test_readUTF8BytescCsrtd��+}tt|dd��}|�t|�ddi�|�tt|�|�t|j�d�Wd�dS1s2wYdS)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�rzr�rNr�r�r%r%r&�test_readTruncatedUTF8Bytes�s
"�z.LogFileReaderTests.test_readTruncatedUTF8BytescCr�)zh
        If the JSON text for a record contains invalid UTF-8 text, ignore that
        record.
        s{"x": "�"}
{"y": 2}
r�rr0rrRz2Unable to decode UTF-8 for JSON record: {record!r}r�s{"x": "�"}
Nr�r�r%r%r&�test_readInvalidUTF8Bytes�s
�"�z,LogFileReaderTests.test_readInvalidUTF8BytescCr�)zD
        If the JSON text for a record is invalid, skip it.
        z{"x": }
{"y": 2}
r�rr0rrR�&Unable to read JSON record: {record!r}r�s{"x": }
Nr�r�r%r%r&�test_readInvalidJSON�r�z'LogFileReaderTests.test_readInvalidJSONcCs�td��3}t|�}|�tt|�|�t|j�d�|�|jddd�|�|jddd�Wd�dS1s:wYdS)	zI
        Multiple events without a record separator are skipped.
        z{"x": 1}
{"y": 2}
r0rrRr�r�s{"x": 1}
{"y": 2}
N)rrrYr�r�r#rcr�r�r%r%r&�test_readUnseparated�s
�"�z'LogFileReaderTests.test_readUnseparatedcCs|tdd�}t��,}t|�}||�|�d�t|�}|�t|�|f�|�t|j�d�Wd�dS1s7wYdS)z�
        Data written by a L{FileLogObserver} returned by L{jsonFileLogObserver}
        and read by L{eventsFromJSONLogFile} is reconstructed properly.
        r0ryrN)	rXrr�seekrr#�tuplercr�)r*r)rtr^rgr%r%r&�test_roundTrip�s

"�z!LogFileReaderTests.test_roundTriprV)Nr�)rLrMrNrqr�r�rrrr"�intr�r�r�r�r�r�r�r�r�r�r�r�r�r%r%r%r&r�s6

����
�

	
	


	





r�N)0rqr�rr�typingrrrrrr	�zope.interfacer
�zope.interface.exceptionsr�zope.interface.verifyr�twisted.python.failurer
�twisted.trial.unittestr�_flattenr�_formatr�_globalr�_interfacesrr�_jsonrrrrrr��_levelsr�_loggerr�	_observerrr"r'r(rrr�r%r%r%r&�<module>s* R

Anon7 - 2022
AnonSec Team