Sfoglia il codice sorgente

First unit tests using gtest and gmock

Jonas Pflanzer 4 anni fa
parent
commit
8d6ce34343

+ 2 - 13
daemon/CMakeLists.txt

@@ -5,17 +5,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 
 project(ccats)
 
-add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp src/base64.cpp src/JsonCommander.cpp src/FileManager.cpp)
+include(src/CMakeLists.txt)
 
-# use pkg-config to fix building on debian unstable
-find_package(PkgConfig REQUIRED)
-pkg_check_modules(TINS REQUIRED libtins>=4.2 libpcap)
-pkg_check_modules(JSONCPP REQUIRED jsoncpp)
-
-
-find_package(Threads REQUIRED)
-find_package(Boost 1.67 REQUIRED COMPONENTS system)
-# find_package(libtins 4.2 REQUIRED)
-
-include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR})
-target_link_libraries(ccats PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${TINS_LIBRARIES} ${PCAP_LIBRARIES} ${JSONCPP_LIBRARIES})
+include(test/CMakeLists.txt)

+ 9 - 14
daemon/include/FileManager.h

@@ -56,25 +56,25 @@ public:
    * Checks if an upload is running
    * @return true - upload running | false - no upload
    */
-  bool isUploading();
+  virtual bool isUploading();
 
   /**
    * Check if a download is running
    * @return true - download running | false - no download
    */
-  bool isDownloading();
+  virtual bool isDownloading();
 
   /**
    * Opens put file if it doesn't exist
    * @return true - file is open | false - file is not open
    */
-  bool openPutFile(const std::string &filename);
+  virtual bool openPutFile(const std::string &filename);
 
   /**
    * Opens get file if it exists and reports the amount of chunks
    * @return true - file is open | false - file is not open
    */
-  bool openGetFile(const std::string &filename, int &chunks);
+  virtual bool openGetFile(const std::string &filename, int &chunks);
 
   /**
    * Closes file
@@ -89,7 +89,7 @@ public:
   /**
    * Closes put file and deletes it
    */
-  void cancelPut();
+  virtual void cancelPut();
 
   /**
    * Checks if a file name is valid
@@ -101,28 +101,23 @@ public:
    * Return the name of the download file
    * @return name of the download file
    */
-  std::string getGetBaseFileName();
+  virtual std::string getGetBaseFileName();
 
   /**
    * Return the name of the upload file
    * @return name of the upload file
    */
-  std::string getPutBaseFileName();
+  virtual std::string getPutBaseFileName();
 
   /**
    * Writes data to put file
    */
-  void writePut(const std::vector<char> &data);
+  virtual void writePut(const std::vector<char> &data);
 
   /**
    * Reads data from get file
    */
-  std::vector<char> readGet();
-
-  /**
-   * Returns number of chunks for download
-   */
-  int getGetChunks();
+  virtual std::vector<char> readGet();
 };
 
 #endif

+ 21 - 0
daemon/src/CMakeLists.txt

@@ -0,0 +1,21 @@
+cmake_minimum_required(VERSION 2.8)
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
+
+project(ccats)
+
+add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp src/base64.cpp src/JsonCommander.cpp src/FileManager.cpp)
+
+# use pkg-config to fix building on debian unstable
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(TINS REQUIRED libtins>=4.2 libpcap)
+pkg_check_modules(JSONCPP REQUIRED jsoncpp)
+
+
+find_package(Threads REQUIRED)
+find_package(Boost 1.67 REQUIRED COMPONENTS system)
+# find_package(libtins 4.2 REQUIRED)
+
+include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR})
+target_link_libraries(ccats PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${TINS_LIBRARIES} ${PCAP_LIBRARIES} ${JSONCPP_LIBRARIES})

+ 32 - 0
daemon/test/CMakeLists.txt

@@ -0,0 +1,32 @@
+cmake_minimum_required(VERSION 2.8)
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
+
+project(ccats)
+
+# use pkg-config to fix building on debian unstable
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(JSONCPP REQUIRED jsoncpp)
+
+
+find_package(Threads REQUIRED)
+find_package(Boost 1.67 REQUIRED COMPONENTS system)
+
+# Setup testing
+enable_testing()
+
+pkg_check_modules(GMOCK REQUIRED gmock)
+
+find_package(GTest REQUIRED)
+
+include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR} ${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR})
+
+# Add test cpp file
+add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp)
+target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GMOCK_LIBRARIES} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
+
+add_test(
+  NAME jsonCommanderTest
+  COMMAND jsonCommanderTest
+)

+ 29 - 0
daemon/test/FileManagerMock.h

@@ -0,0 +1,29 @@
+#ifndef FILEMANAGERMOCK_H
+#define FILEMANAGERMOCK_H
+
+#include <gmock/gmock.h>
+#include "../include/FileManager.h"
+
+/**
+ * @class FileManagerMock
+ *
+ * Gmock stub class for FileManager so you can test without writing and reading actual files.
+ */
+class FileManagerMock : public FileManager {
+public:
+  MOCK_METHOD(bool, openGetFile, (const std::string &filename, int &chunks), (override));
+  MOCK_METHOD(bool, openPutFile, (const std::string &filename), (override));
+
+  MOCK_METHOD(bool, isDownloading, (), (override));
+  MOCK_METHOD(bool, isUploading, (), (override));
+
+  MOCK_METHOD(void, cancelPut, (), (override));
+
+  MOCK_METHOD(std::string, getGetBaseFileName, (), (override));
+  MOCK_METHOD(std::string, getPutBaseFileName, (), (override));
+
+  MOCK_METHOD(void, writePut, (const std::vector<char> &data), (override));
+  MOCK_METHOD(std::vector<char>, readGet, (), (override));
+};
+
+#endif

+ 110 - 0
daemon/test/JsonCommanderTest.cpp

@@ -0,0 +1,110 @@
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+#include "../include/JsonCommander.h"
+#include "FileManagerMock.h"
+
+namespace {
+  /* Version tests */
+  TEST(testVersion, Positive) {
+    FileManagerMock fileManager;
+
+    JsonCommander jsonCommander(fileManager);
+
+    const std::string versionString = "0.2";
+    Json::Value version;
+    version["version"] = versionString;
+
+    JsonCommander::Response response = jsonCommander.testVersion(version);
+    EXPECT_TRUE(response.action == JsonCommander::Action::send);
+    EXPECT_TRUE(response.json["accept"].asBool());
+    EXPECT_EQ(response.json["version"].asString(), versionString);
+  }
+
+  TEST(testVersion, Negative) {
+    FileManagerMock fileManager;
+
+    JsonCommander jsonCommander(fileManager);
+
+    const std::string versionString = "0.1";
+    Json::Value version;
+    version["version"] = versionString;
+
+    JsonCommander::Response response = jsonCommander.testVersion(version);
+    EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
+    EXPECT_FALSE(response.json["accept"].asBool());
+    EXPECT_FALSE(response.json["version"].asString().compare(versionString) == 0);
+  }
+
+  /* Status tests */
+  TEST(Status, Ok) {
+    FileManagerMock fileManager;
+
+    JsonCommander jsonCommander(fileManager);
+
+    const std::string command = "status";
+    Json::Value message;
+    message["command"] = command;
+
+    JsonCommander::Response response = jsonCommander.execute(message);
+
+    EXPECT_TRUE(response.action == JsonCommander::Action::send);
+    EXPECT_EQ(response.json["command"].asString(), command);
+    EXPECT_EQ(response.json["response"].asString(), "ok");
+  }
+
+  TEST(Status, Downloading) {
+    FileManagerMock fileManager;
+
+    JsonCommander jsonCommander(fileManager);
+
+    const std::string command = "status";
+    Json::Value message;
+    message["command"] = command;
+
+    ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
+
+    JsonCommander::Response response = jsonCommander.execute(message);
+
+    EXPECT_TRUE(response.action == JsonCommander::Action::send);
+    EXPECT_EQ(response.json["command"].asString(), command);
+    EXPECT_EQ(response.json["response"].asString(), "download running");
+  }
+
+  TEST(Status, Uploading) {
+    FileManagerMock fileManager;
+
+    JsonCommander jsonCommander(fileManager);
+
+    const std::string command = "status";
+    Json::Value message;
+    message["command"] = command;
+
+    ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
+
+    JsonCommander::Response response = jsonCommander.execute(message);
+
+    EXPECT_TRUE(response.action == JsonCommander::Action::send);
+    EXPECT_EQ(response.json["command"].asString(), command);
+    EXPECT_EQ(response.json["response"].asString(), "upload running");
+  }
+
+  TEST(Status, UploadingAndDownloading) {
+    FileManagerMock fileManager;
+
+    JsonCommander jsonCommander(fileManager);
+
+    const std::string command = "status";
+    Json::Value message;
+    message["command"] = command;
+
+    ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
+    ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
+
+    JsonCommander::Response response = jsonCommander.execute(message);
+
+    EXPECT_TRUE(response.action == JsonCommander::Action::send);
+    EXPECT_EQ(response.json["command"].asString(), command);
+    EXPECT_EQ(response.json["response"].asString(), "download and upload running");
+  }
+}