Add Comment; Chang the CMake structure of the Project;
This commit is contained in:
parent
a3a1bef753
commit
cf8e3cf696
@ -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)
|
||||
add_subdirectory(test)
|
@ -11,7 +11,7 @@
|
||||
|
||||
#include "type.h"
|
||||
#include "cpart.h"
|
||||
#include "sha256_cpp_binding.h"
|
||||
#include "util/sha256_generator.h"
|
||||
#include "sql.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;
|
||||
|
@ -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 <string>
|
||||
#include <map>
|
||||
@ -14,7 +14,8 @@ using std::pair;
|
||||
|
||||
//提示信息打印类函数
|
||||
namespace Net {
|
||||
namespace error {
|
||||
|
||||
namespace printTools {
|
||||
|
||||
using FormalItem = pair<string, string>;
|
||||
|
||||
@ -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<FormalItem> body);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif //NET_ERROR_H
|
||||
#endif //NET_PRINT_TOOLS_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 <openssl/rsa.h>
|
||||
#include <openssl/pem.h>
|
||||
@ -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
|
@ -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){
|
2
src/CMakeLists.txt
Normal file
2
src/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
add_subdirectory(utils)
|
||||
add_subdirectory(debug_tools)
|
1
src/debug_tools/CMakeLists.txt
Normal file
1
src/debug_tools/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
add_library(debugTools STATIC 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<FormalItem> 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());
|
||||
}
|
||||
}
|
1
src/utils/CMakeLists.txt
Normal file
1
src/utils/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
add_library(utils STATIC rsa_key_chain.cpp)
|
@ -2,7 +2,10 @@
|
||||
// Created by Eric Saturn on 2019/12/10.
|
||||
//
|
||||
|
||||
#include "rsa_cpp_binding.h"
|
||||
#include <stdexcept>
|
||||
#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;
|
||||
}
|
@ -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;
|
8
test/CMakeLists.txt
Normal file
8
test/CMakeLists.txt
Normal file
@ -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)
|
@ -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:
|
||||
|
@ -1,52 +0,0 @@
|
||||
//
|
||||
// Created by Eric Saturn on 2019/12/12.
|
||||
//
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <rsa_cpp_binding.h>
|
||||
|
||||
#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<RSAPubKey>(new RSAPubKey(_env->rsa->getRSA()));
|
||||
|
||||
}
|
||||
|
||||
TEST(RSATest, prv_key_get_test_1){
|
||||
_env->prvKey = shared_ptr<RSAPrvKey>(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);
|
||||
}
|
9
test/test_util/CMakeLists.txt
Normal file
9
test/test_util/CMakeLists.txt
Normal file
@ -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)
|
@ -4,14 +4,12 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#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<GlobalTestEnv *>(::testing::AddGlobalTestEnvironment(new GlobalTestEnv));
|
||||
env = dynamic_cast<GlobalTestEnv *>(::testing::AddGlobalTestEnvironment(new GlobalTestEnv));
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
55
test/test_util/test_rsa.cpp
Normal file
55
test/test_util/test_rsa.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
//
|
||||
// Created by Eric Saturn on 2019/12/12.
|
||||
//
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#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<RSAPubKey>(env->rsa->getRSA());
|
||||
|
||||
}
|
||||
|
||||
TEST(RSATest, prv_key_get_test_1){
|
||||
env->prvKey = std::make_shared<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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user