B
    u9aV              !   @   sD  d dl mZ d dlZ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 dZe e_G dd	 d	Zd
d Zdedfdedfdedfdedfdedfdi 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d0d,gd-gd.gd/fd1d,gd-gd.gd/fd2d3gd4gd5gd6gd7gd8gd9gd:gd;fd<d=gd>gd?gd,gd@gdAgdBfgZdCdD ZdEdF ZdGdH ZdIdJ ZG dKdL dLejZdMZdNZdOZdPZdQZedRkr@e   dS )S    )check_warningsN)
namedtuple)StringIOBytesIO)supportc               @   s   e Zd Zg ZejZdS )HackedSysModuleN)__name__
__module____qualname__argvsysstdin r   r   /usr/lib/python3.7/test_cgi.pyr      s   r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ComparableExceptionc             C   s
   || _ d S )N)err)selfr   r   r   r   __init__   s    zComparableException.__init__c             C   s
   t | jS )N)strr   )r   r   r   r   __str__   s    zComparableException.__str__c             C   s*   t |tstS | jj|jko(| jj|jkS )N)
isinstance	ExceptionNotImplementedr   	__class__args)r   ZanExcr   r   r   __eq__   s    
zComparableException.__eq__c             C   s   t | j|S )N)getattrr   )r   attrr   r   r   __getattr__!   s    zComparableException.__getattr__N)r   r	   r
   r   r   r   r   r   r   r   r   r      s   r   c          
   C   s   i }|dkr"d }d|d< | |d< nD|dkrZt | d}d|d< d|d< tt| |d< ntd	| ytj||d
dS  tk
r } zt|S d }~X Y nX d S )NGETREQUEST_METHODQUERY_STRINGPOSTzlatin-1z!application/x-www-form-urlencodedCONTENT_TYPECONTENT_LENGTHzunknown method: %s   )Zstrict_parsing)	r   encoder   len
ValueErrorcgiparser   r   )bufmethodenvfpr   r   r   r   do_test$   s    
r/    zbad query field: ''&z&&;z;&;=z=&=z=;=z=aaz&=az=a&z=&azbad query field: 'a'zb=abzb+=azb za=b=aza=+b=az b=az&b=azb&=azbad query field: 'b'za=a+b&b=b+cza bzb c)r4   r5   za=a+b&a=b+azb azx=1&y=2.0&z=2-3.%2b01z2.0z2-3.+0)xyzzx=1;y=2.0&z=2-3.%2b0zx=1;y=2.0;z=2-3.%2b0zHbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=envZ400006rZ	964546263z130003.300038zen-USZ 0bb2e248638833d48cb7fed300000f1br-   Zbustomer)z1Hbc5161168c542333633315dee1182227:key_store_seqidZcuyerZexpireZkidZlobaleZorder_idssZviewzQgroup_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=BrowseZBrowseZ31392Z100Z5470Zcustom)ZSUBMITZ_assigned_toZ	_categoryZ_statusZgroup_idsetc             C   s   t | tdS )N)key)sortedrepr)seqr   r   r   norme   s    rA   c             C   s   dd | D S )Nc             S   s   g | ]}|d  qS )r   r   ).0pr   r   r   
<listcomp>i   s    zfirst_elts.<locals>.<listcomp>r   )listr   r   r   
first_eltsh   s    rF   c             C   s   dd | D S )Nc             S   s    g | ]}|d  |d d  fqS )r   r%   r   )rB   rC   r   r   r   rD   l   s    z%first_second_elts.<locals>.<listcomp>r   )rE   r   r   r   first_second_eltsk   s    rG   c             C   sl   d}t | |}|d tj|||d}i }x6t| D ]&\}}t|trZ|	|p^|j
||< q>W |S )Nzlatin-1r   )r.   environencoding)r   r&   seekr)   FieldStoragedictitemsr   rE   Zgetlistvalue)datarH   rI   Z
fake_stdinZformresultkvr   r   r   
gen_resultn   s    
"rS   c               @   s   e Z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d Zdd Zdd Zdd d!gd"d#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5S )6CgiTestsc             C   sP   t td}tddd}t||}dgdgdgdgd}| || d S )	Nlatin1558)boundaryzCONTENT-LENGTHz Add 1234s   Testing 123.
r0   )submitidfiletitle)r   POSTDATAr&   BOUNDARYr)   parse_multipartassertEqual)r   r.   r-   rP   expectedr   r   r   test_parse_multipart|   s    zCgiTests.test_parse_multipartc             C   s   d}d}t |d}|dtt|dd}tj||ddd}d	d
gi}| || | dd|d	 d dd d S )NJfISa01u]   --JfISa01
Content-Disposition: form-data; name="submit-name"
Content-Length: 3

☃
--JfISa01utf8rU   )rW   zCONTENT-LENGTHasciisurrogateescape)rI   errorszsubmit-nameu	   u   ☃r   )r   r&   r   r'   r)   r_   r`   )r   r^   r]   r.   r-   rP   ra   r   r   r   %test_parse_multipart_invalid_encoding   s    

z.CgiTests.test_parse_multipart_invalid_encodingc             C   s^   t  }| | | dt| | t|t|  |jt	ddd | 
| d S )NrK   ZMockFieldStoragenameZ
fieldvalue)r)   rK   assertFalseassertInr?   r`   rE   keysappendr   
assertTrue)r   fsr   r   r   test_fieldstorage_properties   s    
z%CgiTests.test_fieldstorage_propertiesc             C   sL   | j ttjdddid |  ttjdd tjddid	}|  tt| d S )
Nznot-a-file-objr    PUT)rH   ZfooZbarzcontent-typez
text/plain)Zheaders)assertRaises	TypeErrorr)   rK   bool)r   ro   r   r   r   test_fieldstorage_invalid   s
    z"CgiTests.test_fieldstorage_invalidc          	   C   s^   t  L t ddt | dtd | dtd | dtdd	 W d Q R X d S )
Nignorezcgi\.escapeztest &amp; stringztest & stringz&lt;test string&gt;z<test string>z&quot;test string&quot;z"test string"T)warningscatch_warningsfilterwarningsDeprecationWarningr`   r)   escape)r   r   r   r   test_escape   s    
zCgiTests.test_escapec             C   s  xt D  ]\}}t|d}| ||dt|  t|d}| ||dt|  d|i}tj|d}t|tr| t|t| | 	|
 |
  | |ddd xZ|
 D ]N}|| }| || t|d	kr| ||| q| |||d
  qW qW d S )Nr   zError parsing %s method GETr"   zError parsing %s method POSTr!   )rH   znonexistent fielddefaultr%   r   )parse_strict_test_casesr/   r`   r?   r)   rK   r   rL   r'   ZassertCountEqualrl   getvaluerk   )r   Zorigexpectdr-   ro   r=   Z
expect_valr   r   r   test_strict   s"    


zCgiTests.test_strictc             C   s~   t d t t _t dd t dd | t j d tj	tj
rzd t _tj
t _t dd | t j t d d S )NZ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closelog)r   r   r   r   test_log   s    
zCgiTests.test_logc             C   s~   G dd d}|t d}| |j |dd  |d ddi}tj||d	}| |jj | 	|j
d
 |  d S )Nc               @   s&   e Zd Zdd ZdddZdd ZdS )	z=CgiTests.test_fieldstorage_readline.<locals>.TestReadlineFilec             S   s   || _ d| _d S )Nr   )r[   numcalls)r   r[   r   r   r   r      s    zFCgiTests.test_fieldstorage_readline.<locals>.TestReadlineFile.__init__Nc             S   s,   |  j d7  _ |r| j|S | j S d S )Nr%   )r   r[   readline)r   sizer   r   r   r      s    zFCgiTests.test_fieldstorage_readline.<locals>.TestReadlineFile.readlinec             S   s.   | j d }t||}t|ts*t| || |S )Nr[   )__dict__r   r   intsetattr)r   ri   r[   r4   r   r   r   r      s
    


zICgiTests.test_fieldstorage_readline.<locals>.TestReadlineFile.__getattr__)N)r   r	   r
   r   r   r   r   r   r   r   TestReadlineFile   s   
r   zwb+s   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxi   r   r    rq   )r.   rH      )tempfileZTemporaryFiler   closewriterJ   r)   rK   r[   ZassertGreaterr   )r   r   fr-   ro   r   r   r   test_fieldstorage_readline   s    
z#CgiTests.test_fieldstorage_readlinec       	      C   s   dd tdd}ttd}tj||dd}| t|j	d dd d	d
dd dd
dddd
dd dd
g}xLt
t|j	D ]:}x4||  D ]$\}}t|j	| |}| || qW q~W d S )Nr"   z multipart/form-data; boundary={}rV   )r    r#   r$   zlatin-1)rH   rI      rZ   rX   )ri   filenamerN   r\   r0   r[   ztest.txts   Testing 123.
rY   z Add )formatr^   r   r]   r&   r)   rK   r`   r'   rE   rangerM   r   )	r   r-   r.   ro   r   r7   rQ   expgotr   r   r   test_fieldstorage_multipart   s    


z$CgiTests.test_fieldstorage_multipartc       	      C   s   dd tdd}tdtd }tj||dd}| t|j	d d	d d
ddd dddddddd ddg}xLt
t|j	D ]:}x4||  D ]$\}}t|j	| |}| || qW qW d S )Nr"   z multipart/form-data; boundary={}Z560)r    r#   r$   s   
zlatin-1)rH   rI   r   rZ   rX   )ri   r   rN   r\   r0   r[   ztest.txts   Testing 123.
rY   z Add )r   r^   r   r]   r&   r)   rK   r`   r'   rE   r   rM   r   )	r   r-   r.   ro   r   r7   rQ   r   r   r   r   r   .test_fieldstorage_multipart_leading_whitespace  s    


z7CgiTests.test_fieldstorage_multipart_leading_whitespacec       
      C   s   dd tdd}xdD ]}tt|}tj|||d}| t|j	d dd d	d
g}xLt
t|j	D ]:}x4||  D ]$\}}t|j	| |}	| |	| q|W qjW qW d S )Nr"   z multipart/form-data; boundary={}rV   )r    r#   r$   )z
iso-8859-1zutf-8)rH   rI   r%   rZ   u   çñ)ri   r   rN   )r   r^   r   POSTDATA_NON_ASCIIr&   r)   rK   r`   r'   rE   r   rM   r   )
r   r-   rI   r.   ro   r   r7   rQ   r   r   r   r   r   %test_fieldstorage_multipart_non_ascii!  s    
z.CgiTests.test_fieldstorage_multipart_non_asciic                s^   d}d  _  fdd}|d|d   |d|d  d  |d|d  d d|d    d S )Ni   c                sD   d dd|  }tt|ddd} t||d| di d S )	Nzp---123
Content-Disposition: form-data; name="upload"; filename="fake.txt"
Content-Type: text/plain

%s
---123--

z
z"multipart/form-data; boundary=-123r"   )r$   r#   r    uploadrU   )replacer   r'   r`   rS   r&   )contentrO   rH   )r   r   r   check4  s    
z;CgiTests.test_fieldstorage_multipart_maxline.<locals>.checkr7   r%   r8   )ZmaxDiff)r   Zmaxliner   r   )r   r   #test_fieldstorage_multipart_maxline0  s    z,CgiTests.test_fieldstorage_multipart_maxlinec       
      C   s  dd ttttd}ttd}tj||dd}| 	t|j
d | 	|j
d jd | 	|j
d jd	 | 	|j
d
 jd |j
d
 j}| 	t|d d dddd dddg}xHtt|D ]8}x2||  D ]"\}}t|| |}	| 	|	| qW qW d S )Nr"   z multipart/form-data; boundary={})r    r#   r$   zlatin-1)rH   rI   r   r   zsubmit-nameLarryr%   filesz	file1.txts   ... contents of file1.txt ...)ri   r   rN   z	file2.gifs   ...contents of file2.gif...)r   BOUNDARY_W3r   r'   POSTDATA_W3r   r&   r)   rK   r`   rE   ri   rN   r   rM   r   )
r   r-   r.   ro   r   r   r7   rQ   r   r   r   r   r   test_fieldstorage_multipart_w3cG  s"    
z(CgiTests.test_fieldstorage_multipart_w3cc             C   s~   d}d}dd |tt|d}t|d}tj||dd}| t|jd | |jd	 j	d
 | |jd	 j
d d S )Nrc   z_--JfISa01
Content-Disposition: form-data; name="submit-name"
Content-Length: 5

Larry
--JfISa01r"   z multipart/form-data; boundary={})r    r#   r$   zlatin-1)rH   rI   r%   r   zsubmit-namer   )r   r   r'   r   r&   r)   rK   r`   rE   ri   rN   )r   r^   r]   r-   r.   ro   r   r   r   %test_fieldstorage_part_content_length\  s    z.CgiTests.test_fieldstorage_part_content_lengthc          	   C   s   t d}ddi}tj||d}|j }| |jj W d Q R X | |jj | |d | 	t
d |j  W d Q R X d S )Ns
   xxxxxxxxxxr    rq   )r.   rH   Z
xxxxxxxxxxzI/O operation on closed file)r   r)   rK   r[   readrj   closedrn   r`   ZassertRaisesRegexr(   )r   r.   r-   ro   r   r   r   r   $test_fieldstorage_as_context_managern  s    
z-CgiTests.test_fieldstorage_as_context_managerZvalue1Zvalue2xZvalue2yZvalue3Zvalue4)Zkey1Zkey2Zkey3Zkey4c             C   s6   d}t t|dddd}t||}| | j| d S )Nz$key2=value2x&key3=value3&key4=value4z!application/x-www-form-urlencodedzkey1=value1&key2=value2yr"   )r$   r#   r!   r    )r   r'   rS   r`   
_qs_result)r   rO   rH   rR   r   r   r   testQSAndUrlEncode  s    

zCgiTests.testQSAndUrlEncodec          	   C   s   d dgd }tt|ddd}| t tjt| |dd W d Q R X d	}tt|d
ddd}| t tjt| |dd W d Q R X tjt| |dd d S )Nr1   za=a   z!application/x-www-form-urlencodedr"   )r$   r#   r    
   )r.   rH   Z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--
z"multipart/form-data; boundary=-123za=1&a=2)r$   r#   r!   r    r      )	joinr   r'   rr   r(   r)   rK   r   r&   )r   rO   rH   r   r   r   test_max_num_fields  s.    




zCgiTests.test_max_num_fieldsc             C   s6   d}t t|dddd}t||}| | j| d S )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--
z"multipart/form-data; boundary=-123zkey1=value1&key2=value2xr"   )r$   r#   r!   r    )r   r'   rS   r`   r   )r   rO   rH   rR   r   r   r   testQSAndFormData  s    

zCgiTests.testQSAndFormDatac             C   sL   d}t t|dddd}| j }|ddi t||}| || d S )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--
z"multipart/form-data; boundary=-123zkey1=value1&key2=value2xr"   )r$   r#   r!   r    r   s%   this is the content of the fake file
)r   r'   r   copyupdaterS   r`   )r   rO   rH   rP   rR   r   r   r   testQSAndFormDataFile  s    



zCgiTests.testQSAndFormDataFilec          	   C   s<   t dtf& | dgdgdgdtd W d Q R X d S )Nz=cgi.parse_qs is deprecated, use urllib.parse.parse_qs insteadA1B3B2)r4   Br5   za=A1&b=B2&B=B3)r   rz   r`   r)   Zparse_qs)r   r   r   r   test_deprecated_parse_qs  s    
z!CgiTests.test_deprecated_parse_qsc          	   C   s4   t dtf | dddgtd W d Q R X d S )Nz?cgi.parse_qsl is deprecated, use urllib.parse.parse_qsl instead)r4   r   )r5   r   )r   r   za=A1&b=B2&B=B3)r   rz   r`   r)   Z	parse_qsl)r   r   r   r   test_deprecated_parse_qsl  s    
z"CgiTests.test_deprecated_parse_qslc             C   s   |  tddi f |  tddi f |  tddddif |  tddddif |  tdddd	d
f |  tddddif |  tddddif |  tdddddf |  tdddddf d S )Nz
text/plainz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   Zanotherz attachment; filename="silly.txt"Z
attachmentr   z	silly.txtz#attachment; filename="strange;name"zstrange;namez-attachment; filename="strange;name";size=123;Z123)r   r   z-form-data; name="files"; filename="fo\"o;bar"z	form-datar   zfo"o;bar)ri   r   )r`   r)   Zparse_header)r   r   r   r   test_parse_header  s6    

zCgiTests.test_parse_headerc          	   C   s*   ddddddddd	h	}t j| t|d
 d S )Nr   r   r   ZdologZnologr   r   maxlenZvalid_boundary)	blacklist)r   Zcheck__all__r)   )r   r   r   r   r   test_all%  s    
zCgiTests.test_allN)r   r	   r
   rb   rh   rp   ru   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rT   z   s6   		%6"rT   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
Z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__)!Ztest.supportr   r)   r   r   r   Zunittestrw   collectionsr   ior   r   Ztestr   r   r   r/   r(   r~   rA   rF   rG   rS   ZTestCaserT   r^   r]   r   r   r   r   mainr   r   r   r   <module>   s   










   4
