OCT 1.对部分功能进行单元测试

This commit is contained in:
dongwenze 2023-03-20 15:43:33 +08:00
parent 7d95eda1b6
commit f1524656e4
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,41 @@
//
// 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]);
}
}
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);
}
}

View File

@ -0,0 +1,54 @@
//
// Created by dongwenzhe on 2023/3/17.
//
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "s2j/s2j.h"
typedef struct {
char info[16];
} Message;
typedef struct {
int id;
char name[16];
Message msg;
} Service;
TEST_SUITE("S2J_TEST") {
TEST_CASE("S2J") {
Service origin_Server;
origin_Server.id = 3;
strcpy(origin_Server.name, "dhcpServer");
strcpy(origin_Server.msg.info, "opendhcpd");
Service *pServer = &origin_Server;
s2j_create_json_obj(testServer);
s2j_json_set_basic_element(testServer, pServer, int, id);
s2j_json_set_basic_element(testServer, pServer, string, name);
s2j_json_set_struct_element(testInfo, testServer, pMsg, pServer, Message, msg);
s2j_json_set_basic_element(testInfo, pMsg, string, info);
int i;
CHECK_EQ(testServer->child->valueint, 3);
for(i = 0; i < strlen(origin_Server.name); i++) {
CHECK_EQ(origin_Server.name[i], testServer->child->next->valuestring[i]);
}
for(i = 0; i < strlen(origin_Server.msg.info); i++) {
CHECK_EQ(origin_Server.msg.info[i], testServer->child->next->next->child->valuestring[i]);
}
}
TEST_CASE("J2S") {
char originServer[] = R"({"id":3, "name":"dhcpServer", "message":{"info":"opendhcpd"}})";
cJSON *testServer = cJSON_Parse(originServer);
s2j_create_struct_obj(pServer, Service);
s2j_struct_get_basic_element(pServer, testServer, int, id);
s2j_struct_get_basic_element(pServer, testServer, string, name);
s2j_struct_get_struct_element(pMsg, pServer, testInfo, testServer, Message, msg);
s2j_struct_get_basic_element(pMsg, testInfo, string, info);
CHECK_EQ(pServer->id, 3);
}
}