base: Small fix UnixSocket::SendStr testing method
A small change to the Send(const std::string&) method of
UnixSocket, which is used only in tests. The current method
appends a null-terminator to the string sent. This makes it
impossible to use with cases (upcoming in future CLs) where
we want to just send a larger buffer in chunks, as it injects
unwanted \0 in the middle.
This method seems used only in unix_socket_unittest.cc. Even
there, most usages are fine because ReceiveString already
null-terminates the received string.
In order to avoid accidental behavioral changes in code outside
of the repo, I am changing the name of the method, so if there
is any subtle dependency on the old behavior it will break
rather than silently omit terminators.
Test: perfetto_unittests
Bug: 205274609
Change-Id: Ia17ad2c10ad8979b5dc241b0387e41b7189ec519
diff --git a/src/base/unix_socket_unittest.cc b/src/base/unix_socket_unittest.cc
index b16f3ff..86a6bb5 100644
--- a/src/base/unix_socket_unittest.cc
+++ b/src/base/unix_socket_unittest.cc
@@ -131,8 +131,8 @@
// On Windows the first send immediately after the disconnection succeeds, the
// kernel will detect the disconnection only later.
- cli->Send(".");
- EXPECT_FALSE(cli->Send("should_fail_both_on_win_and_unix"));
+ cli->SendStr(".");
+ EXPECT_FALSE(cli->SendStr("should_fail_both_on_win_and_unix"));
task_runner_.RunUntilCheckpoint("cli_disconnected");
}
@@ -177,8 +177,8 @@
ASSERT_EQ("cli>srv", s->ReceiveString());
srv_did_recv();
}));
- ASSERT_TRUE(cli->Send("cli>srv"));
- ASSERT_TRUE(srv_conn->Send("srv>cli"));
+ ASSERT_TRUE(cli->SendStr("cli>srv"));
+ ASSERT_TRUE(srv_conn->SendStr("srv>cli"));
task_runner_.RunUntilCheckpoint("cli_did_recv");
task_runner_.RunUntilCheckpoint("srv_did_recv");
@@ -192,8 +192,8 @@
ASSERT_EQ("", cli->ReceiveString());
ASSERT_EQ(0u, srv_conn->Receive(&msg, sizeof(msg)));
ASSERT_EQ("", srv_conn->ReceiveString());
- ASSERT_FALSE(cli->Send("foo"));
- ASSERT_FALSE(srv_conn->Send("bar"));
+ ASSERT_FALSE(cli->SendStr("foo"));
+ ASSERT_FALSE(srv_conn->SendStr("bar"));
srv->Shutdown(true);
task_runner_.RunUntilCheckpoint("cli_disconnected");
task_runner_.RunUntilCheckpoint("srv_disconnected");
@@ -250,7 +250,7 @@
EXPECT_CALL(event_listener_, OnDataAvailable(s))
.WillOnce(Invoke([](UnixSocket* t) {
ASSERT_EQ("PING", t->ReceiveString());
- ASSERT_TRUE(t->Send("PONG"));
+ ASSERT_TRUE(t->SendStr("PONG"));
}));
}));
@@ -261,7 +261,7 @@
EXPECT_CALL(event_listener_, OnConnect(cli[i].get(), true))
.WillOnce(Invoke([](UnixSocket* s, bool success) {
ASSERT_TRUE(success);
- ASSERT_TRUE(s->Send("PING"));
+ ASSERT_TRUE(s->SendStr("PING"));
}));
auto checkpoint = task_runner_.CreateCheckpoint(std::to_string(i));
@@ -405,7 +405,7 @@
task_runner_.RunUntilCheckpoint("cli_connected");
srv->Shutdown(true);
- cli->Send("test");
+ cli->SendStr("test");
ASSERT_NE(peer, nullptr);
auto raw_sock = peer->ReleaseSocket();
@@ -413,10 +413,10 @@
EXPECT_CALL(event_listener_, OnDataAvailable(_)).Times(0);
task_runner_.RunUntilIdle();
- char buf[sizeof("test")];
+ char buf[5];
ASSERT_TRUE(raw_sock);
- ASSERT_EQ(raw_sock.Receive(buf, sizeof(buf)),
- static_cast<ssize_t>(sizeof(buf)));
+ ASSERT_EQ(raw_sock.Receive(buf, sizeof(buf)), 4);
+ buf[sizeof(buf) - 1] = '\0';
ASSERT_STREQ(buf, "test");
}
@@ -445,7 +445,7 @@
.WillRepeatedly(Invoke([](UnixSocket* cli_sock) {
cli_sock->ReceiveString(); // Read connection EOF;
}));
- ASSERT_TRUE(s->Send("welcome"));
+ ASSERT_TRUE(s->SendStr("welcome"));
}));
for (size_t i = 0; i < kNumClients; i++) {
@@ -717,7 +717,7 @@
// Now change the shared memory and ping the other process.
memcpy(mem, "rock more", 10);
- ASSERT_TRUE(s->Send("change notify"));
+ ASSERT_TRUE(s->SendStr("change notify"));
checkpoint();
}));
task_runner_.RunUntilCheckpoint("change_seen_by_client");