LwIP 2.2.1在STM32F407VET6平台 移植

This commit is contained in:
冯佳
2026-01-29 17:26:04 +08:00
parent ffd33c4644
commit e7e10a8328
2207 changed files with 409723 additions and 165 deletions

View File

@ -0,0 +1,325 @@
#include "tcp_helper.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/stats.h"
#include "lwip/pbuf.h"
#include "lwip/inet.h"
#include "lwip/inet_chksum.h"
#include "lwip/ip_addr.h"
#if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
#error "This tests needs TCP- and MEMP-statistics enabled"
#endif
const ip_addr_t test_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1);
const ip_addr_t test_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2);
const ip_addr_t test_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
/** Remove all pcbs on the given list. */
static void
tcp_remove(struct tcp_pcb* pcb_list)
{
struct tcp_pcb *pcb = pcb_list;
struct tcp_pcb *pcb2;
while(pcb != NULL) {
pcb2 = pcb;
pcb = pcb->next;
if (pcb2->state == LISTEN) {
tcp_close(pcb2);
} else {
tcp_abort(pcb2);
}
}
}
/** Remove all pcbs on listen-, active- and time-wait-list (bound- isn't exported). */
void
tcp_remove_all(void)
{
tcp_remove(tcp_listen_pcbs.pcbs);
tcp_remove(tcp_bound_pcbs);
tcp_remove(tcp_active_pcbs);
tcp_remove(tcp_tw_pcbs);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_SEG) == 0);
fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
}
/** Create a TCP segment usable for passing to tcp_input */
static struct pbuf*
tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip,
u16_t src_port, u16_t dst_port, void* data, size_t data_len,
u32_t seqno, u32_t ackno, u8_t headerflags, u16_t wnd)
{
struct pbuf *p, *q;
struct ip_hdr* iphdr;
struct tcp_hdr* tcphdr;
u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len);
LWIP_ASSERT("data_len too big", data_len <= 0xFFFF);
p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
EXPECT_RETNULL(p != NULL);
/* first pbuf must be big enough to hold the headers */
EXPECT_RETNULL(p->len >= (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
if (data_len > 0) {
/* first pbuf must be big enough to hold at least 1 data byte, too */
EXPECT_RETNULL(p->len > (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
}
for(q = p; q != NULL; q = q->next) {
memset(q->payload, 0, q->len);
}
iphdr = (struct ip_hdr*)p->payload;
/* fill IP header */
iphdr->dest.addr = ip_2_ip4(dst_ip)->addr;
iphdr->src.addr = ip_2_ip4(src_ip)->addr;
IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
IPH_TOS_SET(iphdr, 0);
IPH_LEN_SET(iphdr, htons(p->tot_len));
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
/* let p point to TCP header */
pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
tcphdr = (struct tcp_hdr*)p->payload;
tcphdr->src = htons(src_port);
tcphdr->dest = htons(dst_port);
tcphdr->seqno = htonl(seqno);
tcphdr->ackno = htonl(ackno);
TCPH_HDRLEN_SET(tcphdr, sizeof(struct tcp_hdr)/4);
TCPH_FLAGS_SET(tcphdr, headerflags);
tcphdr->wnd = htons(wnd);
if (data_len > 0) {
/* let p point to TCP data */
pbuf_header(p, -(s16_t)sizeof(struct tcp_hdr));
/* copy data */
pbuf_take(p, data, (u16_t)data_len);
/* let p point to TCP header again */
pbuf_header(p, sizeof(struct tcp_hdr));
}
/* calculate checksum */
tcphdr->chksum = ip_chksum_pseudo(p,
IP_PROTO_TCP, p->tot_len, src_ip, dst_ip);
pbuf_header(p, sizeof(struct ip_hdr));
return p;
}
/** Create a TCP segment usable for passing to tcp_input */
struct pbuf*
tcp_create_segment(ip_addr_t* src_ip, ip_addr_t* dst_ip,
u16_t src_port, u16_t dst_port, void* data, size_t data_len,
u32_t seqno, u32_t ackno, u8_t headerflags)
{
return tcp_create_segment_wnd(src_ip, dst_ip, src_port, dst_port, data,
data_len, seqno, ackno, headerflags, TCP_WND);
}
/** Create a TCP segment usable for passing to tcp_input
* - IP-addresses, ports, seqno and ackno are taken from pcb
* - seqno and ackno can be altered with an offset
*/
struct pbuf*
tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_len, u32_t seqno_offset,
u32_t ackno_offset, u8_t headerflags)
{
return tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags);
}
/** Create a TCP segment usable for passing to tcp_input
* - IP-addresses, ports, seqno and ackno are taken from pcb
* - seqno and ackno can be altered with an offset
* - TCP window can be adjusted
*/
struct pbuf* tcp_create_rx_segment_wnd(struct tcp_pcb* pcb, void* data, size_t data_len,
u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags, u16_t wnd)
{
return tcp_create_segment_wnd(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags, wnd);
}
/** Safely bring a tcp_pcb into the requested state */
void
tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, const ip_addr_t* local_ip,
const ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port)
{
u32_t iss;
/* @todo: are these all states? */
/* @todo: remove from previous list */
pcb->state = state;
iss = tcp_next_iss(pcb);
pcb->snd_wl2 = iss;
pcb->snd_nxt = iss;
pcb->lastack = iss;
pcb->snd_lbb = iss;
if (state == ESTABLISHED) {
TCP_REG(&tcp_active_pcbs, pcb);
ip_addr_copy(pcb->local_ip, *local_ip);
pcb->local_port = local_port;
ip_addr_copy(pcb->remote_ip, *remote_ip);
pcb->remote_port = remote_port;
} else if(state == LISTEN) {
TCP_REG(&tcp_listen_pcbs.pcbs, pcb);
ip_addr_copy(pcb->local_ip, *local_ip);
pcb->local_port = local_port;
} else if(state == TIME_WAIT) {
TCP_REG(&tcp_tw_pcbs, pcb);
ip_addr_copy(pcb->local_ip, *local_ip);
pcb->local_port = local_port;
ip_addr_copy(pcb->remote_ip, *remote_ip);
pcb->remote_port = remote_port;
} else {
fail();
}
}
void
test_tcp_counters_err(void* arg, err_t err)
{
struct test_tcp_counters* counters = (struct test_tcp_counters*)arg;
EXPECT_RET(arg != NULL);
counters->err_calls++;
counters->last_err = err;
}
static void
test_tcp_counters_check_rxdata(struct test_tcp_counters* counters, struct pbuf* p)
{
struct pbuf* q;
u32_t i, received;
if(counters->expected_data == NULL) {
/* no data to compare */
return;
}
EXPECT_RET(counters->recved_bytes + p->tot_len <= counters->expected_data_len);
received = counters->recved_bytes;
for(q = p; q != NULL; q = q->next) {
char *data = (char*)q->payload;
for(i = 0; i < q->len; i++) {
EXPECT_RET(data[i] == counters->expected_data[received]);
received++;
}
}
EXPECT(received == counters->recved_bytes + p->tot_len);
}
err_t
test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
{
struct test_tcp_counters* counters = (struct test_tcp_counters*)arg;
EXPECT_RETX(arg != NULL, ERR_OK);
EXPECT_RETX(pcb != NULL, ERR_OK);
EXPECT_RETX(err == ERR_OK, ERR_OK);
if (p != NULL) {
if (counters->close_calls == 0) {
counters->recv_calls++;
test_tcp_counters_check_rxdata(counters, p);
counters->recved_bytes += p->tot_len;
} else {
counters->recv_calls_after_close++;
counters->recved_bytes_after_close += p->tot_len;
}
pbuf_free(p);
} else {
counters->close_calls++;
}
EXPECT(counters->recv_calls_after_close == 0 && counters->recved_bytes_after_close == 0);
return ERR_OK;
}
/** Allocate a pcb and set up the test_tcp_counters_* callbacks */
struct tcp_pcb*
test_tcp_new_counters_pcb(struct test_tcp_counters* counters)
{
struct tcp_pcb* pcb = tcp_new();
if (pcb != NULL) {
/* set up args and callbacks */
tcp_arg(pcb, counters);
tcp_recv(pcb, test_tcp_counters_recv);
tcp_err(pcb, test_tcp_counters_err);
pcb->snd_wnd = TCP_WND;
pcb->snd_wnd_max = TCP_WND;
}
return pcb;
}
/** Calls tcp_input() after adjusting current_iphdr_dest */
void test_tcp_input(struct pbuf *p, struct netif *inp)
{
struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
/* these lines are a hack, don't use them as an example :-) */
ip_addr_copy_from_ip4(*ip_current_dest_addr(), iphdr->dest);
ip_addr_copy_from_ip4(*ip_current_src_addr(), iphdr->src);
ip_current_netif() = inp;
ip_data.current_ip4_header = iphdr;
ip_data.current_input_netif = inp;
/* since adding IPv6, p->payload must point to tcp header, not ip header */
pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
tcp_input(p, inp);
ip_addr_set_zero(ip_current_dest_addr());
ip_addr_set_zero(ip_current_src_addr());
ip_current_netif() = NULL;
ip_data.current_ip4_header = NULL;
}
static err_t test_tcp_netif_output(struct netif *netif, struct pbuf *p,
const ip4_addr_t *ipaddr)
{
struct test_tcp_txcounters *txcounters = (struct test_tcp_txcounters*)netif->state;
LWIP_UNUSED_ARG(ipaddr);
if (txcounters != NULL)
{
txcounters->num_tx_calls++;
txcounters->num_tx_bytes += p->tot_len;
if (txcounters->copy_tx_packets) {
struct pbuf *p_copy = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
err_t err;
EXPECT(p_copy != NULL);
err = pbuf_copy(p_copy, p);
EXPECT(err == ERR_OK);
if (txcounters->tx_packets == NULL) {
txcounters->tx_packets = p_copy;
} else {
pbuf_cat(txcounters->tx_packets, p_copy);
}
}
}
return ERR_OK;
}
void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters,
const ip_addr_t *ip_addr, const ip_addr_t *netmask)
{
struct netif *n;
memset(netif, 0, sizeof(struct netif));
if (txcounters != NULL) {
memset(txcounters, 0, sizeof(struct test_tcp_txcounters));
netif->state = txcounters;
}
netif->output = test_tcp_netif_output;
netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP;
ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask));
ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr));
for (n = netif_list; n != NULL; n = n->next) {
if (n == netif) {
return;
}
}
netif->next = NULL;
netif_list = netif;
}

View File

@ -0,0 +1,58 @@
#ifndef LWIP_HDR_TCP_HELPER_H
#define LWIP_HDR_TCP_HELPER_H
#include "../lwip_check.h"
#include "lwip/arch.h"
#include "lwip/tcp.h"
#include "lwip/netif.h"
/* counters used for test_tcp_counters_* callback functions */
struct test_tcp_counters {
u32_t recv_calls;
u32_t recved_bytes;
u32_t recv_calls_after_close;
u32_t recved_bytes_after_close;
u32_t close_calls;
u32_t err_calls;
err_t last_err;
char* expected_data;
u32_t expected_data_len;
};
struct test_tcp_txcounters {
u32_t num_tx_calls;
u32_t num_tx_bytes;
u8_t copy_tx_packets;
struct pbuf *tx_packets;
};
extern const ip_addr_t test_local_ip;
extern const ip_addr_t test_remote_ip;
extern const ip_addr_t test_netmask;
#define TEST_REMOTE_PORT 0x100
#define TEST_LOCAL_PORT 0x101
/* Helper functions */
void tcp_remove_all(void);
struct pbuf* tcp_create_segment(ip_addr_t* src_ip, ip_addr_t* dst_ip,
u16_t src_port, u16_t dst_port, void* data, size_t data_len,
u32_t seqno, u32_t ackno, u8_t headerflags);
struct pbuf* tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_len,
u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags);
struct pbuf* tcp_create_rx_segment_wnd(struct tcp_pcb* pcb, void* data, size_t data_len,
u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags, u16_t wnd);
void tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, const ip_addr_t* local_ip,
const ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port);
void test_tcp_counters_err(void* arg, err_t err);
err_t test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err);
struct tcp_pcb* test_tcp_new_counters_pcb(struct test_tcp_counters* counters);
void test_tcp_input(struct pbuf *p, struct netif *inp);
void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters,
const ip_addr_t *ip_addr, const ip_addr_t *netmask);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
#ifndef LWIP_HDR_TEST_TCP_H
#define LWIP_HDR_TEST_TCP_H
#include "../lwip_check.h"
Suite *tcp_suite(void);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
#ifndef LWIP_HDR_TEST_TCP_OOS_H
#define LWIP_HDR_TEST_TCP_OOS_H
#include "../lwip_check.h"
Suite *tcp_oos_suite(void);
#endif

View File

@ -0,0 +1,665 @@
#include "test_tcp_state.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/stats.h"
#include "tcp_helper.h"
#include "lwip/inet_chksum.h"
#ifdef _MSC_VER
#pragma warning(disable: 4307) /* we explicitly wrap around TCP seqnos */
#endif
#if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
#error "This tests needs TCP- and MEMP-statistics enabled"
#endif
static struct netif test_netif = {0};
static struct test_tcp_txcounters test_txcounters = {0};
#define SEQNO1 (0xFFFFFF00 - TCP_MSS)
#define ISS 6510
static u8_t test_tcp_timer;
/* our own version of tcp_tmr so we can reset fast/slow timer state */
static void
test_tcp_tmr(void)
{
tcp_fasttmr();
if (++test_tcp_timer & 1) {
tcp_slowtmr();
}
}
/* Get TCP flags from packets */
static u8_t
get_tcp_flags_from_packet(struct pbuf *p, u16_t tcp_hdr_offset)
{
struct tcp_hdr tcphdr;
u16_t ret;
EXPECT_RETX(p != NULL, 0);
EXPECT_RETX(p->len >= tcp_hdr_offset + sizeof(struct tcp_hdr), 0);
ret = pbuf_copy_partial(p, &tcphdr, sizeof(struct tcp_hdr), tcp_hdr_offset);
EXPECT(ret == sizeof(struct tcp_hdr));
return TCPH_FLAGS(&tcphdr);
}
/* Create listening tcp_pcb */
static struct tcp_pcb_listen *
create_listening_pcb(u16_t local_port, struct test_tcp_counters *counters)
{
struct tcp_pcb *pcb;
struct tcp_pcb_listen *lpcb=NULL;
err_t err;
u16_t port = local_port?local_port:1234;
if (counters) {
pcb = test_tcp_new_counters_pcb(counters);
} else {
pcb = tcp_new();
}
EXPECT(pcb != NULL);
if (pcb) {
err = tcp_bind(pcb, &test_netif.ip_addr, port);
EXPECT(err == ERR_OK);
lpcb = (struct tcp_pcb_listen *)tcp_listen(pcb);
}
return lpcb;
}
/* Setup/teardown functions */
static struct netif* old_netif_list;
static struct netif* old_netif_default;
static void
tcp_state_setup(void)
{
struct tcp_pcb dummy_pcb; /* we need this for tcp_next_iss() only */
/* reset iss to default (6510) */
tcp_ticks = 0;
tcp_ticks = 0 - (tcp_next_iss(&dummy_pcb) - 6510);
tcp_next_iss(&dummy_pcb);
tcp_ticks = 0;
test_tcp_timer = 0;
old_netif_list = netif_list;
old_netif_default = netif_default;
netif_list = NULL;
netif_default = NULL;
tcp_remove_all();
test_tcp_init_netif(&test_netif, &test_txcounters, &test_local_ip, &test_netmask);
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
tcp_state_teardown(void)
{
netif_list = NULL;
netif_default = NULL;
tcp_remove_all();
/* restore netif_list for next tests (e.g. loopif) */
netif_list = old_netif_list;
netif_default = old_netif_default;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
/* helper functions */
static void
test_rst_generation_with_incoming_packet(struct pbuf *p,
struct netif *netif, struct test_tcp_txcounters *tx_counters)
{
u16_t tcp_flags;
EXPECT_RET(p != NULL);
memset(tx_counters, 0, sizeof(struct test_tcp_txcounters));
/* pass the segment to tcp_input */
tx_counters->copy_tx_packets = 1;
test_tcp_input(p, netif);
tx_counters->copy_tx_packets = 0;
/* check if packets are as expected */
EXPECT(tx_counters->tx_packets != NULL);
if (tx_counters->tx_packets) {
tcp_flags = get_tcp_flags_from_packet(tx_counters->tx_packets, 20);
EXPECT(tcp_flags & TCP_RST);
pbuf_free(tx_counters->tx_packets);
tx_counters->tx_packets = NULL;
}
}
/* Test functions */
/* Call tcp_new() and test memp stats (max number) */
START_TEST(test_tcp_new_max_num)
{
struct tcp_pcb* pcb[MEMP_NUM_TCP_PCB + 1];
int i;
LWIP_UNUSED_ARG(_i);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
for(i = 0;i < MEMP_NUM_TCP_PCB; i++) {
pcb[i] = tcp_new();
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == (i + 1));
}
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
/* Trying to remove the oldest pcb in TIME_WAIT,LAST_ACK,CLOSING state when pcb full */
pcb[MEMP_NUM_TCP_PCB] = tcp_new();
fail_unless(pcb[MEMP_NUM_TCP_PCB] == NULL);
tcp_set_state(pcb[0], TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
pcb[MEMP_NUM_TCP_PCB] = tcp_new();
fail_unless(pcb[MEMP_NUM_TCP_PCB] != NULL);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
for (i = 1; i <= MEMP_NUM_TCP_PCB; i++)
{
tcp_abort(pcb[i]);
}
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* pcbs in TIME_WAIT state will be deleted when creating new pcb reach the max number */
START_TEST(test_tcp_new_max_num_remove_TIME_WAIT)
{
struct tcp_pcb* pcb;
struct tcp_pcb* pcb_list[MEMP_NUM_TCP_PCB + 1];
int i;
LWIP_UNUSED_ARG(_i);
/* create a pcb in TIME_WAIT state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
EXPECT_RET(pcb->state == TIME_WAIT);
/* Create max number pcbs */
for(i = 0;i < MEMP_NUM_TCP_PCB-1; i++) {
pcb_list[i] = tcp_new();
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == (i + 2));
}
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
/* Create one more pcb, and expect that the pcb in the TIME_WAIT state is deleted */
pcb_list[MEMP_NUM_TCP_PCB-1] = tcp_new();
EXPECT_RET(pcb_list[MEMP_NUM_TCP_PCB-1] != NULL);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
for (i = 0; i <= MEMP_NUM_TCP_PCB-1; i++)
{
tcp_abort(pcb_list[i]);
}
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* Call tcp_connect to check active open */
START_TEST(test_tcp_connect_active_open)
{
struct test_tcp_counters counters;
struct tcp_pcb *pcb;
struct pbuf *p;
err_t err;
u16_t test_port = 1234;
u32_t seqno = 0;
LWIP_UNUSED_ARG(_i);
/* create and initialize the pcb */
tcp_ticks = SEQNO1 - ISS;
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
/* Get seqno from SYN packet */
test_txcounters.copy_tx_packets = 1;
err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
test_txcounters.copy_tx_packets = 0;
EXPECT(err == ERR_OK);
EXPECT(pcb->state == SYN_SENT);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT_RET(test_txcounters.tx_packets != NULL);
if (test_txcounters.tx_packets != NULL) {
struct tcp_hdr tcphdr;
u16_t ret;
ret = pbuf_copy_partial(test_txcounters.tx_packets, &tcphdr, 20, 20);
EXPECT(ret == 20);
EXPECT(TCPH_FLAGS(&tcphdr) & TCP_SYN);
pbuf_free(test_txcounters.tx_packets);
test_txcounters.tx_packets = NULL;
seqno = lwip_htonl(tcphdr.seqno);
EXPECT(seqno == pcb->lastack);
}
/* check correct syn packet */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, test_port,
pcb->local_port, NULL, 0, 12345, seqno + 1, TCP_SYN|TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT_RET(pcb->state == ESTABLISHED);
EXPECT_RET(test_txcounters.num_tx_calls == 2);
/* make sure the pcb is freed */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
START_TEST(test_tcp_active_close)
{
struct tcp_pcb *pcb, *pcbl;
struct test_tcp_counters counters;
struct pbuf *p;
err_t err;
u32_t i;
LWIP_UNUSED_ARG(_i);
/* create TCP in LISTEN state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
err = tcp_bind(pcb, &test_netif.ip_addr, 1234);
EXPECT_RET(err == ERR_OK);
pcbl = tcp_listen(pcb);
EXPECT_RET(pcbl != NULL);
EXPECT_RET(pcbl->state == LISTEN);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
memset(&test_txcounters, 0, sizeof(test_txcounters));
err = tcp_close(pcbl);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0);
EXPECT(test_txcounters.num_tx_calls == 0);
/* close TCP in SYN_SENT state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_netif.gw, 1234, NULL);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(pcb->state == SYN_SENT);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
memset(&test_txcounters, 0, sizeof(test_txcounters));
err = tcp_close(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT(test_txcounters.num_tx_calls == 0);
/* close TCP in ESTABLISHED state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
memset(&test_txcounters, 0, sizeof(test_txcounters));
err = tcp_close(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(pcb->state == FIN_WAIT_1);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
/* test_tcp_tmr(); */
EXPECT(test_txcounters.num_tx_calls == 1);
/* create a segment ACK and pass it to tcp_input */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 1, TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT_RET(pcb->state == FIN_WAIT_2);
/* create a segment FIN and pass it to tcp_input */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_FIN);
EXPECT_RET(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT_RET(pcb->state == TIME_WAIT);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
for (i = 0; i < 2 * TCP_MSL / TCP_TMR_INTERVAL + 1; i++) {
test_tcp_tmr();
}
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
START_TEST(test_tcp_imultaneous_close)
{
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf* p;
char data = 0x0f;
err_t err;
u32_t i;
LWIP_UNUSED_ARG(_i);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = 1;
counters.expected_data = &data;
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
err = tcp_close(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(pcb->state == FIN_WAIT_1);
/* create a FIN segment */
p = tcp_create_rx_segment(pcb, &data, 0, 0, 0, TCP_FIN);
EXPECT(p != NULL);
if (p != NULL) {
test_tcp_input(p, &test_netif);
}
EXPECT_RET(pcb->state == CLOSING);
/* create an ACK segment */
p = tcp_create_rx_segment(pcb, &data, 0, 0, 1, TCP_ACK);
EXPECT(p != NULL);
if (p != NULL) {
test_tcp_input(p, &test_netif);
}
EXPECT_RET(pcb->state == TIME_WAIT);
for (i = 0; i < 2 * TCP_MSL / TCP_TMR_INTERVAL + 1; i++) {
test_tcp_tmr();
}
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* RST was generated when receive any incoming segment in CLOSED state */
START_TEST(test_tcp_gen_rst_in_CLOSED)
{
struct pbuf *p;
ip_addr_t src_addr = test_remote_ip;
ip_addr_t dst_addr = test_local_ip;
LWIP_UNUSED_ARG(_i);
/* Do not create any pcb */
/* create a segment */
p = tcp_create_segment(&src_addr, &dst_addr, TEST_REMOTE_PORT,
TEST_LOCAL_PORT, NULL, 0, 12345, 54321, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
}
END_TEST
/* RST was generated when receive ACK in LISTEN state */
START_TEST(test_tcp_gen_rst_in_LISTEN)
{
struct tcp_pcb_listen *lpcb;
struct pbuf *p;
ip_addr_t src_addr = test_remote_ip;
LWIP_UNUSED_ARG(_i);
/* create a pcb in LISTEN state */
lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
EXPECT_RET(lpcb != NULL);
/* create a segment */
p = tcp_create_segment(&src_addr,&lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 12345, 54321, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
/* the PCB still in LISTEN state */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
/* can not use tcp_abort() */
tcp_close((struct tcp_pcb *)lpcb);
}
}
END_TEST
/* RST was generated when receive an SYN in TIME_WAIT state */
START_TEST(test_tcp_gen_rst_in_TIME_WAIT)
{
struct tcp_pcb *pcb;
struct pbuf *p;
LWIP_UNUSED_ARG(_i);
/* create a pcb in LISTEN state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
/* create a segment */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_SYN);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
EXPECT(pcb->state == TIME_WAIT);
}
END_TEST
/* receive TCP_RST with different seqno */
START_TEST(test_tcp_process_rst_seqno)
{
struct test_tcp_counters counters;
struct tcp_pcb *pcb;
struct pbuf *p;
err_t err;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in SYN_SENT state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_remote_ip, TEST_REMOTE_PORT, NULL);
EXPECT_RET(err == ERR_OK);
/* a RST segment with incorrect seqno will not be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
pcb->local_port, NULL, 0, 12345, pcb->snd_nxt-10, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 0);
/* a RST segment with correct seqno will be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
pcb->local_port, NULL, 0, 12345, pcb->snd_nxt, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 1);
counters.err_calls = 0;
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
/* create another pcb in ESTABLISHED state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
/* a RST segment with incorrect seqno will not be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
pcb->local_port, NULL, 0, pcb->rcv_nxt-10, 54321, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 0);
/* a RST segment with correct seqno will be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
pcb->local_port, NULL, 0, pcb->rcv_nxt, 54321, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 1);
counters.err_calls = 0;
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* RST was generated when receive an SYN+ACK with incorrect ACK number in SYN_SENT state */
START_TEST(test_tcp_gen_rst_in_SYN_SENT_ackseq)
{
struct tcp_pcb *pcb;
struct pbuf *p;
u16_t test_port = 1234;
err_t err;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
EXPECT_RET(err == ERR_OK);
/* create a SYN+ACK segment with incorrect seqno */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
pcb->local_port, NULL, 0, 12345, pcb->lastack-10, TCP_SYN|TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
/* LWIP: send RST then re-send SYN immediately */
EXPECT(test_txcounters.num_tx_calls == 2);
}
END_TEST
/* RST was generated when receive an ACK without SYN in SYN_SENT state */
START_TEST(test_tcp_gen_rst_in_SYN_SENT_non_syn_ack)
{
struct tcp_pcb *pcb;
struct pbuf *p;
u16_t test_port = 1234;
err_t err;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
EXPECT_RET(err == ERR_OK);
/* create a SYN+ACK segment with incorrect seqno */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
pcb->local_port, NULL, 0, 12345, pcb->lastack, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
/* LWIP: send RST then re-send SYN immediately */
EXPECT(test_txcounters.num_tx_calls == 2);
}
END_TEST
/* RST was generated when receive an ACK with incorrect seqno in SYN_RCVD state */
START_TEST(test_tcp_gen_rst_in_SYN_RCVD)
{
struct tcp_pcb_listen *lpcb;
struct pbuf *p;
u32_t ack_seqno = 0;
ip_addr_t src_addr = test_remote_ip;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
EXPECT_RET(lpcb != NULL);
/* LISTEN -> SYN_RCVD */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1000, 54321, TCP_SYN);
EXPECT(p != NULL);
memset(&test_txcounters, 0, sizeof(struct test_tcp_txcounters));
test_tcp_input(p, &test_netif);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1) {
ack_seqno = tcp_active_pcbs[0].lastack;
}
/* create a ACK segment with incorrect seqno */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1001, ack_seqno+1111, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
/* the active pcb still exists */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
/* can not use tcp_abort() */
tcp_close((struct tcp_pcb *)lpcb);
}
}
END_TEST
/* a listen pcb returns to LISTEN from SYN_RCVD when RST received */
START_TEST(test_tcp_receive_rst_SYN_RCVD_to_LISTEN)
{
struct tcp_pcb_listen *lpcb;
struct pbuf *p;
u16_t tcp_flags;
ip_addr_t src_addr = test_remote_ip;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
EXPECT_RET(lpcb != NULL);
/* create a SYN segment */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1000, 54321, TCP_SYN);
EXPECT(p != NULL);
/* pass the segment to tcp_input */
memset(&test_txcounters, 0, sizeof(struct test_tcp_txcounters));
test_txcounters.copy_tx_packets = 1;
test_tcp_input(p, &test_netif);
test_txcounters.copy_tx_packets = 0;
/* check if packets are as expected */
EXPECT(test_txcounters.num_tx_calls == 1);
tcp_flags = get_tcp_flags_from_packet(test_txcounters.tx_packets, 20);
pbuf_free(test_txcounters.tx_packets);
test_txcounters.tx_packets = NULL;
EXPECT((tcp_flags & TCP_SYN) && (tcp_flags & TCP_ACK));
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
/* create a RST segment */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1001, 54321, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
/* can not use tcp_abort() */
tcp_close((struct tcp_pcb *)lpcb);
}
}
END_TEST
/** Create the suite including all tests for this module */
Suite *
tcp_state_suite(void)
{
testfunc tests[] = {
TESTFUNC(test_tcp_new_max_num),
TESTFUNC(test_tcp_new_max_num_remove_TIME_WAIT),
TESTFUNC(test_tcp_connect_active_open),
TESTFUNC(test_tcp_active_close),
TESTFUNC(test_tcp_imultaneous_close),
TESTFUNC(test_tcp_gen_rst_in_CLOSED),
TESTFUNC(test_tcp_gen_rst_in_LISTEN),
TESTFUNC(test_tcp_gen_rst_in_TIME_WAIT),
TESTFUNC(test_tcp_process_rst_seqno),
TESTFUNC(test_tcp_gen_rst_in_SYN_SENT_ackseq),
TESTFUNC(test_tcp_gen_rst_in_SYN_SENT_non_syn_ack),
TESTFUNC(test_tcp_gen_rst_in_SYN_RCVD),
TESTFUNC(test_tcp_receive_rst_SYN_RCVD_to_LISTEN),
};
return create_suite("TCP_STATE", tests, sizeof(tests) / sizeof(testfunc), tcp_state_setup, tcp_state_teardown);
}

View File

@ -0,0 +1,8 @@
#ifndef LWIP_HDR_TEST_TCP_STATE_H
#define LWIP_HDR_TEST_TCP_STATE_H
#include "../lwip_check.h"
Suite *tcp_state_suite(void);
#endif