-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashFinderTest.cpp
218 lines (195 loc) · 7.09 KB
/
HashFinderTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2012, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Jerome Meinke <[email protected]>.
#include <gtest/gtest.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "./HashFinder.h"
// Test parsing the command line arguments
TEST(HashFinderTest, parseCommandLineArguments) {
HashFinder hashfinder;
::testing::FLAGS_gtest_death_test_style = "threadsafe";
// Call without options and without argument.
{
int argc = 1;
char* argv[2] = { const_cast<char*>("egal") };
ASSERT_DEATH(hashfinder.parseCommandLineArguments(argc, argv), "Usage:.*");
}
// Regular call with input-file option and a valid MD5-Hash
{
int argc = 3;
char* argv[3] = {
const_cast<char*>("HashFinderMain"),
const_cast<char*>("--input-file=notExisting.txt"),
const_cast<char*>("35e5d160921d131d9114f1b4ee5f9d55")
};
hashfinder.parseCommandLineArguments(argc, argv);
ASSERT_TRUE(hashfinder._md5);
ASSERT_STREQ("notExisting.txt", hashfinder._inputFileName);
ASSERT_STREQ("abcdefghijklmnopqrstuvwxyz0123456789",
hashfinder._allowedCharacters);
ASSERT_STREQ("35e5d160921d131d9114f1b4ee5f9d55", hashfinder._hashToFind);
}
// Regular call with input-file option and a valid SHA-1-Hash
{
int argc = 4;
char* argv[4] = {
const_cast<char*>("HashFinderMain"),
const_cast<char*>("--input-file=notExisting.txt"),
const_cast<char*>("--hash-algo=sha1"),
const_cast<char*>("586b64caacfbdfd67d2a8d323510ed5b72a61e0b")
};
hashfinder.parseCommandLineArguments(argc, argv);
ASSERT_FALSE(hashfinder._md5);
ASSERT_STREQ("notExisting.txt", hashfinder._inputFileName);
ASSERT_STREQ("abcdefghijklmnopqrstuvwxyz0123456789",
hashfinder._allowedCharacters);
ASSERT_STREQ("586b64caacfbdfd67d2a8d323510ed5b72a61e0b",
hashfinder._hashToFind);
}
// Regular call with algorithm option, characters and SHA-1
{
int argc = 4;
char* argv[4] = {
const_cast<char*>("HashFinderMain"),
const_cast<char*>("--hash-algo=sha1"),
const_cast<char*>("--characters=sha1"),
const_cast<char*>("586b64caacfbdfd67d2a8d323510ed5b72a61e0b")
};
hashfinder.parseCommandLineArguments(argc, argv);
ASSERT_FALSE(hashfinder._md5);
ASSERT_STREQ("sha1", hashfinder._allowedCharacters);
ASSERT_STREQ(NULL, hashfinder._inputFileName);
ASSERT_STREQ("586b64caacfbdfd67d2a8d323510ed5b72a61e0b",
hashfinder._hashToFind);
}
}
// Test loading a dictionary-file
TEST(HashFinderTest, readDictionary) {
HashFinder hashfinder;
// first write the test file
const char* testFileName = "exampleDictionary.txt";
std::ofstream myfile(testFileName);
if (myfile.is_open()) {
myfile << "Dauerschlaf\n";
myfile << "Radschaufel\n";
myfile << "Schaufelrad";
myfile.close();
} else {
printf("Unable to open exampleDictionary for writing.\n");
FAIL();
}
// Now try to load the test file
hashfinder._inputFileName = "exampleDictionary.txt";
ASSERT_TRUE(hashfinder.readDictionary());
remove(testFileName);
// Check if we have the same amount of entries as lines
ASSERT_EQ(hashfinder._dictionary.size(), 3);
// Check the first word of the dictionary
ASSERT_STREQ("Dauerschlaf", hashfinder._dictionary[0].c_str());
}
// Test find MD5 and SHA1 in a dictionary file or in combinations
TEST(HashFinderTest, process) {
HashFinder hashfinder;
// first write the test file
const char* testFileName = "exampleDictionary.txt";
std::ofstream myfile(testFileName);
if (myfile.is_open()) {
myfile << "Dauerschlaf\n";
myfile << "Radschaufel\n";
myfile << "Schaufelrad";
myfile.close();
} else {
printf("Unable to open exampleDictionary for writing.\n");
FAIL();
}
{
printf("Testing MD5 + combinations...\n");
int argc = 6;
char* argv[6] = {
const_cast<char*>("HashFinderMain"),
const_cast<char*>("--hash-algo=md5"),
const_cast<char*>("--min-length=4"),
const_cast<char*>("--max-length=4"),
const_cast<char*>("--characters=fhlvz79"),
const_cast<char*>("27b411b92e1ffa0250a1765b6fd152f2") // hvl7
};
hashfinder.parseCommandLineArguments(argc, argv);
ASSERT_TRUE(hashfinder._md5);
ASSERT_STREQ("fhlvz79",
hashfinder._allowedCharacters);
ASSERT_STREQ("27b411b92e1ffa0250a1765b6fd152f2",
hashfinder._hashToFind);
// Now start processing and let's check if the _collision
// variable has been filled with the right word
hashfinder.process(1, 1);
ASSERT_STREQ("hvl7", hashfinder._collision);
}
{
printf("Testing MD5 + dictionary...\n");
int argc = 4;
char* argv[4] = {
const_cast<char*>("HashFinderMain"),
const_cast<char*>("--input-file=exampleDictionary.txt"),
const_cast<char*>("--hash-algo=md5"),
const_cast<char*>("af26c4895ba73daaf0c05c7700d13041")
};
hashfinder.parseCommandLineArguments(argc, argv);
ASSERT_TRUE(hashfinder._md5);
ASSERT_STREQ("exampleDictionary.txt", hashfinder._inputFileName);
ASSERT_STREQ("af26c4895ba73daaf0c05c7700d13041",
hashfinder._hashToFind);
// Now load the dictionary file
ASSERT_TRUE(hashfinder.readDictionary());
// Now start processing and let's check if the _collision
// variable has been filled with the right word
hashfinder.process(1, 1);
ASSERT_STREQ("Radschaufel", hashfinder._collision);
}
{
printf("Testing SHA-1 + combinations...\n");
int argc = 6;
char* argv[6] = {
const_cast<char*>("HashFinderMain"),
const_cast<char*>("--hash-algo=sha-1"),
const_cast<char*>("--min-length=4"),
const_cast<char*>("--max-length=4"),
const_cast<char*>("--characters=fhlvz79"),
const_cast<char*>("754174b26b4e39f5089da8a3fda7f752832b82a0") // hvl7
};
hashfinder.parseCommandLineArguments(argc, argv);
ASSERT_FALSE(hashfinder._md5);
ASSERT_STREQ("fhlvz79",
hashfinder._allowedCharacters);
ASSERT_STREQ("754174b26b4e39f5089da8a3fda7f752832b82a0",
hashfinder._hashToFind);
// Now start processing and let's check if the _collision
// variable has been filled with the right word
hashfinder.process(1, 1);
ASSERT_STREQ("hvl7", hashfinder._collision);
}
{
printf("Testing SHA-1 + dictionary...\n");
int argc = 4;
char* argv[4] = {
const_cast<char*>("HashFinderMain"),
const_cast<char*>("--input-file=exampleDictionary.txt"),
const_cast<char*>("--hash-algo=sha1"),
const_cast<char*>("16115caaf988bd19f019d2812fe3dc0ba3a68e52")
};
hashfinder.parseCommandLineArguments(argc, argv);
ASSERT_FALSE(hashfinder._md5);
ASSERT_STREQ("exampleDictionary.txt", hashfinder._inputFileName);
ASSERT_STREQ("16115caaf988bd19f019d2812fe3dc0ba3a68e52",
hashfinder._hashToFind);
// Now load the dictionary file
ASSERT_TRUE(hashfinder.readDictionary());
// Now start processing and let's check if the _collision
// variable has been filled with the right word
hashfinder.process(1, 1);
ASSERT_STREQ("Radschaufel", hashfinder._collision);
}
remove(testFileName);
}