#include #include "mpack.h" #include "control_packet.h" static u_char control_packet_active0[] = { 0x85, 0xA5, 0x74, 0x73, 0x79, 0x6E, 0x63, 0xA3, 0x32, 0x2E, 0x30, 0xAA, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x5F, 0x69, 0x64, 0xCF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xA5, 0x73, 0x74, 0x61, 0x74, 0x65, 0xA6, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0xA6, 0x6D, 0x65, 0x74, 0x68, 0x6F, 0x64, 0xAD, 0x70, 0x6F, 0x6C, 0x69, 0x63, 0x79, 0x5F, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0xA6, 0x70, 0x61, 0x72, 0x61, 0x6D, 0x73, 0x83, 0xA3, 0x73, 0x63, 0x65, 0x81, 0xA8, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xA6, 0x73, 0x68, 0x61, 0x70, 0x65, 0x72, 0x81, 0xA8, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xA5, 0x70, 0x72, 0x6F, 0x78, 0x79, 0x82, 0xA8, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xAD, 0x74, 0x63, 0x70, 0x5F, 0x68, 0x61, 0x6E, 0x64, 0x73, 0x68, 0x61, 0x6B, 0x65, 0xDC, 0x00, 0x24, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0x92, 0x01, 0x01, 0x92, 0x01, 0x01}; static u_char control_packet_active1[] = { 0x85, 0xA5, 0x74, 0x73, 0x79, 0x6E, 0x63, 0xA3, 0x32, 0x2E, 0x30, 0xAA, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x5F, 0x69, 0x64, 0xCF, 0x04, 0x08, 0x02, 0x1B, 0x68, 0x4C, 0x03, 0xE9, 0xA5, 0x73, 0x74, 0x61, 0x74, 0x65, 0xA6, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0xA6, 0x6D, 0x65, 0x74, 0x68, 0x6F, 0x64, 0xAD, 0x70, 0x6F, 0x6C, 0x69, 0x63, 0x79, 0x5F, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0xA6, 0x70, 0x61, 0x72, 0x61, 0x6D, 0x73, 0x81, 0xA3, 0x73, 0x63, 0x65, 0x81, 0xA8, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x91, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; static u_char control_packet_opening[] = { 0x83, 0xA5, 0x74, 0x73, 0x79, 0x6E, 0x63, 0xA3, 0x32, 0x2E, 0x30, 0xAA, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x5F, 0x69, 0x64, 0xCF, 0x04, 0x08, 0x02, 0x1B, 0x68, 0x4C, 0x03, 0xE9, 0xA5, 0x73, 0x74, 0x61, 0x74, 0x65, 0xA7, 0x6F, 0x70, 0x65, 0x6E, 0x69, 0x6E, 0x67}; static u_char control_packet_send[] = { 0x85, 0xA5, 0x74, 0x73, 0x79, 0x6E, 0x63, 0xA3, 0x32, 0x2E, 0x30, 0xAA, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x5F, 0x69, 0x64, 0x01, 0xA5, 0x73, 0x74, 0x61, 0x74, 0x65, 0xA6, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0xA6, 0x6D, 0x65, 0x74, 0x68, 0x6F, 0x64, 0xAA, 0x6C, 0x6F, 0x67, 0x5F, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0xA6, 0x70, 0x61, 0x72, 0x61, 0x6D, 0x73, 0x81, 0xA3, 0x73, 0x63, 0x65, 0x83, 0xB1, 0x73, 0x63, 0x5F, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x75, 0x75, 0x69, 0x64, 0x5F, 0x6C, 0x69, 0x73, 0x74, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xB4, 0x73, 0x63, 0x5F, 0x72, 0x73, 0x70, 0x5F, 0x72, 0x61, 0x77, 0x5F, 0x75, 0x75, 0x69, 0x64, 0x5F, 0x6C, 0x69, 0x73, 0x74, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xBA, 0x73, 0x63, 0x5F, 0x72, 0x73, 0x70, 0x5F, 0x64, 0x65, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x5F, 0x75, 0x75, 0x69, 0x64, 0x5F, 0x6C, 0x69, 0x73, 0x74, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04}; static uuid_t uuid1; static uuid_t uuid2; static uuid_t uuid3; static uuid_t uuid4; static uuid_t uuid5; static uuid_t uuid6; static const char *uuid1_str = "00000000-0000-0000-0000-000000000001"; static const char *uuid2_str = "00000000-0000-0000-0000-000000000002"; static const char *uuid3_str = "00000000-0000-0000-0000-000000000003"; static const char *uuid4_str = "00000000-0000-0000-0000-000000000004"; static const char *uuid5_str = "00000000-0000-0000-0000-000000000005"; static const char *uuid6_str = "00000000-0000-0000-0000-000000000006"; #if 1 TEST(CONTROL_PACKET, PACKAGE0) { char *data; size_t size; mpack_writer_t writer; mpack_writer_init_growable(&writer, &data, &size); // write the example on the msgpack homepage mpack_build_map(&writer); // root begin // tsync mpack_write_cstr(&writer, "tsync"); mpack_write_cstr(&writer, "2.0"); // session_id mpack_write_cstr(&writer, "session_id"); mpack_write_u64(&writer, 18446744073709551615); // state mpack_write_cstr(&writer, "state"); mpack_write_cstr(&writer, "active"); // method mpack_write_cstr(&writer, "method"); mpack_write_cstr(&writer, "policy_update"); // params { mpack_write_cstr(&writer, "params"); mpack_build_map(&writer); // params value begin // sce { mpack_write_cstr(&writer, "sce"); mpack_build_map(&writer); mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); mpack_write_bin(&writer, (const char *)&uuid1, sizeof(uuid1)); mpack_write_bin(&writer, (const char *)&uuid2, sizeof(uuid2)); mpack_complete_array(&writer); mpack_complete_map(&writer); } // shaper { mpack_write_cstr(&writer, "shaper"); mpack_build_map(&writer); mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); mpack_write_bin(&writer, (const char *)&uuid3, sizeof(uuid3)); mpack_write_bin(&writer, (const char *)&uuid4, sizeof(uuid4)); mpack_complete_array(&writer); mpack_complete_map(&writer); } // proxy { mpack_write_cstr(&writer, "proxy"); mpack_build_map(&writer); // proxy value begin mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); mpack_write_bin(&writer, (const char *)&uuid5, sizeof(uuid5)); mpack_write_bin(&writer, (const char *)&uuid6, sizeof(uuid6)); mpack_complete_array(&writer); mpack_write_cstr(&writer, "tcp_handshake"); mpack_build_array(&writer); // tcp_handshake value begin mpack_write_u32(&writer, 1); // tcp_seq mpack_write_u32(&writer, 1); // tcp_ack mpack_write_u16(&writer, 1); // tcp_mss_client mpack_write_u16(&writer, 1); // tcp_mss_server mpack_write_u8(&writer, 1); // tcp_wsacle_client mpack_write_u8(&writer, 1); // tcp_wsacle_server mpack_write_u8(&writer, 1); // tcp_sack_client mpack_write_u8(&writer, 1); // tcp_sack_server mpack_write_u8(&writer, 1); // tcp_ts_client mpack_write_u8(&writer, 1); // tcp_ts_server mpack_write_u8(&writer, 1); // tcp_protocol mpack_write_u16(&writer, 1); // tcp_window_client mpack_write_u16(&writer, 1); // tcp_window_server mpack_write_u32(&writer, 1); // tcp_ts_client_val mpack_write_u32(&writer, 1); // tcp_ts_server_val mpack_write_u8(&writer, 1); // tcp_info_packet_cur_dir mpack_write_str(&writer, "123", 3); // src_sub_id mpack_write_str(&writer, "123", 3); // dst_sub_id mpack_write_str(&writer, "123", 3); // src_asn mpack_write_str(&writer, "123", 3); // dst_asn mpack_write_str(&writer, "123", 3); // src_organization mpack_write_str(&writer, "123", 3); // dst_organization mpack_write_str(&writer, "123", 3); // src_ip_location_country mpack_write_str(&writer, "123", 3); // dst_ip_location_country mpack_write_str(&writer, "123", 3); // src_ip_location_provine mpack_write_str(&writer, "123", 3); // dst_ip_location_provine mpack_write_str(&writer, "123", 3); // src_ip_location_city mpack_write_str(&writer, "123", 3); // dst_ip_location_city mpack_write_str(&writer, "123", 3); // src_ip_location_subdivision mpack_write_str(&writer, "123", 3); // dst_ip_location_subdivision mpack_write_str(&writer, "123", 3); // ssl_client_ja3_fingerprint mpack_build_array(&writer); // fqdn_cat_id_val mpack_write_u32(&writer, 5555); mpack_write_u32(&writer, 6666); mpack_complete_array(&writer); mpack_build_array(&writer); // tcp_seq_sids mpack_write_u16(&writer, 5555); mpack_write_u16(&writer, 6666); mpack_complete_array(&writer); mpack_build_array(&writer); // tcp_ack_sids mpack_write_u16(&writer, 5555); mpack_write_u16(&writer, 6666); mpack_complete_array(&writer); mpack_build_array(&writer); // tcp_seq_route_ctx mpack_write_u8(&writer, 1); mpack_write_u8(&writer, 1); mpack_complete_array(&writer); mpack_build_array(&writer); // tcp_ack_route_ctx mpack_write_u8(&writer, 1); mpack_write_u8(&writer, 1); mpack_complete_array(&writer); mpack_complete_array(&writer); // tcp_handshake value end mpack_complete_map(&writer); // proxy value end } mpack_complete_map(&writer); // params value end } mpack_complete_map(&writer); // root end // finish writing EXPECT_TRUE(mpack_writer_destroy(&writer) == mpack_ok); EXPECT_TRUE(size == sizeof(control_packet_active0)); EXPECT_TRUE(memcmp(data, control_packet_active0, size) == 0); // data to hex for (size_t i = 0; i < size; i++) { printf("%02X, ", data[i]); } printf("\n"); free(data); } #endif #if 1 TEST(CONTROL_PACKET, PACKAGE1) { char *data; size_t size; mpack_writer_t writer; mpack_writer_init_growable(&writer, &data, &size); // write the example on the msgpack homepage mpack_build_map(&writer); // root begin // tsync mpack_write_cstr(&writer, "tsync"); mpack_write_cstr(&writer, "2.0"); // session_id mpack_write_cstr(&writer, "session_id"); mpack_write_u64(&writer, 290484492702581737); // state mpack_write_cstr(&writer, "state"); mpack_write_cstr(&writer, "active"); // method mpack_write_cstr(&writer, "method"); mpack_write_cstr(&writer, "policy_update"); // params { mpack_write_cstr(&writer, "params"); mpack_build_map(&writer); // params value begin // sce { mpack_write_cstr(&writer, "sce"); mpack_build_map(&writer); mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); mpack_write_bin(&writer, (const char *)&uuid1, sizeof(uuid1)); mpack_complete_array(&writer); mpack_complete_map(&writer); } mpack_complete_map(&writer); // params value end } mpack_complete_map(&writer); // root end // finish writing EXPECT_TRUE(mpack_writer_destroy(&writer) == mpack_ok); EXPECT_TRUE(size == sizeof(control_packet_active1)); EXPECT_TRUE(memcmp(data, control_packet_active1, size) == 0); // data to hex for (size_t i = 0; i < size; i++) { printf("%02X, ", data[i]); } printf("\n"); free(data); } #endif #if 1 TEST(CONTROL_PACKET, PACKAGE2) { char *data; size_t size; mpack_writer_t writer; mpack_writer_init_growable(&writer, &data, &size); // write the example on the msgpack homepage mpack_build_map(&writer); // root begin // tsync mpack_write_cstr(&writer, "tsync"); mpack_write_cstr(&writer, "2.0"); // session_id mpack_write_cstr(&writer, "session_id"); mpack_write_u64(&writer, 290484492702581737); // state mpack_write_cstr(&writer, "state"); mpack_write_cstr(&writer, "opening"); mpack_complete_map(&writer); // root end // finish writing EXPECT_TRUE(mpack_writer_destroy(&writer) == mpack_ok); EXPECT_TRUE(size == sizeof(control_packet_opening)); EXPECT_TRUE(memcmp(data, control_packet_opening, size) == 0); // data to hex for (size_t i = 0; i < size; i++) { printf("%02X, ", data[i]); } printf("\n"); free(data); } #endif #if 1 TEST(CONTROL_PACKET, PARSE0) { char rule_uuid_str[UUID_STRING_SIZE]; struct control_packet handler; EXPECT_TRUE(control_packet_parse(&handler, (const char *)control_packet_active0, sizeof(control_packet_active0)) == CTRL_PKT_SUCCESS); control_packet_dump(&handler); EXPECT_STREQ(handler.tsync, "2.0"); EXPECT_TRUE(handler.session_id == 18446744073709551615); EXPECT_TRUE(handler.state == SESSION_STATE_ACTIVE); EXPECT_STREQ(handler.method, "policy_update"); EXPECT_TRUE(uuid_array_get_count(&handler.rule_uuid_array) == 2); uuid_unparse(*uuid_array_get_at(&handler.rule_uuid_array, 0), rule_uuid_str); EXPECT_STREQ(rule_uuid_str, uuid1_str); uuid_unparse(*uuid_array_get_at(&handler.rule_uuid_array, 1), rule_uuid_str); EXPECT_STREQ(rule_uuid_str, uuid2_str); } #endif #if 1 TEST(CONTROL_PACKET, PARSE1) { char rule_uuid_str[UUID_STRING_SIZE]; struct control_packet handler; EXPECT_TRUE(control_packet_parse(&handler, (const char *)control_packet_active1, sizeof(control_packet_active1)) == CTRL_PKT_SUCCESS); control_packet_dump(&handler); EXPECT_STREQ(handler.tsync, "2.0"); EXPECT_TRUE(handler.session_id == 290484492702581737); EXPECT_TRUE(handler.state == SESSION_STATE_ACTIVE); EXPECT_STREQ(handler.method, "policy_update"); EXPECT_TRUE(uuid_array_get_count(&handler.rule_uuid_array) == 1); uuid_unparse(*uuid_array_get_at(&handler.rule_uuid_array, 0), rule_uuid_str); EXPECT_STREQ(rule_uuid_str, uuid1_str); } #endif #if 1 TEST(CONTROL_PACKET, PARSE2) { struct control_packet handler; EXPECT_TRUE(control_packet_parse(&handler, (const char *)control_packet_opening, sizeof(control_packet_opening)) == CTRL_PKT_SUCCESS); control_packet_dump(&handler); EXPECT_STREQ(handler.tsync, "2.0"); EXPECT_TRUE(handler.session_id == 290484492702581737); EXPECT_TRUE(handler.state == SESSION_STATE_OPENING); EXPECT_STREQ(handler.method, ""); EXPECT_TRUE(uuid_array_get_count(&handler.rule_uuid_array) == 0); } #endif #if 1 TEST(CONTROL_PACKET, SEND) { uint64_t session_id = 1; struct uuid_array rule_uuid_array; uuid_array_init(&rule_uuid_array); uuid_array_append(&rule_uuid_array, uuid1); uuid_array_append(&rule_uuid_array, uuid2); int rule_num = uuid_array_get_count(&rule_uuid_array); struct uuid_array sf_uuid_array; uuid_array_init(&sf_uuid_array); uuid_array_append(&sf_uuid_array, uuid3); uuid_array_append(&sf_uuid_array, uuid4); int sf_num = uuid_array_get_count(&sf_uuid_array); char *data; size_t size; mpack_writer_t writer; mpack_writer_init_growable(&writer, &data, &size); // write the example on the msgpack homepage mpack_build_map(&writer); // root begin // tsync mpack_write_cstr(&writer, "tsync"); mpack_write_cstr(&writer, "2.0"); // session_id mpack_write_cstr(&writer, "session_id"); mpack_write_u64(&writer, session_id); // state mpack_write_cstr(&writer, "state"); mpack_write_cstr(&writer, "active"); // method mpack_write_cstr(&writer, "method"); mpack_write_cstr(&writer, "log_update"); // params { mpack_write_cstr(&writer, "params"); mpack_build_map(&writer); // params value begin // sce { mpack_write_cstr(&writer, "sce"); mpack_build_map(&writer); // sce value begin { mpack_write_cstr(&writer, "sc_rule_uuid_list"); mpack_build_array(&writer); // sc_rule_list begin for (int i = 0; i < rule_num; i++) { mpack_write_bin(&writer, (const char *)uuid_array_get_at(&rule_uuid_array, i), sizeof(uuid_t)); } mpack_complete_array(&writer); // sc_rule_list end } { mpack_write_cstr(&writer, "sc_rsp_raw_uuid_list"); mpack_build_array(&writer); // sc_rsp_raw begin for (int i = 0; i < sf_num; i++) { mpack_write_bin(&writer, (const char *)uuid_array_get_at(&sf_uuid_array, i), sizeof(uuid_t)); } mpack_complete_array(&writer); // sc_rsp_raw end } { mpack_write_cstr(&writer, "sc_rsp_decrypted_uuid_list"); mpack_build_array(&writer); // sc_rsp_decrypted begin for (int i = 0; i < sf_num; i++) { mpack_write_bin(&writer, (const char *)uuid_array_get_at(&sf_uuid_array, i), sizeof(uuid_t)); } mpack_complete_array(&writer); // sc_rsp_decrypted end } mpack_complete_map(&writer); // sce value end } mpack_complete_map(&writer); // params value end } mpack_complete_map(&writer); // root end // finish writing if (mpack_writer_destroy(&writer) != mpack_ok) { assert(0); if (data) { free(data); data = NULL; } } for (size_t i = 0; i < size; i++) { printf("%02X, ", data[i]); } printf("\n"); EXPECT_STREQ(data, (const char *)control_packet_send); free(data); } #endif int main(int argc, char **argv) { uuid_parse(uuid1_str, uuid1); uuid_parse(uuid2_str, uuid2); uuid_parse(uuid3_str, uuid3); uuid_parse(uuid4_str, uuid4); uuid_parse(uuid5_str, uuid5); uuid_parse(uuid6_str, uuid6); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }