Skip to content

Commit 4be9b6e

Browse files
committed
Unit test for use-secure-protocols
1 parent 1ea36a7 commit 4be9b6e

File tree

2 files changed

+136
-5
lines changed

2 files changed

+136
-5
lines changed

src/core_codemods/sonar/sonar_use_secure_protocols.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def leave_SimpleStatementLine(self, original_node, updated_node):
8484
match original_node.body:
8585
# match the first statement that is either selected or is an assignment whose value is selected
8686
case [cst.Assign() as a, *_] if self.node_is_selected(a.value):
87-
8887
return self._match_and_handle_statement(
8988
a.value, original_node, updated_node
9089
)
@@ -126,10 +125,9 @@ def leave_Call(self, original_node, updated_node):
126125
)
127126
match maybe_port_value:
128127
case None:
129-
self._change_to_smtp_ssl(original_node, updated_node)
130-
case cst.Integer() if maybe_port_value == "0":
131-
self._change_to_smtp_ssl(original_node, updated_node)
132-
128+
return self._change_to_smtp_ssl(original_node, updated_node)
129+
case cst.Integer() if maybe_port_value.value == "0":
130+
return self._change_to_smtp_ssl(original_node, updated_node)
133131
return updated_node
134132

135133
def _change_to_smtp_ssl(self, original_node, updated_node):
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)