ztp/unit_test/test/json_test.cpp

138 lines
2.6 KiB
C++
Raw Normal View History

//
// Created by xajhu on 2019/11/29 0029.
//
#include <iostream>
using namespace std;
#include "gtest/gtest.h"
extern "C" {
#include "err_code.h"
#include "json_interface.h"
#include "restful.h"
}
TEST(json_encode_test, retOK)
{
int ret = 0;
const char *pJson;
AUTH_ZTH_REQ ztpReq;
memset(&ztpReq, 0, sizeof(AUTH_ZTH_REQ));
ztpReq.pESN = (char *)"ace08484843";
pJson = Struct2Json(&ztpReq, JE_AUTH_ZTP, false, &ret);
if(pJson) {
free((void *)pJson);
}
ASSERT_EQ(ERR_OK, ret);
}
TEST(json_decode_test, retOK)
{
#define JSON_STR ("{\"code\":\"fBBx8Q\",\"ip\":\"172.28.73.38\",\"iptype\":\"1\",\"status\":\"SUCCESS\"}")
int ret = 0;
PAUTH_ZTH_RSP pZTPRsp;
char *pJson = (char*)JSON_STR;
ret = Json2Struct(pJson, &pZTPRsp, JE_AUTH_ZTP, false);
if(pZTPRsp) {
if(pZTPRsp->status) {
free(pZTPRsp->status);
}
if(pZTPRsp->code) {
free(pZTPRsp->code);
}
if(pZTPRsp->ip) {
free(pZTPRsp->ip);
}
free(pZTPRsp);
}
ASSERT_EQ(0, ret);
}
TEST(ztp_auth, success)
{
#define JSON_REQ ("{\"ESN\": \"tt21\"}")
#define REQ_URL ("http://172.28.73.43:8088/device/esn")
int ret;
int size = 4096;
char buf[4096];
PAUTH_ZTH_RSP pZTPRsp;
memset(buf, 0, 4096);
ret = http_post_request(REQ_URL, JSON_REQ, (unsigned char*)buf, &size);
EXPECT_EQ(ERR_OK, ret);
ret = Json2Struct(buf, &pZTPRsp, JE_AUTH_ZTP, false);
ASSERT_EQ(ERR_OK, ret);
ASSERT_STRCASEEQ(pZTPRsp->status, "SUCCESS");
if(pZTPRsp) {
if(pZTPRsp->status) {
free(pZTPRsp->status);
}
if(pZTPRsp->code) {
free(pZTPRsp->code);
}
if(pZTPRsp->ip) {
free(pZTPRsp->ip);
}
free(pZTPRsp);
}
}
TEST(ztp_auth, failed)
{
#define JSON_REQ_FAIL ("{\"ESN\": \"ace08484843\"}")
int ret;
int size = 4096;
char buf[4096];
PAUTH_ZTH_RSP pZTPRsp;
memset(buf, 0, 4096);
ret = http_post_request(REQ_URL, JSON_REQ_FAIL, (unsigned char*)buf, &size);
EXPECT_EQ(ERR_OK, ret);
ret = Json2Struct(buf, &pZTPRsp, JE_AUTH_ZTP, false);
ASSERT_EQ(ERR_OK, ret);
ASSERT_STRCASEEQ(pZTPRsp->status, "FAILED");
if(pZTPRsp) {
if(pZTPRsp->status) {
free(pZTPRsp->status);
}
if(pZTPRsp->code) {
free(pZTPRsp->code);
}
if(pZTPRsp->ip) {
free(pZTPRsp->ip);
}
free(pZTPRsp);
}
}
auto main(int argc, char *argv[]) -> int
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}