From c7ba2112bb27fbc4e4d144154ebfc0640060a83b Mon Sep 17 00:00:00 2001 From: "yangwei.yao" Date: Sat, 11 May 2019 14:08:30 +0800 Subject: [PATCH 1/9] 05.11 Former-commit-id: 22e7d5214db47ac228d50989b01e3a5e71a719d4 --- cpp/CMakeLists.txt | 2 +- cpp/src/license/BoostArchive.h | 133 +++++++ cpp/src/license/License.cpp | 371 +++++++++++++++++++ cpp/src/license/License.h | 81 ++++ cpp/src/license/LicenseCreateTime.cpp | 51 +++ cpp/src/license/LicenseCreateuuidshafile.cpp | 31 ++ cpp/src/license/LicenseGenerator.cpp | 2 +- cpp/src/license/LicensePublic.cpp | 284 ++++++++++++++ cpp/src/license/LicensePublic.h | 68 ++++ cpp/src/license/LicenseRun.cpp | 13 + cpp/src/utils/Error.h | 1 + cpp/unittest/license/CMakeLists.txt | 22 +- cpp/unittest/license/license_tests.cpp | 174 ++++++++- 13 files changed, 1218 insertions(+), 15 deletions(-) create mode 100644 cpp/src/license/BoostArchive.h create mode 100644 cpp/src/license/License.cpp create mode 100644 cpp/src/license/License.h create mode 100644 cpp/src/license/LicenseCreateTime.cpp create mode 100644 cpp/src/license/LicenseCreateuuidshafile.cpp create mode 100644 cpp/src/license/LicensePublic.cpp create mode 100644 cpp/src/license/LicensePublic.h create mode 100644 cpp/src/license/LicenseRun.cpp diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index e6f65de454..d249baf1c3 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -69,7 +69,7 @@ link_directories(${VECWISE_THIRD_PARTY_BUILD}/lib64) # WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/third_party) add_subdirectory(src) -add_subdirectory(test_client) +#add_subdirectory(test_client) if (BUILD_UNIT_TEST) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/unittest) diff --git a/cpp/src/license/BoostArchive.h b/cpp/src/license/BoostArchive.h new file mode 100644 index 0000000000..4074cc0758 --- /dev/null +++ b/cpp/src/license/BoostArchive.h @@ -0,0 +1,133 @@ +// +// Created by zilliz on 19-5-10. +// + +#ifndef VECWISE_ENGINE_BOOSTARCHIVE_H +#define VECWISE_ENGINE_BOOSTARCHIVE_H +#include +#include +#include +#include +#include + +using std::list; +using std::ifstream; +using std::ofstream; +using std::string; +using std::map; + +template +class BoostArchive +{ +public: + typedef T entity_type; + typedef boost::archive::binary_iarchive InputArchive; + typedef boost::archive::binary_oarchive OutputArchive; + + BoostArchive(const string & archive_file_path) + : _file_path_name(archive_file_path) + , _p_ofs(NULL) + , _p_output_archive(NULL) + , _entity_nums(0) + { + load_arvhive_info(); + } + ~BoostArchive() + { + close_output(); + } + //存储一个对象,序列化 + void store(const entity_type & entity); + + //反序列化, 提取所有对象 + bool restore(list & entitys); + + size_t size() const + { + return _entity_nums; + } + +private: + void save_archive_info() //保存已序列化的对象个数信息 + { + ofstream ofs; + ofs.open(get_archive_info_file_path(),std::ios::out | std::ios::trunc); + if (ofs.is_open()) + { + ofs << _entity_nums; + } + ofs.close(); + } + + void load_arvhive_info()//读取已序列化的对象个数信息 + { + ifstream ifs; + ifs.open(get_archive_info_file_path(),std::ios_base::in); + if (ifs.is_open() && !ifs.eof()) + { + int enity_num = 0; + ifs >> enity_num; + _entity_nums = enity_num; + } + ifs.close(); + } + + string get_archive_info_file_path() + { + return "/tmp/vecwise_engine.meta"; + } + + void close_output() + { + if (NULL != _p_output_archive) + { + delete _p_output_archive; + _p_output_archive = NULL; + save_archive_info(); + } + if (NULL != _p_ofs) + { + delete _p_ofs; + _p_ofs = NULL; + } + } + +private: + size_t _entity_nums; + string _file_path_name; + ofstream * _p_ofs; + OutputArchive * _p_output_archive; +}; + +template +bool BoostArchive::restore( list & entitys ) +{ + close_output(); + load_arvhive_info(); + ifstream ifs(_file_path_name); + if (ifs) + { + InputArchive ia(ifs); + for (size_t cnt = 0; cnt < _entity_nums; ++cnt) + { + entity_type entity; + ia & entity; + entitys.push_back(entity); + } + return true; + } + return false; +} + +template +void BoostArchive::store( const entity_type & entity ) +{ + if (NULL == _p_output_archive) + { + _p_ofs = new ofstream(_file_path_name); + _p_output_archive = new OutputArchive(*_p_ofs); + } + (*_p_output_archive) & entity; + ++_entity_nums; +} +#endif //VECWISE_ENGINE_BOOSTARCHIVE_H diff --git a/cpp/src/license/License.cpp b/cpp/src/license/License.cpp new file mode 100644 index 0000000000..193a322e50 --- /dev/null +++ b/cpp/src/license/License.cpp @@ -0,0 +1,371 @@ +#include "License.h" + +namespace zilliz { +namespace vecwise { +namespace server { + +ServerError +LicenseSave(const std::string& path,const int& deviceCount,const std::vector& shas) +{ + std::ofstream file(path); + + boost::archive::binary_oarchive oa(file); + oa << deviceCount; + for(int i=0;i& shas) +{ + std::ifstream file(path); + + boost::archive::binary_iarchive ia(file); + ia >> deviceCount; + std::string sha; + for(int i=0;i> sha; + shas.push_back(sha); + } + file.close(); + return SERVER_SUCCESS; +} + +ServerError +Licensefileread(const std::string& path) +{ + std::ifstream fileread; + fileread.open(path,std::ios::in); + if(!fileread) + { + printf("Can't open file\n"); + return SERVER_UNEXPECTED_ERROR; + } + fileread.close(); + return SERVER_SUCCESS; +} + +ServerError +Licensefileread (const std::string& path,std::ifstream& fileread){ + fileread.open(path,std::ios::in); + if(!fileread) + { + printf("Can't open file\n"); + return SERVER_UNEXPECTED_ERROR; + } + return SERVER_SUCCESS; +} + +ServerError +Licensefilewrite (const std::string& path,std::ofstream& filewrite) { + filewrite.open(path,std::ios::out); + if(!filewrite) + { + printf("Can't write file\n"); + return SERVER_UNEXPECTED_ERROR; + } + return SERVER_SUCCESS; +} + +ServerError +LicenseGetCount(int &deviceCount) +{ + nvmlReturn_t result = nvmlInit(); + if (NVML_SUCCESS != result) + { + printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); + return SERVER_UNEXPECTED_ERROR; + } + cudaError_t error_id = cudaGetDeviceCount(&deviceCount); + if (error_id != cudaSuccess) + { + printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id)); + printf("Result = FAIL\n"); + return SERVER_UNEXPECTED_ERROR; + } + return SERVER_SUCCESS; +} + +ServerError +LicenseGetuuid(int& deviceCount, std::vector& uuids) +{ + nvmlReturn_t result = nvmlInit(); + if (NVML_SUCCESS != result) + { + printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); + return SERVER_UNEXPECTED_ERROR; + } + cudaError_t error_id = cudaGetDeviceCount(&deviceCount); + if (error_id != cudaSuccess) + { + printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id)); + printf("Result = FAIL\n"); + return SERVER_UNEXPECTED_ERROR; + } + + if (deviceCount == 0) + { + printf("There are no available device(s) that support CUDA\n"); + return SERVER_UNEXPECTED_ERROR; + } + + for (int dev = 0; dev < deviceCount; ++dev) + { + nvmlDevice_t device; + result = nvmlDeviceGetHandleByIndex(dev, &device); + printf("device id: %d\n", dev); + if (NVML_SUCCESS != result) + { + printf("Failed to get handle for device %i: %s\n", dev, nvmlErrorString(result)); + return SERVER_UNEXPECTED_ERROR; + } + + char* uuid = (char*)malloc(80); + unsigned int length = 80; + nvmlReturn_t err = nvmlDeviceGetUUID(device, uuid, length); + if(err != NVML_SUCCESS) { + printf("nvmlDeviceGetUUID error: %d\n", err); + return SERVER_UNEXPECTED_ERROR; + } + + printf("\n device: %d, uuid = %s \n", dev, uuid); + uuids.push_back(std::string(uuid)); + free(uuid); + uuid = NULL; + } + return SERVER_SUCCESS; +} + +ServerError +LicenseGetuuidmd5(const int& deviceCount,std::vector& uuids,std::vector& md5s) +{ + MD5_CTX ctx; + unsigned char outmd[16]; + char temp[2]; + std::string md5=""; + for(int dev=0;dev& uuids,std::vector& shas) +{ + SHA256_CTX ctx; + unsigned char outmd[32]; + char temp[2]; + std::string sha=""; + for(int dev=0;dev uuids; + LicenseGetuuid(deviceCount,uuids); + std::vector shas; + LicenseGetuuidsha(deviceCount,uuids,shas); + + + int deviceCountcheck; + std::vector shascheck; + LicenseLoad(path,deviceCountcheck,shascheck); + + if(deviceCount!=deviceCountcheck) + { + printf("deviceCount is wrong\n"); + return SERVER_UNEXPECTED_ERROR; + } + for(int i=0;i> filemd5; + ia >> last_time; + file.close(); + return SERVER_SUCCESS; +} + +ServerError +LicenseIntegritycheck(const std::string& path,const std::string& path2) + { + std::string filemd5; + LicenseGetfilemd5(path,filemd5); + time_t last_time; + LicenseGetfiletime(path,last_time); + + time_t last_timecheck; + std::string filemd5check; + LicensefileLoad(path2,filemd5check,last_timecheck); + + if(filemd5!=filemd5check) + { + printf("This file has been modified\n"); + return SERVER_UNEXPECTED_ERROR; + } + if(last_time!=last_timecheck) + { + printf("last_time is wrong\n"); + return SERVER_UNEXPECTED_ERROR; + } + + return SERVER_SUCCESS; + } + +ServerError +LicenseValidate(const std::string& path) { + + + return SERVER_SUCCESS; +} + + + +} +} +} \ No newline at end of file diff --git a/cpp/src/license/License.h b/cpp/src/license/License.h new file mode 100644 index 0000000000..6f67edb6f1 --- /dev/null +++ b/cpp/src/license/License.h @@ -0,0 +1,81 @@ +#pragma once + +#include "utils/Error.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "boost/archive/binary_oarchive.hpp" +#include "boost/archive/binary_iarchive.hpp" + +#include +#include +#include +#include + + +namespace zilliz { +namespace vecwise { +namespace server { + + +ServerError +LicenseSave(const std::string& path,const int& deviceCount,const std::vector& shas); + +ServerError +LicenseLoad(const std::string& path,int& deviceCount,std::vector& shas); + +ServerError +Licensefileread (const std::string& path,std::ifstream& fileread); + +ServerError +Licensefilewrite (const std::string& path,std::ofstream& filewrite); + +ServerError +LicenseGetuuid(int& deviceCount, std::vector& uuids); + +ServerError +LicenseGetuuidmd5(const int& deviceCount,std::vector& uuids,std::vector& md5s); + +ServerError +LicenseGetfilemd5(const std::string& path,std::string& filemd5); + +ServerError +LicenseGetuuidsha(const int& deviceCount,std::vector& uuids,std::vector& shas); + +ServerError +LicenseIntegritycheck(const std::string& path,const std::string& path2); + +ServerError +LicenseGetfiletime(const std::string& path,time_t& last_time); + +ServerError +LicenseLegalitycheck(const std::string& path); + +ServerError +LicensefileSave(const std::string& path,const std::string& path2); + +ServerError +LicensefileLoad(const std::string& path2,std::string& filemd5,time_t& last_time); + + +ServerError +LicenseGetfilesize(const std::string& path,off_t& file_size); + +ServerError +LicenseValidate(const std::string& path); + +ServerError +Licensefileread(const std::string& path); + +ServerError +LicenseGetCount(int &deviceCount); +} +} +} + + diff --git a/cpp/src/license/LicenseCreateTime.cpp b/cpp/src/license/LicenseCreateTime.cpp new file mode 100644 index 0000000000..7683fae24c --- /dev/null +++ b/cpp/src/license/LicenseCreateTime.cpp @@ -0,0 +1,51 @@ +// +// Created by zilliz on 19-5-11. +// + +#include +#include +#include + +#include "utils/Error.h" +#include "license/License.h" +#include "license/LicensePublic.h" + +using namespace zilliz::vecwise; + +//TEST(LicenseTest, LICENSE_TEST) { +// +// std::string path1 = "/tmp/vecwise_engine.sha"; +// std::string path2 = "/tmp/vecwise_engine.license"; +// std::cout << "This is create licenseTime " << std::endl; +// +// server::ServerError err; +// int deviceCount=0; +// +// std::vector shas; +// err = server::LicenseLoad(path1,deviceCount,shas); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// std::map uuidEncryption; +// std::cout<< "deviceCount : " << deviceCount << std::endl; +// for(int i=0;i> RemainingTime ; +// +// err = server::LiSave(path2,deviceCount,uuidEncryption,RemainingTime); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// int64_t RemainingTimecheck; +// std::map uuidEncryptioncheck; +// int deviceCountcheck; +// err = server::LiLoad(path2,deviceCountcheck,uuidEncryptioncheck,RemainingTimecheck); +// +// printf("\n deviceCountcheck = %d\n",deviceCountcheck); +// std::cout<< "RemainingTimecheck: " << RemainingTimecheck<< std::endl; +// std::cout<< "success" << std::endl; +// +//} \ No newline at end of file diff --git a/cpp/src/license/LicenseCreateuuidshafile.cpp b/cpp/src/license/LicenseCreateuuidshafile.cpp new file mode 100644 index 0000000000..93179ba75a --- /dev/null +++ b/cpp/src/license/LicenseCreateuuidshafile.cpp @@ -0,0 +1,31 @@ +// +// Created by zilliz on 19-5-11. +// +#include +#include + +#include "utils/Error.h" +#include "license/License.h" +using namespace zilliz::vecwise; + +// +//TEST(LicenseTest, LICENSE_TEST) { +// +// std::string path1 = "/tmp/vecwise_engine.sha"; +// std::cout << "This is create uuidshafile " << std::endl; +// +// server::ServerError err; +// int deviceCount=0; +// std::vector uuids; +// +// err = server::LicenseGetuuid(deviceCount,uuids); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// std::vector shas; +// err = server::LicenseGetuuidsha(deviceCount,uuids,shas); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// err = server::LicenseSave(path1,deviceCount,shas); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +//} \ No newline at end of file diff --git a/cpp/src/license/LicenseGenerator.cpp b/cpp/src/license/LicenseGenerator.cpp index f82e948c30..278c132962 100644 --- a/cpp/src/license/LicenseGenerator.cpp +++ b/cpp/src/license/LicenseGenerator.cpp @@ -1,6 +1,6 @@ #include - int main() { std::cout << "This is license generator" << std::endl; + return 0; } \ No newline at end of file diff --git a/cpp/src/license/LicensePublic.cpp b/cpp/src/license/LicensePublic.cpp new file mode 100644 index 0000000000..6534674476 --- /dev/null +++ b/cpp/src/license/LicensePublic.cpp @@ -0,0 +1,284 @@ +// +// Created by zilliz on 19-5-10. +// + +#include "LicensePublic.h" +#include "license/License.h" +//#include "BoostArchive.h" +#define RTime 100 + +using std::string; +using std::map; +namespace zilliz { +namespace vecwise { +namespace server { + +// GET /tmp/vecwise_engine.license +class Licensedata1 +{ +public: + Licensedata1() + :_deviceCount(0) + ,_RemainingTime(RTime) + {} + + Licensedata1(const int& deviceCount,const map& uuidEncryption) + :_deviceCount(deviceCount) + ,_uuidEncryption(uuidEncryption) + ,_RemainingTime(RTime) + {} + + Licensedata1(const int& deviceCount,const map& uuidEncryption,const int64_t& RemainingTime) + :_deviceCount(deviceCount) + ,_uuidEncryption(uuidEncryption) + ,_RemainingTime(RemainingTime) + {} + + int GetdeviceCount() + { + return _deviceCount; + } + map GetuuidEncryption() + { + return _uuidEncryption; + } + int64_t GetRemainingTime() + { + return _RemainingTime; + } + +private: + friend class boost::serialization::access; + template + void serialize(Archive &ar, const unsigned int version) + { + ar & _deviceCount; + ar & _uuidEncryption; + ar & _RemainingTime; + } + +public: + int _deviceCount; + map _uuidEncryption; + int64_t _RemainingTime; +}; + + +class STLlicensedata +{ +private: + friend class boost::serialization::access; + template + void serialize(Archive& ar, const unsigned int version) + { + ar & m_licensedata; + } + +public: + Licensedata1* m_licensedata; +}; + + +ServerError +LiSave(const string& path,const int& deviceCount,const map& uuidEncryption) +{ + std::ofstream file(path); + boost::archive::binary_oarchive oa(file); + oa.register_type(); + STLlicensedata stllicensedata; + + Licensedata1 *p = new Licensedata1(deviceCount,uuidEncryption); + stllicensedata.m_licensedata = p; + oa << stllicensedata; + + delete p; + file.close(); + return SERVER_SUCCESS; +} + +ServerError +LiSave(const string& path,const int& deviceCount,const map& uuidEncryption, const int64_t& RemainingTime) +{ + std::ofstream file(path); + boost::archive::binary_oarchive oa(file); + oa.register_type(); + STLlicensedata stllicensedata; + + Licensedata1 *p = new Licensedata1(deviceCount,uuidEncryption,RemainingTime); + stllicensedata.m_licensedata = p; + oa << stllicensedata; + + delete p; + file.close(); + return SERVER_SUCCESS; +} + +ServerError +LiLoad(const string& path,int& deviceCount,map& uuidEncryption,int64_t& RemainingTime) +{ + std::ifstream file(path); + boost::archive::binary_iarchive ia(file); + ia.register_type(); + STLlicensedata stllicensedata; + ia >> stllicensedata; + deviceCount = stllicensedata.m_licensedata->GetdeviceCount(); + uuidEncryption = stllicensedata.m_licensedata->GetuuidEncryption(); + RemainingTime = stllicensedata.m_licensedata->GetRemainingTime(); + file.close(); + return SERVER_SUCCESS; +} + + +// GET /tmp/vecwise_engine2.license + +class Licensefiledata +{ +public: + Licensefiledata() + :_update_time(0) + ,_file_size(0) + ,_filemd5("") + {} + + Licensefiledata(const time_t& update_time,const off_t& file_size,const string& filemd5) + :_update_time(update_time) + ,_file_size(file_size) + ,_filemd5(filemd5) + {} + + time_t Getupdate_time() + { + return _update_time; + } + off_t Getfile_size() + { + return _file_size; + } + string Getfilemd5() + { + return _filemd5; + } + +private: + friend class boost::serialization::access; + template + void serialize(Archive &ar, const unsigned int version) + { + ar & _update_time; + ar & _file_size; + ar & _filemd5; + } + +public: + time_t _update_time; + off_t _file_size; + string _filemd5; +}; + + +class STLlicensefiledata +{ +private: + friend class boost::serialization::access; + template + void serialize(Archive& ar, const unsigned int version) + { + ar & m_licensefiledata; + } + +public: + Licensefiledata* m_licensefiledata; +}; + + +ServerError +LifileSave(const string& path,const time_t& update_time,const off_t& file_size,const string& filemd5) +{ + std::ofstream file(path); + boost::archive::binary_oarchive oa(file); + oa.register_type(); + STLlicensefiledata stllicensefiledata; + + Licensefiledata *p = new Licensefiledata(update_time,file_size,filemd5); + stllicensefiledata.m_licensefiledata = p; + oa << stllicensefiledata; + + delete p; + file.close(); + return SERVER_SUCCESS; +} + +ServerError +LifileLoad(const string& path,time_t& update_time,off_t& file_size,string& filemd5) + { + std::ifstream file(path); + boost::archive::binary_iarchive ia(file); + ia.register_type(); + STLlicensefiledata stllicensefiledata; + ia >> stllicensefiledata; + update_time = stllicensefiledata.m_licensefiledata->Getupdate_time(); + file_size = stllicensefiledata.m_licensefiledata->Getfile_size(); + filemd5 = stllicensefiledata.m_licensefiledata->Getfilemd5(); + file.close(); + return SERVER_SUCCESS; + } + + +void Alterfile(const string &path1, const string &path2 ,const boost::system::error_code &ec, boost::asio::deadline_timer* pt) +{ + int deviceCount; + map uuidEncryption; + int64_t RemainingTime; + LiLoad(path1,deviceCount,uuidEncryption,RemainingTime); + + std::cout<< "RemainingTime: " << RemainingTime < uuidEncryptioncheck; + int64_t RemainingTimecheck; + LiLoad(path1,deviceCountcheck,uuidEncryptioncheck,RemainingTimecheck); + + std::cout<< "RemainingTimecheck: " << RemainingTimecheck <expires_at(pt->expires_at() + boost::posix_time::hours(1)) ; + pt->async_wait(boost::bind(Alterfile,path1,path2, boost::asio::placeholders::error, pt)); + +} +void Runtime(const string &path1, const string &path2 ) +{ + boost::asio::io_service io; + boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); + t.async_wait(boost::bind(Alterfile,path1,path2, boost::asio::placeholders::error, &t)); + io.run(); + return;; +} + +} +} +} \ No newline at end of file diff --git a/cpp/src/license/LicensePublic.h b/cpp/src/license/LicensePublic.h new file mode 100644 index 0000000000..ea2e439d99 --- /dev/null +++ b/cpp/src/license/LicensePublic.h @@ -0,0 +1,68 @@ +// +// Created by zilliz on 19-5-10. +// + +#pragma once + +#include "utils/Error.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +namespace zilliz { +namespace vecwise { +namespace server { + +class BoostArchive; +class Licensedata1; + + +class Licensefiledata; +class STLlicensefiledata; + + +ServerError +LiSave(const std::string& path,const int& deviceCount,const std::map& uuidEncryption); + +ServerError +LiLoad(const std::string& path,int& deviceCount,std::map& uuidEncryption,int64_t& RemainingTime); + +ServerError +LifileSave(const std::string& path,const time_t& update_time,const off_t& file_size,const std::string& filemd5); + +ServerError +LifileLoad(const std::string& path,time_t& update_time,off_t& file_size,std::string& filemd5); + +ServerError +LiSave(const std::string& path,const int& deviceCount,const std::map& uuidEncryption, const int64_t& RemainingTime); + +void Alterfile(const std::string &path1, const std::string &path2 ,const boost::system::error_code &ec, boost::asio::deadline_timer* pt); +void Runtime(const std::string &path1, const std::string &path2 ); + +// void Print(const boost::system::error_code &ec,boost::asio::deadline_timer* pt, int * pcount ); +// void Run(); + +} +} +} \ No newline at end of file diff --git a/cpp/src/license/LicenseRun.cpp b/cpp/src/license/LicenseRun.cpp new file mode 100644 index 0000000000..660fd5fa0c --- /dev/null +++ b/cpp/src/license/LicenseRun.cpp @@ -0,0 +1,13 @@ +// +// Created by zilliz on 19-5-11. +// +#include +#include +#include + +#include "utils/Error.h" +#include "license/License.h" +#include "license/LicensePublic.h" +using namespace zilliz::vecwise; + + diff --git a/cpp/src/utils/Error.h b/cpp/src/utils/Error.h index 83da0cea9f..88e697ec2e 100644 --- a/cpp/src/utils/Error.h +++ b/cpp/src/utils/Error.h @@ -31,6 +31,7 @@ constexpr ServerError SERVER_INVALID_ARGUMENT = ToGlobalServerErrorCode(0x004); constexpr ServerError SERVER_FILE_NOT_FOUND = ToGlobalServerErrorCode(0x005); constexpr ServerError SERVER_NOT_IMPLEMENT = ToGlobalServerErrorCode(0x006); constexpr ServerError SERVER_BLOCKING_QUEUE_EMPTY = ToGlobalServerErrorCode(0x007); +constexpr ServerError SERVER_LICENSE_VALIDATION_FAIL = ToGlobalServerErrorCode(0x008); class ServerException : public std::exception { public: diff --git a/cpp/unittest/license/CMakeLists.txt b/cpp/unittest/license/CMakeLists.txt index dd742d66fe..92eef5d556 100644 --- a/cpp/unittest/license/CMakeLists.txt +++ b/cpp/unittest/license/CMakeLists.txt @@ -11,34 +11,32 @@ aux_source_directory(../../src/cache cache_srcs) aux_source_directory(../../src/wrapper wrapper_src) include_directories(/usr/local/cuda/include) -link_directories("/usr/local/cuda/lib64") +link_directories(/usr/local/cuda) +link_directories(/usr/local/cuda/lib64) +link_directories(/usr/lib/x86_64-linux-gnu) +link_directories(/usr/lib/nvidia-415) set(require_files - ../../src/server/ServerConfig.cpp - ../../src/utils/CommonUtil.cpp - ../../src/utils/TimeRecorder.cpp ../../src/license/License.cpp - ) + ../../src/license/LicensePublic.cpp + ../../src/license/LicenseCreateuuidshafile.cpp + ) set(db_test_src - ${unittest_srcs} - ${config_files} - ${cache_srcs} - ${db_srcs} - ${wrapper_src} ${require_files} license_tests.cpp) cuda_add_executable(license_test ${db_test_src}) set(db_libs - faiss + nvidia-ml cudart cublas - sqlite3 boost_system boost_filesystem lz4 + crypto + boost_serialization ) target_link_libraries(license_test ${unittest_libs} ${db_libs}) diff --git a/cpp/unittest/license/license_tests.cpp b/cpp/unittest/license/license_tests.cpp index 798160f671..4ee6898dd9 100644 --- a/cpp/unittest/license/license_tests.cpp +++ b/cpp/unittest/license/license_tests.cpp @@ -5,12 +5,184 @@ //////////////////////////////////////////////////////////////////////////////// #include #include "license/License.h" +#include "license/LicensePublic.h" #include "utils/Error.h" + using namespace zilliz::vecwise; TEST(LicenseTest, LICENSE_TEST) { + std::string path1 = "/tmp/vecwise_engine.license"; - server::ServerError err = server::LicenseValidate(path1); + std::string path2 = "/tmp/vecwise_engine2.license"; + std::cout << "This is run " << std::endl; + + server::ServerError err; + + err = server::Licensefileread(path1); + if(err!=server::SERVER_SUCCESS) + { + exit(1); + } + + int deviceCount=0; + std::vector uuids; + + err = server::LicenseGetuuid(deviceCount,uuids); ASSERT_EQ(err, server::SERVER_SUCCESS); + + std::vector shas; + err = server::LicenseGetuuidsha(deviceCount,uuids,shas); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + err = server::LicenseSave(path1,deviceCount,shas); + ASSERT_EQ(err, server::SERVER_SUCCESS); + } + +//TEST(LicenseTest, LICENSE_TEST) { +// std::string path1 = "/tmp/vecwise_engine.license"; +// std::string path2 = "/tmp/vecwise_engine2.license"; +// +// server::ServerError err; +// server::Runtime(path1,path2); + + +// time_t update_time; +// off_t file_size; +// std::string filemd5; +// +// time_t update_timecheck; +// off_t file_sizecheck; +// std::string filemd5check; +// +// err = server::LicenseGetfiletime(path1,update_time); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// err = server::LicenseGetfilesize(path1,file_size); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// err = server::LicenseGetfilemd5(path1,filemd5); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// err = server::LifileSave(path2,update_time,file_size,filemd5); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// err = server::LifileLoad(path2,update_timecheck,file_sizecheck,filemd5check); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// +// std::cout<< "update_time : " << update_time < uuids; +// +// deviceCount = 2; +// uuids.push_back("121"); +// uuids.push_back("324"); +// err = server::LicenseGetuuid(deviceCount,uuids); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// printf("\n deviceCount = %d\n",deviceCount); +// +// std::vector uuidmd5s; +// err = server::LicenseGetuuidmd5(deviceCount,uuids,uuidmd5s); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// printf(" md5s \n"); +// for(int i=0;i uuidshas; +// err = server::LicenseGetuuidsha(deviceCount,uuids,uuidshas); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// std::map uuidEncryption; +// for(int i=0;i uuidEncryptioncheck; +// err = server::LiLoad(path1,deviceCountcheck,uuidEncryptioncheck,RemainingTime); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// printf("----- checking ----\n"); +// printf("\n deviceCount = %d\n",deviceCountcheck); +// for(auto it : uuidEncryptioncheck) +// { +// std::cout<< "uuidshas : " << it.second << std::endl; +// } +// std::cout<< "RemainingTime :" << RemainingTime << std::endl; +// +// printf(" shas \n"); +// for(int i=0;i uuidshascheck; +// +// err = server::LicenseLoad(path1,deviceCountcheck,uuidshascheck); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// std::cout<<" deviceCountcheck :" << deviceCountcheck << std::endl; +// std::cout<<" uuidshascheck :" << uuidshascheck[0] << std::endl; +// +// +// std::string filemd5; +// err = server::LicenseGetfilemd5(path1,filemd5); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// std::cout<<" filemd5 :" << filemd5 << std::endl; +// +// err= server::LicensefileSave(path1,path2); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// time_t last_timecheck; +// std::string filemd5check; +// err= server::LicensefileLoad(path2,filemd5check,last_timecheck); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// std::cout<<" filemd5check :" << filemd5check << std::endl; +// +// time_t last_time; +// err = server::LicenseGetfiletime(path1,last_time); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// std::cout<<" last_time : " << last_time << std::endl; +// std::cout<<" last_timecheck :" << last_timecheck << std::endl; +// +// err = server::LicenseIntegritycheck(path1,path2); +// ASSERT_EQ(err, server::SERVER_SUCCESS); + +//} From 5eb854b1b6217887250fb0c406fea15a6ead52b9 Mon Sep 17 00:00:00 2001 From: "yangwei.yao" Date: Sat, 11 May 2019 15:13:36 +0800 Subject: [PATCH 2/9] license_run Former-commit-id: dbfbeafc516f45c89cf2b693f40ac19abf903df3 --- cpp/src/license/License.cpp | 2 +- cpp/src/license/LicenseCreateTime.cpp | 34 +++++++++--- cpp/src/license/LicensePublic.cpp | 75 +++++++++++++++++++++++++- cpp/src/license/LicensePublic.h | 32 ++++++++--- cpp/src/license/LicenseRun.cpp | 29 +++++++++- cpp/unittest/license/license_tests.cpp | 32 ++++++----- 6 files changed, 172 insertions(+), 32 deletions(-) diff --git a/cpp/src/license/License.cpp b/cpp/src/license/License.cpp index 193a322e50..eb6c563e4f 100644 --- a/cpp/src/license/License.cpp +++ b/cpp/src/license/License.cpp @@ -43,7 +43,7 @@ Licensefileread(const std::string& path) fileread.open(path,std::ios::in); if(!fileread) { - printf("Can't open file\n"); + printf("NO License\n"); return SERVER_UNEXPECTED_ERROR; } fileread.close(); diff --git a/cpp/src/license/LicenseCreateTime.cpp b/cpp/src/license/LicenseCreateTime.cpp index 7683fae24c..0c2f951511 100644 --- a/cpp/src/license/LicenseCreateTime.cpp +++ b/cpp/src/license/LicenseCreateTime.cpp @@ -12,10 +12,14 @@ using namespace zilliz::vecwise; + + //TEST(LicenseTest, LICENSE_TEST) { // // std::string path1 = "/tmp/vecwise_engine.sha"; // std::string path2 = "/tmp/vecwise_engine.license"; +// std::string path3 = "/tmp/vecwise_engine2.license"; +// // std::cout << "This is create licenseTime " << std::endl; // // server::ServerError err; @@ -39,13 +43,29 @@ using namespace zilliz::vecwise; // err = server::LiSave(path2,deviceCount,uuidEncryption,RemainingTime); // ASSERT_EQ(err, server::SERVER_SUCCESS); // -// int64_t RemainingTimecheck; -// std::map uuidEncryptioncheck; -// int deviceCountcheck; -// err = server::LiLoad(path2,deviceCountcheck,uuidEncryptioncheck,RemainingTimecheck); +//// int64_t RemainingTimecheck; +//// std::map uuidEncryptioncheck; +//// int deviceCountcheck; +//// err = server::LiLoad(path2,deviceCountcheck,uuidEncryptioncheck,RemainingTimecheck); +//// +//// printf("\n deviceCountcheck = %d\n",deviceCountcheck); +//// std::cout<< "RemainingTimecheck: " << RemainingTimecheck<< std::endl; +// +// time_t update_time; +// off_t file_size; +// std::string filemd5; +// +// server::LicenseGetfiletime(path2,update_time); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// server::LicenseGetfilesize(path2,file_size); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// server::LicenseGetfilemd5(path2,filemd5); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// +// err = server::LifileSave(path3,update_time,file_size,filemd5); +// ASSERT_EQ(err, server::SERVER_SUCCESS); // -// printf("\n deviceCountcheck = %d\n",deviceCountcheck); -// std::cout<< "RemainingTimecheck: " << RemainingTimecheck<< std::endl; // std::cout<< "success" << std::endl; // -//} \ No newline at end of file +//} diff --git a/cpp/src/license/LicensePublic.cpp b/cpp/src/license/LicensePublic.cpp index 6534674476..632b4cb73d 100644 --- a/cpp/src/license/LicensePublic.cpp +++ b/cpp/src/license/LicensePublic.cpp @@ -223,6 +223,78 @@ LifileLoad(const string& path,time_t& update_time,off_t& file_size,string& filem return SERVER_SUCCESS; } +ServerError +LicenseLegality_check(const std::string& path) +{ + int deviceCount; + std::vector uuids; + LicenseGetuuid(deviceCount,uuids); + + std::vector shas; + LicenseGetuuidsha(deviceCount,uuids,shas); + + + int deviceCountcheck; + std::map uuidEncryption; + int64_t RemainingTime; + LiLoad(path,deviceCountcheck,uuidEncryption,RemainingTime); + + if(deviceCount!=deviceCountcheck) + { + printf("deviceCount is wrong\n"); + return SERVER_UNEXPECTED_ERROR; + } + for(int i=0;i& uuidEncryption, const int64_t& RemainingTime); -void Alterfile(const std::string &path1, const std::string &path2 ,const boost::system::error_code &ec, boost::asio::deadline_timer* pt); -void Runtime(const std::string &path1, const std::string &path2 ); +ServerError +LicenseIntegrity_check(const std::string& path,const std::string& path2); + +ServerError +LicenseLegality_check(const std::string& path); + +void +Alterfile(const std::string &path1, const std::string &path2 ,const boost::system::error_code &ec, boost::asio::deadline_timer* pt); + +void +Runtime(const std::string &path1, const std::string &path2 ); + + + -// void Print(const boost::system::error_code &ec,boost::asio::deadline_timer* pt, int * pcount ); -// void Run(); } } diff --git a/cpp/src/license/LicenseRun.cpp b/cpp/src/license/LicenseRun.cpp index 660fd5fa0c..a059d2f7e2 100644 --- a/cpp/src/license/LicenseRun.cpp +++ b/cpp/src/license/LicenseRun.cpp @@ -10,4 +10,31 @@ #include "license/LicensePublic.h" using namespace zilliz::vecwise; - +//TEST(LicenseTest, LICENSE_TEST) { +// +// std::string path1 = "/tmp/vecwise_engine.license"; +// std::string path2 = "/tmp/vecwise_engine2.license"; +// std::cout << "This is run " << std::endl; +// +// server::ServerError err; +// +// err = server::Licensefileread(path1); +// if(err!=server::SERVER_SUCCESS) +// { +// exit(1); +// } +// err = server::LicenseIntegrity_check(path1,path2); +// if(err!=server::SERVER_SUCCESS) +// { +// std::cout << "Integrity_check is wrong " << std::endl; +// exit(1); +// } +// err = server::LicenseLegality_check(path1); +// if(err!=server::SERVER_SUCCESS) +// { +// std::cout << "Legality_check is wrong " << std::endl; +// exit(1); +// } +// std::cout << " runing " << std::endl; +// server::Runtime(path1,path2); +//} \ No newline at end of file diff --git a/cpp/unittest/license/license_tests.cpp b/cpp/unittest/license/license_tests.cpp index 4ee6898dd9..3da698c1d1 100644 --- a/cpp/unittest/license/license_tests.cpp +++ b/cpp/unittest/license/license_tests.cpp @@ -24,22 +24,26 @@ TEST(LicenseTest, LICENSE_TEST) { { exit(1); } - - int deviceCount=0; - std::vector uuids; - - err = server::LicenseGetuuid(deviceCount,uuids); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - std::vector shas; - err = server::LicenseGetuuidsha(deviceCount,uuids,shas); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - err = server::LicenseSave(path1,deviceCount,shas); - ASSERT_EQ(err, server::SERVER_SUCCESS); - + err = server::LicenseIntegrity_check(path1,path2); + if(err!=server::SERVER_SUCCESS) + { + std::cout << "Integrity_check is wrong " << std::endl; + exit(1); + } + err = server::LicenseLegality_check(path1); + if(err!=server::SERVER_SUCCESS) + { + std::cout << "Legality_check is wrong " << std::endl; + exit(1); + } + std::cout << " runing " << std::endl; + server::Runtime(path1,path2); } + + + + //TEST(LicenseTest, LICENSE_TEST) { // std::string path1 = "/tmp/vecwise_engine.license"; // std::string path2 = "/tmp/vecwise_engine2.license"; From 06dd990b189240df64829fb5c720a68af4a755c1 Mon Sep 17 00:00:00 2001 From: jinhai Date: Sat, 11 May 2019 20:58:11 +0800 Subject: [PATCH 3/9] Refactor code Former-commit-id: 31caf05ec1b75d9cd1de476104cdc0abf62acedd --- cpp/src/CMakeLists.txt | 17 +- cpp/src/license/GPUInfoFile.h | 69 ++++ cpp/src/license/GetSysInfo.cpp | 6 + cpp/src/license/LicenseFile.h | 74 ++++ cpp/src/license/LicenseLibrary.cpp | 244 +++++++++++++ cpp/src/license/LicenseLibrary.h | 71 ++++ cpp/src/license/SecretFile.h | 72 ++++ cpp/unittest/license/CMakeLists.txt | 14 +- .../license/license_library_tests.cpp | 134 +++++++ cpp/unittest/license/license_tests.cpp | 343 +++++++++--------- 10 files changed, 862 insertions(+), 182 deletions(-) create mode 100644 cpp/src/license/GPUInfoFile.h create mode 100644 cpp/src/license/GetSysInfo.cpp create mode 100644 cpp/src/license/LicenseFile.h create mode 100644 cpp/src/license/LicenseLibrary.cpp create mode 100644 cpp/src/license/LicenseLibrary.h create mode 100644 cpp/src/license/SecretFile.h create mode 100644 cpp/unittest/license/license_library_tests.cpp diff --git a/cpp/src/CMakeLists.txt b/cpp/src/CMakeLists.txt index 8b02188979..8fab8929cb 100644 --- a/cpp/src/CMakeLists.txt +++ b/cpp/src/CMakeLists.txt @@ -12,7 +12,8 @@ aux_source_directory(db db_files) aux_source_directory(wrapper wrapper_files) set(license_check_files - ${CMAKE_CURRENT_SOURCE_DIR}/license/License.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/license/LicenseLibrary.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/license/LicenseLibrary.h ) set(service_files @@ -28,6 +29,12 @@ set(vecwise_engine_src ${wrapper_files} ) +set(license_generator_src + ${CMAKE_CURRENT_SOURCE_DIR}/license/LicenseGenerator.cpp) + +set(get_sys_info_src + ${CMAKE_CURRENT_SOURCE_DIR}/license/GetSysInfo.cpp) + include_directories(/usr/include) include_directories(/usr/local/cuda/include) @@ -65,7 +72,6 @@ add_executable(vecwise_server ${server_files} ${utils_files} ${service_files} - ${license_check_files} ${VECWISE_THIRD_PARTY_BUILD}/include/easylogging++.cc ) @@ -87,9 +93,10 @@ set(server_libs target_link_libraries(vecwise_server ${server_libs}) -set(license_generator_src - ${CMAKE_CURRENT_SOURCE_DIR}/license/LicenseGenerator.cpp) - add_executable(license_generator ${license_generator_src}) +<<<<<<< b84d32553f910adf71e4ee069889e1d9fdd8a09f install(TARGETS vecwise_server DESTINATION bin) +======= +add_executable(get_sys_info ${get_sys_info_src}) +>>>>>>> Refactor code diff --git a/cpp/src/license/GPUInfoFile.h b/cpp/src/license/GPUInfoFile.h new file mode 100644 index 0000000000..d22765bd71 --- /dev/null +++ b/cpp/src/license/GPUInfoFile.h @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ +#pragma once + +#include +#include +#include + + +class GPUInfoFile { + public: + GPUInfoFile() = default; + + GPUInfoFile(const int &device_count, const std::map &uuid_encryption_map) + : device_count_(device_count), uuid_encryption_map_(uuid_encryption_map) {} + + int get_device_count() { + return device_count_; + } + std::map &get_uuid_encryption_map() { + return uuid_encryption_map_; + } + + + public: + friend class boost::serialization::access; + + template + void serialize(Archive &ar, const unsigned int version) { + ar & device_count_; + ar & uuid_encryption_map_; + } + + public: + int device_count_ = 0; + std::map uuid_encryption_map_; +}; + +class SerializedGPUInfoFile { + public: + ~SerializedGPUInfoFile() { + if (gpu_info_file_ != nullptr) { + delete (gpu_info_file_); + gpu_info_file_ = nullptr; + } + } + + void + set_gpu_info_file(GPUInfoFile *gpu_info_file) { + gpu_info_file_ = gpu_info_file; + } + + GPUInfoFile *get_gpu_info_file() { + return gpu_info_file_; + } + private: + friend class boost::serialization::access; + + template + void serialize(Archive &ar, const unsigned int version) { + ar & gpu_info_file_; + } + + private: + GPUInfoFile *gpu_info_file_ = nullptr; +}; diff --git a/cpp/src/license/GetSysInfo.cpp b/cpp/src/license/GetSysInfo.cpp new file mode 100644 index 0000000000..9c35b89864 --- /dev/null +++ b/cpp/src/license/GetSysInfo.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char* argv[]) { + std::cout << "GetSysInfo" << std::endl; + return 0; +} \ No newline at end of file diff --git a/cpp/src/license/LicenseFile.h b/cpp/src/license/LicenseFile.h new file mode 100644 index 0000000000..322cea8b85 --- /dev/null +++ b/cpp/src/license/LicenseFile.h @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ +#pragma once + + +#include +#include +#include + +class LicenseFile { + public: + LicenseFile() = default; + + LicenseFile(const int &device_count, const std::map &uuid_encryption_map, const int64_t &remaining_hour) + : device_count_(device_count), uuid_encryption_map_(uuid_encryption_map), remaining_hour_(remaining_hour) {} + + int get_device_count() { + return device_count_; + } + std::map &get_uuid_encryption_map() { + return uuid_encryption_map_; + } + int64_t get_remaining_hour() { + return remaining_hour_; + } + + public: + friend class boost::serialization::access; + + template + void serialize(Archive &ar, const unsigned int version) { + ar & device_count_; + ar & uuid_encryption_map_; + ar & remaining_hour_; + } + + public: + int device_count_ = 0; + std::map uuid_encryption_map_; + int64_t remaining_hour_ = 0; +}; + +class SerializedLicenseFile { + public: + ~SerializedLicenseFile() { + if(license_file_ != nullptr) { + delete(license_file_); + license_file_ = nullptr; + } + } + + void + set_license_file(LicenseFile* license_file) { + license_file_ = license_file; + } + + LicenseFile* get_license_file() { + return license_file_; + } + private: + friend class boost::serialization::access; + + template + void serialize(Archive &ar, const unsigned int version) { + ar & license_file_; + } + + private: + LicenseFile *license_file_ = nullptr; +}; + diff --git a/cpp/src/license/LicenseLibrary.cpp b/cpp/src/license/LicenseLibrary.cpp new file mode 100644 index 0000000000..6e757caa4b --- /dev/null +++ b/cpp/src/license/LicenseLibrary.cpp @@ -0,0 +1,244 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved +// Unauthorized copying of this file, via any medium is strictly prohibited. +// Proprietary and confidential. +//////////////////////////////////////////////////////////////////////////////// + +#include "LicenseLibrary.h" +#include "utils/Log.h" +#include +#include +#include +#include + +#include +#include +//#include +//#include +#include + + +namespace zilliz { +namespace vecwise { +namespace server { + +constexpr int LicenseLibrary::sha256_length_; + +ServerError +LicenseLibrary::GetDeviceCount(int &device_count) { + nvmlReturn_t result = nvmlInit(); + if (NVML_SUCCESS != result) { + printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); + return SERVER_UNEXPECTED_ERROR; + } + cudaError_t error_id = cudaGetDeviceCount(&device_count); + if (error_id != cudaSuccess) { + printf("cudaGetDeviceCount returned %d\n-> %s\n", (int) error_id, cudaGetErrorString(error_id)); + printf("Result = FAIL\n"); + return SERVER_UNEXPECTED_ERROR; + } + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::GetUUID(int device_count, std::vector &uuid_array) { + if (device_count == 0) { + printf("There are no available device(s) that support CUDA\n"); + return SERVER_UNEXPECTED_ERROR; + } + + for (int dev = 0; dev < device_count; ++dev) { + nvmlDevice_t device; + nvmlReturn_t result = nvmlDeviceGetHandleByIndex(dev, &device); + if (NVML_SUCCESS != result) { + printf("Failed to get handle for device %i: %s\n", dev, nvmlErrorString(result)); + return SERVER_UNEXPECTED_ERROR; + } + + char uuid[80]; + unsigned int length = 80; + nvmlReturn_t err = nvmlDeviceGetUUID(device, uuid, length); + if (err != NVML_SUCCESS) { + printf("nvmlDeviceGetUUID error: %d\n", err); + return SERVER_UNEXPECTED_ERROR; + } + + uuid_array.emplace_back(uuid); + } + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::GetUUIDMD5(int device_count, + std::vector &uuid_array, + std::vector &md5_array) { + MD5_CTX ctx; + unsigned char outmd[16]; + char temp[2]; + std::string md5; + for (int dev = 0; dev < device_count; ++dev) { + md5.clear(); + memset(outmd, 0, sizeof(outmd)); + MD5_Init(&ctx); + MD5_Update(&ctx, uuid_array[dev].c_str(), uuid_array[dev].size()); + MD5_Final(outmd, &ctx); + for (int i = 0; i < 16; ++i) { + std::snprintf(temp, 2, "%02X", outmd[i]); + md5 += temp; + } + md5_array.push_back(md5); + } + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::GetUUIDSHA256(const int &device_count, + std::vector &uuid_array, + std::vector &sha_array) { + SHA256_CTX ctx; + unsigned char outmd[sha256_length_]; + char temp[2]; + std::string sha; + for (int dev = 0; dev < device_count; ++dev) { + sha.clear(); + memset(outmd, 0, sizeof(outmd)); + SHA256_Init(&ctx); + SHA256_Update(&ctx, uuid_array[dev].c_str(), uuid_array[dev].size()); + SHA256_Final(outmd, &ctx); + for (int i = 0; i < sha256_length_; ++i) { + std::snprintf(temp, 2, "%02X", outmd[i]); + sha += temp; + } + sha_array.push_back(sha); + } + return SERVER_SUCCESS; +} + +// Part 2: Handle License File +ServerError +LicenseLibrary::LicenseFileSerialization(const std::string &path, + int device_count, + const std::map &uuid_encrption_map, + int64_t remaining_hour) { + + std::ofstream file(path); + boost::archive::binary_oarchive oa(file); + oa.register_type(); + + SerializedLicenseFile serialized_license_file; + + serialized_license_file.set_license_file(new LicenseFile(device_count, uuid_encrption_map, remaining_hour)); + oa << serialized_license_file; + + file.close(); + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::LicenseFileDeserialization(const std::string &path, + int &device_count, + std::map &uuid_encrption_map, + int64_t &remaining_hour) { + std::ifstream file(path); + boost::archive::binary_iarchive ia(file); + ia.register_type(); + + SerializedLicenseFile serialized_license_file; + ia >> serialized_license_file; + + device_count = serialized_license_file.get_license_file()->get_device_count(); + uuid_encrption_map = serialized_license_file.get_license_file()->get_uuid_encryption_map(); + remaining_hour = serialized_license_file.get_license_file()->get_remaining_hour(); + + file.close(); + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::SecretFileSerialization(const std::string &path, + const time_t &update_time, + const off_t &file_size, + const std::string &file_md5) { + std::ofstream file(path); + boost::archive::binary_oarchive oa(file); + oa.register_type(); + + SerializedSecretFile serialized_secret_file; + + serialized_secret_file.set_secret_file(new SecretFile(update_time, file_size, file_md5)); + oa << serialized_secret_file; + + file.close(); + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::SecretFileDeserialization(const std::string &path, + time_t &update_time, + off_t &file_size, + std::string &file_md5) { + std::ifstream file(path); + boost::archive::binary_iarchive ia(file); + ia.register_type(); + SerializedSecretFile serialized_secret_file; + + ia >> serialized_secret_file; + update_time = serialized_secret_file.get_secret_file()->get_update_time(); + file_size = serialized_secret_file.get_secret_file()->get_file_size(); + file_md5 = serialized_secret_file.get_secret_file()->get_file_md5(); + file.close(); + return SERVER_SUCCESS; +} + +// Part 3: File attribute: UpdateTime Time/ Size/ MD5 +ServerError +LicenseLibrary::GetFileUpdateTimeAndSize(const std::string& path, time_t& update_time, off_t& file_size) { + struct stat buf; + int err_no = stat(path.c_str(), &buf); + if(err_no != 0) + { + std::cout << strerror(err_no) << std::endl; + return SERVER_UNEXPECTED_ERROR; + } + + update_time =buf.st_mtime; + file_size =buf.st_size; + + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::GetFileMD5(const std::string &path, std::string &filemd5) { + filemd5.clear(); + + std::ifstream file(path.c_str(), std::ifstream::binary); + if (!file) { + return -1; + } + + MD5_CTX md5Context; + MD5_Init(&md5Context); + + char buf[1024 * 16]; + while (file.good()) { + file.read(buf, sizeof(buf)); + MD5_Update(&md5Context, buf, file.gcount()); + } + + unsigned char result[MD5_DIGEST_LENGTH]; + MD5_Final(result, &md5Context); + + char hex[35]; + memset(hex, 0, sizeof(hex)); + for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) { + sprintf(hex + i * 2, "%02X", result[i]); + } + hex[32] = '\0'; + filemd5 = std::string(hex); + + return SERVER_SUCCESS; +} + +} +} +} \ No newline at end of file diff --git a/cpp/src/license/LicenseLibrary.h b/cpp/src/license/LicenseLibrary.h new file mode 100644 index 0000000000..d284bd4561 --- /dev/null +++ b/cpp/src/license/LicenseLibrary.h @@ -0,0 +1,71 @@ +#pragma once + +#include "LicenseFile.h" +#include "SecretFile.h" + +#include "utils/Error.h" + +#include +#include + + +namespace zilliz { +namespace vecwise { +namespace server { + +class LicenseLibrary { + public: + // Part 1: Get GPU Info + static ServerError + GetDeviceCount(int &device_count); + + static ServerError + GetUUID(int device_count, std::vector &uuid_array); + + static ServerError + GetUUIDMD5(int device_count, std::vector &uuid_array, std::vector &md5_array); + + + static ServerError + GetUUIDSHA256(const int &device_count, + std::vector &uuid_array, + std::vector &sha_array); + + // Part 2: Handle License File + static ServerError + LicenseFileSerialization(const std::string &path, + int device_count, + const std::map &uuid_encrption_map, int64_t remaining_hour); + + static ServerError + LicenseFileDeserialization(const std::string &path, + int &device_count, + std::map &uuid_encrption_map, + int64_t &remaining_hour); + + static ServerError + SecretFileSerialization(const std::string &path, + const time_t &update_time, + const off_t &file_size, + const std::string &file_md5); + + static ServerError + SecretFileDeserialization(const std::string &path, time_t &update_time, off_t &file_size, std::string &file_md5); + + // Part 3: File attribute: UpdateTime Time/ Size/ MD5 + static ServerError + GetFileUpdateTimeAndSize(const std::string &path, time_t &update_time, off_t &file_size); + + static ServerError + GetFileMD5(const std::string &path, std::string &filemd5); + + // Part 4: GPU Info File Serialization/Deserialization + + private: + static constexpr int sha256_length_ = 32; +}; + + +} +} +} diff --git a/cpp/src/license/SecretFile.h b/cpp/src/license/SecretFile.h new file mode 100644 index 0000000000..08f196a5b3 --- /dev/null +++ b/cpp/src/license/SecretFile.h @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ +#pragma once +#include + +class SecretFile { + public: + SecretFile() = default; + + SecretFile(const time_t &update_time, const off_t &file_size, const std::string &file_md5) + : update_time_(update_time), file_size_(file_size), file_md5_(file_md5) {} + + time_t get_update_time() { + return update_time_; + } + off_t get_file_size() { + return file_size_; + } + std::string get_file_md5() { + return file_md5_; + } + + private: + friend class boost::serialization::access; + + template + void serialize(Archive &ar, const unsigned int version) { + ar & update_time_; + ar & file_size_; + ar & file_md5_; + } + + public: + time_t update_time_ = 0; + off_t file_size_ = 0; + std::string file_md5_; +}; + +class SerializedSecretFile { + public: + ~SerializedSecretFile() { + if(secret_file_ != nullptr) { + delete secret_file_; + secret_file_ = nullptr; + } + } + + void + set_secret_file(SecretFile* secret_file) { + secret_file_ = secret_file; + } + + SecretFile* get_secret_file() { + return secret_file_; + } + + private: + friend class boost::serialization::access; + + template + void serialize(Archive &ar, const unsigned int version) { + ar & secret_file_; + } + + private: + SecretFile *secret_file_ = nullptr; +}; + + diff --git a/cpp/unittest/license/CMakeLists.txt b/cpp/unittest/license/CMakeLists.txt index 92eef5d556..2d2ed15fc7 100644 --- a/cpp/unittest/license/CMakeLists.txt +++ b/cpp/unittest/license/CMakeLists.txt @@ -15,16 +15,20 @@ link_directories(/usr/local/cuda) link_directories(/usr/local/cuda/lib64) link_directories(/usr/lib/x86_64-linux-gnu) link_directories(/usr/lib/nvidia-415) +link_directories(/usr/local/cuda/targets/x86_64-linux/lib/stubs/) set(require_files - ../../src/license/License.cpp - ../../src/license/LicensePublic.cpp - ../../src/license/LicenseCreateuuidshafile.cpp + ../../src/license/LicenseLibrary.cpp +# ../../src/license/License.cpp +# ../../src/license/LicensePublic.cpp +# ../../src/license/LicenseCreateuuidshafile.cpp ) set(db_test_src - ${require_files} - license_tests.cpp) + +# license_tests.cpp + license_library_tests.cpp + ${require_files}) cuda_add_executable(license_test ${db_test_src}) diff --git a/cpp/unittest/license/license_library_tests.cpp b/cpp/unittest/license/license_library_tests.cpp new file mode 100644 index 0000000000..eb8b10b8b9 --- /dev/null +++ b/cpp/unittest/license/license_library_tests.cpp @@ -0,0 +1,134 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ + + +#include "utils/Log.h" +#include "license/LicenseLibrary.h" +#include "utils/Error.h" + +#include + + +using namespace zilliz::vecwise; + +TEST(LicenseLibraryTest, GPU_INFO_TEST) { + + int device_count = 0; + server::ServerError err = server::LicenseLibrary::GetDeviceCount(device_count); + ASSERT_EQ(err, server::SERVER_SUCCESS); + std::cout << "Device Count: " << device_count << std::endl; + + std::vector uuid_array; + err = server::LicenseLibrary::GetUUID(device_count, uuid_array); + ASSERT_EQ(err, server::SERVER_SUCCESS); + for (long i = 0; i < device_count; ++i) { + std::cout << "Device Id: " << i << ", UUID: " << uuid_array[i] << std::endl; + } + + std::vector uuid_md5_array; + err = server::LicenseLibrary::GetUUIDMD5(device_count, uuid_array, uuid_md5_array); + ASSERT_EQ(err, server::SERVER_SUCCESS); + for (long i = 0; i < device_count; ++i) { + std::cout << "Device Id: " << i << ", UUID: " << uuid_array[i] << ", UUID_MD5: " << uuid_md5_array[i] + << std::endl; + } + + std::vector uuid_sha256_array; + err = server::LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, uuid_sha256_array); + ASSERT_EQ(err, server::SERVER_SUCCESS); + for (long i = 0; i < device_count; ++i) { + std::cout << "Device Id: " << i << ", UUID: " << uuid_array[i] << ", UUID_SHA256: " + << uuid_sha256_array[i] << std::endl; + } +} + +TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { + + // 1. Get Device Count + int device_count = 0; + server::ServerError err = server::LicenseLibrary::GetDeviceCount(device_count); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 2. Get All GPU UUID + std::vector uuid_array; + err = server::LicenseLibrary::GetUUID(device_count, uuid_array); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 3. Get UUID SHA256 + std::vector uuid_sha256_array; + err = server::LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, uuid_sha256_array); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 4. Set a file name + std::string license_file_path("/tmp/megasearch.license"); + + // 5. Provide remaining hour + int64_t remaining_hour = 2 * 365 * 24; + + // 6. Generate GPU ID map with GPU UUID + std::map uuid_encrption_map; + for (int i = 0; i < device_count; ++i) { + uuid_encrption_map[i] = uuid_sha256_array[i]; + } + + // 7. Generate License File + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, + device_count, + uuid_encrption_map, + remaining_hour); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 8. Define output var + int output_device_count = 0; + std::map output_uuid_encrption_map; + int64_t output_remaining_hour = 0; + + // 9. Read License File + err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, + output_device_count, + output_uuid_encrption_map, + output_remaining_hour); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + ASSERT_EQ(device_count, output_device_count); + ASSERT_EQ(remaining_hour, output_remaining_hour); + for (int i = 0; i < device_count; ++i) { + ASSERT_EQ(uuid_encrption_map[i], output_uuid_encrption_map[i]); + } + + // 10. Get License File Attribute + time_t update_time; + off_t file_size; + err = server::LicenseLibrary::GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 11. Get License File MD5 + std::string file_md5; + err = server::LicenseLibrary::GetFileMD5(license_file_path, file_md5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 12. Generate Secret File + std::string secret_file_path("/tmp/megasearch.secret"); + err = server::LicenseLibrary::SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 13. Define output var + time_t output_update_time; + off_t output_file_size; + std::string output_file_md5; + + // 14. Read License File + err = server::LicenseLibrary::SecretFileDeserialization(secret_file_path, + output_update_time, + output_file_size, + output_file_md5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + ASSERT_EQ(update_time, output_update_time); + ASSERT_EQ(file_size, output_file_size); + ASSERT_EQ(file_md5, output_file_md5); + +} \ No newline at end of file diff --git a/cpp/unittest/license/license_tests.cpp b/cpp/unittest/license/license_tests.cpp index 3da698c1d1..e67352cde6 100644 --- a/cpp/unittest/license/license_tests.cpp +++ b/cpp/unittest/license/license_tests.cpp @@ -4,6 +4,7 @@ // Proprietary and confidential. //////////////////////////////////////////////////////////////////////////////// #include +#include #include "license/License.h" #include "license/LicensePublic.h" #include "utils/Error.h" @@ -11,182 +12,180 @@ using namespace zilliz::vecwise; -TEST(LicenseTest, LICENSE_TEST) { - - std::string path1 = "/tmp/vecwise_engine.license"; - std::string path2 = "/tmp/vecwise_engine2.license"; - std::cout << "This is run " << std::endl; - - server::ServerError err; - - err = server::Licensefileread(path1); - if(err!=server::SERVER_SUCCESS) - { - exit(1); - } - err = server::LicenseIntegrity_check(path1,path2); - if(err!=server::SERVER_SUCCESS) - { - std::cout << "Integrity_check is wrong " << std::endl; - exit(1); - } - err = server::LicenseLegality_check(path1); - if(err!=server::SERVER_SUCCESS) - { - std::cout << "Legality_check is wrong " << std::endl; - exit(1); - } - std::cout << " runing " << std::endl; - server::Runtime(path1,path2); -} - - - - - //TEST(LicenseTest, LICENSE_TEST) { + // std::string path1 = "/tmp/vecwise_engine.license"; // std::string path2 = "/tmp/vecwise_engine2.license"; +// std::cout << "This is run " << std::endl; // // server::ServerError err; +// +// err = server::Licensefileread(path1); +// if(err!=server::SERVER_SUCCESS) +// { +// exit(1); +// } +// err = server::LicenseIntegrity_check(path1,path2); +// if(err!=server::SERVER_SUCCESS) +// { +// std::cout << "Integrity_check is wrong " << std::endl; +// exit(1); +// } +// err = server::LicenseLegality_check(path1); +// if(err!=server::SERVER_SUCCESS) +// { +// std::cout << "Legality_check is wrong " << std::endl; +// exit(1); +// } +// std::cout << " runing " << std::endl; // server::Runtime(path1,path2); - - -// time_t update_time; -// off_t file_size; -// std::string filemd5; -// -// time_t update_timecheck; -// off_t file_sizecheck; -// std::string filemd5check; -// -// err = server::LicenseGetfiletime(path1,update_time); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// err = server::LicenseGetfilesize(path1,file_size); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// err = server::LicenseGetfilemd5(path1,filemd5); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// err = server::LifileSave(path2,update_time,file_size,filemd5); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// err = server::LifileLoad(path2,update_timecheck,file_sizecheck,filemd5check); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// -// std::cout<< "update_time : " << update_time < uuids; -// -// deviceCount = 2; -// uuids.push_back("121"); -// uuids.push_back("324"); -// err = server::LicenseGetuuid(deviceCount,uuids); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// printf("\n deviceCount = %d\n",deviceCount); -// -// std::vector uuidmd5s; -// err = server::LicenseGetuuidmd5(deviceCount,uuids,uuidmd5s); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// printf(" md5s \n"); -// for(int i=0;i uuidshas; -// err = server::LicenseGetuuidsha(deviceCount,uuids,uuidshas); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// std::map uuidEncryption; -// for(int i=0;i uuidEncryptioncheck; -// err = server::LiLoad(path1,deviceCountcheck,uuidEncryptioncheck,RemainingTime); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// printf("----- checking ----\n"); -// printf("\n deviceCount = %d\n",deviceCountcheck); -// for(auto it : uuidEncryptioncheck) -// { -// std::cout<< "uuidshas : " << it.second << std::endl; -// } -// std::cout<< "RemainingTime :" << RemainingTime << std::endl; -// -// printf(" shas \n"); -// for(int i=0;i uuidshascheck; -// -// err = server::LicenseLoad(path1,deviceCountcheck,uuidshascheck); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// std::cout<<" deviceCountcheck :" << deviceCountcheck << std::endl; -// std::cout<<" uuidshascheck :" << uuidshascheck[0] << std::endl; -// -// -// std::string filemd5; -// err = server::LicenseGetfilemd5(path1,filemd5); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// std::cout<<" filemd5 :" << filemd5 << std::endl; -// -// err= server::LicensefileSave(path1,path2); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// time_t last_timecheck; -// std::string filemd5check; -// err= server::LicensefileLoad(path2,filemd5check,last_timecheck); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// std::cout<<" filemd5check :" << filemd5check << std::endl; -// -// time_t last_time; -// err = server::LicenseGetfiletime(path1,last_time); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// std::cout<<" last_time : " << last_time << std::endl; -// std::cout<<" last_timecheck :" << last_timecheck << std::endl; -// -// err = server::LicenseIntegritycheck(path1,path2); -// ASSERT_EQ(err, server::SERVER_SUCCESS); - //} + + + + + +TEST(LicenseTest, LICENSE_TEST) { + std::string path1 = "/tmp/vecwise_engine.license"; + std::string path2 = "/tmp/vecwise_engine2.license"; + + server::ServerError err; + server::Runtime(path1,path2); + + + time_t update_time; + off_t file_size; + std::string filemd5; + + time_t update_timecheck; + off_t file_sizecheck; + std::string filemd5check; + + err = server::LicenseGetfiletime(path1,update_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + err = server::LicenseGetfilesize(path1,file_size); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + err = server::LicenseGetfilemd5(path1,filemd5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + err = server::LifileSave(path2,update_time,file_size,filemd5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + err = server::LifileLoad(path2,update_timecheck,file_sizecheck,filemd5check); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + + std::cout<< "update_time : " << update_time < uuids; + + deviceCount = 2; + uuids.push_back("121"); + uuids.push_back("324"); + err = server::LicenseGetuuid(deviceCount,uuids); + ASSERT_EQ(err, server::SERVER_SUCCESS); + printf("\n deviceCount = %d\n",deviceCount); + + std::vector uuidmd5s; + err = server::LicenseGetuuidmd5(deviceCount,uuids,uuidmd5s); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + printf(" md5s \n"); + for(int i=0;i uuidshas; + err = server::LicenseGetuuidsha(deviceCount,uuids,uuidshas); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + std::map uuidEncryption; + for(int i=0;i uuidEncryptioncheck; +// err = server::LicenseLibrary::LicenseFileDeserialization(path1, deviceCountcheck, uuidEncryptioncheck, RemainingTime); + err = server::LiLoad(path1,deviceCountcheck,uuidEncryptioncheck,RemainingTime); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + printf("----- checking ----\n"); + printf("\n deviceCount = %d\n",deviceCountcheck); + for(auto it : uuidEncryptioncheck) + { + std::cout<< "uuidshas : " << it.second << std::endl; + } + std::cout<< "RemainingTime :" << RemainingTime << std::endl; + + printf(" shas \n"); + for(int i=0;i uuidshascheck; + + err = server::LicenseLoad(path1,deviceCountcheck,uuidshascheck); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + std::cout<<" deviceCountcheck :" << deviceCountcheck << std::endl; + std::cout<<" uuidshascheck :" << uuidshascheck[0] << std::endl; + + + err = server::LicenseGetfilemd5(path1,filemd5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + std::cout<<" filemd5 :" << filemd5 << std::endl; + + err= server::LicensefileSave(path1,path2); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + time_t last_timecheck; + err= server::LicensefileLoad(path2,filemd5check,last_timecheck); + ASSERT_EQ(err, server::SERVER_SUCCESS); + std::cout<<" filemd5check :" << filemd5check << std::endl; + + time_t last_time; + err = server::LicenseGetfiletime(path1,last_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + std::cout<<" last_time : " << last_time << std::endl; + std::cout<<" last_timecheck :" << last_timecheck << std::endl; + + err = server::LicenseIntegritycheck(path1,path2); + ASSERT_EQ(err, server::SERVER_SUCCESS); + +} From 128bc2b42015e35487c7f0db06730dd07f0eba30 Mon Sep 17 00:00:00 2001 From: "yangwei.yao" Date: Sun, 12 May 2019 16:17:18 +0800 Subject: [PATCH 4/9] Refactor Former-commit-id: 5c57d9054cae1e468a5795b928ecab4d18c1d4af --- cpp/src/license/LicenseCreateuuidshafile.cpp | 2 + cpp/src/license/LicenseLibrary.cpp | 173 +++++++++++++++++- cpp/src/license/LicenseLibrary.h | 33 ++++ cpp/src/license/LicensePublic.h | 36 ++-- cpp/src/license/LicenseRun.cpp | 2 + .../license/license_library_tests.cpp | 166 ++++++++++++++++- 6 files changed, 381 insertions(+), 31 deletions(-) diff --git a/cpp/src/license/LicenseCreateuuidshafile.cpp b/cpp/src/license/LicenseCreateuuidshafile.cpp index 93179ba75a..597adb2cfc 100644 --- a/cpp/src/license/LicenseCreateuuidshafile.cpp +++ b/cpp/src/license/LicenseCreateuuidshafile.cpp @@ -6,6 +6,8 @@ #include "utils/Error.h" #include "license/License.h" + + using namespace zilliz::vecwise; // diff --git a/cpp/src/license/LicenseLibrary.cpp b/cpp/src/license/LicenseLibrary.cpp index 6e757caa4b..f9cf6808d7 100644 --- a/cpp/src/license/LicenseLibrary.cpp +++ b/cpp/src/license/LicenseLibrary.cpp @@ -24,6 +24,18 @@ namespace server { constexpr int LicenseLibrary::sha256_length_; +ServerError +LicenseLibrary::OpenFile(const std::string &path) { + std::ifstream fileread; + fileread.open(path, std::ios::in); + if (!fileread) { + std::cout << path << " does not exist" << std::endl; + return SERVER_UNEXPECTED_ERROR; + } + fileread.close(); + return SERVER_SUCCESS; +} + ServerError LicenseLibrary::GetDeviceCount(int &device_count) { nvmlReturn_t result = nvmlInit(); @@ -139,6 +151,7 @@ LicenseLibrary::LicenseFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map, int64_t &remaining_hour) { + OpenFile(path); std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -177,6 +190,7 @@ LicenseLibrary::SecretFileDeserialization(const std::string &path, time_t &update_time, off_t &file_size, std::string &file_md5) { + OpenFile(path); std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -192,23 +206,26 @@ LicenseLibrary::SecretFileDeserialization(const std::string &path, // Part 3: File attribute: UpdateTime Time/ Size/ MD5 ServerError -LicenseLibrary::GetFileUpdateTimeAndSize(const std::string& path, time_t& update_time, off_t& file_size) { +LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update_time, off_t &file_size) { + + OpenFile(path); struct stat buf; int err_no = stat(path.c_str(), &buf); - if(err_no != 0) - { + if (err_no != 0) { std::cout << strerror(err_no) << std::endl; - return SERVER_UNEXPECTED_ERROR; + return SERVER_UNEXPECTED_ERROR; } - update_time =buf.st_mtime; - file_size =buf.st_size; + update_time = buf.st_mtime; + file_size = buf.st_size; return SERVER_SUCCESS; } ServerError LicenseLibrary::GetFileMD5(const std::string &path, std::string &filemd5) { + + OpenFile(path); filemd5.clear(); std::ifstream file(path.c_str(), std::ifstream::binary); @@ -238,6 +255,150 @@ LicenseLibrary::GetFileMD5(const std::string &path, std::string &filemd5) { return SERVER_SUCCESS; } +// Part 4: GPU Info File Serialization/Deserialization +ServerError +LicenseLibrary::GPUinfoFileSerialization(const std::string &path, + int device_count, + const std::map &uuid_encrption_map) { + std::ofstream file(path); + boost::archive::binary_oarchive oa(file); + oa.register_type(); + + SerializedGPUInfoFile serialized_gpu_info_file; + + serialized_gpu_info_file.set_gpu_info_file(new GPUInfoFile(device_count, uuid_encrption_map)); + oa << serialized_gpu_info_file; + + file.close(); + return SERVER_SUCCESS; +} +ServerError +LicenseLibrary::GPUinfoFileDeserialization(const std::string &path, + int &device_count, + std::map &uuid_encrption_map) { + OpenFile(path); + std::ifstream file(path); + boost::archive::binary_iarchive ia(file); + ia.register_type(); + + SerializedGPUInfoFile serialized_gpu_info_file; + ia >> serialized_gpu_info_file; + + device_count = serialized_gpu_info_file.get_gpu_info_file()->get_device_count(); + uuid_encrption_map = serialized_gpu_info_file.get_gpu_info_file()->get_uuid_encryption_map(); + + file.close(); + return SERVER_SUCCESS; +} + +// Part 5: Integrity check and Legality check +ServerError +LicenseLibrary::IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path) { + + std::string file_md5; + GetFileMD5(license_file_path, file_md5); + time_t update_time; + off_t file_size; + GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); + + std::string output_file_md5; + time_t output_update_time; + off_t output_file_size; + SecretFileDeserialization(secret_file_path, output_update_time, output_file_size, output_file_md5); + if (file_md5 != output_file_md5) { + printf("License file has been modified\n"); + return SERVER_UNEXPECTED_ERROR; + } + if (update_time != output_update_time) { + printf("update_time is wrong\n"); + return SERVER_UNEXPECTED_ERROR; + } + if (file_size != output_file_size) { + printf("file_size is wrong\n"); + return SERVER_UNEXPECTED_ERROR; + } + std::cout << "Integrity Check Success" << std::endl; + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::LegalityCheck(const std::string &license_file_path) { + + int device_count; + GetDeviceCount(device_count); + std::vector uuid_array; + GetUUID(device_count, uuid_array); + + std::vector sha_array; + GetUUIDSHA256(device_count, uuid_array, sha_array); + + int output_device_count; + std::map uuid_encryption_map; + int64_t remaining_time; + LicenseFileDeserialization(license_file_path, output_device_count, uuid_encryption_map, remaining_time); + + if (device_count != output_device_count) { + printf("device_count is wrong\n"); + return SERVER_UNEXPECTED_ERROR; + } + for (int i = 0; i < device_count; ++i) { + if (sha_array[i] != uuid_encryption_map[i]) { + printf("uuid_encryption_map %d is wrong\n", i); + return SERVER_UNEXPECTED_ERROR; + } + } + if (remaining_time <= 0) { + printf("License expired\n"); + return SERVER_UNEXPECTED_ERROR; + } + std::cout << "Legality Check Success" << std::endl; + return SERVER_SUCCESS; +} + +// Part 6: Timer +ServerError +LicenseLibrary::AlterFile(const std::string &license_file_path, + const std::string &secret_file_path, + const boost::system::error_code &ec, + boost::asio::deadline_timer *pt) { + int device_count; + std::map uuid_encryption_map; + int64_t remaining_time; + LicenseFileDeserialization(license_file_path, device_count, uuid_encryption_map, remaining_time); + + std::cout << "remaining_time: " << remaining_time << std::endl; + + if (remaining_time <= 0) { + std::cout << "License expired" << std::endl; + exit(1); + } + --remaining_time; + LicenseFileSerialization(license_file_path, device_count, uuid_encryption_map, remaining_time); + + time_t update_time; + off_t file_size; + GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); + std::string file_md5; + GetFileMD5(license_file_path, file_md5); + SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); + + pt->expires_at(pt->expires_at() + boost::posix_time::hours(1)); + pt->async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, pt)); + return SERVER_SUCCESS; +} + +ServerError +LicenseLibrary::StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path) { + + OpenFile(license_file_path); + OpenFile(secret_file_path); + boost::asio::io_service io; + boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); + t.async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, &t)); + std::cout << "Timing begins" << std::endl; + io.run(); + return SERVER_SUCCESS; +} } } diff --git a/cpp/src/license/LicenseLibrary.h b/cpp/src/license/LicenseLibrary.h index d284bd4561..da79a4770d 100644 --- a/cpp/src/license/LicenseLibrary.h +++ b/cpp/src/license/LicenseLibrary.h @@ -2,9 +2,14 @@ #include "LicenseFile.h" #include "SecretFile.h" +#include "GPUInfoFile.h" #include "utils/Error.h" +#include +#include +#include + #include #include @@ -15,6 +20,8 @@ namespace server { class LicenseLibrary { public: + static ServerError + OpenFile(const std::string &path); // Part 1: Get GPU Info static ServerError GetDeviceCount(int &device_count); @@ -60,6 +67,32 @@ class LicenseLibrary { GetFileMD5(const std::string &path, std::string &filemd5); // Part 4: GPU Info File Serialization/Deserialization + static ServerError + GPUinfoFileSerialization(const std::string &path, + int device_count, + const std::map &uuid_encrption_map); + static ServerError + GPUinfoFileDeserialization(const std::string &path, + int &device_count, + std::map &uuid_encrption_map); + + // Part 5: Integrity check and Legality check + static ServerError + IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path); + + static ServerError + LegalityCheck(const std::string &license_file_path); + + // Part 6: Timer + static ServerError + AlterFile(const std::string &license_file_path, + const std::string &secret_file_path, + const boost::system::error_code &ec, + boost::asio::deadline_timer *pt); + + static ServerError + StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path); + private: static constexpr int sha256_length_ = 32; diff --git a/cpp/src/license/LicensePublic.h b/cpp/src/license/LicensePublic.h index 394dc808c8..a40b6266c5 100644 --- a/cpp/src/license/LicensePublic.h +++ b/cpp/src/license/LicensePublic.h @@ -30,53 +30,57 @@ #include #include + namespace zilliz { namespace vecwise { namespace server { class -BoostArchive; + BoostArchive; class -Licensedata1; + Licensedata1; class -Licensefiledata; + Licensefiledata; class -STLlicensefiledata; + STLlicensefiledata; ServerError -LiSave(const std::string& path,const int& deviceCount,const std::map& uuidEncryption); +LiSave(const std::string &path, const int &deviceCount, const std::map &uuidEncryption); ServerError -LiLoad(const std::string& path,int& deviceCount,std::map& uuidEncryption,int64_t& RemainingTime); +LiLoad(const std::string &path, int &deviceCount, std::map &uuidEncryption, int64_t &RemainingTime); ServerError -LifileSave(const std::string& path,const time_t& update_time,const off_t& file_size,const std::string& filemd5); +LifileSave(const std::string &path, const time_t &update_time, const off_t &file_size, const std::string &filemd5); ServerError -LifileLoad(const std::string& path,time_t& update_time,off_t& file_size,std::string& filemd5); +LifileLoad(const std::string &path, time_t &update_time, off_t &file_size, std::string &filemd5); ServerError -LiSave(const std::string& path,const int& deviceCount,const std::map& uuidEncryption, const int64_t& RemainingTime); +LiSave(const std::string &path, + const int &deviceCount, + const std::map &uuidEncryption, + const int64_t &RemainingTime); ServerError -LicenseIntegrity_check(const std::string& path,const std::string& path2); +LicenseIntegrity_check(const std::string &path, const std::string &path2); ServerError -LicenseLegality_check(const std::string& path); +LicenseLegality_check(const std::string &path); void -Alterfile(const std::string &path1, const std::string &path2 ,const boost::system::error_code &ec, boost::asio::deadline_timer* pt); +Alterfile(const std::string &path1, + const std::string &path2, + const boost::system::error_code &ec, + boost::asio::deadline_timer *pt); void -Runtime(const std::string &path1, const std::string &path2 ); - - - +Runtime(const std::string &path1, const std::string &path2); } diff --git a/cpp/src/license/LicenseRun.cpp b/cpp/src/license/LicenseRun.cpp index a059d2f7e2..93c6c463b0 100644 --- a/cpp/src/license/LicenseRun.cpp +++ b/cpp/src/license/LicenseRun.cpp @@ -8,6 +8,8 @@ #include "utils/Error.h" #include "license/License.h" #include "license/LicensePublic.h" + + using namespace zilliz::vecwise; //TEST(LicenseTest, LICENSE_TEST) { diff --git a/cpp/unittest/license/license_library_tests.cpp b/cpp/unittest/license/license_library_tests.cpp index eb8b10b8b9..ddce463d9a 100644 --- a/cpp/unittest/license/license_library_tests.cpp +++ b/cpp/unittest/license/license_library_tests.cpp @@ -62,6 +62,7 @@ TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { err = server::LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, uuid_sha256_array); ASSERT_EQ(err, server::SERVER_SUCCESS); + // 4. Set a file name std::string license_file_path("/tmp/megasearch.license"); @@ -74,19 +75,44 @@ TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { uuid_encrption_map[i] = uuid_sha256_array[i]; } - // 7. Generate License File + // 7.GPU_info File + std::string GPU_info_file_path("/tmp/megasearch.info"); + + + // 8. Generate GPU_info File + err = server::LicenseLibrary::GPUinfoFileSerialization(GPU_info_file_path, + device_count, + uuid_encrption_map); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 9. Define output var + int output_info_device_count = 0; + std::map output_info_uuid_encrption_map; + + // 10. Read GPU_info File + err = server::LicenseLibrary::GPUinfoFileDeserialization(GPU_info_file_path, + output_info_device_count, + output_info_uuid_encrption_map); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + ASSERT_EQ(device_count, output_info_device_count); + for (int i = 0; i < device_count; ++i) { + ASSERT_EQ(uuid_encrption_map[i], output_info_uuid_encrption_map[i]); + } + + // 11. Generate License File err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, device_count, uuid_encrption_map, remaining_hour); ASSERT_EQ(err, server::SERVER_SUCCESS); - // 8. Define output var + // 12. Define output var int output_device_count = 0; std::map output_uuid_encrption_map; - int64_t output_remaining_hour = 0; + int64_t output_remaining_hour; - // 9. Read License File + // 13. Read License File err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, output_device_count, output_uuid_encrption_map, @@ -99,28 +125,28 @@ TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { ASSERT_EQ(uuid_encrption_map[i], output_uuid_encrption_map[i]); } - // 10. Get License File Attribute + // 14. Get License File Attribute time_t update_time; off_t file_size; err = server::LicenseLibrary::GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); ASSERT_EQ(err, server::SERVER_SUCCESS); - // 11. Get License File MD5 + // 15. Get License File MD5 std::string file_md5; err = server::LicenseLibrary::GetFileMD5(license_file_path, file_md5); ASSERT_EQ(err, server::SERVER_SUCCESS); - // 12. Generate Secret File + // 16. Generate Secret File std::string secret_file_path("/tmp/megasearch.secret"); err = server::LicenseLibrary::SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); ASSERT_EQ(err, server::SERVER_SUCCESS); - // 13. Define output var + // 17. Define output var time_t output_update_time; off_t output_file_size; std::string output_file_md5; - // 14. Read License File + // 18. Read License File err = server::LicenseLibrary::SecretFileDeserialization(secret_file_path, output_update_time, output_file_size, @@ -131,4 +157,126 @@ TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { ASSERT_EQ(file_size, output_file_size); ASSERT_EQ(file_md5, output_file_md5); + // 19. Integrity check and Legality check + err = server::LicenseLibrary::IntegrityCheck(license_file_path, secret_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + err = server::LicenseLibrary::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); + +} + +TEST(LicenseLibraryTest, Timer_TEST) { + + server::ServerError err; + std::string license_file_path("/tmp/megasearch.license"); + std::string secret_file_path("/tmp/megasearch.secret"); + + // 19. Integrity check and Legality check + err = server::LicenseLibrary::IntegrityCheck(license_file_path, secret_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + err = server::LicenseLibrary::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 20. Start counting down + err = server::LicenseLibrary::StartCountingDown(license_file_path, secret_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); +} + +TEST(LicenseLibraryTest, GET_GPU_INFO_FILE) { + + // 1. Get Device Count + int device_count = 0; + server::ServerError err = server::LicenseLibrary::GetDeviceCount(device_count); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 2. Get All GPU UUID + std::vector uuid_array; + err = server::LicenseLibrary::GetUUID(device_count, uuid_array); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 3. Get UUID SHA256 + std::vector uuid_sha256_array; + err = server::LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, uuid_sha256_array); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 4. Generate GPU ID map with GPU UUID + std::map uuid_encrption_map; + for (int i = 0; i < device_count; ++i) { + uuid_encrption_map[i] = uuid_sha256_array[i]; + } + + // 5.GPU_info File + std::string GPU_info_file_path("/tmp/megasearch.info"); + + // 6. Generate GPU_info File + err = server::LicenseLibrary::GPUinfoFileSerialization(GPU_info_file_path, + device_count, + uuid_encrption_map); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + std::cout << "Generate GPU_info File Success" << std::endl; +} + +TEST(LicenseLibraryTest, GET_LICENSE_FILE) { + + server::ServerError err; + // 1.GPU_info File + std::string GPU_info_file_path("/tmp/megasearch.info"); + + // 2. License File + std::string license_file_path("/tmp/megasearch.license"); + + // 3. Define output var + int output_info_device_count = 0; + std::map output_info_uuid_encrption_map; + + // 4. Read GPU_info File + err = server::LicenseLibrary::GPUinfoFileDeserialization(GPU_info_file_path, + output_info_device_count, + output_info_uuid_encrption_map); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 5. Enter time + int64_t remaining_hour = 5; + std::cout << "Please enter the authorization time (hours)" << std::endl; +// std::cin >> remaining_hour; + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, + output_info_device_count, + output_info_uuid_encrption_map, + remaining_hour); + ASSERT_EQ(err, server::SERVER_SUCCESS); + std::cout << "Generate License File Success" << std::endl; + +} + +TEST(LicenseLibraryTest, GET_SECRET_FILE) { + + server::ServerError err; + std::string license_file_path("/tmp/megasearch.license"); + std::string secret_file_path("/tmp/megasearch.secret"); + + // 14. Get License File Attribute + time_t update_time; + off_t file_size; + err = server::LicenseLibrary::GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 15. Get License File MD5 + std::string file_md5; + err = server::LicenseLibrary::GetFileMD5(license_file_path, file_md5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 16. Generate Secret File + err = server::LicenseLibrary::SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + std::cout << "Generate Secret File Success" << std::endl; +} + +TEST(LicenseLibraryTest, CIN_TEST) { + int a; + std::cin >> a; + std::cout << 2 * a << std::endl; } \ No newline at end of file From 7c512bf75219f6aed02df327c4d41485049722d4 Mon Sep 17 00:00:00 2001 From: jinhai Date: Sun, 12 May 2019 20:28:10 +0800 Subject: [PATCH 5/9] Update on license check Former-commit-id: ef039b5663e0a68c1dfdea7244de4d2e7e16c10e --- cpp/src/license/GetSysInfo.cpp | 47 ++- cpp/src/license/License.cpp | 371 ------------------ cpp/src/license/License.h | 81 ---- cpp/src/license/LicenseCheck.cpp | 29 ++ cpp/src/license/LicenseCheck.h | 14 + cpp/src/license/LicenseCreateTime.cpp | 2 +- cpp/src/license/LicenseCreateuuidshafile.cpp | 2 +- cpp/src/license/LicenseLibrary.cpp | 47 ++- cpp/src/license/LicenseLibrary.h | 6 +- cpp/src/license/LicensePublic.cpp | 2 +- cpp/src/license/LicenseRun.cpp | 2 +- cpp/src/server/Server.cpp | 2 +- cpp/src/utils/Error.h | 1 + .../license/license_library_tests.cpp | 12 + cpp/unittest/license/license_tests.cpp | 2 +- 15 files changed, 142 insertions(+), 478 deletions(-) delete mode 100644 cpp/src/license/License.cpp delete mode 100644 cpp/src/license/License.h create mode 100644 cpp/src/license/LicenseCheck.cpp create mode 100644 cpp/src/license/LicenseCheck.h diff --git a/cpp/src/license/GetSysInfo.cpp b/cpp/src/license/GetSysInfo.cpp index 9c35b89864..added44360 100644 --- a/cpp/src/license/GetSysInfo.cpp +++ b/cpp/src/license/GetSysInfo.cpp @@ -1,6 +1,51 @@ #include +#include +#include + +// Not provide path: current work path will be used and system.info. + +void +print_usage(const std::string &app_name) { + printf("\n Usage: %s [OPTIONS]\n\n", app_name.c_str()); + printf(" Options:\n"); + printf(" -h --help Print this help\n"); + printf(" -d --sysinfo filename Generate system info file as given name\n"); + printf("\n"); +} int main(int argc, char* argv[]) { - std::cout << "GetSysInfo" << std::endl; + std::string app_name = argv[0]; + if(argc != 1 && argc != 3) { + print_usage(app_name); + return EXIT_FAILURE; + } + + static struct option long_options[] = {{"system_info", required_argument, 0, 'd'}, + {"help", no_argument, 0, 'h'}, + {NULL, 0, 0, 0}}; + int value = 0; + int option_index = 0; + std::string system_info_filename = "./system.info"; + while ((value = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) { + switch (value) { + case 'd': { + char *system_info_filename_ptr = strdup(optarg); + system_info_filename = system_info_filename_ptr; + free(system_info_filename_ptr); +// printf("Generate system info file: %s\n", system_info_filename.c_str()); + break; + } + case 'h': + print_usage(app_name); + return EXIT_SUCCESS; + case '?': + print_usage(app_name); + return EXIT_FAILURE; + default: + print_usage(app_name); + break; + } + } + return 0; } \ No newline at end of file diff --git a/cpp/src/license/License.cpp b/cpp/src/license/License.cpp deleted file mode 100644 index eb6c563e4f..0000000000 --- a/cpp/src/license/License.cpp +++ /dev/null @@ -1,371 +0,0 @@ -#include "License.h" - -namespace zilliz { -namespace vecwise { -namespace server { - -ServerError -LicenseSave(const std::string& path,const int& deviceCount,const std::vector& shas) -{ - std::ofstream file(path); - - boost::archive::binary_oarchive oa(file); - oa << deviceCount; - for(int i=0;i& shas) -{ - std::ifstream file(path); - - boost::archive::binary_iarchive ia(file); - ia >> deviceCount; - std::string sha; - for(int i=0;i> sha; - shas.push_back(sha); - } - file.close(); - return SERVER_SUCCESS; -} - -ServerError -Licensefileread(const std::string& path) -{ - std::ifstream fileread; - fileread.open(path,std::ios::in); - if(!fileread) - { - printf("NO License\n"); - return SERVER_UNEXPECTED_ERROR; - } - fileread.close(); - return SERVER_SUCCESS; -} - -ServerError -Licensefileread (const std::string& path,std::ifstream& fileread){ - fileread.open(path,std::ios::in); - if(!fileread) - { - printf("Can't open file\n"); - return SERVER_UNEXPECTED_ERROR; - } - return SERVER_SUCCESS; -} - -ServerError -Licensefilewrite (const std::string& path,std::ofstream& filewrite) { - filewrite.open(path,std::ios::out); - if(!filewrite) - { - printf("Can't write file\n"); - return SERVER_UNEXPECTED_ERROR; - } - return SERVER_SUCCESS; -} - -ServerError -LicenseGetCount(int &deviceCount) -{ - nvmlReturn_t result = nvmlInit(); - if (NVML_SUCCESS != result) - { - printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); - return SERVER_UNEXPECTED_ERROR; - } - cudaError_t error_id = cudaGetDeviceCount(&deviceCount); - if (error_id != cudaSuccess) - { - printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id)); - printf("Result = FAIL\n"); - return SERVER_UNEXPECTED_ERROR; - } - return SERVER_SUCCESS; -} - -ServerError -LicenseGetuuid(int& deviceCount, std::vector& uuids) -{ - nvmlReturn_t result = nvmlInit(); - if (NVML_SUCCESS != result) - { - printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); - return SERVER_UNEXPECTED_ERROR; - } - cudaError_t error_id = cudaGetDeviceCount(&deviceCount); - if (error_id != cudaSuccess) - { - printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id)); - printf("Result = FAIL\n"); - return SERVER_UNEXPECTED_ERROR; - } - - if (deviceCount == 0) - { - printf("There are no available device(s) that support CUDA\n"); - return SERVER_UNEXPECTED_ERROR; - } - - for (int dev = 0; dev < deviceCount; ++dev) - { - nvmlDevice_t device; - result = nvmlDeviceGetHandleByIndex(dev, &device); - printf("device id: %d\n", dev); - if (NVML_SUCCESS != result) - { - printf("Failed to get handle for device %i: %s\n", dev, nvmlErrorString(result)); - return SERVER_UNEXPECTED_ERROR; - } - - char* uuid = (char*)malloc(80); - unsigned int length = 80; - nvmlReturn_t err = nvmlDeviceGetUUID(device, uuid, length); - if(err != NVML_SUCCESS) { - printf("nvmlDeviceGetUUID error: %d\n", err); - return SERVER_UNEXPECTED_ERROR; - } - - printf("\n device: %d, uuid = %s \n", dev, uuid); - uuids.push_back(std::string(uuid)); - free(uuid); - uuid = NULL; - } - return SERVER_SUCCESS; -} - -ServerError -LicenseGetuuidmd5(const int& deviceCount,std::vector& uuids,std::vector& md5s) -{ - MD5_CTX ctx; - unsigned char outmd[16]; - char temp[2]; - std::string md5=""; - for(int dev=0;dev& uuids,std::vector& shas) -{ - SHA256_CTX ctx; - unsigned char outmd[32]; - char temp[2]; - std::string sha=""; - for(int dev=0;dev uuids; - LicenseGetuuid(deviceCount,uuids); - std::vector shas; - LicenseGetuuidsha(deviceCount,uuids,shas); - - - int deviceCountcheck; - std::vector shascheck; - LicenseLoad(path,deviceCountcheck,shascheck); - - if(deviceCount!=deviceCountcheck) - { - printf("deviceCount is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - for(int i=0;i> filemd5; - ia >> last_time; - file.close(); - return SERVER_SUCCESS; -} - -ServerError -LicenseIntegritycheck(const std::string& path,const std::string& path2) - { - std::string filemd5; - LicenseGetfilemd5(path,filemd5); - time_t last_time; - LicenseGetfiletime(path,last_time); - - time_t last_timecheck; - std::string filemd5check; - LicensefileLoad(path2,filemd5check,last_timecheck); - - if(filemd5!=filemd5check) - { - printf("This file has been modified\n"); - return SERVER_UNEXPECTED_ERROR; - } - if(last_time!=last_timecheck) - { - printf("last_time is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - - return SERVER_SUCCESS; - } - -ServerError -LicenseValidate(const std::string& path) { - - - return SERVER_SUCCESS; -} - - - -} -} -} \ No newline at end of file diff --git a/cpp/src/license/License.h b/cpp/src/license/License.h deleted file mode 100644 index 6f67edb6f1..0000000000 --- a/cpp/src/license/License.h +++ /dev/null @@ -1,81 +0,0 @@ -#pragma once - -#include "utils/Error.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include "boost/archive/binary_oarchive.hpp" -#include "boost/archive/binary_iarchive.hpp" - -#include -#include -#include -#include - - -namespace zilliz { -namespace vecwise { -namespace server { - - -ServerError -LicenseSave(const std::string& path,const int& deviceCount,const std::vector& shas); - -ServerError -LicenseLoad(const std::string& path,int& deviceCount,std::vector& shas); - -ServerError -Licensefileread (const std::string& path,std::ifstream& fileread); - -ServerError -Licensefilewrite (const std::string& path,std::ofstream& filewrite); - -ServerError -LicenseGetuuid(int& deviceCount, std::vector& uuids); - -ServerError -LicenseGetuuidmd5(const int& deviceCount,std::vector& uuids,std::vector& md5s); - -ServerError -LicenseGetfilemd5(const std::string& path,std::string& filemd5); - -ServerError -LicenseGetuuidsha(const int& deviceCount,std::vector& uuids,std::vector& shas); - -ServerError -LicenseIntegritycheck(const std::string& path,const std::string& path2); - -ServerError -LicenseGetfiletime(const std::string& path,time_t& last_time); - -ServerError -LicenseLegalitycheck(const std::string& path); - -ServerError -LicensefileSave(const std::string& path,const std::string& path2); - -ServerError -LicensefileLoad(const std::string& path2,std::string& filemd5,time_t& last_time); - - -ServerError -LicenseGetfilesize(const std::string& path,off_t& file_size); - -ServerError -LicenseValidate(const std::string& path); - -ServerError -Licensefileread(const std::string& path); - -ServerError -LicenseGetCount(int &deviceCount); -} -} -} - - diff --git a/cpp/src/license/LicenseCheck.cpp b/cpp/src/license/LicenseCheck.cpp new file mode 100644 index 0000000000..1566f4e951 --- /dev/null +++ b/cpp/src/license/LicenseCheck.cpp @@ -0,0 +1,29 @@ +#include "LicenseCheck.h" + +namespace zilliz { +namespace vecwise { +namespace server { + +class LicenseCheck { + public: + static LicenseCheck& + GetInstance() { + static LicenseCheck instance; + return instance; + } + + ServerError + Check(); + + ServerError + PeriodicUpdate(); + + private: + LicenseCheck() {}; + + +}; + +} +} +} \ No newline at end of file diff --git a/cpp/src/license/LicenseCheck.h b/cpp/src/license/LicenseCheck.h new file mode 100644 index 0000000000..4078a45e4c --- /dev/null +++ b/cpp/src/license/LicenseCheck.h @@ -0,0 +1,14 @@ +#pragma once + +#include "utils/Error.h" + +namespace zilliz { +namespace vecwise { +namespace server { + + +} +} +} + + diff --git a/cpp/src/license/LicenseCreateTime.cpp b/cpp/src/license/LicenseCreateTime.cpp index 0c2f951511..f14d6bb7a5 100644 --- a/cpp/src/license/LicenseCreateTime.cpp +++ b/cpp/src/license/LicenseCreateTime.cpp @@ -7,7 +7,7 @@ #include #include "utils/Error.h" -#include "license/License.h" +#include "license/LicenseCheck.h" #include "license/LicensePublic.h" using namespace zilliz::vecwise; diff --git a/cpp/src/license/LicenseCreateuuidshafile.cpp b/cpp/src/license/LicenseCreateuuidshafile.cpp index 597adb2cfc..9476c89bd7 100644 --- a/cpp/src/license/LicenseCreateuuidshafile.cpp +++ b/cpp/src/license/LicenseCreateuuidshafile.cpp @@ -5,7 +5,7 @@ #include #include "utils/Error.h" -#include "license/License.h" +#include "license/LicenseCheck.h" using namespace zilliz::vecwise; diff --git a/cpp/src/license/LicenseLibrary.cpp b/cpp/src/license/LicenseLibrary.cpp index f9cf6808d7..237f1679b8 100644 --- a/cpp/src/license/LicenseLibrary.cpp +++ b/cpp/src/license/LicenseLibrary.cpp @@ -15,7 +15,9 @@ #include //#include //#include +#include #include +#include namespace zilliz { @@ -24,18 +26,23 @@ namespace server { constexpr int LicenseLibrary::sha256_length_; -ServerError -LicenseLibrary::OpenFile(const std::string &path) { - std::ifstream fileread; - fileread.open(path, std::ios::in); - if (!fileread) { - std::cout << path << " does not exist" << std::endl; - return SERVER_UNEXPECTED_ERROR; +bool +LicenseLibrary::IsFileExistent(const std::string& path) { + + boost::system::error_code error; + auto file_status = boost::filesystem::status(path, error); + if (error) { + return false; } - fileread.close(); - return SERVER_SUCCESS; + + if (!boost::filesystem::exists(file_status)) { + return false; + } + + return !boost::filesystem::is_directory(file_status); } + ServerError LicenseLibrary::GetDeviceCount(int &device_count) { nvmlReturn_t result = nvmlInit(); @@ -151,7 +158,8 @@ LicenseLibrary::LicenseFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map, int64_t &remaining_hour) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -190,7 +198,8 @@ LicenseLibrary::SecretFileDeserialization(const std::string &path, time_t &update_time, off_t &file_size, std::string &file_md5) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -208,7 +217,8 @@ LicenseLibrary::SecretFileDeserialization(const std::string &path, ServerError LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update_time, off_t &file_size) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + struct stat buf; int err_no = stat(path.c_str(), &buf); if (err_no != 0) { @@ -225,7 +235,8 @@ LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update ServerError LicenseLibrary::GetFileMD5(const std::string &path, std::string &filemd5) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + filemd5.clear(); std::ifstream file(path.c_str(), std::ifstream::binary); @@ -276,7 +287,8 @@ ServerError LicenseLibrary::GPUinfoFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -317,7 +329,7 @@ LicenseLibrary::IntegrityCheck(const std::string &license_file_path, const std:: printf("file_size is wrong\n"); return SERVER_UNEXPECTED_ERROR; } - std::cout << "Integrity Check Success" << std::endl; + return SERVER_SUCCESS; } @@ -390,8 +402,9 @@ LicenseLibrary::AlterFile(const std::string &license_file_path, ServerError LicenseLibrary::StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path) { - OpenFile(license_file_path); - OpenFile(secret_file_path); + if(!IsFileExistent(license_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; + if(!IsFileExistent(secret_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; + boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); t.async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, &t)); diff --git a/cpp/src/license/LicenseLibrary.h b/cpp/src/license/LicenseLibrary.h index da79a4770d..dd78c221ad 100644 --- a/cpp/src/license/LicenseLibrary.h +++ b/cpp/src/license/LicenseLibrary.h @@ -20,8 +20,10 @@ namespace server { class LicenseLibrary { public: - static ServerError - OpenFile(const std::string &path); + // Part 0: File check + static bool + IsFileExistent(const std::string& path); + // Part 1: Get GPU Info static ServerError GetDeviceCount(int &device_count); diff --git a/cpp/src/license/LicensePublic.cpp b/cpp/src/license/LicensePublic.cpp index 632b4cb73d..186c502ac1 100644 --- a/cpp/src/license/LicensePublic.cpp +++ b/cpp/src/license/LicensePublic.cpp @@ -3,7 +3,7 @@ // #include "LicensePublic.h" -#include "license/License.h" +#include "license/LicenseCheck.h" //#include "BoostArchive.h" #define RTime 100 diff --git a/cpp/src/license/LicenseRun.cpp b/cpp/src/license/LicenseRun.cpp index 93c6c463b0..dd06a64540 100644 --- a/cpp/src/license/LicenseRun.cpp +++ b/cpp/src/license/LicenseRun.cpp @@ -6,7 +6,7 @@ #include #include "utils/Error.h" -#include "license/License.h" +#include "license/LicenseCheck.h" #include "license/LicensePublic.h" diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index 930c29fac9..876771fafd 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -9,7 +9,7 @@ #include "utils/Log.h" #include "utils/SignalUtil.h" #include "utils/TimeRecorder.h" -#include "license/License.h" +#include "license/LicenseCheck.h" #include #include diff --git a/cpp/src/utils/Error.h b/cpp/src/utils/Error.h index 88e697ec2e..8684f8607f 100644 --- a/cpp/src/utils/Error.h +++ b/cpp/src/utils/Error.h @@ -32,6 +32,7 @@ constexpr ServerError SERVER_FILE_NOT_FOUND = ToGlobalServerErrorCode(0x005); constexpr ServerError SERVER_NOT_IMPLEMENT = ToGlobalServerErrorCode(0x006); constexpr ServerError SERVER_BLOCKING_QUEUE_EMPTY = ToGlobalServerErrorCode(0x007); constexpr ServerError SERVER_LICENSE_VALIDATION_FAIL = ToGlobalServerErrorCode(0x008); +constexpr ServerError SERVER_LICENSE_FILE_NOT_EXIST = ToGlobalServerErrorCode(0x009); class ServerException : public std::exception { public: diff --git a/cpp/unittest/license/license_library_tests.cpp b/cpp/unittest/license/license_library_tests.cpp index ddce463d9a..c990467f44 100644 --- a/cpp/unittest/license/license_library_tests.cpp +++ b/cpp/unittest/license/license_library_tests.cpp @@ -14,6 +14,18 @@ using namespace zilliz::vecwise; +TEST(LicenseLibraryTest, FILE_EXISTENT_TEST) { + + std::string hosts_file = "/etc/hosts"; + ASSERT_EQ(server::LicenseLibrary::IsFileExistent(hosts_file), true); + + std::string no_exist_file = "/temp/asdaasd"; + ASSERT_EQ(server::LicenseLibrary::IsFileExistent(no_exist_file), false); + + std::string directory = "/tmp"; + ASSERT_EQ(server::LicenseLibrary::IsFileExistent(directory), false); +} + TEST(LicenseLibraryTest, GPU_INFO_TEST) { int device_count = 0; diff --git a/cpp/unittest/license/license_tests.cpp b/cpp/unittest/license/license_tests.cpp index e67352cde6..cff0beeb35 100644 --- a/cpp/unittest/license/license_tests.cpp +++ b/cpp/unittest/license/license_tests.cpp @@ -5,7 +5,7 @@ //////////////////////////////////////////////////////////////////////////////// #include #include -#include "license/License.h" +#include "license/LicenseCheck.h" #include "license/LicensePublic.h" #include "utils/Error.h" From 384b9b8af2f43f50f4f6e76a89f1a0ae1fbfd210 Mon Sep 17 00:00:00 2001 From: "yangwei.yao" Date: Sun, 12 May 2019 20:38:20 +0800 Subject: [PATCH 6/9] 1 Former-commit-id: 3642cd8f4089038eade6857854d1d1b42c5eceb8 --- cpp/unittest/license/license_library_tests.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/cpp/unittest/license/license_library_tests.cpp b/cpp/unittest/license/license_library_tests.cpp index c990467f44..ec9ee9fac0 100644 --- a/cpp/unittest/license/license_library_tests.cpp +++ b/cpp/unittest/license/license_library_tests.cpp @@ -10,7 +10,7 @@ #include "utils/Error.h" #include - +#include using namespace zilliz::vecwise; @@ -251,7 +251,7 @@ TEST(LicenseLibraryTest, GET_LICENSE_FILE) { ASSERT_EQ(err, server::SERVER_SUCCESS); // 5. Enter time - int64_t remaining_hour = 5; + int64_t remaining_hour = 24*7 ; std::cout << "Please enter the authorization time (hours)" << std::endl; // std::cin >> remaining_hour; err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, @@ -286,9 +286,3 @@ TEST(LicenseLibraryTest, GET_SECRET_FILE) { std::cout << "Generate Secret File Success" << std::endl; } - -TEST(LicenseLibraryTest, CIN_TEST) { - int a; - std::cin >> a; - std::cout << 2 * a << std::endl; -} \ No newline at end of file From 35eeae6f080eb4b09edd0c82c48ad7c5233c823e Mon Sep 17 00:00:00 2001 From: "yangwei.yao" Date: Tue, 14 May 2019 20:13:24 +0800 Subject: [PATCH 7/9] license check is vaild Former-commit-id: 7381ad310d5b554c99f1f19f7fc460b33c36659f --- cpp/CMakeLists.txt | 4 + cpp/conf/server_config.yaml | 3 + cpp/src/CMakeLists.txt | 56 ++- cpp/src/license/BoostArchive.h | 133 ------- cpp/src/license/GetSysInfo.cpp | 58 ++- cpp/src/license/LicenseCheck.cpp | 135 ++++++- cpp/src/license/LicenseCheck.h | 36 ++ cpp/src/license/LicenseCreateTime.cpp | 71 ---- cpp/src/license/LicenseCreateuuidshafile.cpp | 33 -- cpp/src/license/LicenseFile.h | 32 +- cpp/src/license/LicenseGenerator.cpp | 136 ++++++- cpp/src/license/LicenseLibrary.cpp | 353 +++++++++-------- cpp/src/license/LicenseLibrary.h | 64 ++-- cpp/src/license/LicensePublic.cpp | 357 ------------------ cpp/src/license/LicensePublic.h | 88 ----- cpp/src/license/LicenseRun.cpp | 42 --- cpp/src/license/SecretFile.h | 72 ---- cpp/src/server/Server.cpp | 21 +- cpp/src/server/ServerConfig.h | 3 + cpp/unittest/license/CMakeLists.txt | 7 +- cpp/unittest/license/license_check_test.cpp | 68 ++++ .../license/license_library_tests.cpp | 171 +++++---- 22 files changed, 842 insertions(+), 1101 deletions(-) delete mode 100644 cpp/src/license/BoostArchive.h delete mode 100644 cpp/src/license/LicenseCreateTime.cpp delete mode 100644 cpp/src/license/LicenseCreateuuidshafile.cpp delete mode 100644 cpp/src/license/LicensePublic.cpp delete mode 100644 cpp/src/license/LicensePublic.h delete mode 100644 cpp/src/license/LicenseRun.cpp delete mode 100644 cpp/src/license/SecretFile.h create mode 100644 cpp/unittest/license/license_check_test.cpp diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index d249baf1c3..d5dbdcc05e 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -45,6 +45,10 @@ endif () if(CMAKE_BUILD_TYPE STREQUAL "Release") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIC -DELPP_THREAD_SAFE") + if (GPU_VERSION STREQUAL "ON") + set(ENABLE_LICENSE "ON") + add_definitions("-DENABLE_LICENSE") + endif () else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -fPIC -DELPP_THREAD_SAFE") endif() diff --git a/cpp/conf/server_config.yaml b/cpp/conf/server_config.yaml index d91161830b..cd66e0efb5 100644 --- a/cpp/conf/server_config.yaml +++ b/cpp/conf/server_config.yaml @@ -10,6 +10,9 @@ db_config: db_flush_interval: 5 #unit: second idmapper_max_open_file: 128 +license_config: + license_path: "/tmp/megasearch/abc.license" + log_config: global: format: "%datetime | %level | %logger | %msg" diff --git a/cpp/src/CMakeLists.txt b/cpp/src/CMakeLists.txt index 8fab8929cb..61918b562b 100644 --- a/cpp/src/CMakeLists.txt +++ b/cpp/src/CMakeLists.txt @@ -12,8 +12,13 @@ aux_source_directory(db db_files) aux_source_directory(wrapper wrapper_files) set(license_check_files - ${CMAKE_CURRENT_SOURCE_DIR}/license/LicenseLibrary.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/license/LicenseLibrary.h + license/LicenseLibrary.cpp + license/LicenseCheck.cpp + ) + +set(license_generator_src + license/LicenseGenerator.cpp + license/LicenseLibrary.cpp ) set(service_files @@ -29,11 +34,8 @@ set(vecwise_engine_src ${wrapper_files} ) -set(license_generator_src - ${CMAKE_CURRENT_SOURCE_DIR}/license/LicenseGenerator.cpp) - set(get_sys_info_src - ${CMAKE_CURRENT_SOURCE_DIR}/license/GetSysInfo.cpp) + license/GetSysInfo.cpp) include_directories(/usr/include) include_directories(/usr/local/cuda/include) @@ -60,13 +62,32 @@ else() libgfortran.a libquadmath.a libsqlite3.a - ) + ) endif () -cuda_add_library(vecwise_engine STATIC ${vecwise_engine_src}) +if (ENABLE_LICENSE STREQUAL "ON") + link_directories(/usr/local/cuda/lib64/stubs) + link_directories(/usr/local/cuda/lib64) + set(license_libs + nvidia-ml + libboost_system.a + libboost_filesystem.a + libboost_serialization.a + crypto + cudart + cublas + ) +endif () + +cuda_add_library(vecwise_engine STATIC ${vecwise_engine_src}) target_link_libraries(vecwise_engine ${engine_libs}) +if (ENABLE_LICENSE STREQUAL "ON") + add_library(vecwise_license STATIC ${license_check_files}) + target_link_libraries(vecwise_license ${license_libs}) +endif () + add_executable(vecwise_server ${config_files} ${server_files} @@ -91,12 +112,17 @@ set(server_libs dl ) -target_link_libraries(vecwise_server ${server_libs}) +if (ENABLE_LICENSE STREQUAL "ON") + target_link_libraries(vecwise_server ${server_libs} vecwise_license) +else () + target_link_libraries(vecwise_server ${server_libs}) +endif() -add_executable(license_generator ${license_generator_src}) -<<<<<<< b84d32553f910adf71e4ee069889e1d9fdd8a09f +if (ENABLE_LICENSE STREQUAL "ON") + add_executable(license_generator ${license_generator_src}) + add_executable(get_sys_info ${get_sys_info_src}) + target_link_libraries(get_sys_info ${license_libs} vecwise_license) + target_link_libraries(license_generator ${license_libs}) +endif () -install(TARGETS vecwise_server DESTINATION bin) -======= -add_executable(get_sys_info ${get_sys_info_src}) ->>>>>>> Refactor code +install(TARGETS vecwise_server DESTINATION bin) \ No newline at end of file diff --git a/cpp/src/license/BoostArchive.h b/cpp/src/license/BoostArchive.h deleted file mode 100644 index 4074cc0758..0000000000 --- a/cpp/src/license/BoostArchive.h +++ /dev/null @@ -1,133 +0,0 @@ -// -// Created by zilliz on 19-5-10. -// - -#ifndef VECWISE_ENGINE_BOOSTARCHIVE_H -#define VECWISE_ENGINE_BOOSTARCHIVE_H -#include -#include -#include -#include -#include - -using std::list; -using std::ifstream; -using std::ofstream; -using std::string; -using std::map; - -template -class BoostArchive -{ -public: - typedef T entity_type; - typedef boost::archive::binary_iarchive InputArchive; - typedef boost::archive::binary_oarchive OutputArchive; - - BoostArchive(const string & archive_file_path) - : _file_path_name(archive_file_path) - , _p_ofs(NULL) - , _p_output_archive(NULL) - , _entity_nums(0) - { - load_arvhive_info(); - } - ~BoostArchive() - { - close_output(); - } - //存储一个对象,序列化 - void store(const entity_type & entity); - - //反序列化, 提取所有对象 - bool restore(list & entitys); - - size_t size() const - { - return _entity_nums; - } - -private: - void save_archive_info() //保存已序列化的对象个数信息 - { - ofstream ofs; - ofs.open(get_archive_info_file_path(),std::ios::out | std::ios::trunc); - if (ofs.is_open()) - { - ofs << _entity_nums; - } - ofs.close(); - } - - void load_arvhive_info()//读取已序列化的对象个数信息 - { - ifstream ifs; - ifs.open(get_archive_info_file_path(),std::ios_base::in); - if (ifs.is_open() && !ifs.eof()) - { - int enity_num = 0; - ifs >> enity_num; - _entity_nums = enity_num; - } - ifs.close(); - } - - string get_archive_info_file_path() - { - return "/tmp/vecwise_engine.meta"; - } - - void close_output() - { - if (NULL != _p_output_archive) - { - delete _p_output_archive; - _p_output_archive = NULL; - save_archive_info(); - } - if (NULL != _p_ofs) - { - delete _p_ofs; - _p_ofs = NULL; - } - } - -private: - size_t _entity_nums; - string _file_path_name; - ofstream * _p_ofs; - OutputArchive * _p_output_archive; -}; - -template -bool BoostArchive::restore( list & entitys ) -{ - close_output(); - load_arvhive_info(); - ifstream ifs(_file_path_name); - if (ifs) - { - InputArchive ia(ifs); - for (size_t cnt = 0; cnt < _entity_nums; ++cnt) - { - entity_type entity; - ia & entity; - entitys.push_back(entity); - } - return true; - } - return false; -} - -template -void BoostArchive::store( const entity_type & entity ) -{ - if (NULL == _p_output_archive) - { - _p_ofs = new ofstream(_file_path_name); - _p_output_archive = new OutputArchive(*_p_ofs); - } - (*_p_output_archive) & entity; - ++_entity_nums; -} -#endif //VECWISE_ENGINE_BOOSTARCHIVE_H diff --git a/cpp/src/license/GetSysInfo.cpp b/cpp/src/license/GetSysInfo.cpp index added44360..4ddf671485 100644 --- a/cpp/src/license/GetSysInfo.cpp +++ b/cpp/src/license/GetSysInfo.cpp @@ -1,51 +1,83 @@ + +#include "utils/Log.h" +#include "LicenseLibrary.h" +#include "utils/Error.h" + #include #include #include - // Not provide path: current work path will be used and system.info. +using namespace zilliz::vecwise; void print_usage(const std::string &app_name) { printf("\n Usage: %s [OPTIONS]\n\n", app_name.c_str()); printf(" Options:\n"); printf(" -h --help Print this help\n"); - printf(" -d --sysinfo filename Generate system info file as given name\n"); + printf(" -s --sysinfo filename Generate system info file as given name\n"); printf("\n"); } -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { std::string app_name = argv[0]; - if(argc != 1 && argc != 3) { + if (argc != 1 && argc != 3) { print_usage(app_name); return EXIT_FAILURE; } - static struct option long_options[] = {{"system_info", required_argument, 0, 'd'}, + static struct option long_options[] = {{"system_info", required_argument, 0, 's'}, {"help", no_argument, 0, 'h'}, {NULL, 0, 0, 0}}; int value = 0; int option_index = 0; std::string system_info_filename = "./system.info"; - while ((value = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) { + while ((value = getopt_long(argc, argv, "s:h", long_options, &option_index)) != -1) { switch (value) { - case 'd': { + case 's': { char *system_info_filename_ptr = strdup(optarg); system_info_filename = system_info_filename_ptr; free(system_info_filename_ptr); // printf("Generate system info file: %s\n", system_info_filename.c_str()); break; } - case 'h': - print_usage(app_name); + case 'h':print_usage(app_name); return EXIT_SUCCESS; - case '?': - print_usage(app_name); + case '?':print_usage(app_name); return EXIT_FAILURE; - default: - print_usage(app_name); + default:print_usage(app_name); break; } } + int device_count = 0; + server::ServerError err = server::LicenseLibrary::GetDeviceCount(device_count); + if (err != server::SERVER_SUCCESS) return -1; + + // 2. Get All GPU UUID + std::vector uuid_array; + err = server::LicenseLibrary::GetUUID(device_count, uuid_array); + if (err != server::SERVER_SUCCESS) return -1; + + // 3. Get UUID SHA256 + std::vector uuid_sha256_array; + err = server::LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, uuid_sha256_array); + if (err != server::SERVER_SUCCESS) return -1; + + // 4. Generate GPU ID map with GPU UUID + std::map uuid_encrption_map; + for (int i = 0; i < device_count; ++i) { + uuid_encrption_map[i] = uuid_sha256_array[i]; + } + + + // 6. Generate GPU_info File + err = server::LicenseLibrary::GPUinfoFileSerialization(system_info_filename, + device_count, + uuid_encrption_map); + if (err != server::SERVER_SUCCESS) return -1; + + printf("Generate GPU_info File Success\n"); + + return 0; } \ No newline at end of file diff --git a/cpp/src/license/LicenseCheck.cpp b/cpp/src/license/LicenseCheck.cpp index 1566f4e951..7c3e4a30de 100644 --- a/cpp/src/license/LicenseCheck.cpp +++ b/cpp/src/license/LicenseCheck.cpp @@ -1,28 +1,137 @@ #include "LicenseCheck.h" +#include +#include +#include +//#include +//#include +#include +#include +#include + namespace zilliz { namespace vecwise { namespace server { -class LicenseCheck { - public: - static LicenseCheck& - GetInstance() { - static LicenseCheck instance; - return instance; + +//ServerError +//LicenseCheck::IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path) { +// +// std::string file_md5; +// LicenseLibrary::GetFileMD5(license_file_path, file_md5); +// time_t update_time; +// off_t file_size; +// LicenseLibrary::GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); +// time_t system_time; +// LicenseLibrary::GetSystemTime(system_time); +// +// std::string output_file_md5; +// time_t output_update_time; +// off_t output_file_size; +// time_t output_starting_time; +// time_t output_end_time; +// LicenseLibrary::SecretFileDeserialization(secret_file_path, +// output_update_time, +// output_file_size, +// output_starting_time, +// output_end_time, +// output_file_md5); +// if (file_md5 != output_file_md5) { +// printf("License file has been modified\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// if (update_time != output_update_time) { +// printf("update_time is wrong\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// if (file_size != output_file_size) { +// printf("file_size is wrong\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// if (system_time < output_starting_time || system_time > output_end_time) { +// printf("License expired\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// return SERVER_SUCCESS; +//} + +// Part 1: Legality check + +ServerError +LicenseCheck::LegalityCheck(const std::string &license_file_path) { + + int device_count; + LicenseLibrary::GetDeviceCount(device_count); + std::vector uuid_array; + LicenseLibrary::GetUUID(device_count, uuid_array); + + std::vector sha_array; + LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, sha_array); + + int output_device_count; + std::map uuid_encryption_map; + time_t starting_time; + time_t end_time; + ServerError err = LicenseLibrary::LicenseFileDeserialization(license_file_path, + output_device_count, + uuid_encryption_map, + starting_time, + end_time); + if(err !=SERVER_SUCCESS) + { + printf("License check error: 01\n"); + return SERVER_UNEXPECTED_ERROR; } + time_t system_time; + LicenseLibrary::GetSystemTime(system_time); - ServerError - Check(); + if (device_count != output_device_count) { + printf("License check error: 02\n"); + return SERVER_UNEXPECTED_ERROR; + } + for (int i = 0; i < device_count; ++i) { + if (sha_array[i] != uuid_encryption_map[i]) { + printf("License check error: 03\n"); + return SERVER_UNEXPECTED_ERROR; + } + } + if (system_time < starting_time || system_time > end_time) { + printf("License check error: 04\n"); + return SERVER_UNEXPECTED_ERROR; + } + printf("Legality Check Success\n"); + return SERVER_SUCCESS; +} - ServerError - PeriodicUpdate(); +// Part 2: Timing check license - private: - LicenseCheck() {}; +ServerError +LicenseCheck::AlterFile(const std::string &license_file_path, + const boost::system::error_code &ec, + boost::asio::deadline_timer *pt) { + ServerError err = LegalityCheck(license_file_path); + if(err!=SERVER_SUCCESS) + { + exit(1); + } + printf("---runing---\n"); + pt->expires_at(pt->expires_at() + boost::posix_time::hours(1)); + pt->async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, pt)); + return SERVER_SUCCESS; -}; +} + +ServerError +LicenseCheck::StartCountingDown(const std::string &license_file_path) { + + if (!LicenseLibrary::IsFileExistent(license_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; + boost::asio::io_service io; + boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); + t.async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, &t)); + io.run(); + return SERVER_SUCCESS; +} } } diff --git a/cpp/src/license/LicenseCheck.h b/cpp/src/license/LicenseCheck.h index 4078a45e4c..e3c5b3ac32 100644 --- a/cpp/src/license/LicenseCheck.h +++ b/cpp/src/license/LicenseCheck.h @@ -1,11 +1,47 @@ #pragma once #include "utils/Error.h" +#include "LicenseLibrary.h" + +#include +#include +#include + namespace zilliz { namespace vecwise { namespace server { +class LicenseCheck { + public: + static LicenseCheck & + GetInstance() { + static LicenseCheck instance; + return instance; + }; + +// static ServerError +// IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path); + + // Part 1: Legality check + static ServerError + LegalityCheck(const std::string &license_file_path); + + + // Part 2: Timing check license + static ServerError + AlterFile(const std::string &license_file_path, + const boost::system::error_code &ec, + boost::asio::deadline_timer *pt); + + + static ServerError + StartCountingDown(const std::string &license_file_path); + + private: + + +}; } } diff --git a/cpp/src/license/LicenseCreateTime.cpp b/cpp/src/license/LicenseCreateTime.cpp deleted file mode 100644 index f14d6bb7a5..0000000000 --- a/cpp/src/license/LicenseCreateTime.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// -// Created by zilliz on 19-5-11. -// - -#include -#include -#include - -#include "utils/Error.h" -#include "license/LicenseCheck.h" -#include "license/LicensePublic.h" - -using namespace zilliz::vecwise; - - - -//TEST(LicenseTest, LICENSE_TEST) { -// -// std::string path1 = "/tmp/vecwise_engine.sha"; -// std::string path2 = "/tmp/vecwise_engine.license"; -// std::string path3 = "/tmp/vecwise_engine2.license"; -// -// std::cout << "This is create licenseTime " << std::endl; -// -// server::ServerError err; -// int deviceCount=0; -// -// std::vector shas; -// err = server::LicenseLoad(path1,deviceCount,shas); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// std::map uuidEncryption; -// std::cout<< "deviceCount : " << deviceCount << std::endl; -// for(int i=0;i> RemainingTime ; -// -// err = server::LiSave(path2,deviceCount,uuidEncryption,RemainingTime); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -//// int64_t RemainingTimecheck; -//// std::map uuidEncryptioncheck; -//// int deviceCountcheck; -//// err = server::LiLoad(path2,deviceCountcheck,uuidEncryptioncheck,RemainingTimecheck); -//// -//// printf("\n deviceCountcheck = %d\n",deviceCountcheck); -//// std::cout<< "RemainingTimecheck: " << RemainingTimecheck<< std::endl; -// -// time_t update_time; -// off_t file_size; -// std::string filemd5; -// -// server::LicenseGetfiletime(path2,update_time); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// server::LicenseGetfilesize(path2,file_size); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// server::LicenseGetfilemd5(path2,filemd5); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// -// err = server::LifileSave(path3,update_time,file_size,filemd5); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// std::cout<< "success" << std::endl; -// -//} diff --git a/cpp/src/license/LicenseCreateuuidshafile.cpp b/cpp/src/license/LicenseCreateuuidshafile.cpp deleted file mode 100644 index 9476c89bd7..0000000000 --- a/cpp/src/license/LicenseCreateuuidshafile.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by zilliz on 19-5-11. -// -#include -#include - -#include "utils/Error.h" -#include "license/LicenseCheck.h" - - -using namespace zilliz::vecwise; - -// -//TEST(LicenseTest, LICENSE_TEST) { -// -// std::string path1 = "/tmp/vecwise_engine.sha"; -// std::cout << "This is create uuidshafile " << std::endl; -// -// server::ServerError err; -// int deviceCount=0; -// std::vector uuids; -// -// err = server::LicenseGetuuid(deviceCount,uuids); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// std::vector shas; -// err = server::LicenseGetuuidsha(deviceCount,uuids,shas); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// err = server::LicenseSave(path1,deviceCount,shas); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -//} \ No newline at end of file diff --git a/cpp/src/license/LicenseFile.h b/cpp/src/license/LicenseFile.h index 322cea8b85..bf076b0d68 100644 --- a/cpp/src/license/LicenseFile.h +++ b/cpp/src/license/LicenseFile.h @@ -10,12 +10,19 @@ #include #include + class LicenseFile { public: LicenseFile() = default; - LicenseFile(const int &device_count, const std::map &uuid_encryption_map, const int64_t &remaining_hour) - : device_count_(device_count), uuid_encryption_map_(uuid_encryption_map), remaining_hour_(remaining_hour) {} + LicenseFile(const int &device_count, + const std::map &uuid_encryption_map, + const time_t &starting_time, + const time_t &end_time) + : device_count_(device_count), + uuid_encryption_map_(uuid_encryption_map), + starting_time_(starting_time), + end_time_(end_time) {} int get_device_count() { return device_count_; @@ -23,8 +30,11 @@ class LicenseFile { std::map &get_uuid_encryption_map() { return uuid_encryption_map_; } - int64_t get_remaining_hour() { - return remaining_hour_; + time_t get_starting_time() { + return starting_time_; + } + time_t get_end_time() { + return end_time_; } public: @@ -34,30 +44,32 @@ class LicenseFile { void serialize(Archive &ar, const unsigned int version) { ar & device_count_; ar & uuid_encryption_map_; - ar & remaining_hour_; + ar & starting_time_; + ar & end_time_; } public: int device_count_ = 0; std::map uuid_encryption_map_; - int64_t remaining_hour_ = 0; + time_t starting_time_ = 0; + time_t end_time_ = 0; }; class SerializedLicenseFile { public: ~SerializedLicenseFile() { - if(license_file_ != nullptr) { - delete(license_file_); + if (license_file_ != nullptr) { + delete (license_file_); license_file_ = nullptr; } } void - set_license_file(LicenseFile* license_file) { + set_license_file(LicenseFile *license_file) { license_file_ = license_file; } - LicenseFile* get_license_file() { + LicenseFile *get_license_file() { return license_file_; } private: diff --git a/cpp/src/license/LicenseGenerator.cpp b/cpp/src/license/LicenseGenerator.cpp index 278c132962..2160c916ad 100644 --- a/cpp/src/license/LicenseGenerator.cpp +++ b/cpp/src/license/LicenseGenerator.cpp @@ -1,6 +1,136 @@ + #include -int main() { - std::cout << "This is license generator" << std::endl; +#include +#include + +#include "utils/Log.h" +#include "license/LicenseLibrary.h" +#include "utils/Error.h" + + +using namespace zilliz::vecwise; +// Not provide path: current work path will be used and system.info. + +void +print_usage(const std::string &app_name) { + printf("\n Usage: %s [OPTIONS]\n\n", app_name.c_str()); + printf(" Options:\n"); + printf(" -h --help Print this help\n"); + printf(" -s --sysinfo filename sysinfo file location\n"); + printf(" -l --license filename Generate license file as given name\n"); + printf(" -b --starting time Set start time Similar to year-month-day\n"); + printf(" -e --end time Set end time Similar to year-month-day \n"); + printf("\n"); +} + +int main(int argc, char *argv[]) { + std::string app_name = argv[0]; +// if (argc != 1 && argc != 3) { +// print_usage(app_name); +// return EXIT_FAILURE; +// } + static struct option long_options[] = {{"system_info", required_argument, 0, 's'}, + {"license", optional_argument, 0, 'l'}, + {"help", no_argument, 0, 'h'}, + {"starting_time", required_argument, 0, 'b'}, + {"end_time", required_argument, 0, 'e'}, + {NULL, 0, 0, 0}}; + server::ServerError err; + int value = 0; + int option_index = 0; + std::string system_info_filename = "./system.info"; + std::string license_filename = "./system.license"; + char *string_starting_time = NULL; + char *string_end_time = NULL; + time_t starting_time = 0; + time_t end_time = 0; + int flag_s = 1; + int flag_b = 1; + int flag_e = 1; + while ((value = getopt_long(argc, argv, "hl:s:b:e:", long_options, NULL)) != -1) { + switch (value) { + case 's': { + flag_s = 0; + system_info_filename = (std::string) (optarg); +// char *system_info_filename_ptr = strdup(optarg); +// system_info_filename = system_info_filename_ptr; +// free(system_info_filename_ptr); +// printf("Generate system info file: %s\n", system_info_filename.c_str()); + break; + } + case 'b': { + flag_b = 0; + string_starting_time = optarg; +// char *time = strdup(optarg); +// err = server::LicenseLibrary::GetDateTime(time, starting_time); +// if (err != server::SERVER_SUCCESS) return -1; +// free(time); + break; + } + case 'e': { + flag_e = 0; + string_end_time = optarg; +// char *time = strdup(optarg); +// err = server::LicenseLibrary::GetDateTime(time, end_time); +// if (err != server::SERVER_SUCCESS) return -1; +// free(time); + break; + } + case 'l': { + license_filename = (std::string) (optarg); +// char *system_info_filename_ptr = strdup(optarg); +// license_filename = system_info_filename_ptr; +// free(system_info_filename_ptr); + break; + } + case 'h':print_usage(app_name); + return EXIT_SUCCESS; + case '?':print_usage(app_name); + return EXIT_FAILURE; + default:print_usage(app_name); + break; + } + + } + if (flag_s) { + printf("Error: sysinfo file location must be entered\n"); + return 1; + } + if (flag_b) { + printf("Error: start time must be entered\n"); + return 1; + } + if (flag_e) { + printf("Error: end time must be entered\n"); + return 1; + } + + err = server::LicenseLibrary::GetDateTime(string_starting_time, starting_time); + if (err != server::SERVER_SUCCESS) return -1; + + err = server::LicenseLibrary::GetDateTime(string_end_time, end_time); + if (err != server::SERVER_SUCCESS) return -1; + + + int output_info_device_count = 0; + std::map output_info_uuid_encrption_map; + + + err = server::LicenseLibrary::GPUinfoFileDeserialization(system_info_filename, + output_info_device_count, + output_info_uuid_encrption_map); + if (err != server::SERVER_SUCCESS) return -1; + + + err = server::LicenseLibrary::LicenseFileSerialization(license_filename, + output_info_device_count, + output_info_uuid_encrption_map, + starting_time, + end_time); + if (err != server::SERVER_SUCCESS) return -1; + + + printf("Generate License File Success\n"); return 0; -} \ No newline at end of file +} diff --git a/cpp/src/license/LicenseLibrary.cpp b/cpp/src/license/LicenseLibrary.cpp index 237f1679b8..f891ed113d 100644 --- a/cpp/src/license/LicenseLibrary.cpp +++ b/cpp/src/license/LicenseLibrary.cpp @@ -27,7 +27,7 @@ namespace server { constexpr int LicenseLibrary::sha256_length_; bool -LicenseLibrary::IsFileExistent(const std::string& path) { +LicenseLibrary::IsFileExistent(const std::string &path) { boost::system::error_code error; auto file_status = boost::filesystem::status(path, error); @@ -133,12 +133,19 @@ LicenseLibrary::GetUUIDSHA256(const int &device_count, return SERVER_SUCCESS; } +ServerError +LicenseLibrary::GetSystemTime(time_t &system_time) { + system_time = time(NULL); + return SERVER_SUCCESS; +} + // Part 2: Handle License File ServerError LicenseLibrary::LicenseFileSerialization(const std::string &path, int device_count, const std::map &uuid_encrption_map, - int64_t remaining_hour) { + time_t starting_time, + time_t end_time) { std::ofstream file(path); boost::archive::binary_oarchive oa(file); @@ -146,7 +153,10 @@ LicenseLibrary::LicenseFileSerialization(const std::string &path, SerializedLicenseFile serialized_license_file; - serialized_license_file.set_license_file(new LicenseFile(device_count, uuid_encrption_map, remaining_hour)); + serialized_license_file.set_license_file(new LicenseFile(device_count, + uuid_encrption_map, + starting_time, + end_time)); oa << serialized_license_file; file.close(); @@ -157,9 +167,9 @@ ServerError LicenseLibrary::LicenseFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map, - int64_t &remaining_hour) { - if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; - + time_t &starting_time, + time_t &end_time) { + if (!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -169,55 +179,62 @@ LicenseLibrary::LicenseFileDeserialization(const std::string &path, device_count = serialized_license_file.get_license_file()->get_device_count(); uuid_encrption_map = serialized_license_file.get_license_file()->get_uuid_encryption_map(); - remaining_hour = serialized_license_file.get_license_file()->get_remaining_hour(); + starting_time = serialized_license_file.get_license_file()->get_starting_time(); + end_time = serialized_license_file.get_license_file()->get_end_time(); file.close(); return SERVER_SUCCESS; } -ServerError -LicenseLibrary::SecretFileSerialization(const std::string &path, - const time_t &update_time, - const off_t &file_size, - const std::string &file_md5) { - std::ofstream file(path); - boost::archive::binary_oarchive oa(file); - oa.register_type(); - - SerializedSecretFile serialized_secret_file; - - serialized_secret_file.set_secret_file(new SecretFile(update_time, file_size, file_md5)); - oa << serialized_secret_file; - - file.close(); - return SERVER_SUCCESS; -} - -ServerError -LicenseLibrary::SecretFileDeserialization(const std::string &path, - time_t &update_time, - off_t &file_size, - std::string &file_md5) { - if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; - - std::ifstream file(path); - boost::archive::binary_iarchive ia(file); - ia.register_type(); - SerializedSecretFile serialized_secret_file; - - ia >> serialized_secret_file; - update_time = serialized_secret_file.get_secret_file()->get_update_time(); - file_size = serialized_secret_file.get_secret_file()->get_file_size(); - file_md5 = serialized_secret_file.get_secret_file()->get_file_md5(); - file.close(); - return SERVER_SUCCESS; -} +//ServerError +//LicenseLibrary::SecretFileSerialization(const std::string &path, +// const time_t &update_time, +// const off_t &file_size, +// const time_t &starting_time, +// const time_t &end_time, +// const std::string &file_md5) { +// std::ofstream file(path); +// boost::archive::binary_oarchive oa(file); +// oa.register_type(); +// +// SerializedSecretFile serialized_secret_file; +// +// serialized_secret_file.set_secret_file(new SecretFile(update_time, file_size, starting_time, end_time, file_md5)); +// oa << serialized_secret_file; +// +// file.close(); +// return SERVER_SUCCESS; +//} +// +//ServerError +//LicenseLibrary::SecretFileDeserialization(const std::string &path, +// time_t &update_time, +// off_t &file_size, +// time_t &starting_time, +// time_t &end_time, +// std::string &file_md5) { +// if (!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; +// +// std::ifstream file(path); +// boost::archive::binary_iarchive ia(file); +// ia.register_type(); +// SerializedSecretFile serialized_secret_file; +// +// ia >> serialized_secret_file; +// update_time = serialized_secret_file.get_secret_file()->get_update_time(); +// file_size = serialized_secret_file.get_secret_file()->get_file_size(); +// starting_time = serialized_secret_file.get_secret_file()->get_starting_time(); +// end_time = serialized_secret_file.get_secret_file()->get_end_time(); +// file_md5 = serialized_secret_file.get_secret_file()->get_file_md5(); +// file.close(); +// return SERVER_SUCCESS; +//} // Part 3: File attribute: UpdateTime Time/ Size/ MD5 ServerError LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update_time, off_t &file_size) { - if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + if (!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; struct stat buf; int err_no = stat(path.c_str(), &buf); @@ -235,7 +252,7 @@ LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update ServerError LicenseLibrary::GetFileMD5(const std::string &path, std::string &filemd5) { - if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + if (!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; filemd5.clear(); @@ -287,7 +304,7 @@ ServerError LicenseLibrary::GPUinfoFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map) { - if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + if (!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; std::ifstream file(path); boost::archive::binary_iarchive ia(file); @@ -303,115 +320,143 @@ LicenseLibrary::GPUinfoFileDeserialization(const std::string &path, return SERVER_SUCCESS; } +ServerError +LicenseLibrary::GetDateTime(char *cha, time_t &data_time) { + tm tm_; + int year, month, day; + sscanf(cha, "%d-%d-%d", &year, &month, &day); + tm_.tm_year = year - 1900; + tm_.tm_mon = month - 1; + tm_.tm_mday = day; + tm_.tm_hour = 0; + tm_.tm_min = 0; + tm_.tm_sec = 0; + tm_.tm_isdst = 0; + data_time = mktime(&tm_); + return SERVER_SUCCESS; + +} // Part 5: Integrity check and Legality check -ServerError -LicenseLibrary::IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path) { +//ServerError +//LicenseLibrary::IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path) { +// +// std::string file_md5; +// GetFileMD5(license_file_path, file_md5); +// time_t update_time; +// off_t file_size; +// GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); +// time_t system_time; +// GetSystemTime(system_time); +// +// std::string output_file_md5; +// time_t output_update_time; +// off_t output_file_size; +// time_t output_starting_time; +// time_t output_end_time; +// SecretFileDeserialization(secret_file_path, +// output_update_time, +// output_file_size, +// output_starting_time, +// output_end_time, +// output_file_md5); +// if (file_md5 != output_file_md5) { +// printf("License file has been modified\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// if (update_time != output_update_time) { +// printf("update_time is wrong\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// if (file_size != output_file_size) { +// printf("file_size is wrong\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// if (system_time < output_starting_time || system_time > output_end_time) { +// printf("License expired\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// return SERVER_SUCCESS; +//} +// +//ServerError +//LicenseLibrary::LegalityCheck(const std::string &license_file_path) { +// +// int device_count; +// GetDeviceCount(device_count); +// std::vector uuid_array; +// GetUUID(device_count, uuid_array); +// +// std::vector sha_array; +// GetUUIDSHA256(device_count, uuid_array, sha_array); +// +// int output_device_count; +// std::map uuid_encryption_map; +// int64_t remaining_time; +// LicenseFileDeserialization(license_file_path, output_device_count, uuid_encryption_map, remaining_time); +// +// if (device_count != output_device_count) { +// printf("device_count is wrong\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// for (int i = 0; i < device_count; ++i) { +// if (sha_array[i] != uuid_encryption_map[i]) { +// printf("uuid_encryption_map %d is wrong\n", i); +// return SERVER_UNEXPECTED_ERROR; +// } +// } +// if (remaining_time <= 0) { +// printf("License expired\n"); +// return SERVER_UNEXPECTED_ERROR; +// } +// std::cout << "Legality Check Success" << std::endl; +// return SERVER_SUCCESS; +//} - std::string file_md5; - GetFileMD5(license_file_path, file_md5); - time_t update_time; - off_t file_size; - GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); - - std::string output_file_md5; - time_t output_update_time; - off_t output_file_size; - SecretFileDeserialization(secret_file_path, output_update_time, output_file_size, output_file_md5); - if (file_md5 != output_file_md5) { - printf("License file has been modified\n"); - return SERVER_UNEXPECTED_ERROR; - } - if (update_time != output_update_time) { - printf("update_time is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - if (file_size != output_file_size) { - printf("file_size is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - - return SERVER_SUCCESS; -} - -ServerError -LicenseLibrary::LegalityCheck(const std::string &license_file_path) { - - int device_count; - GetDeviceCount(device_count); - std::vector uuid_array; - GetUUID(device_count, uuid_array); - - std::vector sha_array; - GetUUIDSHA256(device_count, uuid_array, sha_array); - - int output_device_count; - std::map uuid_encryption_map; - int64_t remaining_time; - LicenseFileDeserialization(license_file_path, output_device_count, uuid_encryption_map, remaining_time); - - if (device_count != output_device_count) { - printf("device_count is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - for (int i = 0; i < device_count; ++i) { - if (sha_array[i] != uuid_encryption_map[i]) { - printf("uuid_encryption_map %d is wrong\n", i); - return SERVER_UNEXPECTED_ERROR; - } - } - if (remaining_time <= 0) { - printf("License expired\n"); - return SERVER_UNEXPECTED_ERROR; - } - std::cout << "Legality Check Success" << std::endl; - return SERVER_SUCCESS; -} - -// Part 6: Timer -ServerError -LicenseLibrary::AlterFile(const std::string &license_file_path, - const std::string &secret_file_path, - const boost::system::error_code &ec, - boost::asio::deadline_timer *pt) { - int device_count; - std::map uuid_encryption_map; - int64_t remaining_time; - LicenseFileDeserialization(license_file_path, device_count, uuid_encryption_map, remaining_time); - - std::cout << "remaining_time: " << remaining_time << std::endl; - - if (remaining_time <= 0) { - std::cout << "License expired" << std::endl; - exit(1); - } - --remaining_time; - LicenseFileSerialization(license_file_path, device_count, uuid_encryption_map, remaining_time); - - time_t update_time; - off_t file_size; - GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); - std::string file_md5; - GetFileMD5(license_file_path, file_md5); - SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); - - pt->expires_at(pt->expires_at() + boost::posix_time::hours(1)); - pt->async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, pt)); - return SERVER_SUCCESS; -} - -ServerError -LicenseLibrary::StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path) { - - if(!IsFileExistent(license_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; - if(!IsFileExistent(secret_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; - - boost::asio::io_service io; - boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); - t.async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, &t)); - std::cout << "Timing begins" << std::endl; - io.run(); - return SERVER_SUCCESS; -} +//// Part 6: Timer +//ServerError +//LicenseLibrary::AlterFile(const std::string &license_file_path, +// const std::string &secret_file_path, +// const boost::system::error_code &ec, +// boost::asio::deadline_timer *pt) { +// int device_count; +// std::map uuid_encryption_map; +// int64_t remaining_time; +// LicenseFileDeserialization(license_file_path, device_count, uuid_encryption_map, remaining_time); +// +// std::cout << "remaining_time: " << remaining_time << std::endl; +// +// if (remaining_time <= 0) { +// std::cout << "License expired" << std::endl; +// exit(1); +// } +// --remaining_time; +// LicenseFileSerialization(license_file_path, device_count, uuid_encryption_map, remaining_time); +// +// time_t update_time; +// off_t file_size; +// GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); +// std::string file_md5; +// GetFileMD5(license_file_path, file_md5); +// SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); +// +// pt->expires_at(pt->expires_at() + boost::posix_time::hours(1)); +// pt->async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, pt)); +// return SERVER_SUCCESS; +//} +// +//ServerError +//LicenseLibrary::StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path) { +// +// if (!IsFileExistent(license_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; +// if (!IsFileExistent(secret_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; +// +// boost::asio::io_service io; +// boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); +// t.async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, &t)); +// std::cout << "Timing begins" << std::endl; +// io.run(); +// return SERVER_SUCCESS; +//} } } diff --git a/cpp/src/license/LicenseLibrary.h b/cpp/src/license/LicenseLibrary.h index dd78c221ad..3fd27a41c6 100644 --- a/cpp/src/license/LicenseLibrary.h +++ b/cpp/src/license/LicenseLibrary.h @@ -1,7 +1,6 @@ #pragma once #include "LicenseFile.h" -#include "SecretFile.h" #include "GPUInfoFile.h" #include "utils/Error.h" @@ -12,6 +11,7 @@ #include #include +#include namespace zilliz { @@ -22,7 +22,7 @@ class LicenseLibrary { public: // Part 0: File check static bool - IsFileExistent(const std::string& path); + IsFileExistent(const std::string &path); // Part 1: Get GPU Info static ServerError @@ -40,26 +40,39 @@ class LicenseLibrary { std::vector &uuid_array, std::vector &sha_array); + static ServerError + GetSystemTime(time_t &system_time); + // Part 2: Handle License File static ServerError LicenseFileSerialization(const std::string &path, int device_count, - const std::map &uuid_encrption_map, int64_t remaining_hour); + const std::map &uuid_encrption_map, + time_t starting_time, + time_t end_time); static ServerError LicenseFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map, - int64_t &remaining_hour); + time_t &starting_time, + time_t &end_time); - static ServerError - SecretFileSerialization(const std::string &path, - const time_t &update_time, - const off_t &file_size, - const std::string &file_md5); - - static ServerError - SecretFileDeserialization(const std::string &path, time_t &update_time, off_t &file_size, std::string &file_md5); +// static ServerError +// SecretFileSerialization(const std::string &path, +// const time_t &update_time, +// const off_t &file_size, +// const time_t &starting_time, +// const time_t &end_time, +// const std::string &file_md5); +// +// static ServerError +// SecretFileDeserialization(const std::string &path, +// time_t &update_time, +// off_t &file_size, +// time_t &starting_time, +// time_t &end_time, +// std::string &file_md5); // Part 3: File attribute: UpdateTime Time/ Size/ MD5 static ServerError @@ -78,22 +91,25 @@ class LicenseLibrary { int &device_count, std::map &uuid_encrption_map); - // Part 5: Integrity check and Legality check static ServerError - IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path); + GetDateTime(char *cha, time_t &data_time); - static ServerError - LegalityCheck(const std::string &license_file_path); + // Part 5: Integrity check and Legality check +// static ServerError +// IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path); +// +// static ServerError +// LegalityCheck(const std::string &license_file_path); // Part 6: Timer - static ServerError - AlterFile(const std::string &license_file_path, - const std::string &secret_file_path, - const boost::system::error_code &ec, - boost::asio::deadline_timer *pt); - - static ServerError - StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path); +// static ServerError +// AlterFile(const std::string &license_file_path, +// const std::string &secret_file_path, +// const boost::system::error_code &ec, +// boost::asio::deadline_timer *pt); +// +// static ServerError +// StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path); private: diff --git a/cpp/src/license/LicensePublic.cpp b/cpp/src/license/LicensePublic.cpp deleted file mode 100644 index 186c502ac1..0000000000 --- a/cpp/src/license/LicensePublic.cpp +++ /dev/null @@ -1,357 +0,0 @@ -// -// Created by zilliz on 19-5-10. -// - -#include "LicensePublic.h" -#include "license/LicenseCheck.h" -//#include "BoostArchive.h" -#define RTime 100 - -using std::string; -using std::map; -namespace zilliz { -namespace vecwise { -namespace server { - -// GET /tmp/vecwise_engine.license -class Licensedata1 -{ -public: - Licensedata1() - :_deviceCount(0) - ,_RemainingTime(RTime) - {} - - Licensedata1(const int& deviceCount,const map& uuidEncryption) - :_deviceCount(deviceCount) - ,_uuidEncryption(uuidEncryption) - ,_RemainingTime(RTime) - {} - - Licensedata1(const int& deviceCount,const map& uuidEncryption,const int64_t& RemainingTime) - :_deviceCount(deviceCount) - ,_uuidEncryption(uuidEncryption) - ,_RemainingTime(RemainingTime) - {} - - int GetdeviceCount() - { - return _deviceCount; - } - map GetuuidEncryption() - { - return _uuidEncryption; - } - int64_t GetRemainingTime() - { - return _RemainingTime; - } - -private: - friend class boost::serialization::access; - template - void serialize(Archive &ar, const unsigned int version) - { - ar & _deviceCount; - ar & _uuidEncryption; - ar & _RemainingTime; - } - -public: - int _deviceCount; - map _uuidEncryption; - int64_t _RemainingTime; -}; - - -class STLlicensedata -{ -private: - friend class boost::serialization::access; - template - void serialize(Archive& ar, const unsigned int version) - { - ar & m_licensedata; - } - -public: - Licensedata1* m_licensedata; -}; - - -ServerError -LiSave(const string& path,const int& deviceCount,const map& uuidEncryption) -{ - std::ofstream file(path); - boost::archive::binary_oarchive oa(file); - oa.register_type(); - STLlicensedata stllicensedata; - - Licensedata1 *p = new Licensedata1(deviceCount,uuidEncryption); - stllicensedata.m_licensedata = p; - oa << stllicensedata; - - delete p; - file.close(); - return SERVER_SUCCESS; -} - -ServerError -LiSave(const string& path,const int& deviceCount,const map& uuidEncryption, const int64_t& RemainingTime) -{ - std::ofstream file(path); - boost::archive::binary_oarchive oa(file); - oa.register_type(); - STLlicensedata stllicensedata; - - Licensedata1 *p = new Licensedata1(deviceCount,uuidEncryption,RemainingTime); - stllicensedata.m_licensedata = p; - oa << stllicensedata; - - delete p; - file.close(); - return SERVER_SUCCESS; -} - -ServerError -LiLoad(const string& path,int& deviceCount,map& uuidEncryption,int64_t& RemainingTime) -{ - std::ifstream file(path); - boost::archive::binary_iarchive ia(file); - ia.register_type(); - STLlicensedata stllicensedata; - ia >> stllicensedata; - deviceCount = stllicensedata.m_licensedata->GetdeviceCount(); - uuidEncryption = stllicensedata.m_licensedata->GetuuidEncryption(); - RemainingTime = stllicensedata.m_licensedata->GetRemainingTime(); - file.close(); - return SERVER_SUCCESS; -} - - -// GET /tmp/vecwise_engine2.license - -class Licensefiledata -{ -public: - Licensefiledata() - :_update_time(0) - ,_file_size(0) - ,_filemd5("") - {} - - Licensefiledata(const time_t& update_time,const off_t& file_size,const string& filemd5) - :_update_time(update_time) - ,_file_size(file_size) - ,_filemd5(filemd5) - {} - - time_t Getupdate_time() - { - return _update_time; - } - off_t Getfile_size() - { - return _file_size; - } - string Getfilemd5() - { - return _filemd5; - } - -private: - friend class boost::serialization::access; - template - void serialize(Archive &ar, const unsigned int version) - { - ar & _update_time; - ar & _file_size; - ar & _filemd5; - } - -public: - time_t _update_time; - off_t _file_size; - string _filemd5; -}; - - -class STLlicensefiledata -{ -private: - friend class boost::serialization::access; - template - void serialize(Archive& ar, const unsigned int version) - { - ar & m_licensefiledata; - } - -public: - Licensefiledata* m_licensefiledata; -}; - - -ServerError -LifileSave(const string& path,const time_t& update_time,const off_t& file_size,const string& filemd5) -{ - std::ofstream file(path); - boost::archive::binary_oarchive oa(file); - oa.register_type(); - STLlicensefiledata stllicensefiledata; - - Licensefiledata *p = new Licensefiledata(update_time,file_size,filemd5); - stllicensefiledata.m_licensefiledata = p; - oa << stllicensefiledata; - - delete p; - file.close(); - return SERVER_SUCCESS; -} - -ServerError -LifileLoad(const string& path,time_t& update_time,off_t& file_size,string& filemd5) - { - std::ifstream file(path); - boost::archive::binary_iarchive ia(file); - ia.register_type(); - STLlicensefiledata stllicensefiledata; - ia >> stllicensefiledata; - update_time = stllicensefiledata.m_licensefiledata->Getupdate_time(); - file_size = stllicensefiledata.m_licensefiledata->Getfile_size(); - filemd5 = stllicensefiledata.m_licensefiledata->Getfilemd5(); - file.close(); - return SERVER_SUCCESS; - } - -ServerError -LicenseLegality_check(const std::string& path) -{ - int deviceCount; - std::vector uuids; - LicenseGetuuid(deviceCount,uuids); - - std::vector shas; - LicenseGetuuidsha(deviceCount,uuids,shas); - - - int deviceCountcheck; - std::map uuidEncryption; - int64_t RemainingTime; - LiLoad(path,deviceCountcheck,uuidEncryption,RemainingTime); - - if(deviceCount!=deviceCountcheck) - { - printf("deviceCount is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - for(int i=0;i uuidEncryption; - int64_t RemainingTime; - LiLoad(path1,deviceCount,uuidEncryption,RemainingTime); - - std::cout<< "RemainingTime: " << RemainingTime < uuidEncryptioncheck; - int64_t RemainingTimecheck; - LiLoad(path1,deviceCountcheck,uuidEncryptioncheck,RemainingTimecheck); - - std::cout<< "RemainingTimecheck: " << RemainingTimecheck <expires_at(pt->expires_at() + boost::posix_time::hours(1)) ; - pt->async_wait(boost::bind(Alterfile,path1,path2, boost::asio::placeholders::error, pt)); - -} -void Runtime(const string &path1, const string &path2 ) -{ - boost::asio::io_service io; - boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); - t.async_wait(boost::bind(Alterfile,path1,path2, boost::asio::placeholders::error, &t)); - io.run(); - return;; -} - -} -} -} \ No newline at end of file diff --git a/cpp/src/license/LicensePublic.h b/cpp/src/license/LicensePublic.h deleted file mode 100644 index a40b6266c5..0000000000 --- a/cpp/src/license/LicensePublic.h +++ /dev/null @@ -1,88 +0,0 @@ -// -// Created by zilliz on 19-5-10. -// - -#pragma once - -#include "utils/Error.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - - -namespace zilliz { -namespace vecwise { -namespace server { - -class - BoostArchive; - -class - Licensedata1; - - -class - Licensefiledata; - -class - STLlicensefiledata; - - -ServerError -LiSave(const std::string &path, const int &deviceCount, const std::map &uuidEncryption); - -ServerError -LiLoad(const std::string &path, int &deviceCount, std::map &uuidEncryption, int64_t &RemainingTime); - -ServerError -LifileSave(const std::string &path, const time_t &update_time, const off_t &file_size, const std::string &filemd5); - -ServerError -LifileLoad(const std::string &path, time_t &update_time, off_t &file_size, std::string &filemd5); - -ServerError -LiSave(const std::string &path, - const int &deviceCount, - const std::map &uuidEncryption, - const int64_t &RemainingTime); - -ServerError -LicenseIntegrity_check(const std::string &path, const std::string &path2); - -ServerError -LicenseLegality_check(const std::string &path); - -void -Alterfile(const std::string &path1, - const std::string &path2, - const boost::system::error_code &ec, - boost::asio::deadline_timer *pt); - -void -Runtime(const std::string &path1, const std::string &path2); - - -} -} -} \ No newline at end of file diff --git a/cpp/src/license/LicenseRun.cpp b/cpp/src/license/LicenseRun.cpp deleted file mode 100644 index dd06a64540..0000000000 --- a/cpp/src/license/LicenseRun.cpp +++ /dev/null @@ -1,42 +0,0 @@ -// -// Created by zilliz on 19-5-11. -// -#include -#include -#include - -#include "utils/Error.h" -#include "license/LicenseCheck.h" -#include "license/LicensePublic.h" - - -using namespace zilliz::vecwise; - -//TEST(LicenseTest, LICENSE_TEST) { -// -// std::string path1 = "/tmp/vecwise_engine.license"; -// std::string path2 = "/tmp/vecwise_engine2.license"; -// std::cout << "This is run " << std::endl; -// -// server::ServerError err; -// -// err = server::Licensefileread(path1); -// if(err!=server::SERVER_SUCCESS) -// { -// exit(1); -// } -// err = server::LicenseIntegrity_check(path1,path2); -// if(err!=server::SERVER_SUCCESS) -// { -// std::cout << "Integrity_check is wrong " << std::endl; -// exit(1); -// } -// err = server::LicenseLegality_check(path1); -// if(err!=server::SERVER_SUCCESS) -// { -// std::cout << "Legality_check is wrong " << std::endl; -// exit(1); -// } -// std::cout << " runing " << std::endl; -// server::Runtime(path1,path2); -//} \ No newline at end of file diff --git a/cpp/src/license/SecretFile.h b/cpp/src/license/SecretFile.h deleted file mode 100644 index 08f196a5b3..0000000000 --- a/cpp/src/license/SecretFile.h +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited. - * Proprietary and confidential. - ******************************************************************************/ -#pragma once -#include - -class SecretFile { - public: - SecretFile() = default; - - SecretFile(const time_t &update_time, const off_t &file_size, const std::string &file_md5) - : update_time_(update_time), file_size_(file_size), file_md5_(file_md5) {} - - time_t get_update_time() { - return update_time_; - } - off_t get_file_size() { - return file_size_; - } - std::string get_file_md5() { - return file_md5_; - } - - private: - friend class boost::serialization::access; - - template - void serialize(Archive &ar, const unsigned int version) { - ar & update_time_; - ar & file_size_; - ar & file_md5_; - } - - public: - time_t update_time_ = 0; - off_t file_size_ = 0; - std::string file_md5_; -}; - -class SerializedSecretFile { - public: - ~SerializedSecretFile() { - if(secret_file_ != nullptr) { - delete secret_file_; - secret_file_ = nullptr; - } - } - - void - set_secret_file(SecretFile* secret_file) { - secret_file_ = secret_file; - } - - SecretFile* get_secret_file() { - return secret_file_; - } - - private: - friend class boost::serialization::access; - - template - void serialize(Archive &ar, const unsigned int version) { - ar & secret_file_; - } - - private: - SecretFile *secret_file_ = nullptr; -}; - - diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index 876771fafd..8c79c5a215 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -133,12 +133,6 @@ Server::Daemonize() { int Server::Start() { - std::string license_file_path = "/tmp/vecwise.license"; - if(LicenseValidate(license_file_path) != SERVER_SUCCESS) { - SERVER_LOG_ERROR << "License check failed"; - return 1; - } - if (daemonized_) { Daemonize(); } @@ -157,6 +151,21 @@ Server::Start() { //print config into console and log config.PrintAll(); +#ifdef ENABLE_LICENSE + ConfigNode license_config = config.GetConfig(CONFIG_LICENSE); + std::string license_file_path = license_config.GetValue(CONFIG_LICENSE_PATH); + SERVER_LOG_INFO << "License path: " << license_file_path; + if(server::LicenseCheck::LegalityCheck(license_file_path) != SERVER_SUCCESS) { + SERVER_LOG_ERROR << "License check failed"; + exit(1); + } + + if(server::LicenseCheck::StartCountingDown(license_file_path) != SERVER_SUCCESS) { + SERVER_LOG_ERROR << "License counter start error"; + exit(1); + } +#endif + // Handle Signal signal(SIGINT, SignalUtil::HandleSignal); signal(SIGHUP, SignalUtil::HandleSignal); diff --git a/cpp/src/server/ServerConfig.h b/cpp/src/server/ServerConfig.h index b8c5ae8c19..d840077e15 100644 --- a/cpp/src/server/ServerConfig.h +++ b/cpp/src/server/ServerConfig.h @@ -32,6 +32,9 @@ static const std::string CONFIG_CACHE = "cache_config"; static const std::string CONFIG_CPU_CACHE_CAPACITY = "cpu_cache_capacity"; static const std::string CONFIG_GPU_CACHE_CAPACITY = "gpu_cache_capacity"; +static const std::string CONFIG_LICENSE = "license_config"; +static const std::string CONFIG_LICENSE_PATH = "license_path"; + class ServerConfig { public: static ServerConfig &GetInstance(); diff --git a/cpp/unittest/license/CMakeLists.txt b/cpp/unittest/license/CMakeLists.txt index 2d2ed15fc7..cc7e5ba930 100644 --- a/cpp/unittest/license/CMakeLists.txt +++ b/cpp/unittest/license/CMakeLists.txt @@ -19,15 +19,18 @@ link_directories(/usr/local/cuda/targets/x86_64-linux/lib/stubs/) set(require_files ../../src/license/LicenseLibrary.cpp + ../../src/license/LicenseCheck.cpp # ../../src/license/License.cpp -# ../../src/license/LicensePublic.cpp -# ../../src/license/LicenseCreateuuidshafile.cpp ) + set(db_test_src # license_tests.cpp license_library_tests.cpp + license_check_test.cpp + + ${require_files}) cuda_add_executable(license_test ${db_test_src}) diff --git a/cpp/unittest/license/license_check_test.cpp b/cpp/unittest/license/license_check_test.cpp new file mode 100644 index 0000000000..f47afff357 --- /dev/null +++ b/cpp/unittest/license/license_check_test.cpp @@ -0,0 +1,68 @@ +// +// Created by zilliz on 19-5-13. +// + +#include "utils/Log.h" +#include "license/LicenseCheck.h" +#include "utils/Error.h" + +#include +#include + + +using namespace zilliz::vecwise; + +TEST(LicenseLibraryTest, CHECK_TEST) { + + server::ServerError err; + + std::string license_file_path("/tmp/megasearch/abc.license"); + + + int device_count; + std::map uuid_encryption_map; +// +// err = server::LicenseLibrary::GPUinfoFileDeserialization(sys_info_file_path, device_count, uuid_encryption_map); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// std::cout << " device_count: " << device_count << std::endl; +// for (int i = 0; i < device_count; i++) { +// std::cout << " uuid_encryption_map: " << i << " " << uuid_encryption_map[i] << std::endl; +// } +// + time_t starting_time; + time_t end_time; + err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, + device_count, + uuid_encryption_map, + starting_time, + end_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// std::cout << "device_count: " << device_count << std::endl; +// for (int i = 0; i < device_count; i++) { +// std::cout << " uuid_encryption_map: " << i << " " << uuid_encryption_map[i] << std::endl; +// } +// std::cout << "starting_time: " << starting_time << std::endl; +// std::cout << "end_time: " << end_time << std::endl; + + + time_t system_time; + err = server::LicenseLibrary::GetSystemTime(system_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + system_time+=111100; + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, + device_count, + uuid_encryption_map, + system_time, + end_time); +// ASSERT_EQ(err, server::SERVER_SUCCESS); + // 19. Legality check + err = server::LicenseCheck::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 20. Start counting down + +// err = server::LicenseCheck::StartCountingDown(license_file_path); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +} diff --git a/cpp/unittest/license/license_library_tests.cpp b/cpp/unittest/license/license_library_tests.cpp index ec9ee9fac0..0f4e84a947 100644 --- a/cpp/unittest/license/license_library_tests.cpp +++ b/cpp/unittest/license/license_library_tests.cpp @@ -12,6 +12,7 @@ #include #include + using namespace zilliz::vecwise; TEST(LicenseLibraryTest, FILE_EXISTENT_TEST) { @@ -55,6 +56,12 @@ TEST(LicenseLibraryTest, GPU_INFO_TEST) { std::cout << "Device Id: " << i << ", UUID: " << uuid_array[i] << ", UUID_SHA256: " << uuid_sha256_array[i] << std::endl; } + + time_t systemtime; + err = server::LicenseLibrary::GetSystemTime(systemtime); + ASSERT_EQ(err, server::SERVER_SUCCESS); + std::cout << "System Time: " << systemtime << std::endl; + } TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { @@ -112,27 +119,40 @@ TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { ASSERT_EQ(uuid_encrption_map[i], output_info_uuid_encrption_map[i]); } + + // 16. Get System Time/starting_time ans End Time + time_t starting_time; + err = server::LicenseLibrary::GetSystemTime(starting_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + time_t end_time; + end_time = starting_time + (long) (60 * 60 * 24 * 7); + // 11. Generate License File err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, device_count, uuid_encrption_map, - remaining_hour); + starting_time, + end_time); ASSERT_EQ(err, server::SERVER_SUCCESS); // 12. Define output var int output_device_count = 0; std::map output_uuid_encrption_map; - int64_t output_remaining_hour; + time_t output_starting_time; + time_t output_end_time; // 13. Read License File err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, output_device_count, output_uuid_encrption_map, - output_remaining_hour); + output_starting_time, + output_end_time); ASSERT_EQ(err, server::SERVER_SUCCESS); ASSERT_EQ(device_count, output_device_count); - ASSERT_EQ(remaining_hour, output_remaining_hour); + ASSERT_EQ(starting_time, output_starting_time); + ASSERT_EQ(end_time, output_end_time); + for (int i = 0; i < device_count; ++i) { ASSERT_EQ(uuid_encrption_map[i], output_uuid_encrption_map[i]); } @@ -148,53 +168,49 @@ TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { err = server::LicenseLibrary::GetFileMD5(license_file_path, file_md5); ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 16. Generate Secret File - std::string secret_file_path("/tmp/megasearch.secret"); - err = server::LicenseLibrary::SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); - ASSERT_EQ(err, server::SERVER_SUCCESS); +// std::string secret_file_path("/tmp/megasearch.secret"); +// err = server::LicenseLibrary::SecretFileSerialization(secret_file_path, +// update_time, +// file_size, +// starting_time, +// end_time, +// file_md5); +// ASSERT_EQ(err, server::SERVER_SUCCESS); // 17. Define output var - time_t output_update_time; - off_t output_file_size; - std::string output_file_md5; +// time_t output_update_time; +// off_t output_file_size; +// time_t output_starting_time; +// time_t output_end_time; +// std::string output_file_md5; // 18. Read License File - err = server::LicenseLibrary::SecretFileDeserialization(secret_file_path, - output_update_time, - output_file_size, - output_file_md5); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - ASSERT_EQ(update_time, output_update_time); - ASSERT_EQ(file_size, output_file_size); - ASSERT_EQ(file_md5, output_file_md5); +// err = server::LicenseLibrary::SecretFileDeserialization(secret_file_path, +// output_update_time, +// output_file_size, +// output_starting_time, +// output_end_time, +// output_file_md5); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// ASSERT_EQ(update_time, output_update_time); +// ASSERT_EQ(file_size, output_file_size); +// ASSERT_EQ(starting_time, output_starting_time); +// ASSERT_EQ(end_time, output_end_time); +// ASSERT_EQ(file_md5, output_file_md5); // 19. Integrity check and Legality check - err = server::LicenseLibrary::IntegrityCheck(license_file_path, secret_file_path); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - err = server::LicenseLibrary::LegalityCheck(license_file_path); - ASSERT_EQ(err, server::SERVER_SUCCESS); +// err = server::LicenseLibrary::IntegrityCheck(license_file_path, secret_file_path); +// ASSERT_EQ(err, server::SERVER_SUCCESS); +// +// err = server::LicenseLibrary::LegalityCheck(license_file_path); +// ASSERT_EQ(err, server::SERVER_SUCCESS); } -TEST(LicenseLibraryTest, Timer_TEST) { - - server::ServerError err; - std::string license_file_path("/tmp/megasearch.license"); - std::string secret_file_path("/tmp/megasearch.secret"); - - // 19. Integrity check and Legality check - err = server::LicenseLibrary::IntegrityCheck(license_file_path, secret_file_path); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - err = server::LicenseLibrary::LegalityCheck(license_file_path); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - // 20. Start counting down - err = server::LicenseLibrary::StartCountingDown(license_file_path, secret_file_path); - ASSERT_EQ(err, server::SERVER_SUCCESS); -} TEST(LicenseLibraryTest, GET_GPU_INFO_FILE) { @@ -250,39 +266,64 @@ TEST(LicenseLibraryTest, GET_LICENSE_FILE) { output_info_uuid_encrption_map); ASSERT_EQ(err, server::SERVER_SUCCESS); - // 5. Enter time - int64_t remaining_hour = 24*7 ; - std::cout << "Please enter the authorization time (hours)" << std::endl; -// std::cin >> remaining_hour; + time_t system_time; + err = server::LicenseLibrary::GetSystemTime(system_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + time_t starting_time = system_time; + time_t end_time = system_time + (long) (60 * 60 * 24 * 7); + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, output_info_device_count, output_info_uuid_encrption_map, - remaining_hour); + starting_time, + end_time); ASSERT_EQ(err, server::SERVER_SUCCESS); std::cout << "Generate License File Success" << std::endl; } -TEST(LicenseLibraryTest, GET_SECRET_FILE) { - +TEST(LicenseLibraryTest, GET_DATA_TIME) { server::ServerError err; - std::string license_file_path("/tmp/megasearch.license"); - std::string secret_file_path("/tmp/megasearch.secret"); - - // 14. Get License File Attribute - time_t update_time; - off_t file_size; - err = server::LicenseLibrary::GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); + char * a ="2018-03-06"; + time_t pp; + err = server::LicenseLibrary::GetDateTime(a,pp); ASSERT_EQ(err, server::SERVER_SUCCESS); + std::cout << pp < Date: Wed, 15 May 2019 10:46:06 +0800 Subject: [PATCH 8/9] unit test Former-commit-id: 3806f1d475b2dd1259fe48a013f01389f8975e5c --- cpp/src/license/GetSysInfo.cpp | 8 +- cpp/src/license/LicenseCheck.cpp | 41 --- cpp/src/license/LicenseCheck.h | 2 - cpp/src/license/LicenseGenerator.cpp | 19 +- cpp/src/license/LicenseLibrary.cpp | 126 +------- cpp/src/license/LicenseLibrary.h | 17 -- cpp/unittest/license/CMakeLists.txt | 2 - cpp/unittest/license/license_check_test.cpp | 164 +++++++++-- .../license/license_library_tests.cpp | 277 +++++------------- cpp/unittest/license/license_tests.cpp | 191 ------------ 10 files changed, 231 insertions(+), 616 deletions(-) delete mode 100644 cpp/unittest/license/license_tests.cpp diff --git a/cpp/src/license/GetSysInfo.cpp b/cpp/src/license/GetSysInfo.cpp index 4ddf671485..bc4b43c1d3 100644 --- a/cpp/src/license/GetSysInfo.cpp +++ b/cpp/src/license/GetSysInfo.cpp @@ -53,24 +53,24 @@ int main(int argc, char *argv[]) { server::ServerError err = server::LicenseLibrary::GetDeviceCount(device_count); if (err != server::SERVER_SUCCESS) return -1; - // 2. Get All GPU UUID + // 1. Get All GPU UUID std::vector uuid_array; err = server::LicenseLibrary::GetUUID(device_count, uuid_array); if (err != server::SERVER_SUCCESS) return -1; - // 3. Get UUID SHA256 + // 2. Get UUID SHA256 std::vector uuid_sha256_array; err = server::LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, uuid_sha256_array); if (err != server::SERVER_SUCCESS) return -1; - // 4. Generate GPU ID map with GPU UUID + // 3. Generate GPU ID map with GPU UUID std::map uuid_encrption_map; for (int i = 0; i < device_count; ++i) { uuid_encrption_map[i] = uuid_sha256_array[i]; } - // 6. Generate GPU_info File + // 4. Generate GPU_info File err = server::LicenseLibrary::GPUinfoFileSerialization(system_info_filename, device_count, uuid_encrption_map); diff --git a/cpp/src/license/LicenseCheck.cpp b/cpp/src/license/LicenseCheck.cpp index 7c3e4a30de..ca8fb4f930 100644 --- a/cpp/src/license/LicenseCheck.cpp +++ b/cpp/src/license/LicenseCheck.cpp @@ -14,47 +14,6 @@ namespace vecwise { namespace server { -//ServerError -//LicenseCheck::IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path) { -// -// std::string file_md5; -// LicenseLibrary::GetFileMD5(license_file_path, file_md5); -// time_t update_time; -// off_t file_size; -// LicenseLibrary::GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); -// time_t system_time; -// LicenseLibrary::GetSystemTime(system_time); -// -// std::string output_file_md5; -// time_t output_update_time; -// off_t output_file_size; -// time_t output_starting_time; -// time_t output_end_time; -// LicenseLibrary::SecretFileDeserialization(secret_file_path, -// output_update_time, -// output_file_size, -// output_starting_time, -// output_end_time, -// output_file_md5); -// if (file_md5 != output_file_md5) { -// printf("License file has been modified\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// if (update_time != output_update_time) { -// printf("update_time is wrong\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// if (file_size != output_file_size) { -// printf("file_size is wrong\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// if (system_time < output_starting_time || system_time > output_end_time) { -// printf("License expired\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// return SERVER_SUCCESS; -//} - // Part 1: Legality check ServerError diff --git a/cpp/src/license/LicenseCheck.h b/cpp/src/license/LicenseCheck.h index e3c5b3ac32..9a22f57f5a 100644 --- a/cpp/src/license/LicenseCheck.h +++ b/cpp/src/license/LicenseCheck.h @@ -20,8 +20,6 @@ class LicenseCheck { return instance; }; -// static ServerError -// IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path); // Part 1: Legality check static ServerError diff --git a/cpp/src/license/LicenseGenerator.cpp b/cpp/src/license/LicenseGenerator.cpp index 2160c916ad..c837337a2f 100644 --- a/cpp/src/license/LicenseGenerator.cpp +++ b/cpp/src/license/LicenseGenerator.cpp @@ -18,8 +18,8 @@ print_usage(const std::string &app_name) { printf(" -h --help Print this help\n"); printf(" -s --sysinfo filename sysinfo file location\n"); printf(" -l --license filename Generate license file as given name\n"); - printf(" -b --starting time Set start time Similar to year-month-day\n"); - printf(" -e --end time Set end time Similar to year-month-day \n"); + printf(" -b --starting time Set start time (format: YYYY-MM-DD)\n"); + printf(" -e --end time Set end time (format: YYYY-MM-DD)\n"); printf("\n"); } @@ -52,35 +52,20 @@ int main(int argc, char *argv[]) { case 's': { flag_s = 0; system_info_filename = (std::string) (optarg); -// char *system_info_filename_ptr = strdup(optarg); -// system_info_filename = system_info_filename_ptr; -// free(system_info_filename_ptr); -// printf("Generate system info file: %s\n", system_info_filename.c_str()); break; } case 'b': { flag_b = 0; string_starting_time = optarg; -// char *time = strdup(optarg); -// err = server::LicenseLibrary::GetDateTime(time, starting_time); -// if (err != server::SERVER_SUCCESS) return -1; -// free(time); break; } case 'e': { flag_e = 0; string_end_time = optarg; -// char *time = strdup(optarg); -// err = server::LicenseLibrary::GetDateTime(time, end_time); -// if (err != server::SERVER_SUCCESS) return -1; -// free(time); break; } case 'l': { license_filename = (std::string) (optarg); -// char *system_info_filename_ptr = strdup(optarg); -// license_filename = system_info_filename_ptr; -// free(system_info_filename_ptr); break; } case 'h':print_usage(app_name); diff --git a/cpp/src/license/LicenseLibrary.cpp b/cpp/src/license/LicenseLibrary.cpp index f891ed113d..669810552e 100644 --- a/cpp/src/license/LicenseLibrary.cpp +++ b/cpp/src/license/LicenseLibrary.cpp @@ -26,6 +26,7 @@ namespace server { constexpr int LicenseLibrary::sha256_length_; +// Part 0: File check bool LicenseLibrary::IsFileExistent(const std::string &path) { @@ -42,7 +43,7 @@ LicenseLibrary::IsFileExistent(const std::string &path) { return !boost::filesystem::is_directory(file_status); } - +// Part 1: Get GPU Info ServerError LicenseLibrary::GetDeviceCount(int &device_count) { nvmlReturn_t result = nvmlInit(); @@ -230,6 +231,8 @@ LicenseLibrary::LicenseFileDeserialization(const std::string &path, // return SERVER_SUCCESS; //} + + // Part 3: File attribute: UpdateTime Time/ Size/ MD5 ServerError LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update_time, off_t &file_size) { @@ -336,127 +339,6 @@ LicenseLibrary::GetDateTime(char *cha, time_t &data_time) { return SERVER_SUCCESS; } -// Part 5: Integrity check and Legality check -//ServerError -//LicenseLibrary::IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path) { -// -// std::string file_md5; -// GetFileMD5(license_file_path, file_md5); -// time_t update_time; -// off_t file_size; -// GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); -// time_t system_time; -// GetSystemTime(system_time); -// -// std::string output_file_md5; -// time_t output_update_time; -// off_t output_file_size; -// time_t output_starting_time; -// time_t output_end_time; -// SecretFileDeserialization(secret_file_path, -// output_update_time, -// output_file_size, -// output_starting_time, -// output_end_time, -// output_file_md5); -// if (file_md5 != output_file_md5) { -// printf("License file has been modified\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// if (update_time != output_update_time) { -// printf("update_time is wrong\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// if (file_size != output_file_size) { -// printf("file_size is wrong\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// if (system_time < output_starting_time || system_time > output_end_time) { -// printf("License expired\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// return SERVER_SUCCESS; -//} -// -//ServerError -//LicenseLibrary::LegalityCheck(const std::string &license_file_path) { -// -// int device_count; -// GetDeviceCount(device_count); -// std::vector uuid_array; -// GetUUID(device_count, uuid_array); -// -// std::vector sha_array; -// GetUUIDSHA256(device_count, uuid_array, sha_array); -// -// int output_device_count; -// std::map uuid_encryption_map; -// int64_t remaining_time; -// LicenseFileDeserialization(license_file_path, output_device_count, uuid_encryption_map, remaining_time); -// -// if (device_count != output_device_count) { -// printf("device_count is wrong\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// for (int i = 0; i < device_count; ++i) { -// if (sha_array[i] != uuid_encryption_map[i]) { -// printf("uuid_encryption_map %d is wrong\n", i); -// return SERVER_UNEXPECTED_ERROR; -// } -// } -// if (remaining_time <= 0) { -// printf("License expired\n"); -// return SERVER_UNEXPECTED_ERROR; -// } -// std::cout << "Legality Check Success" << std::endl; -// return SERVER_SUCCESS; -//} - -//// Part 6: Timer -//ServerError -//LicenseLibrary::AlterFile(const std::string &license_file_path, -// const std::string &secret_file_path, -// const boost::system::error_code &ec, -// boost::asio::deadline_timer *pt) { -// int device_count; -// std::map uuid_encryption_map; -// int64_t remaining_time; -// LicenseFileDeserialization(license_file_path, device_count, uuid_encryption_map, remaining_time); -// -// std::cout << "remaining_time: " << remaining_time << std::endl; -// -// if (remaining_time <= 0) { -// std::cout << "License expired" << std::endl; -// exit(1); -// } -// --remaining_time; -// LicenseFileSerialization(license_file_path, device_count, uuid_encryption_map, remaining_time); -// -// time_t update_time; -// off_t file_size; -// GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); -// std::string file_md5; -// GetFileMD5(license_file_path, file_md5); -// SecretFileSerialization(secret_file_path, update_time, file_size, file_md5); -// -// pt->expires_at(pt->expires_at() + boost::posix_time::hours(1)); -// pt->async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, pt)); -// return SERVER_SUCCESS; -//} -// -//ServerError -//LicenseLibrary::StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path) { -// -// if (!IsFileExistent(license_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; -// if (!IsFileExistent(secret_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; -// -// boost::asio::io_service io; -// boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); -// t.async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, &t)); -// std::cout << "Timing begins" << std::endl; -// io.run(); -// return SERVER_SUCCESS; -//} } } diff --git a/cpp/src/license/LicenseLibrary.h b/cpp/src/license/LicenseLibrary.h index 3fd27a41c6..a4202b1a0b 100644 --- a/cpp/src/license/LicenseLibrary.h +++ b/cpp/src/license/LicenseLibrary.h @@ -94,23 +94,6 @@ class LicenseLibrary { static ServerError GetDateTime(char *cha, time_t &data_time); - // Part 5: Integrity check and Legality check -// static ServerError -// IntegrityCheck(const std::string &license_file_path, const std::string &secret_file_path); -// -// static ServerError -// LegalityCheck(const std::string &license_file_path); - - // Part 6: Timer -// static ServerError -// AlterFile(const std::string &license_file_path, -// const std::string &secret_file_path, -// const boost::system::error_code &ec, -// boost::asio::deadline_timer *pt); -// -// static ServerError -// StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path); - private: static constexpr int sha256_length_ = 32; diff --git a/cpp/unittest/license/CMakeLists.txt b/cpp/unittest/license/CMakeLists.txt index cc7e5ba930..55509c195c 100644 --- a/cpp/unittest/license/CMakeLists.txt +++ b/cpp/unittest/license/CMakeLists.txt @@ -20,13 +20,11 @@ link_directories(/usr/local/cuda/targets/x86_64-linux/lib/stubs/) set(require_files ../../src/license/LicenseLibrary.cpp ../../src/license/LicenseCheck.cpp -# ../../src/license/License.cpp ) set(db_test_src -# license_tests.cpp license_library_tests.cpp license_check_test.cpp diff --git a/cpp/unittest/license/license_check_test.cpp b/cpp/unittest/license/license_check_test.cpp index f47afff357..75a8b65010 100644 --- a/cpp/unittest/license/license_check_test.cpp +++ b/cpp/unittest/license/license_check_test.cpp @@ -14,55 +14,171 @@ using namespace zilliz::vecwise; TEST(LicenseLibraryTest, CHECK_TEST) { - server::ServerError err; + server::ServerError err; + // 1. Set license file name std::string license_file_path("/tmp/megasearch/abc.license"); + // 2. Legality check + err = server::LicenseCheck::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); +} + +TEST(LicenseLibraryTest, CHECK_ERROR1_TEST){ + + server::ServerError err; + // 1. Set license file name + std::string license_file_path("/tmp/megasearch/abc"); + + // 2. Legality check + err = server::LicenseCheck::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); +} + +TEST(LicenseLibraryTest, CHECK_ERROR2_TEST){ + + server::ServerError err; + // 1. Set license file name + std::string license_file_path("/tmp/megasearch/abc.license"); + + // 2. Define output var int device_count; std::map uuid_encryption_map; -// -// err = server::LicenseLibrary::GPUinfoFileDeserialization(sys_info_file_path, device_count, uuid_encryption_map); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// std::cout << " device_count: " << device_count << std::endl; -// for (int i = 0; i < device_count; i++) { -// std::cout << " uuid_encryption_map: " << i << " " << uuid_encryption_map[i] << std::endl; -// } -// time_t starting_time; time_t end_time; + + // 3. Read License File err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, device_count, uuid_encryption_map, starting_time, end_time); ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// std::cout << "device_count: " << device_count << std::endl; -// for (int i = 0; i < device_count; i++) { -// std::cout << " uuid_encryption_map: " << i << " " << uuid_encryption_map[i] << std::endl; -// } -// std::cout << "starting_time: " << starting_time << std::endl; -// std::cout << "end_time: " << end_time << std::endl; + // 4. Change device count + ++device_count; + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, + device_count, + uuid_encryption_map, + starting_time, + end_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + // 5. Legality check + err = server::LicenseCheck::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); +} + +TEST(LicenseLibraryTest, CHECK_ERROR3_TEST){ + + server::ServerError err; + // 1. Set license file name + std::string license_file_path("/tmp/megasearch/abc.license"); + + // 2. Define output var + int device_count; + std::map uuid_encryption_map; + time_t starting_time; + time_t end_time; + + // 3. Read License File + err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, + device_count, + uuid_encryption_map, + starting_time, + end_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 4. Change device count + if(device_count) uuid_encryption_map[0]+="u"; + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, + device_count, + uuid_encryption_map, + starting_time, + end_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 5. Legality check + err = server::LicenseCheck::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); +} + +TEST(LicenseLibraryTest, CHECK_ERROR4_1_TEST){ + + server::ServerError err; + // 1. Set license file name + std::string license_file_path("/tmp/megasearch/abc.license"); + + // 2. Define output var + int device_count; + std::map uuid_encryption_map; + time_t starting_time; + time_t end_time; + + // 3. Read License File + err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, + device_count, + uuid_encryption_map, + starting_time, + end_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 4. Change starting time time_t system_time; err = server::LicenseLibrary::GetSystemTime(system_time); ASSERT_EQ(err, server::SERVER_SUCCESS); - system_time+=111100; + system_time+=60*60*24; + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, device_count, uuid_encryption_map, system_time, end_time); -// ASSERT_EQ(err, server::SERVER_SUCCESS); - // 19. Legality check - err = server::LicenseCheck::LegalityCheck(license_file_path); ASSERT_EQ(err, server::SERVER_SUCCESS); - // 20. Start counting down - -// err = server::LicenseCheck::StartCountingDown(license_file_path); -// ASSERT_EQ(err, server::SERVER_SUCCESS); + // 5. Legality check + err = server::LicenseCheck::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); } + +TEST(LicenseLibraryTest, CHECK_ERROR4_2_TEST){ + + server::ServerError err; + // 1. Set license file name + std::string license_file_path("/tmp/megasearch/abc.license"); + + // 2. Define output var + int device_count; + std::map uuid_encryption_map; + time_t starting_time; + time_t end_time; + + // 3. Read License File + err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, + device_count, + uuid_encryption_map, + starting_time, + end_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 4. Change end time + time_t system_time; + err = server::LicenseLibrary::GetSystemTime(system_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + system_time-=100; + + err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, + device_count, + uuid_encryption_map, + starting_time, + system_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 5. Legality check + err = server::LicenseCheck::LegalityCheck(license_file_path); + ASSERT_EQ(err, server::SERVER_SUCCESS); +} + diff --git a/cpp/unittest/license/license_library_tests.cpp b/cpp/unittest/license/license_library_tests.cpp index 0f4e84a947..f47ce5a43f 100644 --- a/cpp/unittest/license/license_library_tests.cpp +++ b/cpp/unittest/license/license_library_tests.cpp @@ -66,153 +66,10 @@ TEST(LicenseLibraryTest, GPU_INFO_TEST) { TEST(LicenseLibraryTest, LICENSE_FILE_TEST) { - // 1. Get Device Count - int device_count = 0; - server::ServerError err = server::LicenseLibrary::GetDeviceCount(device_count); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - // 2. Get All GPU UUID - std::vector uuid_array; - err = server::LicenseLibrary::GetUUID(device_count, uuid_array); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - // 3. Get UUID SHA256 - std::vector uuid_sha256_array; - err = server::LicenseLibrary::GetUUIDSHA256(device_count, uuid_array, uuid_sha256_array); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - - // 4. Set a file name - std::string license_file_path("/tmp/megasearch.license"); - - // 5. Provide remaining hour - int64_t remaining_hour = 2 * 365 * 24; - - // 6. Generate GPU ID map with GPU UUID - std::map uuid_encrption_map; - for (int i = 0; i < device_count; ++i) { - uuid_encrption_map[i] = uuid_sha256_array[i]; - } - - // 7.GPU_info File - std::string GPU_info_file_path("/tmp/megasearch.info"); - - - // 8. Generate GPU_info File - err = server::LicenseLibrary::GPUinfoFileSerialization(GPU_info_file_path, - device_count, - uuid_encrption_map); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - // 9. Define output var - int output_info_device_count = 0; - std::map output_info_uuid_encrption_map; - - // 10. Read GPU_info File - err = server::LicenseLibrary::GPUinfoFileDeserialization(GPU_info_file_path, - output_info_device_count, - output_info_uuid_encrption_map); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - ASSERT_EQ(device_count, output_info_device_count); - for (int i = 0; i < device_count; ++i) { - ASSERT_EQ(uuid_encrption_map[i], output_info_uuid_encrption_map[i]); - } - - - // 16. Get System Time/starting_time ans End Time - time_t starting_time; - err = server::LicenseLibrary::GetSystemTime(starting_time); - ASSERT_EQ(err, server::SERVER_SUCCESS); - time_t end_time; - end_time = starting_time + (long) (60 * 60 * 24 * 7); - - // 11. Generate License File - err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, - device_count, - uuid_encrption_map, - starting_time, - end_time); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - // 12. Define output var - int output_device_count = 0; - std::map output_uuid_encrption_map; - time_t output_starting_time; - time_t output_end_time; - - // 13. Read License File - err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, - output_device_count, - output_uuid_encrption_map, - output_starting_time, - output_end_time); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - ASSERT_EQ(device_count, output_device_count); - ASSERT_EQ(starting_time, output_starting_time); - ASSERT_EQ(end_time, output_end_time); - - for (int i = 0; i < device_count; ++i) { - ASSERT_EQ(uuid_encrption_map[i], output_uuid_encrption_map[i]); - } - - // 14. Get License File Attribute - time_t update_time; - off_t file_size; - err = server::LicenseLibrary::GetFileUpdateTimeAndSize(license_file_path, update_time, file_size); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - // 15. Get License File MD5 - std::string file_md5; - err = server::LicenseLibrary::GetFileMD5(license_file_path, file_md5); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - - - // 16. Generate Secret File -// std::string secret_file_path("/tmp/megasearch.secret"); -// err = server::LicenseLibrary::SecretFileSerialization(secret_file_path, -// update_time, -// file_size, -// starting_time, -// end_time, -// file_md5); -// ASSERT_EQ(err, server::SERVER_SUCCESS); - - // 17. Define output var -// time_t output_update_time; -// off_t output_file_size; -// time_t output_starting_time; -// time_t output_end_time; -// std::string output_file_md5; - - // 18. Read License File -// err = server::LicenseLibrary::SecretFileDeserialization(secret_file_path, -// output_update_time, -// output_file_size, -// output_starting_time, -// output_end_time, -// output_file_md5); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// ASSERT_EQ(update_time, output_update_time); -// ASSERT_EQ(file_size, output_file_size); -// ASSERT_EQ(starting_time, output_starting_time); -// ASSERT_EQ(end_time, output_end_time); -// ASSERT_EQ(file_md5, output_file_md5); - - // 19. Integrity check and Legality check -// err = server::LicenseLibrary::IntegrityCheck(license_file_path, secret_file_path); -// ASSERT_EQ(err, server::SERVER_SUCCESS); -// -// err = server::LicenseLibrary::LegalityCheck(license_file_path); -// ASSERT_EQ(err, server::SERVER_SUCCESS); - -} - - -TEST(LicenseLibraryTest, GET_GPU_INFO_FILE) { + // 0. File check + std::string test("/tmp/a.test"); + bool is = server::LicenseLibrary::IsFileExistent(test); + ASSERT_EQ(is, false); // 1. Get Device Count int device_count = 0; @@ -238,85 +95,91 @@ TEST(LicenseLibraryTest, GET_GPU_INFO_FILE) { // 5.GPU_info File std::string GPU_info_file_path("/tmp/megasearch.info"); + // 6. Generate GPU_info File err = server::LicenseLibrary::GPUinfoFileSerialization(GPU_info_file_path, device_count, uuid_encrption_map); ASSERT_EQ(err, server::SERVER_SUCCESS); - std::cout << "Generate GPU_info File Success" << std::endl; -} - -TEST(LicenseLibraryTest, GET_LICENSE_FILE) { - - server::ServerError err; - // 1.GPU_info File - std::string GPU_info_file_path("/tmp/megasearch.info"); - - // 2. License File - std::string license_file_path("/tmp/megasearch.license"); - - // 3. Define output var + // 7. Define output var int output_info_device_count = 0; std::map output_info_uuid_encrption_map; - // 4. Read GPU_info File + // 8. Read GPU_info File err = server::LicenseLibrary::GPUinfoFileDeserialization(GPU_info_file_path, output_info_device_count, output_info_uuid_encrption_map); ASSERT_EQ(err, server::SERVER_SUCCESS); - time_t system_time; - err = server::LicenseLibrary::GetSystemTime(system_time); - ASSERT_EQ(err, server::SERVER_SUCCESS); - time_t starting_time = system_time; - time_t end_time = system_time + (long) (60 * 60 * 24 * 7); + ASSERT_EQ(device_count, output_info_device_count); + for (int i = 0; i < device_count; ++i) { + ASSERT_EQ(uuid_encrption_map[i], output_info_uuid_encrption_map[i]); + } + // 9. Set license file name + std::string license_file_path("/tmp/megasearch.license"); + + // 10. Get System Time/starting_time ans End Time + time_t sysyem_time; + err = server::LicenseLibrary::GetSystemTime(sysyem_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 11.GetDateTime + time_t starting_time; + time_t end_time; + char *string_starting_time = "2019-05-10"; + char *string_end_time = "2022-05-10"; + err = server::LicenseLibrary::GetDateTime(string_starting_time, starting_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + err = server::LicenseLibrary::GetDateTime(string_end_time, end_time); + ASSERT_EQ(err, server::SERVER_SUCCESS); + + // 12. Generate License File err = server::LicenseLibrary::LicenseFileSerialization(license_file_path, - output_info_device_count, - output_info_uuid_encrption_map, + device_count, + uuid_encrption_map, starting_time, end_time); ASSERT_EQ(err, server::SERVER_SUCCESS); - std::cout << "Generate License File Success" << std::endl; -} + // 13. Define output var + int output_device_count = 0; + std::map output_uuid_encrption_map; + time_t output_starting_time; + time_t output_end_time; -TEST(LicenseLibraryTest, GET_DATA_TIME) { - server::ServerError err; - char * a ="2018-03-06"; - time_t pp; - err = server::LicenseLibrary::GetDateTime(a,pp); + // 14. Read License File + err = server::LicenseLibrary::LicenseFileDeserialization(license_file_path, + output_device_count, + output_uuid_encrption_map, + output_starting_time, + output_end_time); ASSERT_EQ(err, server::SERVER_SUCCESS); - std::cout << pp < -#include -#include "license/LicenseCheck.h" -#include "license/LicensePublic.h" -#include "utils/Error.h" - - -using namespace zilliz::vecwise; - -//TEST(LicenseTest, LICENSE_TEST) { - -// std::string path1 = "/tmp/vecwise_engine.license"; -// std::string path2 = "/tmp/vecwise_engine2.license"; -// std::cout << "This is run " << std::endl; -// -// server::ServerError err; -// -// err = server::Licensefileread(path1); -// if(err!=server::SERVER_SUCCESS) -// { -// exit(1); -// } -// err = server::LicenseIntegrity_check(path1,path2); -// if(err!=server::SERVER_SUCCESS) -// { -// std::cout << "Integrity_check is wrong " << std::endl; -// exit(1); -// } -// err = server::LicenseLegality_check(path1); -// if(err!=server::SERVER_SUCCESS) -// { -// std::cout << "Legality_check is wrong " << std::endl; -// exit(1); -// } -// std::cout << " runing " << std::endl; -// server::Runtime(path1,path2); -//} - - - - - -TEST(LicenseTest, LICENSE_TEST) { - std::string path1 = "/tmp/vecwise_engine.license"; - std::string path2 = "/tmp/vecwise_engine2.license"; - - server::ServerError err; - server::Runtime(path1,path2); - - - time_t update_time; - off_t file_size; - std::string filemd5; - - time_t update_timecheck; - off_t file_sizecheck; - std::string filemd5check; - - err = server::LicenseGetfiletime(path1,update_time); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - err = server::LicenseGetfilesize(path1,file_size); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - err = server::LicenseGetfilemd5(path1,filemd5); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - err = server::LifileSave(path2,update_time,file_size,filemd5); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - err = server::LifileLoad(path2,update_timecheck,file_sizecheck,filemd5check); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - - std::cout<< "update_time : " << update_time < uuids; - - deviceCount = 2; - uuids.push_back("121"); - uuids.push_back("324"); - err = server::LicenseGetuuid(deviceCount,uuids); - ASSERT_EQ(err, server::SERVER_SUCCESS); - printf("\n deviceCount = %d\n",deviceCount); - - std::vector uuidmd5s; - err = server::LicenseGetuuidmd5(deviceCount,uuids,uuidmd5s); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - printf(" md5s \n"); - for(int i=0;i uuidshas; - err = server::LicenseGetuuidsha(deviceCount,uuids,uuidshas); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - std::map uuidEncryption; - for(int i=0;i uuidEncryptioncheck; -// err = server::LicenseLibrary::LicenseFileDeserialization(path1, deviceCountcheck, uuidEncryptioncheck, RemainingTime); - err = server::LiLoad(path1,deviceCountcheck,uuidEncryptioncheck,RemainingTime); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - printf("----- checking ----\n"); - printf("\n deviceCount = %d\n",deviceCountcheck); - for(auto it : uuidEncryptioncheck) - { - std::cout<< "uuidshas : " << it.second << std::endl; - } - std::cout<< "RemainingTime :" << RemainingTime << std::endl; - - printf(" shas \n"); - for(int i=0;i uuidshascheck; - - err = server::LicenseLoad(path1,deviceCountcheck,uuidshascheck); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - std::cout<<" deviceCountcheck :" << deviceCountcheck << std::endl; - std::cout<<" uuidshascheck :" << uuidshascheck[0] << std::endl; - - - err = server::LicenseGetfilemd5(path1,filemd5); - ASSERT_EQ(err, server::SERVER_SUCCESS); - std::cout<<" filemd5 :" << filemd5 << std::endl; - - err= server::LicensefileSave(path1,path2); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - time_t last_timecheck; - err= server::LicensefileLoad(path2,filemd5check,last_timecheck); - ASSERT_EQ(err, server::SERVER_SUCCESS); - std::cout<<" filemd5check :" << filemd5check << std::endl; - - time_t last_time; - err = server::LicenseGetfiletime(path1,last_time); - ASSERT_EQ(err, server::SERVER_SUCCESS); - - std::cout<<" last_time : " << last_time << std::endl; - std::cout<<" last_timecheck :" << last_timecheck << std::endl; - - err = server::LicenseIntegritycheck(path1,path2); - ASSERT_EQ(err, server::SERVER_SUCCESS); - -} From cd88f612d90b938082936289c6eaedb57fed657f Mon Sep 17 00:00:00 2001 From: "yangwei.yao" Date: Wed, 15 May 2019 10:53:07 +0800 Subject: [PATCH 9/9] revert third party Former-commit-id: bf597b90ac10de8308c35d77470f80c8637dab2e