|
| 1 | +import json |
| 2 | + |
| 3 | +from codemodder.codemods.test import BaseSASTCodemodTest |
| 4 | +from core_codemods.sonar.sonar_use_secure_protocols import SonarUseSecureProtocols |
| 5 | + |
| 6 | + |
| 7 | +class TestSonarUseSecureProtocols(BaseSASTCodemodTest): |
| 8 | + codemod = SonarUseSecureProtocols |
| 9 | + tool = "sonar" |
| 10 | + |
| 11 | + def test_name(self): |
| 12 | + assert self.codemod.name == "use-secure-protocols" |
| 13 | + |
| 14 | + def test_replace_in_strings(self, tmpdir): |
| 15 | + input_code = """\ |
| 16 | + url = "http://example.com" |
| 17 | + """ |
| 18 | + expected = """\ |
| 19 | + url = "https://example.com" |
| 20 | + """ |
| 21 | + issues = { |
| 22 | + "hotspots": [ |
| 23 | + { |
| 24 | + "component": "code.py", |
| 25 | + "status": "TO_REVIEW", |
| 26 | + "textRange": { |
| 27 | + "startLine": 1, |
| 28 | + "endLine": 1, |
| 29 | + "startOffset": 6, |
| 30 | + "endOffset": 26, |
| 31 | + }, |
| 32 | + "ruleKey": "python:S5332", |
| 33 | + }, |
| 34 | + ], |
| 35 | + } |
| 36 | + self.run_and_assert( |
| 37 | + tmpdir, input_code, expected, results=json.dumps(issues), num_changes=1 |
| 38 | + ) |
| 39 | + |
| 40 | + def test_ftp_call(self, tmpdir): |
| 41 | + input_code = """\ |
| 42 | + import ftplib |
| 43 | + ftp_con = ftplib.FTP("ftp.example.com") |
| 44 | + """ |
| 45 | + expected = """\ |
| 46 | + import ftplib |
| 47 | + ftp_con = ftplib.FTP_TLS("ftp.example.com") |
| 48 | + """ |
| 49 | + issues = { |
| 50 | + "hotspots": [ |
| 51 | + { |
| 52 | + "component": "code.py", |
| 53 | + "status": "TO_REVIEW", |
| 54 | + "textRange": { |
| 55 | + "startLine": 2, |
| 56 | + "endLine": 2, |
| 57 | + "startOffset": 10, |
| 58 | + "endOffset": 39, |
| 59 | + }, |
| 60 | + "ruleKey": "python:S5332", |
| 61 | + }, |
| 62 | + ], |
| 63 | + } |
| 64 | + self.run_and_assert( |
| 65 | + tmpdir, input_code, expected, results=json.dumps(issues), num_changes=1 |
| 66 | + ) |
| 67 | + |
| 68 | + def test_smtp_call(self, tmpdir): |
| 69 | + input_code = """\ |
| 70 | + import smtplib |
| 71 | + smtp_con = smtplib.SMTP("smtp.example.com", port=587) |
| 72 | + """ |
| 73 | + expected = """\ |
| 74 | + import smtplib |
| 75 | + import ssl |
| 76 | +
|
| 77 | + smtp_context = ssl.create_default_context() |
| 78 | + smtp_context.verify_mode = ssl.CERT_REQUIRED |
| 79 | + smtp_context.check_hostname = True |
| 80 | + smtp_con = smtplib.SMTP("smtp.example.com", port=587) |
| 81 | + smtplib.starttls(context=smtp_context) |
| 82 | + """ |
| 83 | + issues = { |
| 84 | + "hotspots": [ |
| 85 | + { |
| 86 | + "component": "code.py", |
| 87 | + "status": "TO_REVIEW", |
| 88 | + "textRange": { |
| 89 | + "startLine": 2, |
| 90 | + "endLine": 2, |
| 91 | + "startOffset": 11, |
| 92 | + "endOffset": 53, |
| 93 | + }, |
| 94 | + "ruleKey": "python:S5332", |
| 95 | + }, |
| 96 | + ], |
| 97 | + } |
| 98 | + self.run_and_assert( |
| 99 | + tmpdir, input_code, expected, results=json.dumps(issues), num_changes=1 |
| 100 | + ) |
| 101 | + |
| 102 | + def test_smtp_call_default_port(self, tmpdir): |
| 103 | + input_code = """\ |
| 104 | + import smtplib |
| 105 | + smtp_con = smtplib.SMTP("smtp.example.com", port=0) |
| 106 | + """ |
| 107 | + expected = """\ |
| 108 | + import smtplib |
| 109 | + import ssl |
| 110 | +
|
| 111 | + smtp_context = ssl.create_default_context() |
| 112 | + smtp_context.verify_mode = ssl.CERT_REQUIRED |
| 113 | + smtp_context.check_hostname = True |
| 114 | + smtp_con = smtplib.SMTP_SSL("smtp.example.com", port=0, context = smtp_context) |
| 115 | + """ |
| 116 | + issues = { |
| 117 | + "hotspots": [ |
| 118 | + { |
| 119 | + "component": "code.py", |
| 120 | + "status": "TO_REVIEW", |
| 121 | + "textRange": { |
| 122 | + "startLine": 2, |
| 123 | + "endLine": 2, |
| 124 | + "startOffset": 11, |
| 125 | + "endOffset": 51, |
| 126 | + }, |
| 127 | + "ruleKey": "python:S5332", |
| 128 | + }, |
| 129 | + ], |
| 130 | + } |
| 131 | + self.run_and_assert( |
| 132 | + tmpdir, input_code, expected, results=json.dumps(issues), num_changes=1 |
| 133 | + ) |
0 commit comments