
    dafY                        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
 d dlmZ  ej                  d      Z G d d      Z e       e_         G d	 d
      Zd Zdi fd ed      fd ed      fdi fdi fdddgifd ed      fd ed      fd ed      fdddgifdddgifdddgifdddgifd ed      fd  ed!      fd"d#gd$gd%fd&dd#d'gifd(d)gd*gd+gd,fd-d.gd/gd0gd1gd2gd3gd4gd5gd6fd7d8gd9gd:gd)gd;gd<gd=fgZd> Zd? Zd@ ZdA Z G dB dCej0                        ZdDZdEZdFZdGZdHZedIk(  r ej@                          yy)J    N)
namedtuple)StringIOBytesIO)support)warnings_helpercgic                   (    e Zd Zg Zej
                  Zy)HackedSysModuleN)__name__
__module____qualname__argvsysstdin     (/root/Python-3.12.4/Lib/test/test_cgi.pyr
   r
      s     DIIEr   r
   c                   $    e Zd Zd Zd Zd Zd Zy)ComparableExceptionc                     || _         y N)err)selfr   s     r   __init__zComparableException.__init__   s	    r   c                 ,    t        | j                        S r   )strr   r   s    r   __str__zComparableException.__str__   s    488}r   c                     t        |t              st        S | j                  j                  |j                  k(  xr# | j                  j
                  |j
                  k(  S r   )
isinstance	ExceptionNotImplementedr   	__class__args)r   anExcs     r   __eq__zComparableException.__eq__   sF    %+!!""eoo5 ,+	-r   c                 .    t        | j                  |      S r   )getattrr   )r   attrs     r   __getattr__zComparableException.__getattr__"   s    txx&&r   N)r   r   r   r   r   r&   r*   r   r   r   r   r      s    -'r   r   c                 <   i }|dk(  rd }d|d<   | |d<   nO|dk(  r<t        | j                  d            }d|d<   d|d<   t        t        |             |d<   nt	        d	|z        	 t
        j                  ||d
      S # t        $ r}t        |      cY d }~S d }~ww xY w)NGETREQUEST_METHODQUERY_STRINGPOSTlatin-1!application/x-www-form-urlencodedCONTENT_TYPECONTENT_LENGTHzunknown method: %s   )strict_parsing)	r   encoder   len
ValueErrorr   parser!   r   )bufmethodenvfpr   s        r   do_testr>   %   s    
C %!N	6	SZZ	*+ &AN #CH-677(yySy33 ("3''(s   %A= =	B
BBB &bad query field: ''z&&=z=&=z=aaz&=az=a&z=&abad query field: 'a'zb=abzb+=azb za=b=aza=+b=az b=az&b=azb&=abad query field: 'b'za=a+b&b=b+ca bb crC   rE   za=a+b&a=b+ab azx=1&y=2.0&z=2-3.%2b012.02-3.+0xyzzHbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env400006r	964546263z130003.300038zen-US 0bb2e248638833d48cb7fed300000f1br<   bustomer)z1Hbc5161168c542333633315dee1182227:key_store_seqidcuyerexpirekidlobaleorder_idssviewzQgroup_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=BrowseBrowse313921005470custom)SUBMIT_assigned_to	_category_statusgroup_idsetc                 $    t        | t              S )N)key)sortedrepr)seqs    r   normrn   a   s    #4  r   c                 2    | D cg c]  }|d   	 c}S c c}w Nr   r   listps     r   
first_eltsrt   d   s    QAaDs   c                 B    | D cg c]  }|d   |d   d   f c}S c c}w )Nr   r4   r   rq   s     r   first_second_eltsrv   g   s&    %)*QqT1Q47O***s   c                 >   d}t        | j                  |            }|j                  d       t        j	                  |||      }i }t        |      j                         D ]9  \  }}t        |t              xr |j                  |      xs |j                  ||<   ; |S )Nr0   r   )r=   environencoding)r   r6   seekr   FieldStoragedictitemsr    rr   getlistvalue)datarx   ry   
fake_stdinformresultkvs           r   
gen_resultr   j   s    HX./JOOAz7XNDFT
  " G1q$';DLLOFqwwq	G Mr   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	 e
j                  e      d	        Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZdddgdddZd Zd Zd Zd Zd Zd Zy) CgiTestsc                     t        t        j                  d            }t        j                  d      dd}t        j                  ||      }dgdgdgdgd}| j                  ||       y )	Nlatin1558boundaryzCONTENT-LENGTH Add 1234   Testing 123.
r?   )submitidfiletitle)r   POSTDATAr6   BOUNDARYr   parse_multipartassertEqual)r   r=   r<   r   expecteds        r   test_parse_multipartzCgiTests.test_parse_multipartx   sh    X__X./#??84!&($$R-&ix./2$@*r   c                     d}t        |j                  d            }ddj                  d      i}t        j                  ||      }ddgi}| j	                  ||       y )NzY--JfISa01
Content-Disposition: form-data; name="submit-name"

just a string

--JfISa01--
r   r   JfISa01submit-namezjust a string
)r   r6   r   r   r   )r   r   r=   r<   r   r   s         r   +test_parse_multipart_without_content_lengthz4CgiTests.test_parse_multipart_without_content_length   sc     X__X./9++H56$$R-!$5#67*r   c                 x   d}d}t        |j                  d            }|j                  d      t        t        |j                  d                  d}t        j                  ||dd      }d	d
gi}| j                  ||       | j                  dj                  d      |d	   d   j                  dd             y )Nr   u]   --JfISa01
Content-Disposition: form-data; name="submit-name"
Content-Length: 3

☃
--JfISa01utf8r   r   asciisurrogateescape)ry   errorsr   u	   u   ☃r   )r   r6   r   r7   r   r   r   )r   r   r   r=   r<   r   r   s          r   %test_parse_multipart_invalid_encodingz.CgiTests.test_parse_multipart_invalid_encoding   s     X__V,-#??84!$S)@%A!BD$$Rw,= % ?!$8#9:*0.q188ARS	Ur   c                 `   t         j                         }| j                  |       | j                  dt	        |             | j                  t        |      t        |j                                      |j                  j                   t        dd      d             | j                  |       y )Nr{   MockFieldStoragename
fieldvalue)r   r{   assertFalseassertInrl   r   rr   keysappendr   
assertTruer   fss     r   test_fieldstorage_propertiesz%CgiTests.test_fieldstorage_properties   sz    nd2h/b4	?3
=z"4f=lKLr   c                    | j                  t        t        j                  dddi       | j                  t        t        j                  dd       t        j                  ddi	      }| j                  t        t        |       y )
Nznot-a-file-objr-   PUTrx   foobarzcontent-type
text/plain)headers)assertRaises	TypeErrorr   r{   boolr   s     r   test_fieldstorage_invalidz"CgiTests.test_fieldstorage_invalid   sp    )S%5%57GEUV[D\ 	 	^)S%5%5ueD~l&CD)T2.r   c           	         t         D ]{  \  }}t        |d      }| j                  ||dt        |      z         t        |d      }| j                  ||dt        |      z         d|i}t        j                  |      }t        |t              s| j                  t        |      t        |             | j                  |j                         |j                                | j                  |j                  dd      d       |j                         D ]m  }||   }| j                  ||       t        |      d	kD  r"| j                  |j                  |      |       J| j                  |j                  |      |d
          o ~ y )Nr,   zError parsing %s method GETr/   zError parsing %s method POSTr.   r   znonexistent fielddefaultr4   r   )parse_strict_test_casesr>   r   rl   r   r{   r    r|   r7   assertCountEqualr   getvaluer   )r   origexpectdr<   r   rj   
expect_vals           r   test_strictzCgiTests.test_strict   sP   3 	JLD&e$AQ(ET
(RSf%AQ(Fd(ST!4(C!!#!.B&$'  Vc"g6%%fkkmRWWY?   -@)!LiX!;;= JC!'JMM#r*:*((S)9:F((S)9:a=IJ#	Jr   c                 .   ddgdgdfddgdgdgdfdt        d	      fd
t        d	      fdt        d      fdt        d	      fdt        d      fddgdgdfddddgifg	}|D ]  \  }}d|i}t        j                  d|      }t        |t              s2|j                         D ]m  }||   }| j                  ||       t        |      dkD  r"| j                  |j                  |      |       J| j                  |j                  |      |d          o  y )Nz	x=1;y=2.0rK   rL   )rO   rP   zx=1;y=2.0;z=2-3.%2b0rM   rN   ;rA   z;;z=;arD   z;b=azb;=arF   za=a+b;b=b+crG   rH   rI   za=a+b;a=b+arC   rJ   r.   )	separatorrx   r4   r   )
r8   r   r{   r    r|   r   r   r7   r   r   )r   parse_semicolonr   r   r<   r   rj   r   s           r   test_separatorzCgiTests.test_separator   sG   UG45#C5whZ%PQ*234:345J567Z 567Z 6785'89S5%.12

 , 
	JLD&!4(C!!C!=B&$'!;;= JC!'JMM#r*:*((S)9:F((S)9:a=IJ	
	Jr   )categoryc                 @   t         j                  d       t               t         _        t         j	                  dd       t         j                  dd       | j                  t         j                  j                         d       t        j                  j                  t        j                        rod t         _        t        j                  t         _        t         j	                  dd       | j                  t         j                         t         j                  d       y y )NTestingz%szTesting initlog 1zTesting log 2z Testing initlog 1
Testing log 2
zTesting log 3zTesting log 4)r   logr   logfpinitlogr   r   ospathexistsdevnulllogfile
addCleanupcloselogr   s    r   test_logzCgiTests.test_log   s    	J	D-.o&++-/ST77>>"**%CI**CKKKo.OOCLL)GGO$ &r   c                     G d d      } |t        j                  d            }| j                  |j                         |j	                  ddz         |j                  d       ddi}t        j                  ||	      }| j                  |j                  j                         | j                  |j                  d
       |j                          y )Nc                        e Zd Zd ZddZd Zy)=CgiTests.test_fieldstorage_readline.<locals>.TestReadlineFilec                      || _         d| _        y rp   )r   numcalls)r   r   s     r   r   zFCgiTests.test_fieldstorage_readline.<locals>.TestReadlineFile.__init__   s     	 !r   Nc                     | xj                   dz  c_         |r| j                  j                  |      S | j                  j                         S )Nr4   )r   r   readline)r   sizes     r   r   zFCgiTests.test_fieldstorage_readline.<locals>.TestReadlineFile.readline   s;    "99--d3399--//r   c                 v    | j                   d   }t        ||      }t        |t              st	        | ||       |S )Nr   )__dict__r(   r    intsetattr)r   r   r   rC   s       r   r*   zICgiTests.test_fieldstorage_readline.<locals>.TestReadlineFile.__getattr__  s7    }}V,D$'!!S)D$*r   r   )r   r   r   r   r   r*   r   r   r   TestReadlineFiler      s    "0r   r   zwb+s   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxi   r   r-   r   r=   rx      )tempfileTemporaryFiler   closewriterz   r   r{   r   assertGreaterr   )r   r   fr<   r   s        r   test_fieldstorage_readlinez#CgiTests.test_fieldstorage_readline   s    
	 	& X33E:; 	
T!"	q	&C0& 	1::q)		r   c                    ddj                  t              dd}t        t        j	                  d            }t
        j                  ||d      }| j                  t        |j                        d       dd d	d
dd dd
dddd
dd dd
g}t        t        |j                              D ]H  }||   j                         D ]0  \  }}t        |j                  |   |      }| j                  ||       2 J y )Nr/    multipart/form-data; boundary={}r   r-   r2   r3   r0   rx   ry      r   r   r   filenamer   r   r?   r   test.txtr   r   r   formatr   r   r   r6   r   r{   r   r7   rr   ranger}   r(   	r   r<   r=   r   r   rO   r   expgots	            r   test_fieldstorage_multipartz$CgiTests.test_fieldstorage_multipart  s     %>EEhO#% X__Y/0b#	BRWWq)4@!dB? ZARS"tWEG s277|$ 	+A )//+ +3bggaj!,  c*+	+r   c                    ddj                  t              dd}t        dt        j	                  d      z         }t
        j                  ||d      }| j                  t        |j                        d       d	d d
ddd dddddddd ddg}t        t        |j                              D ]H  }||   j                         D ]0  \  }}t        |j                  |   |      }| j                  ||       2 J y )Nr/   r   560r   s   
r0   r   r   r   r   r   r   r?   r   r   r   r   r   r   r   s	            r   .test_fieldstorage_multipart_leading_whitespacez7CgiTests.test_fieldstorage_multipart_leading_whitespace(  s    $>EEhO#% Wxy99:b#	BRWWq)4@!dB? ZARS"tWEG s277|$ 	+A )//+ +3bggaj!,  c*+	+r   c                    ddj                  t              dd}dD ]  }t        t        j	                  |            }t
        j                  |||      }| j                  t        |j                        d       dd d	d
g}t        t        |j                              D ]H  }||   j                         D ]0  \  }}t        |j                  |   |      }	| j                  |	|       2 J  y )Nr/   r   r   r   )z
iso-8859-1zutf-8r   r4   r   u   çñr   )r   r   r   POSTDATA_NON_ASCIIr6   r   r{   r   r7   rr   r   r}   r(   )
r   r<   ry   r=   r   r   rO   r   r   r   s
             r   %test_fieldstorage_multipart_non_asciiz.CgiTests.test_fieldstorage_multipart_non_ascii;  s     &>EEhO"$ / 	/H+228<=B!!"c8!DBS\1-"t^LMF3rww<( /$Qioo/ /FAs!"''!*a0C$$S#.//	/r   c                      d}d  _          fd} |d|dz
  z          |d|dz
  z  dz           |d|dz
  z  dz   d|dz
  z  z          y )Ni   c                     dj                  dd      | z  }t        t        |            ddd}j                  t	        ||      d| j                  d      i       y )	Nzp---123
Content-Disposition: form-data; name="upload"; filename="fake.txt"
Content-Type: text/plain

%s
---123--

z
"multipart/form-data; boundary=-123r/   r3   r2   r-   uploadr   )replacer   r7   r   r   r6   )contentr   rx   r   s      r   checkz;CgiTests.test_fieldstorage_multipart_maxline.<locals>.checkN  sd     GD&G$D %(D	N$H$*G
 Zg6&x(@ACr   rO   r4   rP   )maxDiff)r   maxliner  s   `  r   #test_fieldstorage_multipart_maxlinez,CgiTests.test_fieldstorage_multipart_maxlineJ  sb    	C 	cWq[!"cWq[!D()cWq[!D(3'A++>>?r   c                 8   ddj                  t              t        t        t                    d}t        t        j                  d            }t        j                  ||d      }| j                  t        |j                        d       | j                  |j                  d   j                  d       | j                  |j                  d   j                  d	       | j                  |j                  d
   j                  d       |j                  d
   j                  }| j                  t        |      d       d dddd dddg}t        t        |            D ]>  }||   j                         D ]&  \  }}t        ||   |      }	| j                  |	|       ( @ y )Nr/   r   r   r0   r   r   r   r   Larryr4   filesz	file1.txts   ... contents of file1.txt ...r   z	file2.gifs   ...contents of file2.gif...)r   BOUNDARY_W3r   r7   POSTDATA_W3r   r6   r   r{   r   rr   r   r   r   r}   r(   )
r   r<   r=   r   r  r   rO   r   r   r   s
             r   test_fieldstorage_multipart_w3cz(CgiTests.test_fieldstorage_multipart_w3ca  s`    %>EEkR!#k"235 [''	23b#	BRWWq)-8))73'2
  UQ'[Ccd[Cabds5z" 	+A )//+ +3eAh*  c*+	+r   c                    d}d}ddj                  |      t        t        |            d}t        |j	                  d            }t
        j                  ||d      }| j                  t        |j                        d       | j                  |j                  d	   j                  d
       | j                  |j                  d	   j                  d       y )Nr   z_--JfISa01
Content-Disposition: form-data; name="submit-name"
Content-Length: 5

Larry
--JfISa01r/   r   r   r0   r   r4   r   r   r  )r   r   r7   r   r6   r   r{   r   rr   r   r   )r   r   r   r<   r=   r   s         r   %test_fieldstorage_part_content_lengthz.CgiTests.test_fieldstorage_part_content_lengthv  s     %>EEhO!#h-02 X__Y/0b#	BRWWq)-8))73r   c                     t        d      }dd|d}t        j                  ||      }| j                  t	        |d   j
                  j                               d       y )Nsa   --MyBoundary
Content-Disposition: form-data; name="my-arg"; filename="foo"

Test

--MyBoundary--
r/   z(multipart/form-data; boundary=MyBoundary)r-   r2   z
wsgi.inputr   zmy-arg   )r   r   r{   r   r7   r   read)r   r=   r<   fieldss       r   .test_field_storage_multipart_no_content_lengthz7CgiTests.test_field_storage_multipart_no_content_length  sc       %F

 !!"c!2VH-22779:A>r   c                    t        d      }ddi}t        j                  ||      5 }|j                  j	                         }| j                  |j                  j                         d d d        | j                  j                  j                         | j                  d       | j                  t        d      5  |j                  j	                          d d d        y # 1 sw Y   zxY w# 1 sw Y   y xY w)Ns
   xxxxxxxxxxr-   r   r   
xxxxxxxxxxzI/O operation on closed file)r   r   r{   r   r  r   closedr   r   assertRaisesRegexr8   )r   r=   r<   r   r  s        r   $test_fieldstorage_as_context_managerz-CgiTests.test_fieldstorage_as_context_manager  s    Y'S1 	-RggllnGRWW^^,	- 	'(+##J0NO 	GGLLN	 		- 	-
	 	s   A C <C, C),C5value1value2xvalue2yvalue3value4)key1key2key3key4c                     d}t        t        |            dddd}t        ||      }| j                  | j                  |       y )Nz$key2=value2x&key3=value3&key4=value4r1   zkey1=value1&key2=value2yr/   r3   r2   r.   r-   r   r7   r   r   
_qs_resultr   r   rx   r   s       r   testQSAndUrlEncodezCgiTests.testQSAndUrlEncode  sD    5 #CI C : &	
 tW%!,r   c                 V   dj                  dgdz        }t        t        |            ddd}| j                  t              5  t
        j                  t        |j                               |d       d d d        d	}t        t        |            d
ddd}| j                  t              5  t
        j                  t        |j                               |d       d d d        t
        j                  t        |j                               |d       y # 1 sw Y   xY w# 1 sw Y   ExY w)Nr@   za=a   r1   r/   r  
   )r=   rx   max_num_fieldsz---123
Content-Disposition: form-data; name="a"

3
---123
Content-Type: application/x-www-form-urlencoded

a=4
---123
Content-Type: application/x-www-form-urlencoded

a=5
---123--
r  za=1&a=2r-  r   r  )	joinr   r7   r   r8   r   r{   r   r6   )r   r   rx   s      r   test_max_num_fieldszCgiTests.test_max_num_fields  s   xx
#!#d)n?$
 z* 	4;;=)!  	 !$CI D ) &	
 z* 	4;;=)   	 	t{{}% 	 	
O	 	B	 	s   0D+0DDD(c                     d}t        t        |            dddd}t        ||      }| j                  | j                  |       y )Nz---123
Content-Disposition: form-data; name="key2"

value2y
---123
Content-Disposition: form-data; name="key3"

value3
---123
Content-Disposition: form-data; name="key4"

value4
---123--
r  key1=value1&key2=value2xr/   r-  r.  r0  s       r   testQSAndFormDatazCgiTests.testQSAndFormData  sH     !$CI D : &	
 tW%!,r   c                     d}t        t        |            dddd}| j                  j                         }|j	                  ddi       t        ||      }| j                  ||       y )NaE  ---123
Content-Disposition: form-data; name="key2"

value2y
---123
Content-Disposition: form-data; name="key3"

value3
---123
Content-Disposition: form-data; name="key4"

value4
---123
Content-Disposition: form-data; name="upload"; filename="fake.txt"
Content-Type: text/plain

this is the content of the fake file

---123--
r  r9  r/   r-  r	  s%   this is the content of the fake file
)r   r7   r/  copyupdater   r   )r   r   rx   r   r   s        r   testQSAndFormDataFilezCgiTests.testQSAndFormDataFile  so    * !$CI D : &	
 %%'?
 	 tW%#r   c                    | j                  t        j                  d      di f       | j                  t        j                  d      di f       | j                  t        j                  d      dddif       | j                  t        j                  d      dddif       | j                  t        j                  d      ddd	d
f       | j                  t        j                  d      dddif       | j                  t        j                  d      dddif       | j                  t        j                  d      ddddf       | j                  t        j                  d      ddddf       y )Nr   ztext/vnd.just.made.this.up ; ztext/vnd.just.made.this.upztext/plain;charset=us-asciicharsetzus-asciiztext/plain ; charset="us-ascii"z,text/plain ; charset="us-ascii"; another=optopt)r@  anotherz attachment; filename="silly.txt"
attachmentr   z	silly.txtz#attachment; filename="strange;name"zstrange;namez-attachment; filename="strange;name";size=123;123)r   r   z-form-data; name="files"; filename="fo\"o;bar"z	form-datar  zfo"o;bar)r   r   )r   r   parse_headerr   s    r   test_parse_headerzCgiTests.test_parse_header%  s   \*2	  	<=)2.	0 	:;Iz23	5 	>?Iz23	5 	KLzeDE	G 	?@J45	7 	BCJ78	: 	LMFG	I 	MN7
CD	Fr   c                 D    h d}t        j                  | t        |       y )N>	   r   dologr   nologmaxlenr   r   r   valid_boundary)not_exported)r   check__all__r   )r   rL  s     r   test_allzCgiTests.test_allB  s    ( 	T3\Br   N)r   r   r   r   r   r   r   r   r   r   r   ignore_warningsDeprecationWarningr   r   r   r   r  r  r  r  r  r"  r/  r1  r7  r:  r>  rF  rN  r   r   r   r   r   v   s    ++U$/J4J0 %_$$.@A% B%#J+$+&/@.+*4$?"	 I&	J	-4
l-0 $DF:Cr   r   z6---------------------------721837373350705526688164684a  -----------------------------721837373350705526688164684
Content-Disposition: form-data; name="id"

1234
-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="title"


-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

Testing 123.

-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="submit"

 Add 
-----------------------------721837373350705526688164684--
u   -----------------------------721837373350705526688164684
Content-Disposition: form-data; name="id"

çñ
-----------------------------721837373350705526688164684
AaB03xa  --AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y

--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of file2.gif...
--BbC04y--
--AaB03x--
__main__)!r   r   r   unittestcollectionsr   ior   r   testr   test.supportr   import_deprecatedr   r
   r   r>   r8   r   rn   rt   rv   r   TestCaser   r   r   r  r  r  r   mainr   r   r   <module>r[     s&   	 
   "    ('o''.  
' ' (& H**
+,	:+,-"I
BK	B;
J,-.
J,-.
J-./
S3%LdSE]sUGnfXZ-./Z./05'01S5%.)*C5whZHI w<D:u	56Gl		 Y
i7(Z	A( T! +
PCx   PCf D*  . zHMMO r   