From: Christoph Egger Date: Tue, 3 Mar 2015 16:59:23 +0000 (+0100) Subject: Add testcase for serializing / deserializing BasicBlocks X-Git-Tag: v0.1~83 X-Git-Url: https://git.siccegge.de//index.cgi?a=commitdiff_plain;h=f7cd60ef2104cf8f74ea06f58906acda4aef3e29;p=frida%2Ffrida.git Add testcase for serializing / deserializing BasicBlocks --- diff --git a/CMakeLists.txt b/CMakeLists.txt index e9ff46b..7657461 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ execute_process(COMMAND ${LLVM_CONFIG} --system-libs OUTPUT_VARIABLE LLVM_SYSTEM string(REPLACE -O2 "" LLVM_CFLAGS ${LLVM_CFLAGS}) string(REPLACE -fno-exceptions "" LLVM_CFLAGS ${LLVM_CFLAGS}) string(REPLACE -fno-rtti "" LLVM_CFLAGS ${LLVM_CFLAGS}) +string(REPLACE -DNDEBUG "" LLVM_CFLAGS ${LLVM_CFLAGS}) separate_arguments(LLVM_CFLAGS) add_definitions(${LLVM_CFLAGS}) @@ -43,7 +44,6 @@ separate_arguments(LLVM_LDFLAGS) separate_arguments(LLVM_LIBS) SET(frida_SOURCES - src/main.cxx src/core/InformationManager.cxx src/core/BasicBlock.cxx src/core/Function.cxx @@ -72,7 +72,7 @@ SET(frida_HEADERS src/disassembler/Disassembler.hxx ) -ADD_EXECUTABLE(frida ${frida_SOURCES} ${frida_HEADERS_MOC}) +ADD_EXECUTABLE(frida src/main.cxx ${frida_SOURCES} ${frida_HEADERS_MOC}) #ADD_LIBRARY(llvm SHARED ) ADD_DEFINITIONS(${QT_DEFINITIONS}) @@ -88,11 +88,16 @@ TARGET_LINK_LIBRARIES(frida ${QT_LIBRARIES} ${LLVM_LDFLAGS} -lLLVM-3.5 ${LLVM_SY set(fridatest_SOURCES src/test.cxx + src/core/tests/BasicBlockTest.cxx + ${frida_SOURCES} ) add_executable(fridatest ${fridatest_SOURCES}) + +qt5_use_modules(fridatest Widgets) + #ADD_SUBDIRECTORY(/usr/src/gtest gtest) ADD_SUBDIRECTORY(/usr/src/gmock gmock) enable_testing() -target_link_libraries(fridatest gmock gtest gmock_main) +target_link_libraries(fridatest gmock gtest gmock_main ${QT_LIBRARIES} ${LLVM_LDFLAGS} -lLLVM-3.5 ${LLVM_SYSTEM_LIBS} ${LOG4CXX_LDFLAGS} ${GUILE_LDFLAGS} ${QUAZIP_LIBRARIES}) add_test( fridatest fridatest ) diff --git a/src/core/InformationManager.hxx b/src/core/InformationManager.hxx index 5681842..f6eeb30 100644 --- a/src/core/InformationManager.hxx +++ b/src/core/InformationManager.hxx @@ -6,7 +6,8 @@ #include #include -class Disassembler; +#include "disassembler/Disassembler.hxx" + class Function; class BasicBlock; class Comment; diff --git a/src/core/tests/BasicBlockTest.cxx b/src/core/tests/BasicBlockTest.cxx new file mode 100644 index 0000000..0980af5 --- /dev/null +++ b/src/core/tests/BasicBlockTest.cxx @@ -0,0 +1,62 @@ +#include +#include + +#include "gui/qt.hxx" +#include "core/InformationManager.hxx" +#include "core/BasicBlock.hxx" + +TEST(BasicBlockTest, deserializeValidInstance) { + QFile file("testdata/core/BasicBlock/valid.xml"); + InformationManager manager; + file.open(QFile::ReadOnly | QFile::Text); + QXmlStreamReader reader(&file); + + reader.readNextStartElement(); + BasicBlock* block = BasicBlock::deserialize(reader, &manager); + + ASSERT_NE((void*)NULL, (void*)block); + EXPECT_EQ(0x403e50, block->getStartAddress()); + EXPECT_EQ(0x403e66, block->getEndAddress()); + EXPECT_EQ(0x403e50, block->getNextBlock(0)); + EXPECT_EQ(0x403e66, block->getNextBlock(1)); +} + +TEST(BasicBlockTest, deserializeInvalidInstance) { + QFile file("testdata/core/BasicBlock/invalid.xml"); + InformationManager manager; + file.open(QFile::ReadOnly | QFile::Text); + QXmlStreamReader reader(&file); + + reader.readNextStartElement(); + BasicBlock* block = BasicBlock::deserialize(reader, &manager); + + ASSERT_EQ((void*)NULL, (void*)block); +} + +TEST(BasicBlockTest, deserializeThenSerializeIsIdentity) { + QFile infile("testdata/core/BasicBlock/valid.xml"); + QTemporaryFile outfile; + + InformationManager manager; + infile.open(QFile::ReadOnly | QFile::Text); + outfile.open(); + QXmlStreamReader reader(&infile); + QXmlStreamWriter writer(&outfile); + + reader.readNextStartElement(); + BasicBlock* block = BasicBlock::deserialize(reader, &manager); + block->serialize(writer); + + infile.seek(0); + outfile.seek(0); + + std::string outtext = outfile.readAll().data(); + outtext.erase(remove_if(outtext.begin(), outtext.end(), isspace), + outtext.end()); + + std::string intext = infile.readAll().data(); + intext.erase(remove_if(intext.begin(), intext.end(), isspace), + intext.end()); + + ASSERT_STREQ(outtext.c_str(), intext.c_str()); +} diff --git a/testdata/core/BasicBlock/invalid.xml b/testdata/core/BasicBlock/invalid.xml new file mode 100644 index 0000000..61f9af2 --- /dev/null +++ b/testdata/core/BasicBlock/invalid.xml @@ -0,0 +1,8 @@ + + 403e50 + 403e66 + 403e50 + 403e66 + 666 + + diff --git a/testdata/core/BasicBlock/valid.xml b/testdata/core/BasicBlock/valid.xml new file mode 100644 index 0000000..c0502c7 --- /dev/null +++ b/testdata/core/BasicBlock/valid.xml @@ -0,0 +1,7 @@ + + 403e50 + 403e66 + 403e50 + 403e66 + +