From da47b8044254bf392158284cc232d707b054251c Mon Sep 17 00:00:00 2001 From: yangwei Date: Thu, 14 Jul 2022 14:46:16 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(packet=5Fio):=20add=20gtest=5F?= =?UTF-8?q?packet=5Fio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CMakeLists.txt | 2 ++ src/main.cpp | 14 +++++++------- src/packet_io/CMakeLists.txt | 4 +++- src/packet_io/packet_io.cpp | 14 +++++++------- src/packet_io/packet_io.h | 23 ++++++++++++++--------- src/packet_io/test/CMakeLists.txt | 12 ++++++++++++ src/packet_io/test/gtest_packet_io.cpp | 15 +++++++++++++++ 7 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 src/packet_io/test/CMakeLists.txt create mode 100644 src/packet_io/test/gtest_packet_io.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c2c1b6a..2742da1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,8 @@ include_directories(${CMAKE_SOURCE_DIR}/src/packet_io/) include_directories(${CMAKE_SOURCE_DIR}/src/session_manager/) include_directories(${CMAKE_SOURCE_DIR}/src/plugin_manager/) + +enable_testing() add_subdirectory(packet_io) add_subdirectory(session_manager) add_subdirectory(plugin_manager) diff --git a/src/main.cpp b/src/main.cpp index 59ebfad..b607270 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ struct packet_io_loop_arg { - struct device *dev; + struct packet_io_device *dev; int thread_id; }; @@ -15,7 +15,7 @@ void packet_io_loop(struct packet_io_loop_arg *arg) struct session_event *event; while(1) { - int fetch_num = packetio_rx(arg->dev, arg->thread_id, &rx_pkt, 1); + int fetch_num = packet_io_rx(arg->dev, arg->thread_id, &rx_pkt, 1); if(fetch_num > 0) { event = session_manager_commit(rx_pkt); @@ -26,7 +26,7 @@ void packet_io_loop(struct packet_io_loop_arg *arg) } //clean session_manager event queue - packetio_tx(arg->dev, arg->thread_id, &rx_pkt, 1); + packet_io_tx(arg->dev, arg->thread_id, &rx_pkt, 1); } else { @@ -39,10 +39,10 @@ void packet_io_loop(struct packet_io_loop_arg *arg) } -struct device *packetio_init(int worker_thread_num, const char *instance_name, const char *device_name) +struct packet_io_device *packet_io_init(int worker_thread_num, const char *instance_name, const char *device_name) { - struct packetio_instance * instance = packetio_create(instance_name); - struct device *dev = packetio_open_device(instance, device_name, worker_thread_num, worker_thread_num); + struct packet_io_instance * instance = packet_io_create(PACKET_IO_PCAP, instance_name); + struct packet_io_device *dev = packet_io_open_device(instance, device_name, worker_thread_num, worker_thread_num); return dev; } @@ -51,7 +51,7 @@ int main(int argc, char ** argv) //config_manager_init session_manager_session_event_register(http_decoder, SESSION_TYPE_HTTP); - //packetio_init + //packet_io_init //create_worker_thread diff --git a/src/packet_io/CMakeLists.txt b/src/packet_io/CMakeLists.txt index cafcb27..a409b29 100644 --- a/src/packet_io/CMakeLists.txt +++ b/src/packet_io/CMakeLists.txt @@ -1,4 +1,6 @@ add_library(packet_io packet_io.cpp -) \ No newline at end of file +) + +add_subdirectory(test) \ No newline at end of file diff --git a/src/packet_io/packet_io.cpp b/src/packet_io/packet_io.cpp index eaf144a..43b9e7d 100644 --- a/src/packet_io/packet_io.cpp +++ b/src/packet_io/packet_io.cpp @@ -1,37 +1,37 @@ #include "packet_io.h" -struct packetio_instance *packetio_create(const char *instance_name) +struct packet_io_instance *packet_io_create(packe_io_mode mode, const char *instance_name) { return nullptr; } -struct packetio_instance *packetio_current() +struct packet_io_instance *packet_io_current() { return nullptr; } -void packetio_destory(struct packetio_instance *instance) +void packet_io_destory(struct packet_io_instance *instance) { return; } -struct device *packetio_open_device(struct packetio_instance *instance, const char *dev_sym, uint32_t nr_rxqueue, uint32_t nr_txqueue) +struct packet_io_device *packet_io_open_device(struct packet_io_instance *instance, const char *dev_sym, uint32_t nr_rxqueue, uint32_t nr_txqueue) { return nullptr; } -void packetio_close_device(struct device *dev) +void packet_io_close_device(struct packet_io_device *dev) { return; } -int packetio_rx(struct device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p) +int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p) { return 0; } -int packetio_tx(struct device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p) +int packet_io_tx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p) { return 0; } \ No newline at end of file diff --git a/src/packet_io/packet_io.h b/src/packet_io/packet_io.h index ba7d959..85cc16f 100644 --- a/src/packet_io/packet_io.h +++ b/src/packet_io/packet_io.h @@ -3,16 +3,21 @@ #include -struct packetio_instance; -struct device; +struct packet_io_instance; +struct packet_io_device; -struct packetio_instance *packetio_create(const char *instance_name); -struct packetio_instance *packetio_current(); -void packetio_destory(struct packetio_instance *instance); +enum packe_io_mode +{ + PACKET_IO_PCAP, + PACKET_IO_MARSIO, +}; +struct packet_io_instance *packet_io_create(packe_io_mode mode, const char *instance_name); +struct packet_io_instance *packet_io_current(); +void packet_io_destory(struct packet_io_instance *instance); -struct device *packetio_open_device(struct packetio_instance *instance, const char *dev_sym, uint32_t nr_rxqueue, uint32_t nr_txqueue); -void packetio_close_device(struct device *dev); +struct packet_io_device *packet_io_open_device(struct packet_io_instance *instance, const char *dev_sym, uint32_t nr_rxqueue, uint32_t nr_txqueue); +void packet_io_close_device(struct packet_io_device *dev); -int packetio_rx(struct device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p); -int packetio_tx(struct device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p); \ No newline at end of file +int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p); +int packet_io_tx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p); \ No newline at end of file diff --git a/src/packet_io/test/CMakeLists.txt b/src/packet_io/test/CMakeLists.txt new file mode 100644 index 0000000..94aca33 --- /dev/null +++ b/src/packet_io/test/CMakeLists.txt @@ -0,0 +1,12 @@ +add_executable(gtest_packet_io + gtest_packet_io.cpp +) + +target_link_libraries( + gtest_packet_io + gtest_main + packet_io +) + +include(GoogleTest) +gtest_discover_tests(gtest_packet_io) \ No newline at end of file diff --git a/src/packet_io/test/gtest_packet_io.cpp b/src/packet_io/test/gtest_packet_io.cpp new file mode 100644 index 0000000..ff0ea7f --- /dev/null +++ b/src/packet_io/test/gtest_packet_io.cpp @@ -0,0 +1,15 @@ +#include + +#include "packet_io.h" + +TEST(PACKET_IO_Test, packet_io_create) { + EXPECT_EQ(packet_io_create(PACKET_IO_PCAP, "TEST"), nullptr); +} + +int main(int argc, char ** argv) +{ + int ret=0; + ::testing::InitGoogleTest(&argc, argv); + ret=RUN_ALL_TESTS(); + return ret; +} \ No newline at end of file