diff --git a/src/tsg_sync_state.cpp b/src/tsg_sync_state.cpp index dd7231b..4f4b0ad 100644 --- a/src/tsg_sync_state.cpp +++ b/src/tsg_sync_state.cpp @@ -9,11 +9,9 @@ #include "tsg_send_log.h" #include "mpack.h" -char *mpack_data = NULL; -size_t mpack_size = 0; -static int mpack_init_map(const struct streaminfo *a_stream, mpack_writer_t *writer, const char *state) +static int mpack_init_map(const struct streaminfo *a_stream, mpack_writer_t *writer, const char *state, char **mpack_data, size_t *mpack_size) { - mpack_writer_init_growable(writer, &mpack_data, &mpack_size); + mpack_writer_init_growable(writer, mpack_data, mpack_size); mpack_build_map(writer); // tsync : 2.0 @@ -31,7 +29,7 @@ static int mpack_init_map(const struct streaminfo *a_stream, mpack_writer_t *wri return 0; } -static int mpack_send_pkt(const struct streaminfo *a_stream, mpack_writer_t *writer) +static int mpack_send_pkt(const struct streaminfo *a_stream, mpack_writer_t *writer, char **mpack_data, size_t *mpack_size) { mpack_complete_map(writer); // mpack_init_map if (mpack_writer_destroy(writer) != mpack_ok) @@ -40,11 +38,11 @@ static int mpack_send_pkt(const struct streaminfo *a_stream, mpack_writer_t *wri return -1; } - MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_DEBUG, "MSGPACK_PROXY_BUFF", "send buff_len = %lu", mpack_size); - sapp_inject_ctrl_pkt((struct streaminfo *)a_stream, SIO_DEFAULT, mpack_data, mpack_size, a_stream->routedir); - free(mpack_data); - mpack_data = NULL; - mpack_size = 0; + MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_DEBUG, "MSGPACK_PROXY_BUFF", "send buff_len = %lu", *mpack_size); + sapp_inject_ctrl_pkt((struct streaminfo *)a_stream, SIO_DEFAULT, *mpack_data, *mpack_size, a_stream->routedir); + free(*mpack_data); + *mpack_data = NULL; + *mpack_size = 0; return 0; } @@ -55,21 +53,23 @@ int tsg_send_session_state(const struct streaminfo *a_stream, unsigned char stat return -1; } + char *mpack_data = NULL; + size_t mpack_size = 0; mpack_writer_t writer; if (state == OP_STATE_PENDING) { - mpack_init_map(a_stream, &writer, "opening"); + mpack_init_map(a_stream, &writer, "opening", &mpack_data, &mpack_size); } else if (state == OP_STATE_CLOSE) { - mpack_init_map(a_stream, &writer, "closing"); + mpack_init_map(a_stream, &writer, "closing", &mpack_data, &mpack_size); } else { return -1; } - return mpack_send_pkt(a_stream, &writer); + return mpack_send_pkt(a_stream, &writer, &mpack_data, &mpack_size); } int tsg_sync_resetall_state(const struct streaminfo *a_stream) @@ -79,10 +79,12 @@ int tsg_sync_resetall_state(const struct streaminfo *a_stream) return -1; } + char *mpack_data = NULL; + size_t mpack_size = 0; mpack_writer_t writer; - mpack_init_map(a_stream, &writer, "resetall"); + mpack_init_map(a_stream, &writer, "resetall", &mpack_data, &mpack_size); - return mpack_send_pkt(a_stream, &writer); + return mpack_send_pkt(a_stream, &writer, &mpack_data, &mpack_size); } static void mpack_append_string(mpack_writer_t *writer, char *str) @@ -283,9 +285,10 @@ int tsg_sync_policy_update(const struct streaminfo *a_stream, struct update_poli return -1; } + char *mpack_data = NULL; + size_t mpack_size = 0; mpack_writer_t writer; - - mpack_init_map((struct streaminfo *)a_stream, &writer, "active"); + mpack_init_map((struct streaminfo *)a_stream, &writer, "active", &mpack_data, &mpack_size); // method: policy_update mpack_write_cstr(&writer, "method"); @@ -300,7 +303,7 @@ int tsg_sync_policy_update(const struct streaminfo *a_stream, struct update_poli } mpack_complete_map(&writer); // params - return mpack_send_pkt(a_stream, &writer); + return mpack_send_pkt(a_stream, &writer, &mpack_data, &mpack_size); } int tsg_sync_closing_state(const struct streaminfo *a_stream, unsigned char state) @@ -371,9 +374,9 @@ static void mpack_parse_append_profile_id(mpack_node_t profile_ids_node, uint32_ int mpack_parse_sce_profile_ids(const struct streaminfo *a_stream, mpack_tree_t tree, mpack_node_t sce_node) { mpack_node_t sf_profile_ids = mpack_node_map_cstr(sce_node, "sf_profile_ids"); - if (mpack_node_type(sf_profile_ids) != mpack_type_array) + if (mpack_node_type(sf_profile_ids) != mpack_type_array || mpack_node_array_length(sf_profile_ids) == 0) { - MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_INFO, "PARSE_SCE", "sf_profile_ids error! mpack_node_type(sf_profile_ids): %d", (int)mpack_node_type(sf_profile_ids)); + MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_INFO, "PARSE_SCE", "sf_profile_ids error! mpack_node_type(sf_profile_ids): %d, n_sf_profile_ids = 0", (int)mpack_node_type(sf_profile_ids)); mpack_tree_destroy(&tree); return -1; } @@ -387,13 +390,21 @@ int mpack_parse_sce_profile_ids(const struct streaminfo *a_stream, mpack_tree_t } mpack_parse_append_profile_id(sf_profile_ids, sce_handle->profile_ids, &sce_handle->n_profile_ids, SCE_PROFILE_IDS); - MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_DEBUG, "PARSE_SCE", "n_profile_ids: %lu;", sce_handle->n_profile_ids); + MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_DEBUG, "PARSE_SCE", "n_profile_ids: %lu", sce_handle->n_profile_ids); mpack_tree_destroy(&tree); return 0; } int mpack_parse_shaper_profile_ids(const struct streaminfo *a_stream, mpack_tree_t tree, mpack_node_t shaper_node) { + size_t n_shaper_rule = mpack_node_array_length(shaper_node); + if (n_shaper_rule == 0) + { + MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_INFO, "PARSE_SHAPER", "n_sh_profile_ids: 0"); + mpack_tree_destroy(&tree); + return -1; + } + struct shaper_log_update *shaper_handle = (struct shaper_log_update *)session_log_update_data_get(a_stream, TSG_SERVICE_SHAPING); if (shaper_handle == NULL) { @@ -402,7 +413,7 @@ int mpack_parse_shaper_profile_ids(const struct streaminfo *a_stream, mpack_tree session_log_update_data_put(a_stream, TSG_SERVICE_SHAPING, (void *)shaper_handle); } - shaper_handle->n_shaper_rule = MIN(mpack_node_array_length(shaper_node), SHAPR_RULE_IDS); + shaper_handle->n_shaper_rule = MIN(n_shaper_rule, SHAPR_RULE_IDS); mpack_node_t sh_ids_node; for (int i = 0; i < (int)shaper_handle->n_shaper_rule; i++) { @@ -465,6 +476,13 @@ int tsg_parse_log_update_payload(const struct streaminfo *a_stream, const void * mpack_tree_parse(&tree); mpack_node_t root = mpack_tree_root(&tree); + if (mpack_node_type(root) == mpack_type_nil) + { + MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_INFO, "PARSE_LOG_UPDATE", "mpack_tree_parse error! payload_len = %u", payload_len); + mpack_tree_destroy(&tree); + return -1; + } + mpack_node_t method = mpack_node_map_cstr(root, "method"); if (mpack_node_type(method) != mpack_type_str) { @@ -475,6 +493,7 @@ int tsg_parse_log_update_payload(const struct streaminfo *a_stream, const void * if (mpack_node_strlen(method) != strlen("log_update") || memcmp("log_update", mpack_node_str(method), strlen("log_update")) != 0) { + // mpack_node_str(method) is contiguous memory MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_INFO, "PARSE_LOG_UPDATE", "method error! mpack_node_strlen(method) = %lu", mpack_node_strlen(method)); mpack_tree_destroy(&tree); return -1; @@ -515,5 +534,7 @@ int tsg_parse_log_update_payload(const struct streaminfo *a_stream, const void * return mpack_parse_proxy_intercept_info(a_stream, tree, temp_node); } + MESA_handle_runtime_log(g_tsg_para.logger, RLOG_LV_DEBUG, "PDARSE_LOG_UPDATE", "pkt error! there is no log!"); + mpack_tree_destroy(&tree); return -1; } \ No newline at end of file diff --git a/test/src/gtest_sync_state.cpp b/test/src/gtest_sync_state.cpp index 642cd48..244a6bd 100644 --- a/test/src/gtest_sync_state.cpp +++ b/test/src/gtest_sync_state.cpp @@ -615,7 +615,7 @@ TEST(POLICY_UPDATE, ProxyOverLimit) gtest_cmsg_destroy(&cmsg); } -void gtest_get_sce_mpack_date(char **mpack_data, size_t *mpack_size, uint64_t session_id, char *method, int n_ids) +void gtest_get_sce_mpack_date(char **mpack_data, size_t *mpack_size, uint64_t session_id, char *method, int n_ids, const char *sce) { mpack_writer_t writer; mpack_writer_init_growable(&writer, mpack_data, mpack_size); @@ -641,7 +641,7 @@ void gtest_get_sce_mpack_date(char **mpack_data, size_t *mpack_size, uint64_t se mpack_write_cstr(&writer, "params"); mpack_build_map(&writer); { - mpack_write_cstr(&writer, "sce"); + mpack_write_cstr(&writer, sce); mpack_build_map(&writer); { mpack_write_cstr(&writer, "sf_profile_ids"); @@ -678,17 +678,26 @@ TEST(LOG_UPDATE, SceIllegalLog) size_t mpack_size = 0; const struct streaminfo a_stream = {0}; - // error session - gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 123456789, (char *)"log_update", 3); - EXPECT_EQ(-1, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); + // error payload_len + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 123456789, (char *)"log_update", 3, "sce"); + EXPECT_EQ(-1, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size - 1)); void *result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); EXPECT_FALSE(result); free(mpack_data); mpack_data = NULL; mpack_size = 0; + // error session + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 123456789, (char *)"log_update", 3, "sce"); + EXPECT_EQ(-1, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); + result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); + EXPECT_FALSE(result); + free(mpack_data); + mpack_data = NULL; + mpack_size = 0; + // error method - gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"policy_update", 3); + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"policy_update", 3, "sce"); EXPECT_EQ(-1, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); EXPECT_FALSE(result); @@ -697,7 +706,16 @@ TEST(LOG_UPDATE, SceIllegalLog) mpack_size = 0; // n_profile_ids = 0 - gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 0); + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 0, "sce"); + EXPECT_EQ(-1, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); + result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); + EXPECT_FALSE(result); + free(mpack_data); + mpack_data = NULL; + mpack_size = 0; + + // no sce proxy shaper + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 0, "sce_test_no_log"); EXPECT_EQ(-1, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); EXPECT_FALSE(result); @@ -712,7 +730,7 @@ TEST(LOG_UPDATE, SceNormal) size_t mpack_size = 0; const struct streaminfo a_stream = {0}; - gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 3); + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 3, "sce"); EXPECT_EQ(0, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); void *result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); @@ -741,7 +759,7 @@ TEST(LOG_UPDATE, SceTwice) size_t mpack_size = 0; const struct streaminfo a_stream = {0}; - gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 3); + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 3, "sce"); EXPECT_EQ(0, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); void *result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); @@ -750,7 +768,7 @@ TEST(LOG_UPDATE, SceTwice) mpack_data = NULL; mpack_size = 0; - gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 5); + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 5, "sce"); EXPECT_EQ(0, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); void *result_2 = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); @@ -779,7 +797,7 @@ TEST(LOG_UPDATE, SceOverLimitProfileIds) size_t mpack_size = 0; const struct streaminfo a_stream = {0}; - gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 3 + SCE_PROFILE_IDS); + gtest_get_sce_mpack_date(&mpack_data, &mpack_size, 10, (char *)"log_update", 3 + SCE_PROFILE_IDS, "sce"); EXPECT_EQ(0, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); void *result = session_log_update_data_get(&a_stream, TSG_SERVICE_CHAINING); @@ -855,6 +873,23 @@ void gtest_get_shaper_mpack_date(char **mpack_data, size_t *mpack_size, uint64_t EXPECT_EQ(mpack_writer_destroy(&writer), mpack_ok); } +TEST(LOG_UPDATE, ShaperIllegalLog) +{ + char *mpack_data = NULL; + size_t mpack_size = 0; + const struct streaminfo a_stream = {0}; + long long rule[4] = {2, 3, 4, 5}; + + // n_sh_profile_ids = 0; + gtest_get_shaper_mpack_date(&mpack_data, &mpack_size, 10, 0, rule, 3); + EXPECT_EQ(-1, tsg_parse_log_update_payload(&a_stream, mpack_data, mpack_size)); + void *result = session_log_update_data_get(&a_stream, TSG_SERVICE_SHAPING); + EXPECT_FALSE(result); + free(mpack_data); + mpack_data = NULL; + mpack_size = 0; +} + TEST(LOG_UPDATE, ShaperNormal) { char *mpack_data = NULL;