Skip to content

Commit

Permalink
Tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
babenek committed Sep 16, 2024
1 parent c79499a commit 625a0f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
47 changes: 38 additions & 9 deletions tests/ml_model/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from credsweeper.common.constants import Severity
from credsweeper.credentials import Candidate, LineData
from credsweeper.ml_model.features import SearchInAttribute, CharSet
from credsweeper.ml_model.features import SearchInAttribute, CharSet, WordInPath
from credsweeper.ml_model.features.has_html_tag import HasHtmlTag
from credsweeper.ml_model.features.is_secret_numeric import IsSecretNumeric
from credsweeper.ml_model.features.reny_entropy import RenyiEntropy
Expand All @@ -21,7 +21,7 @@ def setUp(self):
line=AZ_STRING,
line_pos=0,
line_num=1,
path="path",
path="path.ext",
file_type="type",
info="info",
pattern=RE_TEST_PATTERN)
Expand Down Expand Up @@ -57,30 +57,59 @@ def test_estimate_entropy_p(self):
probabilities = test_entropy.get_probabilities(AZ_STRING)
self.assertEqual(4.754887502163468, test_entropy.estimate_entropy(probabilities))

def test_word_in_path_n(self):
self.assertListEqual([[0, 0, 0, 0]],
WordInPath(["dog", "lazy", "small",
"the"])([Candidate([self.line_data], [], "rule", Severity.MEDIUM)]).tolist())

def test_word_in_path_p(self):
self.assertListEqual([[1, 0, 0, 0]],
WordInPath([".ext", "lazy", "small",
"the"])(
[Candidate([self.line_data], [], "rule", Severity.MEDIUM)]).tolist())

def test_word_in_value_n(self):
self.assertListEqual([[0, 0, 0, 0]],
WordInPath(["aaa", "bbb", "ccc",
"ddd"])([Candidate([self.line_data], [], "rule", Severity.MEDIUM)]).tolist())


def test_word_in_value_p(self):
self.assertListEqual([[1, 1, 0, 1]],
WordInValue(["dog", "lazy", "small",
"the"]).extract(Candidate([self.line_data], [], "rule",
Severity.MEDIUM)).tolist())

def test_word_in_value_n(self):
self.assertListEqual([[0, 0]],
WordInValue(["pink",
"quick"]).extract(Candidate([self.line_data], [], "rule",
Severity.MEDIUM)).tolist())
def test_word_in_line_dup_n(self):
with self.assertRaises(Exception):
WordInLine(["fox", "fox"])

def test_word_in_line_n(self):
test = WordInLine(["text"])
self.assertListEqual([[0]], test.extract(Candidate([self.line_data], [], "rule", Severity.MEDIUM)).tolist())
test = WordInLine(["dummy", "text"])
self.assertListEqual([[0, 0]], test.extract(Candidate([self.line_data], [], "rule", Severity.MEDIUM)).tolist())

def test_word_in_line_p(self):
test = WordInLine(["bear", "brown"])
self.assertListEqual([[0, 1]], test.extract(Candidate([self.line_data], [], "rule", Severity.MEDIUM)).tolist())

def test_has_html_tag_n(self):
test = HasHtmlTag()
self.assertFalse(test.extract(Candidate([self.line_data], [], "rule", Severity.MEDIUM)))

def test_has_html_tag_p(self):
test = HasHtmlTag()
self.line_data.line = f"<p>{self.line_data.line}</p>"
self.assertTrue(test.extract(Candidate([self.line_data], [], "rule", Severity.MEDIUM)))

def test_is_secret_numeric_n(self):
test = IsSecretNumeric()
self.assertFalse(test.extract(Candidate([self.line_data], [], "rule", Severity.MEDIUM)))

def test_is_secret_numeric_p(self):
test = IsSecretNumeric()
self.line_data.value = "2.718281828"
self.assertTrue(test.extract(Candidate([self.line_data], [], "rule", Severity.MEDIUM)))

def test_search_in_attribute_n(self):
self.assertFalse(
SearchInAttribute("^the lazy dog$", "line").extract(Candidate([self.line_data], [], "rule",
Expand Down
1 change: 1 addition & 0 deletions tests/utils/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def test_is_binary_p(self):
def test_is_binary_n(self):
self.assertFalse(Util.is_binary("Üben von Xylophon und Querflöte ist ja zweckmäßig".encode(LATIN_1)))
self.assertFalse(Util.is_binary(b"\x7Ffew unprintable letters\x00"))
self.assertFalse(Util.is_binary(b""))

def test_is_ascii_entropy_validate_p(self):
self.assertTrue(Util.is_ascii_entropy_validate(b''))
Expand Down

0 comments on commit 625a0f3

Please sign in to comment.