Skip to content

Commit

Permalink
Fixed a compilation error for unit tests on Windows.
Browse files Browse the repository at this point in the history
Started writing tests for the PE class.
  • Loading branch information
JusticeRage committed Jan 8, 2016
1 parent f438c6a commit 88581fc
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 17 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ addons:
- libboost-test1.55-dev
- gcc-4.8
- g++-4.8
- gcov-4.8
compiler:
- gcc
install:
Expand Down
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ 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]")
add_definitions("/D_DEBUG")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
add_definitions("/D_DEBUG")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
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()
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 Down
4 changes: 0 additions & 4 deletions manape/pe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

#include "manape/pe.h"

// TODO: Remove when Yara doesn't mask min & max anymore
#undef min
#undef max

namespace mana {

PE::PE(const std::string& path)
Expand Down
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,6 @@ void perform_analysis(const std::string& path,

int main(int argc, char** argv)
{
// TODO: Unit tests.

po::variables_map vm;
std::string extraction_directory;
std::vector<std::string> selected_plugins, selected_categories;
Expand Down
3 changes: 1 addition & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 2.6)
project (manalyze-tests)

add_executable(manalyze-tests hash-library.cpp)
add_executable(manalyze-tests hash-library.cpp pe.cpp)

target_link_libraries(
manalyze-tests
Expand All @@ -10,7 +10,6 @@ target_link_libraries(
yara
hash-library
${Boost_LIBRARIES}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
)

if (WIN32)
Expand Down
23 changes: 22 additions & 1 deletion test/hash-library.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
/*
This file is part of Manalyze.
Manalyze is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Manalyze is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Manalyze. If not, see <http://www.gnu.org/licenses/>.
*/

#include <boost/system/api_config.hpp>

#define BOOST_TEST_MODULE ManalyzeTests
#define BOOST_TEST_DYN_LINK
#if !defined BOOST_WINDOWS_API
# define BOOST_TEST_DYN_LINK
#endif

#include <fstream>
#include <boost/filesystem.hpp>
Expand Down
80 changes: 80 additions & 0 deletions test/pe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
This file is part of Manalyze.
Manalyze is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Manalyze is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Manalyze. If not, see <http://www.gnu.org/licenses/>.
*/

#include <boost/test/unit_test.hpp>
#include <boost/filesystem.hpp>

#include "manape/pe.h"

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

class SetWorkingDirectory
{
public:
SetWorkingDirectory()
{
// Save the current working directory
_original_directory = fs::current_path().string();

// Go to the test directory
fs::path working_dir(unit::master_test_suite().argv[0]);
working_dir = working_dir.parent_path();
fs::current_path(working_dir / ".." / "test");
}

~SetWorkingDirectory() {
fs::current_path(_original_directory);
}

private:
std::string _original_directory;
};

BOOST_FIXTURE_TEST_CASE(parse_calc, SetWorkingDirectory)
{
mana::PE pe("testfiles/calc.exe");
BOOST_CHECK_EQUAL(pe.get_filesize(), 115200);

// DOS Header
boost::optional<mana::dos_header> pdos = pe.get_dos_header();
BOOST_ASSERT(pdos);
mana::dos_header dos = *pdos;
BOOST_CHECK(dos.e_magic[0] == 'M' && dos.e_magic[1] == 'Z');
BOOST_CHECK_EQUAL(dos.e_cblp, 0x90);
BOOST_CHECK_EQUAL(dos.e_cp, 3);
BOOST_CHECK_EQUAL(dos.e_crlc, 0);
BOOST_CHECK_EQUAL(dos.e_cparhdr, 4);
BOOST_CHECK_EQUAL(dos.e_minalloc, 0);
BOOST_CHECK_EQUAL(dos.e_maxalloc, 0xFFFF);
BOOST_CHECK_EQUAL(dos.e_ss, 0);
BOOST_CHECK_EQUAL(dos.e_sp, 0xB8);
BOOST_CHECK_EQUAL(dos.e_csum, 0);
BOOST_CHECK_EQUAL(dos.e_ip, 0);
BOOST_CHECK_EQUAL(dos.e_cs, 0);
BOOST_CHECK_EQUAL(dos.e_lfarlc, 0x40);
BOOST_CHECK_EQUAL(dos.e_ovno, 0);
for (int i = 0 ; i < 4 ; ++i) {
BOOST_CHECK_EQUAL(dos.e_res[i], 0);
}
BOOST_CHECK_EQUAL(dos.e_oemid, 0);
BOOST_CHECK_EQUAL(dos.e_oeminfo, 0);
for (int i = 0 ; i < 10 ; ++i) {
BOOST_CHECK_EQUAL(dos.e_res2[i], 0);
}
BOOST_CHECK_EQUAL(dos.e_lfanew, 0xF0);
}

0 comments on commit 88581fc

Please sign in to comment.