123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <time.h>
- #include <cutils/sockets.h>
- #include <gtest/gtest.h>
- static void TestConnectedSockets(cutils_socket_t server, cutils_socket_t client,
- int type) {
- ASSERT_NE(INVALID_SOCKET, server);
- ASSERT_NE(INVALID_SOCKET, client);
- char buffer[128];
- sockaddr_storage addr;
- socklen_t addr_size = sizeof(addr);
-
- ASSERT_EQ(3, send(client, "foo", 3, 0));
- if (type == SOCK_DGRAM) {
- EXPECT_EQ(3, recvfrom(server, buffer, sizeof(buffer), 0,
- reinterpret_cast<sockaddr*>(&addr), &addr_size));
- } else {
- EXPECT_EQ(3, recv(server, buffer, sizeof(buffer), 0));
- }
- EXPECT_EQ(0, memcmp(buffer, "foo", 3));
-
- if (type == SOCK_DGRAM) {
- ASSERT_EQ(3, sendto(server, "bar", 3, 0,
- reinterpret_cast<sockaddr*>(&addr), addr_size));
- } else {
- ASSERT_EQ(3, send(server, "bar", 3, 0));
- }
- EXPECT_EQ(3, recv(client, buffer, sizeof(buffer), 0));
- EXPECT_EQ(0, memcmp(buffer, "bar", 3));
-
- std::string data[] = {"foo", "bar", "12345"};
- cutils_socket_buffer_t socket_buffers[] = { {data[0].data(), data[0].length()},
- {data[1].data(), data[1].length()},
- {data[2].data(), data[2].length()} };
- EXPECT_EQ(11, socket_send_buffers(client, socket_buffers, 3));
- EXPECT_EQ(11, recv(server, buffer, sizeof(buffer), 0));
- EXPECT_EQ(0, memcmp(buffer, "foobar12345", 11));
- EXPECT_EQ(0, socket_close(server));
- EXPECT_EQ(0, socket_close(client));
- }
- void TestReceiveTimeout(cutils_socket_t sock) {
- time_t start_time;
- char buffer[32];
-
- EXPECT_EQ(0, socket_set_receive_timeout(sock, 20));
- start_time = time(nullptr);
- EXPECT_EQ(-1, recv(sock, buffer, sizeof(buffer), 0));
- EXPECT_LE(difftime(time(nullptr), start_time), 1.0);
-
- EXPECT_EQ(0, socket_set_receive_timeout(sock, 1250));
- start_time = time(nullptr);
- EXPECT_EQ(-1, recv(sock, buffer, sizeof(buffer), 0));
- EXPECT_LE(1.0, difftime(time(nullptr), start_time));
- }
- TEST(SocketsTest, TestGetLocalPort) {
- cutils_socket_t server;
-
-
-
- for (int port : {10000, 12345, 15999, 20202, 25000}) {
- for (int type : {SOCK_DGRAM, SOCK_STREAM}) {
- server = socket_inaddr_any_server(port, type);
- if (server != INVALID_SOCKET) {
- EXPECT_EQ(port, socket_get_local_port(server));
- }
- socket_close(server);
- }
- }
-
- EXPECT_EQ(-1, socket_get_local_port(INVALID_SOCKET));
- }
- TEST(SocketsTest, TestIpv4UdpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(0, SOCK_DGRAM);
- cutils_socket_t client = socket_network_client(
- "127.0.0.1", socket_get_local_port(server), SOCK_DGRAM);
- TestConnectedSockets(server, client, SOCK_DGRAM);
- }
- TEST(SocketsTest, TestIpv4TcpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
- ASSERT_NE(INVALID_SOCKET, server);
- cutils_socket_t client = socket_network_client(
- "127.0.0.1", socket_get_local_port(server), SOCK_STREAM);
- cutils_socket_t handler = accept(server, nullptr, nullptr);
- EXPECT_EQ(0, socket_close(server));
- TestConnectedSockets(handler, client, SOCK_STREAM);
- }
- TEST(SocketsTest, TestIpv6UdpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(0, SOCK_DGRAM);
- cutils_socket_t client = socket_network_client(
- "::1", socket_get_local_port(server), SOCK_DGRAM);
- TestConnectedSockets(server, client, SOCK_DGRAM);
- }
- TEST(SocketsTest, TestIpv6TcpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
- ASSERT_NE(INVALID_SOCKET, server);
- cutils_socket_t client = socket_network_client(
- "::1", socket_get_local_port(server), SOCK_STREAM);
- cutils_socket_t handler = accept(server, nullptr, nullptr);
- EXPECT_EQ(0, socket_close(server));
- TestConnectedSockets(handler, client, SOCK_STREAM);
- }
- TEST(SocketsTest, TestUdpReceiveTimeout) {
- cutils_socket_t sock = socket_inaddr_any_server(0, SOCK_DGRAM);
- ASSERT_NE(INVALID_SOCKET, sock);
- TestReceiveTimeout(sock);
- EXPECT_EQ(0, socket_close(sock));
- }
- TEST(SocketsTest, TestTcpReceiveTimeout) {
- cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
- ASSERT_NE(INVALID_SOCKET, server);
- cutils_socket_t client = socket_network_client(
- "localhost", socket_get_local_port(server), SOCK_STREAM);
- cutils_socket_t handler = accept(server, nullptr, nullptr);
- EXPECT_EQ(0, socket_close(server));
- TestReceiveTimeout(handler);
- EXPECT_EQ(0, socket_close(client));
- EXPECT_EQ(0, socket_close(handler));
- }
- TEST(SocketsTest, TestSocketSendBuffersFailure) {
- EXPECT_EQ(-1, socket_send_buffers(INVALID_SOCKET, nullptr, 0));
- }
|