patch 4.0
This commit is contained in:
@ -12,24 +12,25 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "share/ob_encryption_util.h"
|
||||
#define randmod(x) rand() % x
|
||||
#define randmod(x) rand()%x
|
||||
#define private public
|
||||
using namespace oceanbase::common;
|
||||
using namespace oceanbase::share;
|
||||
|
||||
class TestNationalEncrypt : public ::testing::Test {
|
||||
class TestNationalEncrypt : public ::testing::Test
|
||||
{
|
||||
public:
|
||||
bool equal(char* A, char* B, int len);
|
||||
bool equal(char *A, char *B, int len);
|
||||
};
|
||||
|
||||
bool TestNationalEncrypt::equal(char* A, char* B, int len)
|
||||
bool TestNationalEncrypt::equal(char *A, char *B, int len)
|
||||
{
|
||||
int ret = true;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (A[i] != B[i]) {
|
||||
ret = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -54,13 +55,13 @@ TEST_F(TestNationalEncrypt, basic_test)
|
||||
}
|
||||
TEST_F(TestNationalEncrypt, random_test)
|
||||
{
|
||||
// Random encryption and decryption test 20,000 times
|
||||
//Random encryption and decryption test 20,000 times
|
||||
int ret = OB_SUCCESS;
|
||||
srand((unsigned)time(NULL));
|
||||
int64_t index = 20000;
|
||||
while (index--) {
|
||||
int64_t buf_len = randmod(1024) + 1;
|
||||
char* buf = new char[buf_len];
|
||||
int64_t buf_len = randmod(1024) + 1;
|
||||
char *buf = new char[buf_len];
|
||||
char key[16];
|
||||
for (int i = 0; i < buf_len; ++i) {
|
||||
char tmp = 'a';
|
||||
@ -69,27 +70,29 @@ TEST_F(TestNationalEncrypt, random_test)
|
||||
}
|
||||
buf[i] = tmp + randmod(26);
|
||||
}
|
||||
for (auto& c : key) {
|
||||
for (auto &c : key) {
|
||||
c = 'a' + randmod(26);
|
||||
}
|
||||
char* encrypt_buf = new char[(buf_len / 16 + 1) * 16];
|
||||
char* decrypt_buf = new char[(buf_len / 16 + 1) * 16];
|
||||
char *encrypt_buf = new char[(buf_len/16 + 1) * 16];
|
||||
char *decrypt_buf = new char[(buf_len/16 + 1) * 16];
|
||||
int64_t encrypt_len = 0, decrypt_len = 0;
|
||||
ret = ObSm4Encryption::sm4_encrypt(key, 16, buf, buf_len, (buf_len / 16 + 1) * 16, encrypt_buf, encrypt_len);
|
||||
ret = ObSm4Encryption::sm4_encrypt(key, 16, buf, buf_len, (buf_len/16 + 1) * 16, encrypt_buf, encrypt_len);
|
||||
ASSERT_EQ(ret, OB_SUCCESS);
|
||||
ASSERT_EQ(encrypt_len, (buf_len / 16 + 1) * 16);
|
||||
ret = ObSm4Encryption::sm4_decrypt(
|
||||
key, 16, encrypt_buf, encrypt_len, (buf_len / 16 + 1) * 16, decrypt_buf, decrypt_len);
|
||||
ASSERT_EQ(encrypt_len, (buf_len/16 + 1) * 16);
|
||||
ret = ObSm4Encryption::sm4_decrypt(key, 16, encrypt_buf, encrypt_len, (buf_len / 16 + 1) * 16, decrypt_buf, decrypt_len);
|
||||
ASSERT_EQ(ret, OB_SUCCESS);
|
||||
ASSERT_EQ(decrypt_len, buf_len);
|
||||
ASSERT_EQ(true, equal(decrypt_buf, buf, buf_len));
|
||||
delete[] buf;
|
||||
delete[] encrypt_buf;
|
||||
delete[] decrypt_buf;
|
||||
delete []buf;
|
||||
delete []encrypt_buf;
|
||||
delete []decrypt_buf;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
OB_LOGGER.set_log_level("WARN");
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
|
||||
Reference in New Issue
Block a user