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.
 
 
 
 
 
 

114 lines
3.7 KiB

#ifndef UTUN_TEST_FRAMEWORK_H
#define UTUN_TEST_FRAMEWORK_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "utun_test_hooks.h"
#include "test_virtual_tun.h"
#include "test_udp_socket.h"
// Forward declarations
struct UTUN_INSTANCE;
struct UASYNC;
// Test instance configuration
struct test_instance_config {
const char* config_file;
uint64_t node_id;
int port_base;
bool enable_tun;
bool enable_socket_hooks;
bool enable_packet_capture;
const char* log_file;
};
// Test instance with virtual components
struct test_instance {
struct UTUN_INSTANCE* utun;
struct UASYNC* ua;
// Virtual components
struct virtual_tun* vtun;
struct test_udp_socket** udp_sockets;
int udp_socket_count;
int udp_socket_capacity;
// Packet capture
struct {
uint8_t** packets;
size_t* lengths;
char** directions; // "tun_in", "tun_out", "udp_in", "udp_out"
size_t count;
size_t capacity;
} captured_packets;
// Test configuration
struct test_instance_config config;
// Test context
void* user_context;
// Test hooks (for internal use)
struct utun_test_hooks hooks;
bool hooks_installed;
};
// Create test instance with virtual components
struct test_instance* test_create_instance(const struct test_instance_config* config);
// Destroy test instance
void test_destroy_instance(struct test_instance* test);
// Start test instance (initialize and run)
int test_start_instance(struct test_instance* test);
// Stop test instance
void test_stop_instance(struct test_instance* test);
// Inject packet into TUN interface
int test_inject_tun_packet(struct test_instance* test, const uint8_t* packet, size_t len);
// Inject UDP packet
int test_inject_udp_packet(struct test_instance* test, int socket_fd,
const uint8_t* packet, size_t len,
const struct sockaddr* src_addr, socklen_t addr_len);
// Get captured packets
size_t test_get_captured_packets(struct test_instance* test,
const char* direction, // "tun_in", "tun_out", "udp_in", "udp_out"
uint8_t*** packets, size_t** lengths);
// Get captured packet count
size_t test_get_captured_packet_count(struct test_instance* test, const char* direction);
// Clear captured packets
void test_clear_captured_packets(struct test_instance* test);
// Wait for packet with timeout (milliseconds)
int test_wait_for_packet(struct test_instance* test,
const char* direction,
int timeout_ms);
// Two-instance test runner
int test_run_two_instances(const struct test_instance_config* server_config,
const struct test_instance_config* client_config,
void (*test_scenario)(struct test_instance* server,
struct test_instance* client),
int timeout_ms);
// Get test instance statistics
void test_get_instance_stats(struct test_instance* test, size_t* tun_packets_sent,
size_t* tun_packets_received, size_t* udp_packets_sent,
size_t* udp_packets_received);
// Utility functions
const char* test_get_direction_name(const char* direction);
int test_compare_packets(const uint8_t* pkt1, size_t len1, const uint8_t* pkt2, size_t len2);
void test_dump_packet(const char* prefix, const uint8_t* packet, size_t len);
// Default test configurations
extern const struct test_instance_config TEST_SERVER_CONFIG;
extern const struct test_instance_config TEST_CLIENT_CONFIG;
#endif // UTUN_TEST_FRAMEWORK_H