You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.2 KiB
131 lines
4.2 KiB
/** |
|
* Test for packet pool and packet buffer |
|
* Tests allocation, reuse, overflow and basic operations |
|
*/ |
|
|
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <assert.h> |
|
#include <arpa/inet.h> |
|
|
|
#include "../src/packet_pool.h" |
|
#include "../src/packet_buffer.h" |
|
|
|
int main() { |
|
printf("=== Packet Pool Test ===\n"); |
|
|
|
// Test 1: Initialize packet pool |
|
printf("Test 1: Initializing packet pool..."); |
|
packet_pool_t pool; |
|
packet_pool_init(&pool); |
|
printf(" PASS\n"); |
|
|
|
// Test 2: Get statistics (should show some pre-allocated packets) |
|
printf("Test 2: Getting initial statistics..."); |
|
size_t allocs, reuse, overflow; |
|
packet_pool_get_stats(&pool, &allocs, &reuse, &overflow); |
|
printf(" PASS (pre-alloc done)\n"); |
|
|
|
// Test 3: Allocate packet from pool |
|
printf("Test 3: Allocating packet from pool..."); |
|
packet_buffer_t* pkt1 = packet_pool_get(&pool); |
|
assert(pkt1 != NULL); |
|
assert(packet_data_len(pkt1) == 0); |
|
printf(" PASS\n"); |
|
|
|
// Test 4: Set and get data |
|
printf("Test 4: Setting packet data..."); |
|
const char* test_data = "Test packet data"; |
|
size_t test_len = strlen(test_data); |
|
memcpy(packet_data(pkt1), test_data, test_len); |
|
packet_set_data_len(pkt1, test_len); |
|
assert(packet_data_len(pkt1) == test_len); |
|
assert(memcmp(packet_data(pkt1), test_data, test_len) == 0); |
|
printf(" PASS\n"); |
|
|
|
// Test 5: Test metadata |
|
printf("Test 5: Testing packet metadata..."); |
|
packet_set_encrypted(pkt1, 1); |
|
assert(packet_is_encrypted(pkt1) == 1); |
|
packet_set_encrypted(pkt1, 0); |
|
assert(packet_is_encrypted(pkt1) == 0); |
|
|
|
packet_set_fragmented(pkt1, 1); |
|
int frag = packet_is_fragmented(pkt1); |
|
if (frag == 0) { |
|
printf(" FAIL: expected non-zero, got %d\n", frag); |
|
return 1; |
|
} |
|
assert(frag != 0); |
|
printf(" PASS\n"); |
|
|
|
packet_set_stage(pkt1, PACKET_STAGE_RX_DECRYPTED); |
|
assert(packet_stage(pkt1) == PACKET_STAGE_RX_DECRYPTED); |
|
printf(" PASS\n"); |
|
|
|
// Test 6: Return packet to pool |
|
printf("Test 6: Returning packet to pool..."); |
|
packet_pool_put(&pool, pkt1); |
|
printf(" PASS\n"); |
|
|
|
// Test 7: Get another packet (should reuse the returned one) |
|
printf("Test 7: Checking packet reuse..."); |
|
packet_buffer_t* pkt2 = packet_pool_get(&pool); |
|
assert(pkt2 != NULL); |
|
packet_pool_get_stats(&pool, &allocs, &reuse, &overflow); |
|
assert(reuse > 0); // Should have reused some packets |
|
printf(" PASS (reuse detected)\n"); |
|
|
|
// Test 8: Allocate many packets to test overflow |
|
printf("Test 8: Testing pool overflow..."); |
|
packet_buffer_t* overflow_packets[PACKET_POOL_SIZE * 2]; |
|
int overflow_count = 0; |
|
|
|
// Allocate more than pool size |
|
for (int i = 0; i < PACKET_POOL_SIZE * 2; i++) { |
|
overflow_packets[i] = packet_pool_get(&pool); |
|
if (overflow_packets[i] != NULL) { |
|
overflow_count++; |
|
} |
|
} |
|
|
|
// Return all packets |
|
for (int i = 0; i < overflow_count; i++) { |
|
packet_pool_put(&pool, overflow_packets[i]); |
|
} |
|
|
|
packet_pool_get_stats(&pool, &allocs, &reuse, &overflow); |
|
printf(" PASS (allocated %d packets, %zu overflows)\n", overflow_count, overflow); |
|
|
|
// Test 9: Test address metadata |
|
printf("Test 9: Testing address metadata..."); |
|
packet_buffer_t* pkt3 = packet_pool_get(&pool); |
|
|
|
struct sockaddr_in test_addr; |
|
test_addr.sin_family = AF_INET; |
|
test_addr.sin_port = htons(12345); |
|
inet_pton(AF_INET, "192.168.1.1", &test_addr.sin_addr); |
|
|
|
memcpy(packet_src_addr(pkt3), &test_addr, sizeof(test_addr)); |
|
assert(packet_src_port(pkt3) == htons(12345)); |
|
|
|
packet_pool_put(&pool, pkt3); |
|
printf(" PASS\n"); |
|
|
|
// Test 10: Destroy pool |
|
printf("Test 10: Destroying packet pool..."); |
|
packet_pool_destroy(&pool); |
|
printf(" PASS\n"); |
|
|
|
// Test 11: Test packet size constants |
|
printf("Test 11: Checking packet size constants..."); |
|
assert(PACKET_TOTAL_SIZE == 1600); |
|
assert(PACKET_DATA_SIZE == 1536); |
|
assert(PACKET_METADATA_SIZE == 64); |
|
printf(" PASS (metadata=%d, data=%d, total=%d)\n", |
|
PACKET_METADATA_SIZE, PACKET_DATA_SIZE, PACKET_TOTAL_SIZE); |
|
|
|
printf("\n=== All Packet Pool Tests Passed ===\n"); |
|
return 0; |
|
} |