diff options
author | mukesh agrawal <quiche@google.com> | 2016-11-15 19:24:07 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-11-15 19:24:07 +0000 |
commit | 8057c92c09a738396245a6d1e8d25fb1c9737f94 (patch) | |
tree | 91e1417182ea92a4a5035ce5f72573e158d5c31a | |
parent | da734f5096737de07cf8947c7364f7da51d00ec6 (diff) | |
parent | a5bb08c1cf8a8d1ff097fa7a050d7787e9821c6a (diff) | |
download | wifilogd-8057c92c09a738396245a6d1e8d25fb1c9737f94.tar.gz |
Os: add GetControlSocket()
am: a5bb08c1cf
Change-Id: I175d3321b9ed891e8180d88f1bf91d3f8a0e4dc9
-rw-r--r-- | Android.mk | 2 | ||||
-rw-r--r-- | os.cpp | 12 | ||||
-rw-r--r-- | os.h | 7 | ||||
-rw-r--r-- | raw_os.cpp | 6 | ||||
-rw-r--r-- | raw_os.h | 3 | ||||
-rw-r--r-- | tests/mock_os.h | 3 | ||||
-rw-r--r-- | tests/mock_raw_os.h | 1 | ||||
-rw-r--r-- | tests/os_unittest.cpp | 21 |
8 files changed, 55 insertions, 0 deletions
@@ -39,6 +39,7 @@ LOCAL_SRC_FILES := \ raw_os.cpp LOCAL_SHARED_LIBRARIES := \ libbase \ + libcutils \ liblog include $(BUILD_STATIC_LIBRARY) @@ -66,5 +67,6 @@ LOCAL_STATIC_LIBRARIES := \ libwifilogd LOCAL_SHARED_LIBRARIES := \ libbase \ + libcutils \ liblog include $(BUILD_NATIVE_TEST) @@ -33,10 +33,22 @@ namespace { constexpr auto kMaxNanoSeconds = 1000 * 1000 * 1000 - 1; } +constexpr int Os::kInvalidFd; + Os::Os() : raw_os_(new RawOs()) {} Os::Os(std::unique_ptr<RawOs> raw_os) : raw_os_(std::move(raw_os)) {} Os::~Os() {} +std::tuple<int, Os::Errno> Os::GetControlSocket( + const std::string& socket_name) { + int sock_fd = raw_os_->GetControlSocket(socket_name.c_str()); + if (sock_fd < 0) { + return {kInvalidFd, errno}; + } else { + return {sock_fd, 0}; + } +} + Os::Timestamp Os::GetTimestamp(clockid_t clock_id) const { struct timespec now_timespec; int failed = raw_os_->ClockGettime(clock_id, &now_timespec); @@ -21,6 +21,7 @@ #include <cstdint> #include <memory> +#include <string> #include <tuple> #include <utility> @@ -58,6 +59,12 @@ class Os { virtual ~Os(); + // Returns the Android control socket with name |socket_name|. If no such + // socket exists, or the init daemon has not provided this process with + // access to said socket, returns {kInvalidFd, errno}. + virtual std::tuple<int, Errno> GetControlSocket( + const std::string& socket_name); + // Returns the current time, as reported by the clock with |clock_id|. virtual Timestamp GetTimestamp(clockid_t clock_id) const; @@ -17,6 +17,8 @@ #include <time.h> #include <unistd.h> +#include "cutils/sockets.h" + #include "wifilogd/raw_os.h" namespace android { @@ -33,6 +35,10 @@ int RawOs::ClockGettime(clockid_t clock_id, struct timespec* ts) const { return clock_gettime(clock_id, ts); } +int RawOs::GetControlSocket(const char* socket_name) { + return android_get_control_socket(socket_name); +} + ssize_t RawOs::Recv(int sockfd, void* buf, size_t buflen, int flags) { return recv(sockfd, buf, buflen, flags); } @@ -38,6 +38,9 @@ class RawOs { virtual int ClockGettime(clockid_t clock_id, NONNULL struct timespec* tspec) const; + // See android_get_control_socket(). + virtual int GetControlSocket(const char* socket_name); + // See recv(). virtual ssize_t Recv(int sockfd, void* buf, size_t buflen, int flags); diff --git a/tests/mock_os.h b/tests/mock_os.h index 5a378f3..3e02730 100644 --- a/tests/mock_os.h +++ b/tests/mock_os.h @@ -17,6 +17,7 @@ #ifndef TESTS_MOCK_OS_H_ #define TESTS_MOCK_OS_H_ +#include <string> #include <tuple> #include "android-base/macros.h" @@ -33,6 +34,8 @@ class MockOs : public Os { ~MockOs() override; MOCK_CONST_METHOD1(GetTimestamp, Timestamp(clockid_t clock_id)); + MOCK_METHOD1(GetControlSocket, + std::tuple<int, Errno>(const std::string& socket_name)); MOCK_METHOD3(ReceiveDatagram, std::tuple<size_t, Errno>(int fd, void* buf, size_t buflen)); MOCK_METHOD3(Write, std::tuple<size_t, Os::Errno>(int fd, const void* buf, diff --git a/tests/mock_raw_os.h b/tests/mock_raw_os.h index 13149d9..3931cb0 100644 --- a/tests/mock_raw_os.h +++ b/tests/mock_raw_os.h @@ -32,6 +32,7 @@ class MockRawOs : public RawOs { MOCK_CONST_METHOD2(ClockGettime, int(clockid_t clock_id, struct timespec* tspec)); + MOCK_METHOD1(GetControlSocket, int(const char* socket_name)); MOCK_METHOD4(Recv, ssize_t(int sockfd, void* buf, size_t buflen, int flags)); MOCK_METHOD3(Write, ssize_t(int fd, const void* buf, size_t buflen)); diff --git a/tests/os_unittest.cpp b/tests/os_unittest.cpp index 00dea33..e90755a 100644 --- a/tests/os_unittest.cpp +++ b/tests/os_unittest.cpp @@ -34,6 +34,7 @@ using ::testing::Return; using ::testing::SetArgumentPointee; using ::testing::SetErrnoAndReturn; using ::testing::StrictMock; +using ::testing::StrEq; using local_utils::GetMaxVal; @@ -53,6 +54,26 @@ class OsTest : public ::testing::Test { } // namespace +TEST_F(OsTest, GetControlSocketReturnsFdAndZeroOnSuccess) { + constexpr char kSocketName[] = "fake-daemon"; + constexpr int kFakeValidFd = 100; + EXPECT_CALL(*raw_os_, GetControlSocket(StrEq(kSocketName))) + .WillOnce(Return(kFakeValidFd)); + + constexpr std::tuple<int, Os::Errno> kExpectedResult{kFakeValidFd, 0}; + EXPECT_EQ(kExpectedResult, os_->GetControlSocket(kSocketName)); +} + +TEST_F(OsTest, GetControlSocketReturnsInvalidFdAndErrorOnFailure) { + constexpr char kSocketName[] = "fake-daemon"; + constexpr Os::Errno kError = EINVAL; + EXPECT_CALL(*raw_os_, GetControlSocket(StrEq(kSocketName))) + .WillOnce(SetErrnoAndReturn(kError, -1)); + + constexpr std::tuple<int, Os::Errno> kExpectedResult{Os::kInvalidFd, kError}; + EXPECT_EQ(kExpectedResult, os_->GetControlSocket(kSocketName)); +} + TEST_F(OsTest, GetTimestampSucceeds) { constexpr auto kFakeSecs = 1U; constexpr auto kFakeNsecs = 2U; |