This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
mesa-platform-quic/test/quic_test_plug.cpp

138 lines
4.1 KiB
C++

/*
* author:yangwei
* create time:2021-8-21
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "cJSON.h"
#ifdef __cplusplus
extern "C"
{
#endif
#include "quic.h"
#include <stellar/stellar.h>
#include <stellar/session.h>
#include <stellar/session_mq.h>
#include <stellar/session_exdata.h>
extern "C" int commit_test_result_json(cJSON *node, const char *name);
extern "C" int quic_version_int2string(unsigned int version, char *buff, int buff_len);
static int g_result_count = 1;
#if 0
#define DEBUG_PRINT(fmt, args...) fprintf(stderr, fmt, ##args)
#else
#define DEBUG_PRINT(fmt, args...)
#endif
struct quic_gtest_context
{
cJSON *json_root;
};
static void cJSON_Add_QStringToObject(cJSON * object, const char *name, const qstring * qstring)
{
char *tmp = (char *)calloc(1, qstring->iov_len + 1);
memcpy(tmp, qstring->iov_base, qstring->iov_len);
cJSON_AddStringToObject(object, name, tmp);
}
extern "C" void QUIC_TEST_PLUG_ENTRY(struct session *sess, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env)
{
struct quic_gtest_context *qctx = (struct quic_gtest_context *)per_session_ctx;
struct quic_message *qmsg = (struct quic_message *)msg;
enum quic_message_type mtype = quic_message_type_get(qmsg);
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: mtype=%d\n", (int)mtype);
switch(mtype){
case QUIC_NEW:
qctx->json_root = cJSON_CreateObject();
cJSON_AddStringToObject(qctx->json_root, "Tuple4", session_get0_readable_addr(sess));
break;
case QUIC_VERSION:
{
unsigned int version_int = 0;
char version_str[128]={0};
quic_message_get_version(qmsg, &version_int);
assert(version_int != 0);
quic_version_int2string(version_int, version_str, sizeof(version_str));
cJSON_AddStringToObject(qctx->json_root, "VERSION", version_str);
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: version=%x, str_version=%s\n", version_int, version_str);
}
break;
case QUIC_SNI:
{
qstring result = {};
quic_message_get_sni(qmsg, &result);
cJSON_Add_QStringToObject(qctx->json_root, "SNI", &result);
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: len=%d, SNI=%p, %.*s\n", (int)result.str_len, result.str, (int)result.str_len, result.str);
}
break;
case QUIC_USER_AGENT:
{
qstring result = {};
quic_message_get_user_agent(qmsg, &result);
cJSON_Add_QStringToObject(qctx->json_root, "UA", &result);
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: len=%d, UA=%p, %.*s\n", (int)result.str_len, result.str, (int)result.str_len, result.str);
}
break;
case QUIC_FREE:
{
char result_name[16]="";
sprintf(result_name,"QUIC_RESULT_%d", g_result_count);
commit_test_result_json(qctx->json_root, result_name);
g_result_count+=1;
}
break;
default:
break;
}
return ;
}
extern "C" void *quic_gtest_plug_session_ctx_new_cb(struct session *sess, void *plugin_env)
{
struct quic_gtest_context *ctx = (struct quic_gtest_context *)calloc(1, sizeof(struct quic_gtest_context));
return ctx;
}
extern "C" void quic_gtest_session_ctx_free_cb(struct session *sess, void *session_ctx, void *plugin_env)
{
free(session_ctx);
}
extern "C" void *QUIC_TEST_PLUG_INIT(struct stellar *st)
{
void *fake_quic_gtest_plugin_env = (void *)"_fake_plugin_env_";
int quic_gtest_plug_id = stellar_session_plugin_register(st, quic_gtest_plug_session_ctx_new_cb, quic_gtest_session_ctx_free_cb, fake_quic_gtest_plugin_env);
int quic_topic_id = stellar_session_mq_get_topic_id(st, QUIC_DECODER_TOPIC);
assert(quic_topic_id >= 0);
stellar_session_mq_subscribe(st, quic_topic_id, QUIC_TEST_PLUG_ENTRY, quic_gtest_plug_id);
return fake_quic_gtest_plugin_env;
}
extern "C" void QUIC_TEST_PLUG_DESTROY(void *plugin_env)
{
return ;
}
#ifdef __cplusplus
}
#endif