
    Fc#                     B   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	 d dl
Z
d dlZd dlmZ ej                                        s
 e	             d Z e            ZdZdZh dZd	d
hZddhZd Zd Zd Zd Zd Z G d de          Zd Zedk    r e             dS dS )    N)Counter)c_REDc_GREENc_DARK_YELLOWswitch_colour_off)TestCasec                  \   	 t          j        g dt           j        t           j        d          } n^# t           j        $ rL}t	          t          d|                      t	          d           t          j        d           Y d }~nd }~ww xY w| j        	                                
                                }t          j                            t          j                            t          j                            t                     d                    t          j                            t          j                            t          j                            t                     d                    f}||vr0t	          t          d	                     t          j        d           |S )
N)gitz	rev-parsez--show-toplevel
   stdoutstderrtimeout)Error running git (is this a git tree?): .This test is only useful in a git working tree   z../../..z../../../..z/It looks like we have found the wrong git tree!)
subprocessrunPIPESubprocessErrorprintr   sysexitr   decodestripospathabspathjoindirname__file__)perootshould_be_rootss       :/usr/lib/python3/dist-packages/samba/tests/source_chars.py
_find_rootr'       sl   NBBB",/",/#%' ' ' %   eCCCDDEEE>???
 8??""$$D 	RW__X%>%>%/1 1 	2 	2
RW__X%>%>%24 4 	5 	5O ?""eEFFGGGKs   .1 B ABB)z examples/validchars/validchr.comzexamples/tridge/smb.confz$source3/selftest/ktest-krb5_ccache-2z$source3/selftest/ktest-krb5_ccache-3testdata/source-chars-bad.c)z.^third_party/heimdal/lib/hcrypto/passwd_dialogz$^third_party/heimdal/lib/hx509/data/z^third_party/heimdal/poz(^third_party/heimdal/tests/kdc/hdb-mitdbz*^third_party/heimdal/lib/asn1/fuzz-inputs/>   gzbmpcercrlcrtdatdergpgicoldbp12pdfpempngregreqsxdtdbtifdumpkeytabcorruptSAMBABACKUPu   ​u   ﻿z2third_party/heimdal/lib/wind/NormalizationTest.txttestdata/source-chars-bidi.pyc                  f   	 t          j        ddt          ddgt           j        t           j        d          } nL# t           j        $ r:}t          t          d|                      t          d           g cY d }~S d }~ww xY w| j                            d	          }d
 |d d         D             S )Nr
   z-Czls-filesz-zr   r   r   r       c                 6    g | ]}|                                 S  )r   ).0xs     r&   
<listcomp>z!get_git_files.<locals>.<listcomp>   s     ///1AHHJJ///    )	r   r   ROOTr   r   r   r   r   split)r"   r#   	filenamess      r&   get_git_filesrM   w   s    NE $& " #-/",/#%' ' ' %   eCCCDDEEE>???						
 w''I//	#2#////s   69 B/A=7B=Bc               #     K   t                      } | D ]}d}|t          v r t          t          d|                      -t          D ]}t          j        ||          }|r n|r t          t          d|                      rd|v rE|                    dd          d         }|t          v r t          t          d|                      |V  d S )NFzignoring (exact) zignoring (via RE) .r   z	ignoring )	rM   IGNORED_FILESr   r   
IGNORED_RErematchrsplitIGNORED_EXTENSIONS)rL   nameignoreignoredexts        r&   iter_source_filesrZ      s     I  =  - :D : :;;<<<! 	 	Ghw--F   	- ;T ; ;<<===$;;++c1%%a(C(((m$6$6$677888



+ rH   c                 @    dD ]}t          j        ||           r dS dS )N)z!^source4/setup/ad-schema/\w+.ldf$z.^source4/setup/display-specifiers/D[\w-]+.txt$z^third_party/heimdal/cf/pkg.m4$z)^third_party/heimdal/doc/standardisation/TF)rR   rS   )rV   patterns     r&   is_latin1_filer]      s:       8GT"" 	44	5rH   c                     t          | d          5 }|                                }d d d            n# 1 swxY w Y   d}|D ]}|dk    r|r dS d}d}dS )NrbF   T)openread)fullnamefbin_seqcs        r&   is_bad_latin1_filerh      s    
 
h		 FFHH              F  t88 ttFFFF5s   266c                 P    t          j        |           dk    rdS | t          v rdS dS )NCfFT)ucategorySAFE_FORMAT_CHARS)rg   s    r&   is_bad_charrn      s0    z!}}uu4rH   c                        e Zd Zd Zd Zd ZdS )CharacterTestsc                    t                      D ]i}t          j                            t          |          }	 t          |          5 }|                                }ddd           n# 1 swxY w Y   nj# t          $ r]}t          |          r)t          |          r| 
                    d| d           n| 
                    d| d|            Y d}~nd}~ww xY wt                      }t          |          D ]P}t          |          r| 
                    | d           |                    t          j        |                     Qd|v r(d|v r$|t           vr| 
                    | d	| d
           kdS )ah  This test tries to ensure that no source file has unicode control
        characters that can change the apparent order of other
        characters. These characters could make code appear to have
        different semantic meaning it really does.

        This issue is sometimes called "Trojan Source", "CVE-2021-42574",
        or "CVE-2021-42694".
        Nlatin-1 file ! has long sequences of high byteszcould not decode : z' has potentially bad format characters!LRz has LTR and RTL text ())rZ   r   r   r   rJ   ra   rb   UnicodeDecodeErrorr]   rh   failsetrn   addrk   bidirectional
BIDI_FILES)selfrV   rc   rd   sr#   dirsrg   s           r&   test_no_unexpected_format_charsz.CharacterTests.test_no_unexpected_format_chars   s    &'' 	G 	GDw||D$//H?(^^ !qA! ! ! ! ! ! ! ! ! ! ! ! ! ! !% 	? 	? 	? "$'' ?)(33 3		 #2$ #2 #2 #2 3 3 3 II=$==!==>>>	? 55DVV - -q>> PIINNNOOO++,,,,d{{sd{{z))IIEEdEEEFFF1	G 	Gs<   A4A(A4(A,	,A4/A,	0A44
C>ACCc                 R   dD ]\  }}t           j                            t          |          }t	          |          5 }|                                }ddd           n# 1 swxY w Y   t          |          }d |D             }|                     t          |          |           dS )Test the test))r(      Nc                 0    g | ]}t          |          |S rD   )rn   )rE   rg   s     r&   rG   zGCharacterTests.test_unexpected_format_chars_do_fail.<locals>.<listcomp>   s#    <<<q[^^<<<<rH   )	r   r   r   rJ   ra   rb   rz   assertEquallen)r~   rV   n_badrc   rd   r   chars	bad_charss           r&   $test_unexpected_format_chars_do_failz3CharacterTests.test_unexpected_format_chars_do_fail   s    
 	4 	4KD% w||D$//Hh 1FFHH              FFE<<E<<<IS^^U3333	4 	4s   AA!	$A!	c                    dD ]}t           j                            t          |          }t	          |          5 }|                                }ddd           n# 1 swxY w Y   t                      }t          |          D ])}|                    t          j	        |                     *| 
                    d|           | 
                    d|           dS )r   )r@   Nru   rv   )r   r   r   rJ   ra   rb   rz   r{   rk   r|   assertIn)r~   rV   rc   rd   r   r   rg   s          r&   test_unexpected_bidi_failsz)CharacterTests.test_unexpected_bidi_fails   s	   
 	% 	%D w||D$//Hh 1FFHH               55DVV - -++,,,,MM#t$$$MM#t$$$$	% 	%s   AA	!A	N)__name__
__module____qualname__r   r   r   rD   rH   r&   rp   rp      sE        !G !G !GF
4 
4 
4% % % % %rH   rp   c                     t                      } t                      D ]r}t          j                            t
          |          }	 t          |          5 }|                                }ddd           n# 1 swxY w Y   n# t          $ r}t          |          rQt          |          r!t          t          d| d                     nCt          t          d| d                     n"t          t          d| d|                      Y d}~nd}~ww xY w|                     |           t          |          }|D ]M}t!          j        |          dk    r3t          t          | dt!          j        |                                Ntt          t'          |                      g }g }	g }
| D ]o}t!          j        |          }|d	k    r|                    |           2|dk    r|	                    |           N|d
         dk    r|
                    |           pt          d           t          d           t          d           dS )z;If called directly as a script, count the found characters.Nrr   rs   z is finezcan't read rt   rj   z has Ccr   Cz$normal control characters {controls}zformat characters {formats}z!other control characters {others})r   rZ   r   r   r   rJ   ra   rb   rx   r]   rh   r   r   r   updaterz   rk   rl   rV   r   append)countsrV   rc   rd   r   r#   r   rg   controlsformatsothersrF   s               r&   check_file_textr   
  s   YYF!## : :7<<d++	8h 1FFHH              ! 	8 	8 	8d## 8%h// C% !0 !0 !0 !0 1 1 2 2 2 2 '"@$"@"@"@AABBBBe5$55!5566777	8 	aA 	: 	:Az!}}$$g77AF1II7788999	: 
#f++HGF  JqMM99OOA$YYNN1qTS[[MM!	
0111	
'(((	
-.....s=   BA6*B6A:	:B=A:	>B
DBDD__main__) r   r   r   collectionsr   samba.colourr   r   r   r   rR   unicodedatark   samba.testsr   r   isattyr'   rJ   rP   rQ   rU   rm   r}   rM   rZ   r]   rh   rn   rp   r   r   rD   rH   r&   <module>r      s  &     				 



       I I I I I I I I I I I I 				                
z   2 z||
   <   9#
0 0 0$  6	 	 	  $  =% =% =% =% =%X =% =% =%@'/ '/ '/T zO rH   