static void receiver_with_big_timeout (const char *ip, const int port, const char *iface) { LWES_BYTE buffer[500]; struct lwes_net_connection connection; unsigned int i; unsigned int timeout_ms = 10000; for (i = 0; i < 500; i++) buffer[i]=(LWES_BYTE)0; assert (lwes_net_open (&connection, (char*)ip, (char*)iface, (int)port) == 0); for ( i = 0 ; i < num_to_send ; i++ ) { int j; int n; assert ((n = lwes_net_recv_bytes_by (&connection, buffer, 500, timeout_ms)) > 0 ); assert ( n == 45 ); for ( j = 0 ; j < 45 ; j++ ) { assert ( buffer[j] == j ); } } lwes_net_close (&connection); }
static void receiver_with_small_timeout (const char *ip, const int port, const char *iface) { LWES_BYTE buffer[500]; struct lwes_net_connection connection; unsigned int i; unsigned int timeout_ms = 1; int n; for (i = 0; i < 500; i++) buffer[i]=(LWES_BYTE)0; assert (lwes_net_open (&connection, (char*)ip, (char*)iface, (int)port) == 0); sleep (1); assert ((n = lwes_net_recv_bytes_by (&connection, buffer, MAX_MSG_SIZE, timeout_ms)) < 0); lwes_net_close (&connection); }
static void sender (const char *ip, const int port, const char *iface) { LWES_BYTE buffer[500]; struct lwes_net_connection connection; unsigned int i; /* initialize to nothing */ for (i = 0; i < 500; i++) buffer[i]=(LWES_BYTE)0; /* now set 45 bytes */ for (i = 0; i < 45; i++) { buffer[i]=(LWES_BYTE)i; } assert ( lwes_net_open (&connection, (char*)ip, (char*)iface, (int)port) == 0 ); for ( i = 0 ; i < num_to_send; i++ ) { assert ( lwes_net_send_bytes (&connection,buffer,45) > 0 ); } lwes_net_close (&connection); }
static void test_ttl_functions (void) { struct lwes_net_connection connection; assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == 0); /* test success of get */ assert (lwes_net_get_ttl (&connection) == 1); /* now test failure of getsockopt in get */ getsockopt_error_when = IP_MULTICAST_TTL; assert (lwes_net_get_ttl (&connection) < 0); getsockopt_error_when = GETSOCKOPT_NO_ERROR; /* test success of set */ assert (lwes_net_set_ttl (&connection, 3) == 0); /* check success of set */ assert (lwes_net_get_ttl (&connection) == 3); /* now test failure of setsockopt in set */ setsockopt_error_when = IP_MULTICAST_TTL; assert (lwes_net_set_ttl (&connection,5) < 0); setsockopt_error_when = GETSOCKOPT_NO_ERROR; assert (lwes_net_get_ttl (&connection) == 3); }
int lwes_net_sendto_bytes (struct lwes_net_connection *conn, char *address, char *iface, int port, LWES_BYTE_P bytes, size_t len) { struct lwes_net_connection new_conn; int size; if (conn == NULL || bytes == NULL) { return -1; } if (lwes_net_open (&new_conn, address, iface, port) < 0) { return -2; } if ((size = lwes_net_send_bytes (&new_conn, bytes, len)) < 0) { return -3; } /* There's not much we can do about a close error, and it would mask a successful send, so just ignore a bad return value */ (void) lwes_net_close (&new_conn); return size; }
static void test_null_args (void) { LWES_BYTE buffer[500]; unsigned int i; struct lwes_net_connection connection; /* initialize to nothing */ for (i = 0; i < 500; i++) buffer[i]=(LWES_BYTE)0; /* now set 45 bytes */ for (i = 0; i < 45; i++) { buffer[i]=(LWES_BYTE)i; } /* test NULL connection to all calls */ assert (lwes_net_open (NULL, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == -1); assert (lwes_net_close (NULL) == -1); assert (lwes_net_get_ttl (NULL) == -1); assert (lwes_net_set_ttl (NULL, 5) == -1); assert (lwes_net_get_sock_fd (NULL) == -1); assert (lwes_net_send_bytes (NULL, buffer, 45) == -1); assert (lwes_net_send_bytes (&connection, NULL, 45) == -1); assert (lwes_net_sendto_bytes (NULL, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port, buffer, 45) == -1); assert (lwes_net_sendto_bytes (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port, NULL, 45) == -1); assert (lwes_net_recv_bind (NULL) == -1); assert (lwes_net_recv_bytes (NULL, buffer, 500) == -1); assert (lwes_net_recv_bytes (&connection, NULL, 500) == -1); assert (lwes_net_recv_bytes_by (NULL, buffer, 500, 10000) == -1); assert (lwes_net_recv_bytes_by (&connection, NULL, 500, 10000) == -1); }
static void sender_to (const char *ip, const int port, const char *iface) { LWES_BYTE buffer[500]; struct lwes_net_connection connection; unsigned int i; int new_port = port+1; /* freebsd seems to bail on setsockopt for the interface if we have a null address, so test with two real addresses */ char hostname[500]; char ipaddr[500]; struct hostent *hptr; assert (gethostname (hostname, 500) == 0); assert ((hptr = gethostbyname (hostname)) != NULL); switch (hptr->h_addrtype) { case AF_INET: { char **pptr; pptr = hptr->h_addr_list; inet_ntop (hptr->h_addrtype, *pptr, ipaddr, 500); } break; } /* initialize to nothing */ for (i = 0; i < 500; i++) buffer[i]=(LWES_BYTE)0; /* now set 45 bytes */ for (i = 0; i < 45; i++) { buffer[i]=(LWES_BYTE)i; } /* open up on the null interface, but sendto a different interface */ assert ( lwes_net_open (&connection, (char*)ip, (char*)ipaddr, (int)port) == 0 ); for ( i = 0 ; i < num_to_send; i++ ) { assert (lwes_net_sendto_bytes (&connection, (char*)ip, (char*)iface, (int)new_port, buffer,45) > 0); } lwes_net_close (&connection); }
int my_lwes_net_open (struct lwes_net_connection *conn, const char *address, const char *iface, int port) { if (lwes_net_open_error == 0) { return lwes_net_open (conn, address, iface, port); } return -1; }
static void test_get_sock_fd (void) { struct lwes_net_connection connection; assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == 0); assert (lwes_net_get_sock_fd (&connection) == connection.socketfd); lwes_net_close (&connection); }
static void test_large_send () { struct lwes_net_connection connection; unsigned int i; LWES_BYTE buffer [MAX_MSG_SIZE]; for (i=0; i < MAX_MSG_SIZE; i++) { buffer[i] = (LWES_BYTE) i; } assert ( lwes_net_open (&connection, (char *)mcast_ip, (char *)mcast_iface, (int)mcast_port) == 0 ); assert ( lwes_net_send_bytes (&connection, buffer, MAX_MSG_SIZE) > 0); lwes_net_close (&connection); }
static void test_sendto_bytes_failures (void) { LWES_BYTE buffer[500]; struct lwes_net_connection connection; unsigned int i; int new_port = mcast_port+1; /* freebsd seems to bail on setsockopt for the interface if we have a null address, so test with two real addresses */ char hostname[500]; char ipaddr[500]; struct hostent *hptr; assert (gethostname (hostname, 500) == 0); assert ((hptr = gethostbyname (hostname)) != NULL); switch (hptr->h_addrtype) { case AF_INET: { char **pptr; pptr = hptr->h_addr_list; inet_ntop (hptr->h_addrtype, *pptr, ipaddr, 500); } break; } /* initialize to nothing */ for (i = 0; i < 500; i++) buffer[i]=(LWES_BYTE)0; /* now set 45 bytes */ for (i = 0; i < 45; i++) { buffer[i]=(LWES_BYTE)i; } assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == 0); /* lwes_net_open failure */ socket_error = 1; assert (lwes_net_sendto_bytes (&connection, (char*)mcast_ip, (char*)ipaddr, (int)new_port, buffer,45) == -2); socket_error = 0; /* lwes_net_send_bytes failure */ sendto_error = 1; assert (lwes_net_sendto_bytes (&connection, (char*)mcast_ip, (char*)ipaddr, (int)new_port, buffer,45) == -3); sendto_error = 0; assert (lwes_net_close (&connection) == 0); }
static void test_socket_function_failures (void) { struct lwes_net_connection connection; LWES_BYTE buffer[500]; /* socket system call failure */ socket_error = 1; assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == -2); socket_error = 0; /* setsockopt of IP_MULTICAST_IF failure */ setsockopt_error_when = IP_MULTICAST_IF; assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface2, (int)mcast_port) == -3); setsockopt_error_when = SETSOCKOPT_NO_ERROR; /* setsockopt of SO_SNDBUF failure */ setsockopt_error_when = SO_SNDBUF; assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface2, (int)mcast_port) == -4); setsockopt_error_when = SETSOCKOPT_NO_ERROR; /* test failures in recv_bind */ /* need a successful open */ assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == 0); /* First setsockopt SO_REUSEADDR failure */ setsockopt_error_when = SO_REUSEADDR; assert (lwes_net_recv_bind (&connection) == -2); setsockopt_error_when = SETSOCKOPT_NO_ERROR; /* Second setsockopt SO_RCVBUF failure */ setsockopt_error_when = SO_RCVBUF; assert (lwes_net_recv_bind (&connection) == -3); setsockopt_error_when = SETSOCKOPT_NO_ERROR; /* Third bind () failure */ bind_error = 1; assert (lwes_net_recv_bind (&connection) == -4); bind_error = 0; /* Fourth IP_ADD_MEMBERSHIP failure */ setsockopt_error_when = IP_ADD_MEMBERSHIP; assert (lwes_net_recv_bind (&connection) == -5); setsockopt_error_when = SETSOCKOPT_NO_ERROR; assert (lwes_net_close (&connection) == 0); /* successful so we can test close failure */ assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == 0); assert (lwes_net_recv_bind (&connection) == 0); setsockopt_error_when = IP_DROP_MEMBERSHIP; assert (lwes_net_close (&connection) == -2); setsockopt_error_when = SETSOCKOPT_NO_ERROR; assert (lwes_net_close (&connection) == 0); /* Also want to test bind failures in recv functions */ assert (lwes_net_open (&connection, (char*)mcast_ip, (char*)mcast_iface, (int)mcast_port) == 0); bind_error = 1; assert (lwes_net_recv_bytes (&connection, buffer, 500) == -4); assert (lwes_net_recv_bytes_by (&connection, buffer, 500, 10000) == -4); bind_error = 0; assert (lwes_net_close (&connection) == 0); }