standardization pass: conditional statement spacing, removed NULL shorthand making checks more explicit

This commit is contained in:
Joseph Henry
2017-09-14 13:19:13 -07:00
parent 7c586471f4
commit cc468fe8fe
4 changed files with 210 additions and 219 deletions

View File

@@ -195,11 +195,13 @@ std::map<std::string, std::string> testConf;
void displayResults(int *results, int size) { void displayResults(int *results, int size) {
int success = 0, failure = 0; int success = 0, failure = 0;
for(int i=0; i<size; i++) { for(int i=0; i<size; i++) {
if(results[i] == 0) if (results[i] == 0) {
success++; success++;
else }
else {
failure++; failure++;
} }
}
std::cout << "tials: " << size << std::endl; std::cout << "tials: " << size << std::endl;
std::cout << " - success = " << (float)success / (float)size << std::endl; std::cout << " - success = " << (float)success / (float)size << std::endl;
std::cout << " - failure = " << (float)failure / (float)size << std::endl; std::cout << " - failure = " << (float)failure / (float)size << std::endl;
@@ -555,12 +557,11 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4)
} }
struct sockaddr_storage saddr; struct sockaddr_storage saddr;
while(1) { while(true) {
// tx // tx
if ((w = SENDTO(fd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { if ((w = SENDTO(fd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) {
DEBUG_ERROR("error sending packet, err=%d", errno); DEBUG_ERROR("error sending packet, err=%d", errno);
} }
sleep(1);
memset(rbuf, 0, sizeof(rbuf)); memset(rbuf, 0, sizeof(rbuf));
int serverlen = sizeof(struct sockaddr_storage); int serverlen = sizeof(struct sockaddr_storage);
// rx // rx
@@ -621,7 +622,7 @@ void udp_server_4(UDP_UNIT_TEST_SIG_4)
DEBUG_TEST("sending DGRAM(s) to %s : %d", inet_ntoa(remote_addr->sin_addr), ntohs(remote_addr->sin_port)); DEBUG_TEST("sending DGRAM(s) to %s : %d", inet_ntoa(remote_addr->sin_addr), ntohs(remote_addr->sin_port));
// tx // tx
long int tx_ti = get_now_ts(); long int tx_ti = get_now_ts();
while(1) { while(true) {
sleep(1); sleep(1);
if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) {
DEBUG_ERROR("error sending packet, err=%d", errno); DEBUG_ERROR("error sending packet, err=%d", errno);
@@ -675,7 +676,7 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6)
// start sending UDP packets in the hopes that at least one will be picked up by the server // start sending UDP packets in the hopes that at least one will be picked up by the server
struct sockaddr_storage saddr; struct sockaddr_storage saddr;
while(1) { while(true) {
// tx // tx
if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) {
DEBUG_ERROR("error sending packet, err=%d", errno); DEBUG_ERROR("error sending packet, err=%d", errno);
@@ -744,7 +745,7 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6)
// once we receive a UDP packet, spend 10 seconds sending responses in the hopes that the client will see // once we receive a UDP packet, spend 10 seconds sending responses in the hopes that the client will see
// tx // tx
long int tx_ti = get_now_ts(); long int tx_ti = get_now_ts();
while(1) { while(true) {
usleep(100000); usleep(100000);
//DEBUG_TEST("sending UDP packet"); //DEBUG_TEST("sending UDP packet");
if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) {
@@ -808,8 +809,7 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
int next_write = std::min(4096, wrem); int next_write = std::min(4096, wrem);
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
n = WRITE(fd, &txbuf[w], next_write); n = WRITE(fd, &txbuf[w], next_write);
if (n > 0) if (n > 0) {
{
w += n; w += n;
wrem -= n; wrem -= n;
err = n; err = n;
@@ -822,11 +822,10 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
long int rx_ti = 0; long int rx_ti = 0;
while(rrem) { while(rrem) {
n = READ(fd, &rxbuf[r], rrem); n = READ(fd, &rxbuf[r], rrem);
if(!rx_ti) { // wait for first message if (rx_ti == 0) { // wait for first message
rx_ti = get_now_ts(); rx_ti = get_now_ts();
} }
if (n > 0) if (n > 0) {
{
r += n; r += n;
rrem -= n; rrem -= n;
err = n; err = n;
@@ -891,8 +890,7 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
while(wrem) { while(wrem) {
int next_write = std::min(4096, wrem); int next_write = std::min(4096, wrem);
n = WRITE(fd, &txbuf[w], next_write); n = WRITE(fd, &txbuf[w], next_write);
if (n > 0) if (n > 0) {
{
w += n; w += n;
wrem -= n; wrem -= n;
err = n; err = n;
@@ -904,11 +902,10 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
long int rx_ti = 0; long int rx_ti = 0;
while(rrem) { while(rrem) {
n = READ(fd, &rxbuf[r], rrem); n = READ(fd, &rxbuf[r], rrem);
if(!rx_ti) { // wait for first message if (rx_ti == 0) { // wait for first message
rx_ti = get_now_ts(); rx_ti = get_now_ts();
} }
if (n > 0) if (n > 0) {
{
r += n; r += n;
rrem -= n; rrem -= n;
err = n; err = n;
@@ -932,7 +929,7 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
float tx_rate = (float)cnt / (float)tx_dt; float tx_rate = (float)cnt / (float)tx_dt;
float rx_rate = (float)cnt / (float)rx_dt; float rx_rate = (float)cnt / (float)rx_dt;
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", 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",
testname.c_str(), msg.c_str(), match, cnt, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) )); testname.c_str(), match, cnt, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == cnt && w == cnt && match && err>=0); *passed = (r == cnt && w == cnt && match && err>=0);
} }
free(rxbuf); free(rxbuf);
@@ -984,9 +981,8 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
long int rx_ti = 0; long int rx_ti = 0;
while(rrem) { while(rrem) {
n = READ(client_fd, &rxbuf[r], rrem); n = READ(client_fd, &rxbuf[r], rrem);
if (n > 0) if (n > 0) {
{ if (rx_ti == 0) { // wait for first message
if(!rx_ti) { // wait for first message
rx_ti = get_now_ts(); rx_ti = get_now_ts();
} }
r += n; r += n;
@@ -1001,8 +997,7 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
while(wrem) { while(wrem) {
int next_write = std::min(1024, wrem); int next_write = std::min(1024, wrem);
n = WRITE(client_fd, &rxbuf[w], next_write); n = WRITE(client_fd, &rxbuf[w], next_write);
if (n > 0) if (n > 0) {
{
w += n; w += n;
wrem -= n; wrem -= n;
err = n; err = n;
@@ -1074,9 +1069,8 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6)
long int rx_ti = 0; long int rx_ti = 0;
while(rrem) { while(rrem) {
n = READ(client_fd, &rxbuf[r], rrem); n = READ(client_fd, &rxbuf[r], rrem);
if (n > 0) if (n > 0) {
{ if (rx_ti == 0) { // wait for first message
if(!rx_ti) { // wait for first message
rx_ti = get_now_ts(); rx_ti = get_now_ts();
} }
r += n; r += n;
@@ -1090,8 +1084,7 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6)
while(wrem) { while(wrem) {
int next_write = std::min(1024, wrem); int next_write = std::min(1024, wrem);
n = WRITE(client_fd, &rxbuf[w], next_write); n = WRITE(client_fd, &rxbuf[w], next_write);
if (n > 0) if (n > 0) {
{
w += n; w += n;
wrem -= n; wrem -= n;
err = n; err = n;
@@ -1127,7 +1120,6 @@ void udp_client_sustained_4(UDP_UNIT_TEST_SIG_4)
int w, fd, err, len = strlen(msg.c_str()); int w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE]; char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf); memset(rbuf, 0, sizeof rbuf);
if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) { if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) {
DEBUG_ERROR("error creating ZeroTier socket"); DEBUG_ERROR("error creating ZeroTier socket");
perror("socket"); perror("socket");
@@ -1147,7 +1139,6 @@ void udp_client_sustained_4(UDP_UNIT_TEST_SIG_4)
*passed = false; *passed = false;
return; return;
} }
int num_to_send = 10; int num_to_send = 10;
for(int i=0; i<num_to_send; i++) { for(int i=0; i<num_to_send; i++) {
// tx // tx
@@ -1177,7 +1168,6 @@ void udp_server_sustained_4(UDP_UNIT_TEST_SIG_4)
int r, fd, err, len = strlen(msg.c_str()); int r, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE]; char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf); memset(rbuf, 0, sizeof rbuf);
if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) { if ((fd = SOCKET(AF_INET, SOCK_DGRAM, 0)) < 0) {
DEBUG_ERROR("error creating ZeroTier socket"); DEBUG_ERROR("error creating ZeroTier socket");
perror("socket"); perror("socket");
@@ -1190,7 +1180,6 @@ void udp_server_sustained_4(UDP_UNIT_TEST_SIG_4)
*passed = false; *passed = false;
return; return;
} }
int num_to_recv = 3; int num_to_recv = 3;
DEBUG_TEST("waiting for UDP packet..."); DEBUG_TEST("waiting for UDP packet...");
for(int i=0; i<num_to_recv; i++) { for(int i=0; i<num_to_recv; i++) {
@@ -1210,7 +1199,7 @@ void udp_server_sustained_4(UDP_UNIT_TEST_SIG_4)
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE); sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
//err = CLOSE(fd); //err = CLOSE(fd);
DEBUG_TEST("%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r); DEBUG_TEST("%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", testname.c_str(), cnt, err, r); sprintf(details, "%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
DEBUG_TEST("Received : %s", rbuf); DEBUG_TEST("Received : %s", rbuf);
*passed = (r == len && !err) && !strcmp(rbuf, msg.c_str()); *passed = (r == len && !err) && !strcmp(rbuf, msg.c_str());
} }
@@ -1307,7 +1296,7 @@ void udp_server_sustained_6(UDP_UNIT_TEST_SIG_6)
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE); sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
//err = CLOSE(fd); //err = CLOSE(fd);
DEBUG_TEST("%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r); DEBUG_TEST("%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
sprintf(details, "%s, n=%d, err=%d, r=%d, w=%d", testname.c_str(), cnt, err, r); sprintf(details, "%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
DEBUG_TEST("Received : %s", rbuf); DEBUG_TEST("Received : %s", rbuf);
*passed = (r == len && !err) && !strcmp(rbuf, msg.c_str()); *passed = (r == len && !err) && !strcmp(rbuf, msg.c_str());
} }
@@ -1640,7 +1629,7 @@ int slam_api_test()
usleep(SLAM_INTERVAL); usleep(SLAM_INTERVAL);
int port; int port;
while(!(std::find(used_ports.begin(),used_ports.end(),port) == used_ports.end())) { while((std::find(used_ports.begin(),used_ports.end(),port) == used_ports.end()) == false) {
port = MIN_PORT + (rand() % (int)(MAX_PORT - MIN_PORT + 1)); port = MIN_PORT + (rand() % (int)(MAX_PORT - MIN_PORT + 1));
} }
used_ports.push_back(port); used_ports.push_back(port);
@@ -2256,22 +2245,24 @@ int main(int argc , char *argv[])
char device_id[ZT_ID_LEN]; char device_id[ZT_ID_LEN];
zts_get_device_id(device_id); zts_get_device_id(device_id);
DEBUG_TEST("I am %s, %s", device_id, me.c_str()); DEBUG_TEST("I am %s, %s", device_id, me.c_str());
if(mode == TEST_MODE_SERVER) if (mode == TEST_MODE_SERVER) {
DEBUG_TEST("Ready. You should start selftest program on second host now...\n\n"); DEBUG_TEST("Ready. You should start selftest program on second host now...\n\n");
if(mode == TEST_MODE_CLIENT) }
if (mode == TEST_MODE_CLIENT) {
DEBUG_TEST("Ready. Contacting selftest program on first host.\n\n"); DEBUG_TEST("Ready. Contacting selftest program on first host.\n\n");
}
#endif // __SELFTEST__ #endif // __SELFTEST__
// What follows is a long-form of zts_simple_start(): // What follows is a long-form of zts_simple_start():
/* /*
zts_start(path.c_str()); zts_start(path.c_str());
printf("waiting for service to start...\n"); printf("waiting for service to start...\n");
while(!zts_running()) while(zts_running() == false)
sleep(1); sleep(1);
printf("joining network...\n"); printf("joining network...\n");
zts_join(nwid.c_str()); zts_join(nwid.c_str());
printf("waiting for address assignment...\n"); printf("waiting for address assignment...\n");
while(!zts_has_address(nwid.c_str())) while(zts_has_address(nwid.c_str()) == false)
sleep(1); sleep(1);
*/ */