picotcp stack driver edge case bug fixes

This commit is contained in:
Joseph Henry
2017-08-24 11:45:39 -07:00
parent bceadfbe46
commit eff90c7d13
12 changed files with 1385 additions and 931 deletions

View File

@@ -47,6 +47,7 @@
#include <map>
#include <ctime>
#include <sys/time.h>
#include <pthread.h>
#include "libzt.h"
@@ -98,8 +99,6 @@
#define DETAILS_STR_LEN 128
char str[STR_SIZE];
std::map<std::string, std::string> testConf;
/* Tests in this file:
@@ -213,7 +212,6 @@ void RECORD_RESULTS(int *test_number, bool passed, char *details, std::vector<st
(*test_number) = 0;
char *ok_str = (char*)"[ OK ]";
char *fail_str = (char*)"[ FAIL ]";
if(passed == PASSED) {
DEBUG_TEST("[%d]%s", *test_number, ok_str);
results->push_back(std::string(ok_str) + " " + std::string(details));
@@ -242,8 +240,9 @@ void RECORD_RESULTS(int *test_number, bool passed, char *details, std::vector<st
//
void tcp_client_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_client_4\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "tcp_client_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
@@ -252,26 +251,28 @@ void tcp_client_4(TCP_UNIT_TEST_SIG_4)
DEBUG_ERROR("error connecting to remote host (%d)", err);
// TODO: Put this test in the general API section
struct sockaddr_in peer_addr;
struct sockaddr_storage peer_addr;
struct sockaddr_in *in4 = (struct sockaddr_in*)&peer_addr;
socklen_t peer_addrlen;
zts_getpeername(sockfd, (struct sockaddr*)&peer_addr, &peer_addrlen);
DEBUG_INFO("getpeername() => %s : %d", inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port));
DEBUG_INFO("getpeername() => %s : %d", inet_ntoa(in4->sin_addr), ntohs(in4->sin_port));
w = zts_write(sockfd, str, len);
w = zts_write(sockfd, msg.c_str(), len);
r = zts_read(sockfd, rbuf, len);
DEBUG_TEST("Sent : %s", str);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
sprintf(details, "tcp_client_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
//
void tcp_server_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_server_4\n");
int w=0, r=0, sockfd, accfd, err, len = strlen(str);
std::string msg = "tcp_server_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int w=0, r=0, sockfd, accfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
@@ -284,10 +285,11 @@ void tcp_server_4(TCP_UNIT_TEST_SIG_4)
DEBUG_ERROR("error accepting connection (%d)", err);
// TODO: Put this test in the general API section
struct sockaddr_in peer_addr;
struct sockaddr_storage peer_addr;
struct sockaddr_in *in4 = (struct sockaddr_in*)&peer_addr;
socklen_t peer_addrlen;
zts_getpeername(accfd, (struct sockaddr*)&peer_addr, &peer_addrlen);
DEBUG_INFO("getpeername() => %s : %d", inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port));
DEBUG_INFO("getpeername() => %s : %d", inet_ntoa(in4->sin_addr), ntohs(in4->sin_port));
r = zts_read(accfd, rbuf, sizeof rbuf);
w = zts_write(accfd, rbuf, len);
@@ -295,15 +297,16 @@ void tcp_server_4(TCP_UNIT_TEST_SIG_4)
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
err = zts_close(accfd);
sprintf(details, "tcp_server_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
//
void tcp_client_6(TCP_UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_client_6\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "tcp_client_6";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0)
@@ -320,21 +323,22 @@ void tcp_client_6(TCP_UNIT_TEST_SIG_6)
inet_ntop(AF_INET6, &(p6->sin6_addr), peer_addrstr, INET6_ADDRSTRLEN);
DEBUG_INFO("getpeername() => %s : %d", peer_addrstr, ntohs(p6->sin6_port));
w = zts_write(sockfd, str, len);
w = zts_write(sockfd, msg.c_str(), len);
r = zts_read(sockfd, rbuf, len);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
sprintf(details, "tcp_client_6, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
//
void tcp_server_6(TCP_UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_server_6\n");
int w=0, r=0, sockfd, accfd, err, len = strlen(str);
std::string msg = "tcp_sever_6";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int w=0, r=0, sockfd, accfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0)
@@ -361,8 +365,8 @@ void tcp_server_6(TCP_UNIT_TEST_SIG_6)
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
err = zts_close(accfd);
sprintf(details, "tcp_server_6, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
// UDP
@@ -370,8 +374,9 @@ void tcp_server_6(TCP_UNIT_TEST_SIG_6)
//
void udp_client_4(UDP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\nudp_client_4\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "udp_client_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
@@ -386,7 +391,7 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4)
struct sockaddr_in saddr;
while(1) {
// tx
if((w = zts_sendto(sockfd, str, strlen(str), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
if((w = zts_sendto(sockfd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
DEBUG_ERROR("error sending packet, err=%d", errno);
}
sleep(1);
@@ -394,14 +399,14 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4)
int serverlen = sizeof(remote_addr);
// rx
r = zts_recvfrom(sockfd, rbuf, STR_SIZE, 0, (struct sockaddr *)&saddr, (socklen_t *)&serverlen);
if(r == strlen(str)) {
if(r == strlen(msg.c_str())) {
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
DEBUG_INFO("udp_client_4, n=%d, err=%d, r=%d, w=%d\n", count, err, r, w);
sprintf(details, "udp_client_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
DEBUG_INFO("%s, n=%d, err=%d, r=%d, w=%d\n", msg.c_str(), count, err, r, w);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
return;
}
}
@@ -409,8 +414,9 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4)
void udp_server_4(UDP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\nudp_server_4\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "udp_server_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
@@ -419,7 +425,7 @@ void udp_server_4(UDP_UNIT_TEST_SIG_4)
if((err = zts_bind(sockfd, (struct sockaddr *)local_addr, sizeof(struct sockaddr_in)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
// rx
fprintf(stderr, "waiting for UDP packet...\n");
DEBUG_INFO("waiting for UDP packet...");
struct sockaddr_in saddr;
int serverlen = sizeof(saddr);
memset(&saddr, 0, sizeof(saddr));
@@ -433,29 +439,30 @@ void udp_server_4(UDP_UNIT_TEST_SIG_4)
long int tx_ti = get_now_ts();
while(1) {
sleep(1);
DEBUG_INFO("sending UDP packet");
if((w = zts_sendto(sockfd, str, strlen(str), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
//DEBUG_INFO("sending UDP packet");
if((w = zts_sendto(sockfd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
DEBUG_ERROR("error sending packet, err=%d", errno);
}
if(get_now_ts() >= tx_ti + 20000) {
//fprintf(stderr, "tx_ti=%d\n", tx_ti);
DEBUG_INFO("get_now_ts()-tx_ti=%d\n", get_now_ts()-tx_ti);
break;
}
}
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
//err = zts_close(sockfd);
DEBUG_INFO("udp_server_4, n=%d, err=%d, r=%d, w=%d\n", count, err, r, w);
sprintf(details, "udp_server_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
DEBUG_INFO("%s, n=%d, err=%d, r=%d, w=%d\n", msg.c_str(), count, err, r, w);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
//
void udp_client_6(UDP_UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\nudp_client_6\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "udp_client_6";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
@@ -463,29 +470,31 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0))
std::cout << "error setting O_NONBLOCK (errno=" << strerror(errno) << ")" << std::endl;
fprintf(stderr, "sending UDP packets until I get a single response...\n");
DEBUG_INFO("[1] sending UDP packets until I get a single response...\n");
if((err = zts_bind(sockfd, (struct sockaddr *)local_addr, sizeof(struct sockaddr_in6)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
// start sending UDP packets in the hopes that at least one will be picked up by the server
struct sockaddr_in saddr;
while(1) {
// tx
if((w = zts_sendto(sockfd, str, strlen(str), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
if((w = zts_sendto(sockfd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
DEBUG_ERROR("error sending packet, err=%d", errno);
}
sleep(1);
usleep(100000);
memset(rbuf, 0, sizeof(rbuf));
int serverlen = sizeof(remote_addr);
// rx
r = zts_recvfrom(sockfd, rbuf, STR_SIZE, 0, (struct sockaddr *)&saddr, (socklen_t *)&serverlen);
if(r == strlen(str)) {
if(r == strlen(msg.c_str())) {
DEBUG_INFO("[2] complete");
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
DEBUG_INFO("udp_client_6, n=%d, err=%d, r=%d, w=%d\n", count, err, r, w);
sprintf(details, "udp_client_6, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
DEBUG_INFO("%s, n=%d, err=%d, r=%d, w=%d\n", msg.c_str(), count, err, r, w);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
return;
}
}
@@ -493,8 +502,9 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6)
void udp_server_6(UDP_UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\nudp_server_6\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "udp_server_6";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
@@ -503,7 +513,7 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6)
if((err = zts_bind(sockfd, (struct sockaddr *)local_addr, sizeof(struct sockaddr_in6)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
// rx
fprintf(stderr, "waiting for UDP packet...\n");
DEBUG_INFO("[1/4] waiting for UDP packet...\n");
struct sockaddr_in6 saddr;
int serverlen = sizeof(saddr);
memset(&saddr, 0, sizeof(saddr));
@@ -512,29 +522,30 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6)
char addrstr[INET6_ADDRSTRLEN], remote_addrstr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &(saddr.sin6_addr), addrstr, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &(remote_addr->sin6_addr), remote_addrstr, INET6_ADDRSTRLEN);
DEBUG_INFO("received DGRAM from %s : %d", addrstr, ntohs(saddr.sin6_port));
DEBUG_INFO("sending DGRAM(s) to %s : %d", remote_addrstr, ntohs(remote_addr->sin6_port));
DEBUG_INFO("[2/4] received DGRAM from %s : %d", addrstr, ntohs(saddr.sin6_port));
DEBUG_INFO("[2/4] sending DGRAM(s) to %s : %d", remote_addrstr, ntohs(remote_addr->sin6_port));
// once we receive a UDP packet, spend 10 seconds sending responses in the hopes that the client will see
// tx
long int tx_ti = get_now_ts();
while(1) {
sleep(1);
DEBUG_INFO("sending UDP packet");
if((w = zts_sendto(sockfd, str, strlen(str), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
usleep(100000);
//DEBUG_INFO("sending UDP packet");
if((w = zts_sendto(sockfd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)remote_addr, sizeof(remote_addr))) < 0) {
DEBUG_ERROR("error sending packet, err=%d", errno);
}
if(get_now_ts() >= tx_ti + 20000) {
//fprintf(stderr, "tx_ti=%d\n", tx_ti);
DEBUG_INFO("[3/4] get_now_ts()-tx_ti=%d\n", get_now_ts()-tx_ti);
break;
}
}
DEBUG_INFO("[4/4] complete");
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
//err = zts_close(sockfd);
DEBUG_INFO("udp_server_6, n=%d, err=%d, r=%d, w=%d\n", count, err, r, w);
sprintf(details, "udp_server_6, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
DEBUG_INFO("%s, n=%d, err=%d, r=%d, w=%d\n", msg.c_str(), count, err, r, w);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
@@ -546,7 +557,8 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6)
// Maintain transfer for count OR count
void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_client_sustained_4\n");
std::string msg = "tcp_client_sustained_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int n=0, w=0, r=0, sockfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
char *txbuf = (char*)malloc(count*sizeof(char));
@@ -608,8 +620,8 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_client_sustained_4, match=%d, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
match, count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
sprintf(details, "%s, match=%d, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
msg.c_str(), match, count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && match && err>=0);
}
@@ -622,7 +634,8 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
// Maintain transfer for count OR count
void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_client_sustained_6\n");
std::string msg = "tcp_server_sustained_6";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int n=0, w=0, r=0, sockfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
char *txbuf = (char*)malloc(count*sizeof(char));
@@ -685,8 +698,8 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_client_sustained_6, match=%d, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
match, count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
sprintf(details, "%s, match=%d, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
msg.c_str(), msg.c_str(), match, count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && match && err>=0);
}
@@ -698,7 +711,8 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
// Maintain transfer for count OR count
void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_server_sustained_4\n");
std::string msg = "tcp_server_sustained_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int n=0, w=0, r=0, sockfd, accfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
memset(rxbuf, 0, count);
@@ -752,8 +766,8 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_server_sustained_4, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
sprintf(details, "%s, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
msg.c_str(), count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && err>=0);
}
@@ -764,7 +778,8 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
// Maintain transfer for count OR count
void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_server_sustained_6\n");
std::string msg = "tcp_server_sustained_6";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int n=0, w=0, r=0, sockfd, accfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
memset(rxbuf, 0, count);
@@ -816,8 +831,8 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6)
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_server_sustained_6, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
sprintf(details, "%s, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
msg.c_str(), count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && err>=0);
}
@@ -826,15 +841,16 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6)
void udp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\nudp_client_sustained_4\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "udp_client_sustained_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
for(int i=0; i<1000; i++) {
w = zts_sendto(sockfd, str, strlen(str), 0, (struct sockaddr *)addr, sizeof(addr));
w = zts_sendto(sockfd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)addr, sizeof(addr));
}
memset(rbuf, 0, sizeof(rbuf));
int serverlen = sizeof(addr);
@@ -842,16 +858,17 @@ void udp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
sprintf(details, "udp_client_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
void udp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\nudp_server_sustained_4\n");
int r, w, sockfd, err, len = strlen(str);
std::string msg = "udp_server_sustained_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int r, w, sockfd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0)
@@ -867,14 +884,14 @@ void udp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
DEBUG_TEST("Received : %s", rbuf);
}
//memset(rbuf, 0, sizeof(rbuf));
//w = zts_sendto(sockfd, str, strlen(str), 0, (struct sockaddr *)addr, sizeof(addr));
//w = zts_sendto(sockfd, msg.c_str()), strlen(msg.c_str())), 0, (struct sockaddr *)addr, sizeof(addr));
w = r;
//sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
//err = zts_close(sockfd);
sprintf(details, "udp_server_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", msg.c_str(), count, err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
}
/****************************************************************************/
@@ -977,7 +994,8 @@ void tcp_server_perf_4(TCP_UNIT_TEST_SIG_4)
void tcp_perf_tx_echo_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_perf_tx_echo_4\n");
std::string msg = "tcp_perf_tx_echo_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int err = 0;
int tot = 0;
@@ -1035,7 +1053,7 @@ void tcp_perf_tx_echo_4(TCP_UNIT_TEST_SIG_4)
float ts_delta = (end_time - start_time) / (float)1000;
float rate = (float)tot / (float)ts_delta;
sprintf(details, "tcp_perf_tx_echo_4, tot=%d, dt=%.2f, rate=%.2f MB/s", tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
sprintf(details, "%s, tot=%d, dt=%.2f, rate=%.2f MB/s", msg.c_str(), tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
@@ -1045,7 +1063,8 @@ void tcp_perf_tx_echo_4(TCP_UNIT_TEST_SIG_4)
void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_perf_rx_echo_4\n");
std::string msg = "tcp_perf_rx_echo_4";
fprintf(stderr, "\n\n%s\n\n", msg.c_str());
int err = 0;
int mode = 0;
@@ -1102,7 +1121,7 @@ void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4)
long int end_time = get_now_ts();
float ts_delta = (end_time - start_time) / (float)1000;
float rate = (float)tot / (float)ts_delta;
sprintf(details, "tcp_perf_rx_echo_4, tot=%d, dt=%.2f, rate=%.2f MB/s", tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
sprintf(details, "%s, tot=%d, dt=%.2f, rate=%.2f MB/s", msg.c_str(), tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
@@ -1505,24 +1524,145 @@ void test_bad_args()
DEBUG_TEST("SOCK_DGRAM = %d", SOCK_DGRAM);
}
void close_while_writing_test()
{
// TODO: Close a socket while another thread is writing to it or reading from it
}
void* create_socket(void *arg)
{
/*
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
*/
}
void multithread_socket_creation()
{
/*
pthread_t tid[2];
err = pthread_create(&(tid[i]), NULL, &create_socket, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
*/
// TODO:
}
void multithread_rw()
{
// TODO: Test read/writes from multiple threads
}
// Tests rapid opening and closure of sockets
void close_test(struct sockaddr *bind_addr)
{
// BUG: While running an extended test of unassigned closures, the
// stack may crash at: `pico_check_timers at pico_stack.c:608, this appears
// to be a bad pointer to a timer within the stack.
bool extended = false;
int tries = !extended ? 32 : 1024;
int err = 0;
for(int i=0; i<64; i++)
for(int i=0; i<tries; i++)
{
int fd = zts_socket(AF_INET, SOCK_STREAM, 0);
if((err = zts_bind(fd, (struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in)) < 0))
int fd;
if((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
DEBUG_INFO("error creating socket. sleeping until timers are released");
sleep(30);
}
if((err = zts_bind(fd, (struct sockaddr *)bind_addr, sizeof(struct sockaddr_in)) < 0)) {
DEBUG_ERROR("error binding to interface (%d)", err);
}
usleep(100000);
if((err = zts_close(fd)) < 0) {
DEBUG_ERROR("error closing socket (%d)", err);
}
DEBUG_INFO("i=%d, close() = %d", i, err);
usleep(100000);
((struct sockaddr_in *)bind_addr)->sin_port++;
}
}
void bind_to_localhost_test(int port)
{
int fd, err = 0;
// ipv4, 0.0.0.0
struct sockaddr bind_addr;
DEBUG_INFO("binding to 0.0.0.0");
create_addr("0.0.0.0", port, 4, (struct sockaddr *)&bind_addr);
if((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) > 0) {
if((err = zts_bind(fd, (struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in))) == 0) {
usleep(100000);
if((err = zts_close(fd)) < 0) {
DEBUG_ERROR("error closing socket (%d)", err);
}
}
else{
DEBUG_ERROR("error binding to interface (%d)", err);
}
}
else {
DEBUG_ERROR("error creating socket", err);
}
port++;
// ipv4, 127.0.0.1
DEBUG_INFO("binding to 127.0.0.1");
create_addr("127.0.0.1", port, 4, (struct sockaddr *)&bind_addr);
if((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) > 0) {
if((err = zts_bind(fd, (struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in))) == 0) {
usleep(100000);
if((err = zts_close(fd)) < 0) {
DEBUG_ERROR("error closing socket (%d)", err);
}
}
else{
DEBUG_ERROR("error binding to interface (%d)", err);
}
}
else {
DEBUG_ERROR("error creating socket", err);
}
port++;
// ipv6, [::]
DEBUG_INFO("binding to [::]");
create_addr("::", port, 6, (struct sockaddr *)&bind_addr);
if((fd = zts_socket(AF_INET6, SOCK_STREAM, 0)) > 0) {
if((err = zts_bind(fd, (struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in))) == 0) {
usleep(100000);
if((err = zts_close(fd)) < 0) {
DEBUG_ERROR("error closing socket (%d)", err);
}
}
else{
DEBUG_ERROR("error binding to interface (%d)", err);
}
}
else {
DEBUG_ERROR("error creating socket", err);
}
}
/****************************************************************************/
/* main(), calls test_driver(...) */
/****************************************************************************/
@@ -1553,7 +1693,6 @@ int main(int argc , char *argv[])
std::string remote_echo_ipv4, smode;
std::string nwid, stype, path = argv[1];
std::string ipstr, ipstr6, local_ipstr, local_ipstr6, remote_ipstr, remote_ipstr6;
memcpy(str, "welcome to the machine", 22);
// loaf config file
if(path.find(".conf") == std::string::npos) {
@@ -1626,15 +1765,25 @@ int main(int argc , char *argv[])
memset(&details, 0, sizeof details);
bool passed = 0;
struct sockaddr local_addr;
struct sockaddr remote_addr;
struct sockaddr_storage local_addr;
struct sockaddr_storage remote_addr;
// closure test
port = 1000;
/*
port = 1000;
struct sockaddr_in in4;
DEBUG_INFO("testing closures by binding to: %s", local_ipstr.c_str());
create_addr(local_ipstr, port, 4, (struct sockaddr *)&in4);
close_test((struct sockaddr*)&in4);
*/
close_while_writing_test();
// localhost bind test
//bind_to_localhost_test(1000);
// Transmission Tests
@@ -1644,6 +1793,8 @@ int main(int argc , char *argv[])
operation = TEST_OP_N_BYTES;
// ipv4 client/server (UDP)
/*
ipv = 4;
if(mode == TEST_MODE_SERVER) {
create_addr(local_ipstr, port, ipv, (struct sockaddr *)&local_addr);
@@ -1672,12 +1823,14 @@ int main(int argc , char *argv[])
}
RECORD_RESULTS(&test_number, passed, details, &results);
port++;
*/
// ipv6 client/server (UDP)
ipv = 6;
if(mode == TEST_MODE_SERVER) {
create_addr(local_ipstr6, port, ipv, (struct sockaddr *)&local_addr);
create_addr(remote_ipstr6, port, ipv, (struct sockaddr *)&remote_addr);
create_addr(local_ipstr6, port, ipv, (struct sockaddr*)&local_addr);
create_addr(remote_ipstr6, port, ipv, (struct sockaddr*)&remote_addr);
udp_server_6((struct sockaddr_in6 *)&local_addr, (struct sockaddr_in6 *)&remote_addr, operation, count, delay, details, &passed);
}
else if(mode == TEST_MODE_CLIENT) {
@@ -1703,6 +1856,8 @@ int main(int argc , char *argv[])
RECORD_RESULTS(&test_number, passed, details, &results);
port++;
exit(0);
// ipv4 sustained transfer (UDP)
ipv = 4;
if(mode == TEST_MODE_SERVER) {