From cf8e3cf696c7c3a074f3d79b478dee47c185c8d1 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 26 Jun 2020 20:28:56 +0800 Subject: [PATCH] Add Comment; Chang the CMake structure of the Project; --- CMakeLists.txt | 27 ++------- include/cmap.h | 2 +- include/cproj.h | 2 +- .../{error.h => debug_tools/print_tools.h} | 13 +++-- .../rsa_key_chain.h} | 23 +++++--- .../sha256_generator.h} | 4 ++ src/CMakeLists.txt | 2 + src/debug_tools/CMakeLists.txt | 1 + .../print_tools.cpp} | 26 ++++----- src/utils/CMakeLists.txt | 1 + .../rsa_key_chain.cpp} | 7 ++- .../sha256_generator.cpp} | 5 +- test/CMakeLists.txt | 8 +++ test/env.h | 2 +- test/rsa_test.cpp | 52 ------------------ test/test_util/CMakeLists.txt | 9 +++ test/{test_main.cpp => test_util/main.cpp} | 8 +-- test/test_util/test_rsa.cpp | 55 +++++++++++++++++++ 18 files changed, 134 insertions(+), 113 deletions(-) rename include/{error.h => debug_tools/print_tools.h} (70%) rename include/{rsa_cpp_binding.h => utils/rsa_key_chain.h} (88%) rename include/{sha256_cpp_binding.h => utils/sha256_generator.h} (89%) create mode 100644 src/CMakeLists.txt create mode 100644 src/debug_tools/CMakeLists.txt rename src/{error.cpp => debug_tools/print_tools.cpp} (66%) create mode 100644 src/utils/CMakeLists.txt rename src/{rsa_cpp_binding.cpp => utils/rsa_key_chain.cpp} (93%) rename src/{sha256_cpp_binding.cpp => utils/sha256_generator.cpp} (86%) create mode 100644 test/CMakeLists.txt delete mode 100644 test/rsa_test.cpp create mode 100644 test/test_util/CMakeLists.txt rename test/{test_main.cpp => test_util/main.cpp} (53%) create mode 100644 test/test_util/test_rsa.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 39864fd..e4430af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.13) project(Net) set(CMAKE_CXX_STANDARD 14) @@ -7,29 +7,10 @@ include_directories(include/) include_directories(utils/) find_package(Boost REQUIRED) -find_package(OpenSSL REQUIRED) - -message(STATUS SSL ${OPENSSL_LIBRARIES}) - -include(GoogleTest) - -set(OPENSSL_LIBS /usr/local/lib/libssl.dylib /usr/local/lib/libcrypto.dylib) -set(GTEST_LIB /usr/local/lib/) -set(GTEST_LIBS gtest pthread dl) +find_package(OpenSSL 1.1.1 REQUIRED) include_directories(${Boost_INCLUDE_DIRS} ${OPENSSL_INCLUDE_DIR} ${SQLiteCpp_INCLUDE_DIRS}) -link_directories(${GTEST_LIB}) -aux_source_directory(src SOURCE_ALL) +add_subdirectory(src) -add_executable(NetTest test/test_main.cpp) - -gtest_add_tests(TARGET NetTest - TEST_SUFFIX .noArgs - TEST_LIST noArgsTests) - -add_library(m_error STATIC src/error.cpp) -add_library(m_rsa STATIC src/rsa_cpp_binding.cpp) - -target_link_libraries(NetTest m_rsa m_error ${GTEST_LIBS} ssl crypto boost_program_options) -set_tests_properties(${noArgsTests} PROPERTIES TIMEOUT 10) \ No newline at end of file +add_subdirectory(test) \ No newline at end of file diff --git a/include/cmap.h b/include/cmap.h index 9307956..53a8bee 100644 --- a/include/cmap.h +++ b/include/cmap.h @@ -11,7 +11,7 @@ #include "type.h" #include "cpart.h" -#include "sha256_cpp_binding.h" +#include "util/sha256_generator.h" #include "sql.h" //计算模块管理对象间的依赖关系管理结构 diff --git a/include/cproj.h b/include/cproj.h index 949b1cc..fce1c8a 100644 --- a/include/cproj.h +++ b/include/cproj.h @@ -11,7 +11,7 @@ #include "type.h" #include "cpart.h" -#include "sha256_cpp_binding.h" +#include "util/sha256_generator.h" #include "sql.h" class Proj; diff --git a/include/error.h b/include/debug_tools/print_tools.h similarity index 70% rename from include/error.h rename to include/debug_tools/print_tools.h index d20088d..548764e 100644 --- a/include/error.h +++ b/include/debug_tools/print_tools.h @@ -2,8 +2,8 @@ // Created by Eric Saturn on 2019/12/12. // -#ifndef NET_ERROR_H -#define NET_ERROR_H +#ifndef NET_PRINT_TOOLS_H +#define NET_PRINT_TOOLS_H #include #include @@ -14,7 +14,8 @@ using std::pair; //提示信息打印类函数 namespace Net { - namespace error { + + namespace printTools { using FormalItem = pair; @@ -22,17 +23,17 @@ namespace Net { void printWarning(string warning_info); - void printSuccess(string succes_info); + void printSuccess(string success_info); void printRed(string red_info); void printInfo(const string& info, string tag = ""); - void printInfoBuffer(const string& info, string tag = ""); + void printInfoBuffer(const string& info, const string& tag = ""); void printInfoFormal(const string& title, initializer_list body); } } -#endif //NET_ERROR_H +#endif //NET_PRINT_TOOLS_H diff --git a/include/rsa_cpp_binding.h b/include/utils/rsa_key_chain.h similarity index 88% rename from include/rsa_cpp_binding.h rename to include/utils/rsa_key_chain.h index 97a179f..07861ab 100644 --- a/include/rsa_cpp_binding.h +++ b/include/utils/rsa_key_chain.h @@ -2,11 +2,11 @@ // Created by Eric Saturn on 2019/12/10. // -#ifndef NET_RSA_CPP_BINDING_H -#define NET_RSA_CPP_BINDING_H +#ifndef NET_RSA_KEY_CHAIN_H +#define NET_RSA_KEY_CHAIN_H -#include "error.h" -#include "../src/bignumber.cpp" +#include "debug_tools/print_tools.h" +#include "../../src/bignumber.cpp" #include #include @@ -18,6 +18,10 @@ using namespace std; namespace Net { + + /** + * The Public Key For A RSA Key Chain + */ class RSAPubKey{ public: explicit RSAPubKey(const RSA *rsa){ @@ -29,7 +33,7 @@ namespace Net { } void printInfo(){ - error::printInfoFormal("RSAPubKey Info", { + printTools::printInfoFormal("RSAPubKey Info", { {"n", this->n.getDataHex()}, {"e", this->e.getDataHex()} }); @@ -39,6 +43,9 @@ namespace Net { BigNumber e; }; + /** + * The Private Key For A RSA Key Chain + */ class RSAPrvKey{ public: explicit RSAPrvKey(const RSA *rsa) { @@ -54,8 +61,8 @@ namespace Net { this->q.copyFrom(q); } - void printInfo(){ - error::printInfoFormal("RSAPrvKey Info", { + void printInfo() const{ + printTools::printInfoFormal("RSAPrvKey Info", { {"n", this->n.getDataHex()}, {"e", this->e.getDataHex()}, {"d", this->d.getDataHex()}, @@ -134,4 +141,4 @@ namespace Net { } -#endif //NET_RSA_CPP_BINDING_H +#endif //NET_RSA_KEY_CHAIN_H diff --git a/include/sha256_cpp_binding.h b/include/utils/sha256_generator.h similarity index 89% rename from include/sha256_cpp_binding.h rename to include/utils/sha256_generator.h index e3bb3ff..fc58c87 100644 --- a/include/sha256_cpp_binding.h +++ b/include/utils/sha256_generator.h @@ -9,6 +9,10 @@ using std::string; using std::ifstream; using std::stringstream; +/** + * The Generator of SHA256 Hex + * We can use it in an easy way. + */ class SHA256Generator{ public: SHA256Generator(string data){ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..4f991e6 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(utils) +add_subdirectory(debug_tools) \ No newline at end of file diff --git a/src/debug_tools/CMakeLists.txt b/src/debug_tools/CMakeLists.txt new file mode 100644 index 0000000..7c1e1a5 --- /dev/null +++ b/src/debug_tools/CMakeLists.txt @@ -0,0 +1 @@ +add_library(debugTools STATIC print_tools.cpp) \ No newline at end of file diff --git a/src/error.cpp b/src/debug_tools/print_tools.cpp similarity index 66% rename from src/error.cpp rename to src/debug_tools/print_tools.cpp index d6ba7f1..072ab80 100644 --- a/src/error.cpp +++ b/src/debug_tools/print_tools.cpp @@ -2,29 +2,29 @@ // Created by Eric Saturn on 2019/12/12. // -#include "error.h" +#include "debug_tools/print_tools.h" using std::string; namespace Net { - namespace error { - void printError(string error_info) { + namespace printTools { + void printError(const string &error_info) { printf("\033[31mError: %s\033[0m\n", error_info.data()); } - void printWarning(string warning_info) { + void printWarning(const string &warning_info) { printf("\033[33mWarning: %s\033[0m\n", warning_info.data()); } - void printSuccess(string succes_info) { - printf("\033[32m%s\033[0m\n", succes_info.data()); + void printSuccess(const string &success_info) { + printf("\033[32m%s\033[0m\n", success_info.data()); } - void printRed(string red_info) { + void printRed(const string &red_info) { printf("\033[31m%s\n\033[0m", red_info.data()); } - void printInfo(const string& info, string tag) { + void printInfo(const string& info, string &tag) { printf("[DEBUG INFO] %s ", info.data()); if(tag.size()) printf("{ %s }\n",tag.data()); @@ -33,17 +33,17 @@ namespace Net { void printInfoFormal(const string& title, initializer_list body) { printf("\n>>>\n {%s}\n",title.data()); printf(">-------------------------------------\n"); - for(auto item : body){ + for(auto &item : body){ printf("[%s] : \"%s\"; \n", item.first.data(), item.second.data()); } printf("----------------------------------<\n<<<\n\n"); } - void printInfoBuffer(const string &info, string tag) { + void printInfoBuffer(const string &info, const string& tag) { printf("\n[DEBUG INFO (BUFFER)]\n"); printf(">----------------------------------------------\n"); - uint8_t *p_i = (uint8_t *) &info[0]; - uint8_t *p_e = (uint8_t *) &info[info.size()-1]; + auto *p_i = (uint8_t *) &info[0]; + auto *p_e = (uint8_t *) &info[info.size()-1]; for(int c = 0;p_i < p_e; ++p_i, ++c){ if(!(c % 16) && c) printf("\n"); printf("%02x ",*p_i); @@ -51,7 +51,7 @@ namespace Net { } printf("\n"); printf("----------------------------------------------<\n"); - if(tag.size()) + if(!tag.empty()) printf("{ %s }\n\n",tag.data()); } } diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 0000000..c2e71bb --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1 @@ +add_library(utils STATIC rsa_key_chain.cpp) \ No newline at end of file diff --git a/src/rsa_cpp_binding.cpp b/src/utils/rsa_key_chain.cpp similarity index 93% rename from src/rsa_cpp_binding.cpp rename to src/utils/rsa_key_chain.cpp index 6a9b774..9880f19 100644 --- a/src/rsa_cpp_binding.cpp +++ b/src/utils/rsa_key_chain.cpp @@ -2,7 +2,10 @@ // Created by Eric Saturn on 2019/12/10. // -#include "rsa_cpp_binding.h" +#include +#include "utils/rsa_key_chain.h" + +using std::runtime_error; void Net::RSAKeyChain::generateKeyPair() { BigNumber e; @@ -56,6 +59,6 @@ bool Net::RSAKeyChain::checkKey() { if(!this->if_prv_key) throw runtime_error("illegal call of checkKey"); if(this->key_pair == nullptr) throw runtime_error("key pair is invalid"); int return_code = RSA_check_key(this->key_pair); - if(return_code == -1) throw runtime_error("error occur when rsa check key"); + if(return_code == -1) throw runtime_error("printTools occur when rsa check key"); else return return_code == 1; } diff --git a/src/sha256_cpp_binding.cpp b/src/utils/sha256_generator.cpp similarity index 86% rename from src/sha256_cpp_binding.cpp rename to src/utils/sha256_generator.cpp index bfc5301..cd75f18 100644 --- a/src/sha256_cpp_binding.cpp +++ b/src/utils/sha256_generator.cpp @@ -1,6 +1,9 @@ #include "type.h" -#include "sha256_cpp_binding.h" +#include "util/sha256_generator.h" +/** + * Generate SHA256 Hex Code + */ void SHA256Generator::generate() { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..65da75a --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,8 @@ +find_package(GTest REQUIRED) + +set(GTEST_LIB /usr/local/lib/) +set(GTEST_LIBS gtest gmock pthread dl) + +link_directories(${GTEST_LIB}) + +add_subdirectory(test_util) diff --git a/test/env.h b/test/env.h index fd39229..d2f2d1c 100644 --- a/test/env.h +++ b/test/env.h @@ -5,7 +5,7 @@ #ifndef NET_ENV_H #define NET_ENV_H -#include "rsa_cpp_binding.h" +#include "utils/rsa_key_chain.h" class GlobalTestEnv : public testing::Environment{ public: diff --git a/test/rsa_test.cpp b/test/rsa_test.cpp deleted file mode 100644 index 1ae60bd..0000000 --- a/test/rsa_test.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// -// Created by Eric Saturn on 2019/12/12. -// - -#include -#include - -#include "env.h" - -using namespace Net; -using namespace std; - -extern GlobalTestEnv *_env; - -TEST(RSATest, init_test_1) { - -} - -TEST(RSATest, generate_test_1) { - _env->rsa->generateKeyPair(); - error::printInfo(to_string(_env->rsa->getBufferSize()), string("Buffer Size")); -} - -TEST(RSATest, pub_encrypt_test_1) { - string encrypted_data; - _env->rsa->publicKeyEncrypt(_env->rsa_test_data, encrypted_data); - error::printInfoBuffer(encrypted_data, "Encrypted Data"); - _env->rsa_encrypt_data = encrypted_data; -} - -TEST(RSATest, prv_decrypt_test_1){ - string data; - _env->rsa->privateKeyDecrypt(data, _env->rsa_encrypt_data); - error::printInfo(data, "Decrypt Data"); - ASSERT_EQ(data, _env->rsa_test_data); -} - -TEST(RSATest, pub_key_get_test_1){ - _env->pubKey = shared_ptr(new RSAPubKey(_env->rsa->getRSA())); - -} - -TEST(RSATest, prv_key_get_test_1){ - _env->prvKey = shared_ptr(new RSAPrvKey(_env->rsa->getRSA())); - ASSERT_EQ(_env->rsa->checkKey(), true); - _env->prvKey->printInfo(); -} - -TEST(RSATest, prv_key_build_key_chain){ - RSAKeyChain keyChain(*_env->prvKey); - ASSERT_EQ(keyChain.checkKey(), true); -} \ No newline at end of file diff --git a/test/test_util/CMakeLists.txt b/test/test_util/CMakeLists.txt new file mode 100644 index 0000000..6925f9a --- /dev/null +++ b/test/test_util/CMakeLists.txt @@ -0,0 +1,9 @@ + +add_executable(testUtils main.cpp) + +message(${GTEST_LIBS}) + +link_libraries(testUtils utils debugTools ${GTEST_LIBS}) + +add_test(Name testUtils + COMMAND testUtils) \ No newline at end of file diff --git a/test/test_main.cpp b/test/test_util/main.cpp similarity index 53% rename from test/test_main.cpp rename to test/test_util/main.cpp index 39098e8..d9a0cf1 100644 --- a/test/test_main.cpp +++ b/test/test_util/main.cpp @@ -4,14 +4,12 @@ #include -#include "rsa_test.cpp" +#include "../env.h" -#include "env.h" - -GlobalTestEnv *_env; +GlobalTestEnv *env; int main(int argc, char *argv[]){ ::testing::InitGoogleTest(&argc, argv); - _env = dynamic_cast(::testing::AddGlobalTestEnvironment(new GlobalTestEnv)); + env = dynamic_cast(::testing::AddGlobalTestEnvironment(new GlobalTestEnv)); return RUN_ALL_TESTS(); } diff --git a/test/test_util/test_rsa.cpp b/test/test_util/test_rsa.cpp new file mode 100644 index 0000000..79dc25c --- /dev/null +++ b/test/test_util/test_rsa.cpp @@ -0,0 +1,55 @@ +// +// Created by Eric Saturn on 2019/12/12. +// + +#include + +#include + +#include "../env.h" + +#include "utils/rsa_key_chain.h" + +using namespace Net; +using namespace std; + +extern GlobalTestEnv *env; + +TEST(RSATest, init_test_1) { + +} + +TEST(RSATest, generate_test_1) { + env->rsa->generateKeyPair(); + printTools::printInfo(to_string(env->rsa->getBufferSize()), string("Buffer Size")); +} + +TEST(RSATest, pub_encrypt_test_1) { + string encrypted_data; + env->rsa->publicKeyEncrypt(env->rsa_test_data, encrypted_data); + printTools::printInfoBuffer(encrypted_data, "Encrypted Data"); + env->rsa_encrypt_data = encrypted_data; +} + +TEST(RSATest, prv_decrypt_test_1){ + string data; + env->rsa->privateKeyDecrypt(data, env->rsa_encrypt_data); + printTools::printInfo(data, "Decrypt Data"); + ASSERT_EQ(data, env->rsa_test_data); +} + +TEST(RSATest, pub_key_get_test_1){ + env->pubKey = std::make_shared(env->rsa->getRSA()); + +} + +TEST(RSATest, prv_key_get_test_1){ + env->prvKey = std::make_shared(env->rsa->getRSA()); + ASSERT_EQ(env->rsa->checkKey(), true); + env->prvKey->printInfo(); +} + +TEST(RSATest, prv_key_build_key_chain){ + RSAKeyChain keyChain(*env->prvKey); + ASSERT_EQ(keyChain.checkKey(), true); +} \ No newline at end of file