vcpe/unit_test/crypto/crypto_test.cpp

48 lines
1.3 KiB
C++
Raw Permalink Normal View History

//
// Created by dongwenzhe on 2023/3/14.
//
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "crypto.h"
TEST_SUITE("Crypto functions") {
TEST_CASE("BASE64") {
auto *pSrc = (unsigned char *)"HELLOWORLD";
const char *encode;
encode = base64_encode(pSrc, 16);
INFO("BASE64_ENCODING:", encode);
unsigned char *decode;
unsigned int outputSize = 0;
decode = base64_decode(encode, &outputSize);
INFO("BASE64_DECODING:", decode);
for(int i = 0; i<outputSize; i++) {
CHECK_EQ(decode[i], pSrc[i]);
}
2023-03-30 02:09:05 +00:00
free((void *)encode);
free((void *)decode);
}
TEST_CASE("AES") {
AES_TYPE algoType = DES3_CBC_PKCS7PADDING;
const char *data = "hello world";
const char *key = "NmmW4zO4FCrhpugLaoBgjA==";
unsigned char *out;
unsigned char *res;
int outputSize = 0;
int resSize = 0;
symmetric_encrypto(algoType, (unsigned char *)data, strlen(data), &out, &outputSize, key);
CHECK_NE(out, nullptr);
symmetric_decrypto(algoType, out, outputSize, &res, &resSize, key);
CHECK_NE(res, nullptr);
REQUIRE_MESSAGE((strlen(data) == resSize), "the decrypted message:", res);
2023-03-30 02:09:05 +00:00
free((void *)out);
free((void *)res);
}
}