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