
    Si                        d Z ddlZddlmZ ddlZej
                  j                  d e ee      j                  j                  j                               ddl
mZmZ  G d d      Z G d d      Z G d	 d
      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Zedk(  r ej,                  edg       yy)u%   utils/approval.py 테스트 스위트    N)Path)ApprovalResultcheck_commandc                   .    e Zd ZdZd Zd Zd Zd Zd Zy)TestApprovalResultDataclassu&   ApprovalResult dataclass 구조 검증c                 >    t        d      }t        |t              sJ y Nls -la)r   
isinstancer   selfresults     J/home/jay/workspace/.worktrees/task-2117-dev1/utils/tests/test_approval.py(test_safe_command_returns_approvalresultzDTestApprovalResultDataclass.test_safe_command_returns_approvalresult   s    x(&.111    c                     t        d      }t        |d      sJ t        |d      sJ t        |d      sJ t        |d      sJ y )Nz
echo hellois_safe
risk_levelmatched_patternsrecommendation)r   hasattrr   s     r   test_approvalresult_fieldsz6TestApprovalResultDataclass.test_approvalresult_fields   sN    |,vy)))v|,,,v1222v/000r   c                 B    h d}t        d      }|j                  |v sJ y )N>   lowhighsafemediumcriticalls)r   r   )r   valid_levelsr   s      r   test_risk_level_values_validz8TestApprovalResultDataclass.test_risk_level_values_valid   s%    Dt$  L000r   c                 R    t        d      }t        |j                  t              sJ y N
git status)r   r   r   listr   s     r   test_matched_patterns_is_listz9TestApprovalResultDataclass.test_matched_patterns_is_list!   s"    |,&114888r   c                 R    t        d      }t        |j                  t              sJ y r#   )r   r   r   strr   s     r   test_recommendation_is_stringz9TestApprovalResultDataclass.test_recommendation_is_string%   s"    |,&//555r   N)	__name__
__module____qualname____doc__r   r   r!   r&   r)    r   r   r   r      s    0211
96r   r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
TestCriticalPatternsu   critical 위험 패턴 탐지c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nrm -rf /Fr   r   r   r   r   s     r   test_rm_rf_rootz$TestCriticalPatterns.test_rm_rf_root-   s2    z*~~&&&  J...r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzrm -rf / --no-preserve-rootFr   r3   r   s     r   test_rm_rf_root_with_slashz/TestCriticalPatterns.test_rm_rf_root_with_slash2   3    <=~~&&&  J...r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )NDROP TABLE usersFr   r3   r   s     r   test_drop_tablez$TestCriticalPatterns.test_drop_table7   s3    12~~&&&  J...r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )NzDROP DATABASE mydbFr   r3   r   s     r   test_drop_databasez'TestCriticalPatterns.test_drop_database<   s3    34~~&&&  J...r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nz:(){ :|:& };:Fr   r3   r   s     r   test_fork_bombz#TestCriticalPatterns.test_fork_bombA   s2    /~~&&&  J...r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzdd if=/dev/zero of=/dev/sdaFr   r3   r   s     r   test_dd_devzeroz$TestCriticalPatterns.test_dd_devzeroF   r7   r   c                 :    t        d      }|j                  du sJ y )Nr2   Fr   r   r   s     r   test_critical_is_not_safez.TestCriticalPatterns.test_critical_is_not_safeK   s    z*~~&&&r   N)r*   r+   r,   r-   r4   r6   r:   r<   r>   r@   rC   r.   r   r   r0   r0   *   s(    '/
/
/
/
/
/
'r   r0   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	TestHighPatternsu   high 위험 패턴 탐지c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nz*curl https://example.com/install.sh | bashFr   r3   r   s     r   test_curl_pipe_bashz$TestHighPatterns.test_curl_pipe_bashS   s3    KL~~&&&  F***r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nz-wget -O- https://example.com/script.sh | bashFr   r3   r   s     r   test_wget_pipe_bashz$TestHighPatterns.test_wget_pipe_bashX   s3    NO~~&&&  F***r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzeval $(cat malicious.sh)Fr   r3   r   s     r   	test_evalzTestHighPatterns.test_eval]   s3    9:~~&&&  F***r   c                 Z    t        d      }|j                  du sJ |j                  dv sJ y )Nzrm -rf ./node_modulesF)r   r   r3   r   s     r   
test_rm_rfzTestHighPatterns.test_rm_rfb   s4    67~~&&&  $8888r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzgit push --force origin mainFr   r3   r   s     r   test_git_push_forcez$TestHighPatterns.test_git_push_forceg   s3    =>~~&&&  F***r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzchmod 777 /etc/passwdFr   r3   r   s     r   test_chmod_777zTestHighPatterns.test_chmod_777l   s3    67~~&&&  F***r   N)
r*   r+   r,   r-   rG   rI   rK   rM   rO   rQ   r.   r   r   rE   rE   P   s#    #+
+
+
9
+
+r   rE   c                   (    e Zd ZdZd Zd Zd Zd Zy)TestMediumPatternsu   medium 위험 패턴 탐지c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzsudo apt-get updateFr   r3   r   s     r   	test_sudozTestMediumPatterns.test_sudou   s3    45~~&&&  H,,,r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzpip install requestsFr   r3   r   s     r   test_pip_installz#TestMediumPatterns.test_pip_installz   s3    56~~&&&  H,,,r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nznpm install -g typescriptFr   r3   r   s     r   test_npm_install_globalz*TestMediumPatterns.test_npm_install_global   s3    :;~~&&&  H,,,r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nz#docker run --privileged ubuntu bashFr   r3   r   s     r   test_docker_privilegedz)TestMediumPatterns.test_docker_privileged   s3    DE~~&&&  H,,,r   N)r*   r+   r,   r-   rU   rW   rY   r[   r.   r   r   rS   rS   r   s    %-
-
-
-r   rS   c                       e Zd ZdZd Zd Zy)TestLowPatternsu   low 위험 패턴 탐지c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzgit reset HEAD~1Fr   r3   r   s     r   test_git_resetzTestLowPatterns.test_git_reset   s3    12~~&&&  E)))r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzgit checkout .Fr   r3   r   s     r   test_git_checkout_dotz%TestLowPatterns.test_git_checkout_dot   s3    /0~~&&&  E)))r   N)r*   r+   r,   r-   r_   ra   r.   r   r   r]   r]      s    "*
*r   r]   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
TestSafeCommandsu   안전 명령어는 safe 판정c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nr
   Tr   r3   r   s     r   test_lszTestSafeCommands.test_ls   s2    x(~~%%%  F***r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzecho hello worldTr   r3   r   s     r   	test_echozTestSafeCommands.test_echo   3    12~~%%%  F***r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nr$   Tr   r3   r   s     r   test_git_statusz TestSafeCommands.test_git_status   s2    |,~~%%%  F***r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzgit log --oneline -10Tr   r3   r   s     r   test_git_logzTestSafeCommands.test_git_log   s3    67~~%%%  F***r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzpython --versionTr   r3   r   s     r   test_python_versionz$TestSafeCommands.test_python_version   rh   r   c                 \    t        d      }|j                  du sJ |j                  dk(  sJ y )Nzcat README.mdTr   r3   r   s     r   test_cat_readmez TestSafeCommands.test_cat_readme   s2    /~~%%%  F***r   c                 <    t        d      }|j                  g k(  sJ y r	   )r   r   r   s     r    test_safe_matched_patterns_emptyz1TestSafeCommands.test_safe_matched_patterns_empty   s     x(&&",,,r   N)r*   r+   r,   r-   re   rg   rj   rl   rn   rp   rr   r.   r   r   rc   rc      s(    )+
+
+
+
+
+
-r   rc   c                       e Zd ZdZd Zd Zy)TestMatchedPatternsu   matched_patterns 필드 검증c                     t        d      }t        |j                        dkD  sJ t        |j                  d   t              sJ y )Nr2   r   )r   lenr   r   r(   r   s     r   #test_dangerous_has_matched_patternsz7TestMatchedPatterns.test_dangerous_has_matched_patterns   s@    z*6**+a///&11!4c:::r   c                 N    t        d      }t        |j                        dk\  sJ y )Nz5eval $(curl https://bad.example.com/script.sh | bash)   )r   rv   r   r   s     r    test_multiple_patterns_can_matchz4TestMatchedPatterns.test_multiple_patterns_can_match   s&    VW6**+q000r   N)r*   r+   r,   r-   rw   rz   r.   r   r   rt   rt      s    (;
1r   rt   c                       e Zd ZdZd Zd Zy)TestCaseInsensitiveu   대소문자 무감지 탐지c                 :    t        d      }|j                  du sJ y )Nr9   FrB   r   s     r   test_drop_table_uppercasez-TestCaseInsensitive.test_drop_table_uppercase       12~~&&&r   c                 :    t        d      }|j                  du sJ y )Nzdrop table usersFrB   r   s     r   test_drop_table_lowercasez-TestCaseInsensitive.test_drop_table_lowercase   r   r   N)r*   r+   r,   r-   r~   r   r.   r   r   r|   r|      s    '''r   r|   __main__z-v)r-   syspathlibr   pytestpathinsertr(   __file__parentutils.approvalr   r   r   r0   rE   rS   r]   rc   rt   r|   r*   mainr.   r   r   <module>r      s    + 
   3tH~,,33::; < 86 68#' #'L+ +D- -0* *#- #-L1 1	' 	' zFKK4 ! r   