Merge "logd: add a test that logd ignores SIGPIPE"

This commit is contained in:
Tom Cherry 2020-08-12 17:18:57 +00:00 committed by Gerrit Code Review
commit 0df0fcbb9d

View file

@ -30,6 +30,7 @@
#include <android-base/file.h>
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
#include <cutils/sockets.h>
#include <gtest/gtest.h>
#include <log/log_read.h>
@ -41,6 +42,8 @@
#include "LogReader.h" // pickup LOGD_SNDTIMEO
using android::base::unique_fd;
#ifdef __ANDROID__
static void send_to_control(char* buf, size_t len) {
int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED,
@ -836,3 +839,34 @@ TEST(logd, getEventTag_newentry) {
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif
}
TEST(logd, no_epipe) {
#ifdef __ANDROID__
// Actually generating SIGPIPE in logd is racy, since we need to close the socket quicker than
// logd finishes writing the data to it, so we try 10 times, which should be enough to trigger
// SIGPIPE if logd isn't ignoring SIGPIPE
for (int i = 0; i < 10; ++i) {
unique_fd sock1(
socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
ASSERT_GT(sock1, 0);
unique_fd sock2(
socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
ASSERT_GT(sock2, 0);
std::string message = "getStatistics 0 1 2 3 4 5 6 7";
ASSERT_GT(write(sock1, message.c_str(), message.length()), 0);
sock1.reset();
ASSERT_GT(write(sock2, message.c_str(), message.length()), 0);
struct pollfd p = {.fd = sock2, .events = POLLIN, .revents = 0};
int ret = poll(&p, 1, 20);
EXPECT_EQ(ret, 1);
EXPECT_TRUE(p.revents & POLLIN);
EXPECT_FALSE(p.revents & POLL_ERR);
}
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif
}