Skip to content

Commit 59b59ef

Browse files
Jeckelmann ManuelJeckelmann Manuel
authored andcommitted
Added a test case to ensure the correct commitHash is returned (includes a commit comment cross-validation)
1 parent ee5513b commit 59b59ef

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test_all.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import unittest
22
import os
3+
import sys
4+
import json
5+
import io
36
from truffleHog import truffleHog
47

58

@@ -22,5 +25,34 @@ def test_unicode_expection(self):
2225
except UnicodeEncodeError:
2326
self.fail("Unicode print error")
2427

28+
def test_return_correct_commit_hash(self):
29+
# Start at commit d15627104d07846ac2914a976e8e347a663bbd9b, which
30+
# is immediately followed by a secret inserting commit:
31+
# https://github.com/dxa4481/truffleHog/commit/9ed54617547cfca783e0f81f8dc5c927e3d1e345
32+
since_commit = 'd15627104d07846ac2914a976e8e347a663bbd9b'
33+
commit_w_secret = '9ed54617547cfca783e0f81f8dc5c927e3d1e345'
34+
cross_valdiating_commit_w_secret_comment = 'OH no a secret'
35+
36+
json_result = ''
37+
tmp_stdout = io.StringIO()
38+
bak_stdout = sys.stdout
39+
40+
# Redirect STDOUT, run scan and re-establish STDOUT
41+
sys.stdout = tmp_stdout
42+
try:
43+
truffleHog.find_strings("https://github.com/dxa4481/truffleHog.git",
44+
since_commit=since_commit, printJson=True, surpress_output=False)
45+
finally:
46+
sys.stdout = bak_stdout
47+
48+
json_result_list = tmp_stdout.getvalue().split('\n')
49+
results = [json.loads(r) for r in json_result_list if bool(r.strip())]
50+
filtered_results = list(filter(lambda r: r['commitHash'] == commit_w_secret, results))
51+
self.assertEqual(1, len(filtered_results))
52+
self.assertEqual(commit_w_secret, filtered_results[0]['commitHash'])
53+
# Additionally, we cross-validate the commit comment matches the expected comment
54+
self.assertEqual(cross_valdiating_commit_w_secret_comment, filtered_results[0]['commit'].strip())
55+
56+
2557
if __name__ == '__main__':
2658
unittest.main()

0 commit comments

Comments
 (0)