1
1
import unittest
2
2
import os
3
+ import sys
4
+ import json
5
+ import io
3
6
from truffleHog import truffleHog
4
7
5
8
@@ -22,5 +25,34 @@ def test_unicode_expection(self):
22
25
except UnicodeEncodeError :
23
26
self .fail ("Unicode print error" )
24
27
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
+
25
57
if __name__ == '__main__' :
26
58
unittest .main ()
0 commit comments