Skip to content

Commit

Permalink
Started writing unit tests for the project.
Browse files Browse the repository at this point in the history
Trying to integrate it with Coveralls.io.
  • Loading branch information
JusticeRage committed Dec 27, 2015
1 parent bb0ed0a commit f4da863
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
18 changes: 14 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ addons:
apt:
sources:
- boost-latest
- ubuntu-toolchain-r-test
packages:
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
- libboost-system1.55-dev
- libboost-filesystem1.55-dev
- libboost-test1.55-dev
- gcc-4.8
- g++-4.8
compiler:
- clang
before_script:
- cmake .
- gcc
install:
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
before_script:
- cmake . -DTests=ON -DCMAKE_BUILD_TYPE=Debug
- pip install --user cpp-coveralls
script:
- make
- bin/manalyze --version
- bin/manalyze --version
- bin/manalyze-tests
after_success:
- coveralls --exclude external/yara --exclude test --gcov-options '\-lp'
21 changes: 17 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ cmake_minimum_required (VERSION 2.8)

project (manalyze)

option(GitHub "GitHub" ON)
option(Debug "Debug" OFF)
option(GitHub "Allow checking out third-party projects from GitHub" ON)
option(Tests "Generate unit tests" OFF)

if (WIN32)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
endif()

find_package(Boost REQUIRED COMPONENTS regex system filesystem program_options)
if (GitHub STREQUAL ON)
find_package(Git REQUIRED)
endif()
if (Tests STREQUAL OFF)
find_package(Boost REQUIRED COMPONENTS regex system filesystem program_options)
else()
find_package(Boost REQUIRED COMPONENTS regex system filesystem program_options unit_test_framework)
endif()

# Download or update external projects
if (EXISTS external/yara AND GitHub STREQUAL ON)
Expand Down Expand Up @@ -77,11 +81,16 @@ if (WIN32)
else()
string (REGEX MATCH "BSD" IS_BSD ${CMAKE_SYSTEM_NAME}) # Detect if we are compiling on a BSD system.

if (CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]" OR Debug STREQUAL ON)
if (CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
add_definitions("/D_DEBUG")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
endif()

if (Tests STREQUAL ON) # Add coverage option if unit tests were requested.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")
endif()

if (NOT IS_BSD) # No need to link against dl on BSD.
target_link_libraries(manalyze dl)
endif()
Expand All @@ -100,6 +109,10 @@ add_subdirectory(external/yara)
# hash-library dependency
add_subdirectory(external/hash-library)

if (Tests STREQUAL ON)
add_subdirectory(test)
endif()

target_link_libraries(manape yara hash-library manacommons ${Boost_LIBRARIES})

target_link_libraries(
Expand Down
8 changes: 8 additions & 0 deletions docs/writing-plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,11 @@ Internally, all the result data is stored as key-value pairs; if you don't provi
Two
Three

PE objects
==========

Now that we know how to create results, we will look more closely at the ``analyze`` method. Here is how it's declared::

pResult analyze(const mana::PE& pe);

It's return type has been covered already, but what about the argument? This ``PE`` object is all the plugin has to work with, but it contains a lot of information about the input file.
26 changes: 26 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required (VERSION 2.6)
project (manalyze-tests)

add_executable(manalyze-tests hash-library.cpp)

target_link_libraries(
manalyze-tests
manacommons
manape
yara
hash-library
${Boost_LIBRARIES}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
)

if (WIN32)
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -MTd")
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -MTd")
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -MT")
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -MT")
set_target_properties(hash-library PROPERTIES COMPILE_DEFINITIONS "HASHLIB_EXPORT")
else()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
add_definitions(-fPIC)
endif()
endif()
28 changes: 28 additions & 0 deletions test/hash-library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#define BOOST_TEST_MODULE ManalyzeTests
#define BOOST_TEST_DYN_LINK

#include "hash-library/hashes.h"
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(hash_phrase)
{
std::string input("The quick brown fox jumps over the lazy dog");
std::vector<boost::uint8_t> bytes(input.begin(), input.end());
hash::const_shared_strings hashes = hash::hash_bytes(hash::ALL_DIGESTS, bytes);

BOOST_CHECK_EQUAL("9e107d9d372bb6826bd81d3542a419d6", hashes->at(ALL_DIGESTS_MD5));
BOOST_CHECK_EQUAL("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", hashes->at(ALL_DIGESTS_SHA1));
BOOST_CHECK_EQUAL("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", hashes->at(ALL_DIGESTS_SHA256));
BOOST_CHECK_EQUAL("4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15", hashes->at(ALL_DIGESTS_SHA3));
}

BOOST_AUTO_TEST_CASE(null_hash)
{
std::vector<boost::uint8_t> bytes(0);
hash::const_shared_strings hashes = hash::hash_bytes(hash::ALL_DIGESTS, bytes);

BOOST_CHECK_EQUAL("d41d8cd98f00b204e9800998ecf8427e", hashes->at(ALL_DIGESTS_MD5));
BOOST_CHECK_EQUAL("da39a3ee5e6b4b0d3255bfef95601890afd80709", hashes->at(ALL_DIGESTS_SHA1));
BOOST_CHECK_EQUAL("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hashes->at(ALL_DIGESTS_SHA256));
BOOST_CHECK_EQUAL("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", hashes->at(ALL_DIGESTS_SHA3));
}

0 comments on commit f4da863

Please sign in to comment.