Skip to content

Commit 55d7d14

Browse files
authored
Merge pull request #110 from maennel/bugfix/wrong-commit-hash
Bugfix: Fix return of wrong commit hash
2 parents aee194a + 2e064ae commit 55d7d14

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test_all.py

Lines changed: 34 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
from mock import patch
58
from mock import MagicMock
@@ -24,6 +27,37 @@ def test_unicode_expection(self):
2427
except UnicodeEncodeError:
2528
self.fail("Unicode print error")
2629

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

0 commit comments

Comments
 (0)