libcutils: add android_get_control_socket() test am: 547e0dc45a
am: 4ffdd5d805
Change-Id: Ia81161b3ffa791a0bcb5b38c30e205907e6cdd6e
This commit is contained in:
commit
c5e0f78e2a
2 changed files with 108 additions and 17 deletions
|
|
@ -28,11 +28,31 @@
|
||||||
|
|
||||||
// This file contains socket implementation that can be shared between
|
// This file contains socket implementation that can be shared between
|
||||||
// platforms as long as the correct headers are included.
|
// platforms as long as the correct headers are included.
|
||||||
|
#define _GNU_SOURCE 1 // For asprintf
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#endif
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
#include <sys/un.h>
|
||||||
|
#endif
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <cutils/sockets.h>
|
#include <cutils/sockets.h>
|
||||||
|
|
||||||
#if !defined(_WIN32)
|
#ifndef TEMP_FAILURE_RETRY // _WIN32 does not define
|
||||||
#include <netinet/in.h>
|
#define TEMP_FAILURE_RETRY(exp) (exp)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int socket_get_local_port(cutils_socket_t sock) {
|
int socket_get_local_port(cutils_socket_t sock) {
|
||||||
|
|
@ -47,22 +67,56 @@ int socket_get_local_port(cutils_socket_t sock) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int android_get_control_socket(const char* name) {
|
int android_get_control_socket(const char* name) {
|
||||||
char key[64];
|
char *key = NULL;
|
||||||
snprintf(key, sizeof(key), ANDROID_SOCKET_ENV_PREFIX "%s", name);
|
if (asprintf(&key, ANDROID_SOCKET_ENV_PREFIX "%s", name) < 0) return -1;
|
||||||
|
if (!key) return -1;
|
||||||
|
|
||||||
|
char *cp = key;
|
||||||
|
while (*cp) {
|
||||||
|
if (!isalnum(*cp)) *cp = '_';
|
||||||
|
++cp;
|
||||||
|
}
|
||||||
|
|
||||||
const char* val = getenv(key);
|
const char* val = getenv(key);
|
||||||
if (!val) {
|
free(key);
|
||||||
return -1;
|
if (!val) return -1;
|
||||||
}
|
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
long ret = strtol(val, NULL, 10);
|
long fd = strtol(val, NULL, 10);
|
||||||
if (errno) {
|
if (errno) return -1;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (ret < 0 || ret > INT_MAX) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return static_cast<int>(ret);
|
// validity checking
|
||||||
|
if ((fd < 0) || (fd > INT_MAX)) return -1;
|
||||||
|
#if defined(_SC_OPEN_MAX)
|
||||||
|
if (fd >= sysconf(_SC_OPEN_MAX)) return -1;
|
||||||
|
#elif defined(OPEN_MAX)
|
||||||
|
if (fd >= OPEN_MAX) return -1;
|
||||||
|
#elif defined(_POSIX_OPEN_MAX)
|
||||||
|
if (fd >= _POSIX_OPEN_MAX) return -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(F_GETFD)
|
||||||
|
if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1;
|
||||||
|
#elif defined(F_GETFL)
|
||||||
|
if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)) < 0) return -1;
|
||||||
|
#else
|
||||||
|
struct stat s;
|
||||||
|
if (TEMP_FAILURE_RETRY(fstat(fd, &s)) < 0) return -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
socklen_t addrlen = sizeof(addr);
|
||||||
|
int ret = TEMP_FAILURE_RETRY(getsockname(fd, (struct sockaddr *)&addr, &addrlen));
|
||||||
|
if (ret < 0) return -1;
|
||||||
|
char *path = NULL;
|
||||||
|
if (asprintf(&path, ANDROID_SOCKET_DIR"/%s", name) < 0) return -1;
|
||||||
|
if (!path) return -1;
|
||||||
|
int cmp = strcmp(addr.sun_path, path);
|
||||||
|
free(path);
|
||||||
|
if (cmp != 0) return -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// It is what we think it is
|
||||||
|
return static_cast<int>(fd);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,14 @@
|
||||||
// IPv6 capabilities. These tests assume that no UDP packets are lost, which
|
// IPv6 capabilities. These tests assume that no UDP packets are lost, which
|
||||||
// should be the case for loopback communication, but is not guaranteed.
|
// should be the case for loopback communication, but is not guaranteed.
|
||||||
|
|
||||||
#include <cutils/sockets.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/un.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <cutils/sockets.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
// Makes sure the passed sockets are valid, sends data between them, and closes
|
// Makes sure the passed sockets are valid, sends data between them, and closes
|
||||||
|
|
@ -185,3 +189,36 @@ TEST(SocketsTest, TestTcpReceiveTimeout) {
|
||||||
TEST(SocketsTest, TestSocketSendBuffersFailure) {
|
TEST(SocketsTest, TestSocketSendBuffersFailure) {
|
||||||
EXPECT_EQ(-1, socket_send_buffers(INVALID_SOCKET, nullptr, 0));
|
EXPECT_EQ(-1, socket_send_buffers(INVALID_SOCKET, nullptr, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(SocketsTest, android_get_control_socket) {
|
||||||
|
static const char key[] = ANDROID_SOCKET_ENV_PREFIX "SocketsTest.android_get_control_socket";
|
||||||
|
static const char* name = key + strlen(ANDROID_SOCKET_ENV_PREFIX);
|
||||||
|
|
||||||
|
EXPECT_EQ(unsetenv(key), 0);
|
||||||
|
EXPECT_EQ(android_get_control_socket(name), -1);
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
ASSERT_GE(fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0), 0);
|
||||||
|
EXPECT_EQ(android_get_control_socket(name), -1);
|
||||||
|
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s", name);
|
||||||
|
unlink(addr.sun_path);
|
||||||
|
|
||||||
|
EXPECT_EQ(bind(fd, (struct sockaddr*)&addr, sizeof(addr)), 0);
|
||||||
|
EXPECT_EQ(android_get_control_socket(name), -1);
|
||||||
|
|
||||||
|
char val[32];
|
||||||
|
snprintf(val, sizeof(val), "%d", fd);
|
||||||
|
EXPECT_EQ(setenv(key, val, true), 0);
|
||||||
|
|
||||||
|
EXPECT_EQ(android_get_control_socket(name), fd);
|
||||||
|
socket_close(fd);
|
||||||
|
EXPECT_EQ(android_get_control_socket(name), -1);
|
||||||
|
EXPECT_EQ(unlink(addr.sun_path), 0);
|
||||||
|
EXPECT_EQ(android_get_control_socket(name), -1);
|
||||||
|
EXPECT_EQ(unsetenv(key), 0);
|
||||||
|
EXPECT_EQ(android_get_control_socket(name), -1);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue