Skip to content

Commit

Permalink
Added unit tests for the SSDeep functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
JusticeRage committed Jan 21, 2016
1 parent f5798d1 commit 42d50e4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
48 changes: 47 additions & 1 deletion test/hash-library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <boost/test/unit_test.hpp>

#include "hash-library/hashes.h"
#include "hash-library/ssdeep.h"
#include "fixtures.h"

BOOST_AUTO_TEST_CASE(hash_phrase)
Expand Down Expand Up @@ -53,10 +54,55 @@ BOOST_AUTO_TEST_CASE(hash_missing_file)
{
hash::const_shared_strings hashes = hash::hash_file(hash::ALL_DIGESTS, "I_DON'T_EXIST.txt");
BOOST_ASSERT(!hashes);
hash::pString h = hash::hash_file(*hash::ALL_DIGESTS.at(ALL_DIGESTS_MD5), "I_DON'T_EXIST.txt");
pString h = hash::hash_file(*hash::ALL_DIGESTS.at(ALL_DIGESTS_MD5), "I_DON'T_EXIST.txt");
BOOST_CHECK(!h);
}

// ----------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE(ssdeep_hash_buffer)
{
std::vector<boost::uint8_t> buffer(65536);
memset(&buffer[0], 0x41, 65536);
pString s = ssdeep::hash_buffer(buffer);
BOOST_ASSERT(s);
BOOST_CHECK(*s == "3:Wttkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkw:Yu");
}

// ----------------------------------------------------------------------------

BOOST_FIXTURE_TEST_CASE(ssdeep_hash_file, SetWorkingDirectory)
{
pString s = ssdeep::hash_file("testfiles/manatest.exe");
BOOST_ASSERT(s);
BOOST_CHECK(*s == "384:l3a7DCXuMusPxN7gP/zP0JXZO708ijc3pVVPjqUeI7XQ62r:9U8v5N7YYtc708cc5VVPjze966");

// Verify that we obtain the same value by reading the file into a buffer and hashing it.
FILE* f = fopen("testfiles/manatest.exe", "rb");
BOOST_ASSERT(f != nullptr);
std::vector<boost::uint8_t> bytes(static_cast<unsigned int>(fs::file_size("testfiles/manatest.exe")));
int copied = fread(&bytes[0], 1, bytes.size(), f);
if (copied != bytes.size())
{
fclose(f);
BOOST_FAIL("[ssdeep_hash_file] Unable to copy the input file into a buffer.");
}
fclose(f);
s = ssdeep::hash_buffer(bytes);
BOOST_ASSERT(s);
BOOST_CHECK(*s == "384:l3a7DCXuMusPxN7gP/zP0JXZO708ijc3pVVPjqUeI7XQ62r:9U8v5N7YYtc708cc5VVPjze966");
}

// ----------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE(ssdeep_hash_empty_input)
{
pString s = ssdeep::hash_file("I_DON'T_EXIST.txt");
BOOST_CHECK(!s);
s = ssdeep::hash_buffer(std::vector<boost::uint8_t>());
BOOST_CHECK(!s);
}

// ----------------------------------------------------------------------------
BOOST_FIXTURE_TEST_SUITE(hash_files, SetupFiles)
// ----------------------------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions test/imports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ BOOST_AUTO_TEST_CASE(find_imports_no_match)
BOOST_CHECK(pfunctions->size() == 0);
}

// ----------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE(hash_imports)
{
mana::PE pe("testfiles/manatest.exe");
pString h = hash::hash_imports(pe);
BOOST_ASSERT(h);
BOOST_CHECK(*h == "924ac5aa343a9f838d5c16a5d77de2ec");
}

// ----------------------------------------------------------------------------
BOOST_AUTO_TEST_SUITE_END()
// ----------------------------------------------------------------------------
9 changes: 6 additions & 3 deletions test/include/fixtures.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ along with Manalyze. If not, see <http://www.gnu.org/licenses/>.
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/shared_ptr.hpp>

namespace unit = boost::unit_test::framework;
namespace fs = boost::filesystem;
namespace bs = boost::system;
namespace unit = boost::unit_test::framework;
namespace fs = boost::filesystem;
namespace bs = boost::system;

typedef boost::shared_ptr<std::string> pString;

// ----------------------------------------------------------------------------

Expand Down

0 comments on commit 42d50e4

Please sign in to comment.