diff --git a/test/apps/network/sockoption.c b/test/apps/network/sockoption.c index 1219ebc14..0c840a69b 100644 --- a/test/apps/network/sockoption.c +++ b/test/apps/network/sockoption.c @@ -6,74 +6,64 @@ #include #include #include +#include "test.h" -int main() +int sockfd; +int option; + +FN_SETUP(general) { - int sockfd; - int option; + sockfd = CHECK(socket(AF_INET, SOCK_STREAM, 0)); +} +END_SETUP() - // Create tcp socket - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) { - perror("Socket creation failed"); - exit(EXIT_FAILURE); - } - - // Get send buffer size +FN_TEST(buffer_size) +{ int sendbuf; socklen_t sendbuf_len = sizeof(sendbuf); - if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuf, &sendbuf_len) < - 0 || - sendbuf_len != sizeof(sendbuf)) { - perror("Getting SO_SNDBUF option failed"); - exit(EXIT_FAILURE); - } + TEST_RES(getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuf, + &sendbuf_len), + sendbuf_len == sizeof(sendbuf)); +} +END_TEST() +FN_TEST(socket_error) +{ int error; socklen_t error_len = sizeof(error); - if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &error_len) < 0 || - error_len != sizeof(error) || error != 0) { - perror("Getting SO_SNDBUF option failed"); - exit(EXIT_FAILURE); - } + TEST_RES(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &error_len), + error_len == sizeof(error) && error == 0); +} +END_TEST() +FN_TEST(nagle) +{ // Disable Nagle algorithm option = 1; - if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &option, - sizeof(option)) < 0) { - perror("Setting TCP_NODELAY option failed"); - exit(EXIT_FAILURE); - } + CHECK(setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &option, + sizeof(option))); - // Enable reuse addr - option = 1; - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &option, - sizeof(option)) < 0) { - perror("Setting SO_REUSEADDR option failed"); - exit(EXIT_FAILURE); - } - - // Print new value + // Get new value int nagle; socklen_t nagle_len = sizeof(nagle); - if (getsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &nagle, &nagle_len) < - 0 || - nagle != 1) { - perror("Getting TCP_NODELAY option failed."); - exit(EXIT_FAILURE); - } + TEST_RES(getsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &nagle, + &nagle_len), + nagle == 1); +} +END_TEST() +FN_TEST(reuseaddr) +{ + // Enable reuse address + option = 1; + CHECK(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &option, + sizeof(option))); + + // Get new value int reuseaddr; socklen_t reuseaddr_len = sizeof(reuseaddr); - if (getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, - &reuseaddr_len) < 0 || - reuseaddr != 1) { - perror("Getting SO_REUSEADDR option failed."); - exit(EXIT_FAILURE); - } - - // Close socket - close(sockfd); - - return 0; + TEST_RES(getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, + &reuseaddr_len), + reuseaddr == 1); } +END_TEST()