Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Unified Diff: net/socket/unix_domain_client_socket_posix_unittest.cc

Issue 376323002: Refactor unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/socket/unix_domain_client_socket_posix_unittest.cc
diff --git a/net/socket/unix_domain_client_socket_posix_unittest.cc b/net/socket/unix_domain_client_socket_posix_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ee9b88ee27227c9e955379c3b1bf33bdbcd56334
--- /dev/null
+++ b/net/socket/unix_domain_client_socket_posix_unittest.cc
@@ -0,0 +1,237 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/bind.h"
+#include "base/file_util.h"
+#include "base/files/file_path.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/run_loop.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/socket/unix_domain_client_socket_posix.h"
mmenke 2014/07/10 19:07:28 nit: This should go up top
byungchul 2014/07/11 02:56:33 Done.
+#include "net/socket/unix_domain_server_socket_posix.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace {
+
+const char kSocketFilename[] = "unix_domain_socket_for_testing";
+
+std::string MakeSocketPathForTesting() {
mmenke 2014/07/10 19:07:28 nit: Maybe Make -> Get?
mmenke 2014/07/10 19:07:28 nit: "ForTesting" generally not needed in a unitt
byungchul 2014/07/11 02:56:33 Done.
byungchul 2014/07/11 02:56:33 Done.
+ base::FilePath temp_dir;
+ base::GetTempDir(&temp_dir);
+ return temp_dir.Append(kSocketFilename).value();
mmenke 2014/07/10 19:07:28 This doesn't work - multiple tests should be able
byungchul 2014/07/11 02:56:33 Creates a temp dir instead.
+}
+
+bool UserCanConnectCallback(bool allow_user, uid_t uid, gid_t gid) {
+ return allow_user;
+}
+
+UnixDomainServerSocket::AuthCallback GetAuthCallback(bool allow_user) {
+ return base::Bind(&UserCanConnectCallback, allow_user);
+}
+
+void CallbackWithExpectedResultValue(int expected_rv, int rv) {
+ EXPECT_EQ(expected_rv, rv);
+}
+
+CompletionCallback GetCompletionCallback(int expected_rv) {
mmenke 2014/07/10 19:07:28 nit: Maybe Get -> Create?
byungchul 2014/07/11 02:56:33 Done.
+ return base::Bind(&CallbackWithExpectedResultValue, expected_rv);
+}
+
+} // namespace
+
+class UnixDomainClientSocketTest : public testing::Test {
+ protected:
+ virtual void SetUp() OVERRIDE {
+ DeleteSocketPathForTesting();
+ }
+
+ virtual void TearDown() OVERRIDE {
+ DeleteSocketPathForTesting();
+ }
+
+ void DeleteSocketPathForTesting() {
mmenke 2014/07/10 19:07:28 nit: "ForTesting" generally not needed in a unitt
byungchul 2014/07/11 02:56:33 Done.
+ base::DeleteFile(base::FilePath(MakeSocketPathForTesting()), false);
+ }
+};
+
+TEST_F(UnixDomainClientSocketTest, Connect) {
+ const std::string socket_path(MakeSocketPathForTesting());
+ const bool use_abstract_namespace = false;
mmenke 2014/07/10 19:07:28 Sure use kConstNamingScheme for consts, I believe.
byungchul 2014/07/11 02:56:33 Done.
+
+ UnixDomainServerSocket server_socket(GetAuthCallback(true),
+ use_abstract_namespace);
+ EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
+
+ scoped_ptr<StreamSocket> accepted_socket;
+ EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
+ GetCompletionCallback(OK)));
+ EXPECT_TRUE(!accepted_socket);
+
+ UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
+ EXPECT_FALSE(client_socket.IsConnected());
+
+ EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
+ EXPECT_TRUE(client_socket.IsConnected());
+ // Not yet notified to server.
+ EXPECT_TRUE(!accepted_socket);
+
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(accepted_socket);
+ EXPECT_TRUE(accepted_socket->IsConnected());
+}
+
+TEST_F(UnixDomainClientSocketTest, ConnectWithAbstractNamespace) {
+ const std::string socket_path(MakeSocketPathForTesting());
+ const bool use_abstract_namespace = true;
+
+ UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
+ EXPECT_FALSE(client_socket.IsConnected());
+
+#if defined(OS_ANDROID) || defined(OS_LINUX)
+ UnixDomainServerSocket server_socket(GetAuthCallback(true),
+ use_abstract_namespace);
+ EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
+
+ scoped_ptr<StreamSocket> accepted_socket;
+ EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
+ GetCompletionCallback(OK)));
+ EXPECT_TRUE(!accepted_socket);
+
+ EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
+ EXPECT_TRUE(client_socket.IsConnected());
+ // Not yet notified to server.
+ EXPECT_TRUE(!accepted_socket);
+
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(accepted_socket);
+ EXPECT_TRUE(accepted_socket->IsConnected());
+#else
+ EXPECT_EQ(ERR_ADDRESS_INVALID,
+ client_socket.Connect(GetCompletionCallback(OK)));
+#endif
+}
+
+TEST_F(UnixDomainClientSocketTest, ConnectToNonExistingSocket) {
+ const std::string socket_path(MakeSocketPathForTesting());
+ const bool use_abstract_namespace = false;
+
+ UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
+ EXPECT_FALSE(client_socket.IsConnected());
+ EXPECT_EQ(ERR_FILE_NOT_FOUND,
+ client_socket.Connect(GetCompletionCallback(OK)));
+}
+
+TEST_F(UnixDomainClientSocketTest,
+ ConnectToNonExistingSocketWithAbstractNamespace) {
+ const std::string socket_path(MakeSocketPathForTesting());
+ const bool use_abstract_namespace = true;
+
+ UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
+ EXPECT_FALSE(client_socket.IsConnected());
+#if defined(OS_ANDROID) || defined(OS_LINUX)
+ EXPECT_EQ(ERR_CONNECTION_REFUSED,
+ client_socket.Connect(GetCompletionCallback(OK)));
+#else
+ EXPECT_EQ(ERR_ADDRESS_INVALID,
+ client_socket.Connect(GetCompletionCallback(OK)));
+#endif
+}
+
+TEST_F(UnixDomainClientSocketTest, ReadWrite) {
+ const std::string socket_path(MakeSocketPathForTesting());
+
+ UnixDomainServerSocket server_socket(GetAuthCallback(true), false);
+ EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
+ scoped_ptr<StreamSocket> accepted_socket;
+ EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
+ GetCompletionCallback(OK)));
+ UnixDomainClientSocket client_socket(socket_path, false);
+ EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
+
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(accepted_socket->IsConnected());
+ EXPECT_TRUE(client_socket.IsConnected());
+
+ // Sends data from client to server.
+ const int write_data_size = 10;
+ scoped_refptr<IOBuffer> write_buffer(
+ new StringIOBuffer(std::string(write_data_size, 'd')));
+ EXPECT_EQ(write_data_size,
+ client_socket.Write(write_buffer, write_data_size,
+ GetCompletionCallback(OK)));
+ // Buffer bigger than write data size.
+ const int read_buffer_size = write_data_size * 2;
+ scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size));
+ EXPECT_EQ(write_data_size,
+ accepted_socket->Read(read_buffer, read_buffer_size,
+ GetCompletionCallback(OK)));
+
+ // Sends data from server and client
+ EXPECT_EQ(write_data_size,
+ accepted_socket->Write(write_buffer, write_data_size,
+ GetCompletionCallback(OK)));
+ // Reads multiple times.
+ const int small_read_buffer_size = write_data_size / 3;
+ EXPECT_EQ(small_read_buffer_size,
+ client_socket.Read(read_buffer, small_read_buffer_size,
+ GetCompletionCallback(OK)));
+ EXPECT_EQ(write_data_size - small_read_buffer_size,
+ client_socket.Read(read_buffer, read_buffer_size,
+ GetCompletionCallback(OK)));
+ // No more data.
+ EXPECT_EQ(ERR_IO_PENDING,
+ client_socket.Read(read_buffer, read_buffer_size,
+ GetCompletionCallback(ERR_CONNECTION_CLOSED)));
+}
+
+TEST_F(UnixDomainClientSocketTest, ReadWriteAsynchronously) {
+ const std::string socket_path(MakeSocketPathForTesting());
+
+ UnixDomainServerSocket server_socket(GetAuthCallback(true), false);
+ EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
+ scoped_ptr<StreamSocket> accepted_socket;
+ EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
+ GetCompletionCallback(OK)));
+ UnixDomainClientSocket client_socket(socket_path, false);
+ EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
+
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(accepted_socket->IsConnected());
+ EXPECT_TRUE(client_socket.IsConnected());
+
+ // Waits for data from client.
+ const int write_data_size = 10;
+ const int read_buffer_size = write_data_size * 2;
+ const int small_read_buffer_size = write_data_size / 3;
+ // Reads smaller than write data size first.
+ scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size));
+ EXPECT_EQ(
+ ERR_IO_PENDING,
+ accepted_socket->Read(read_buffer, small_read_buffer_size,
+ GetCompletionCallback(small_read_buffer_size)));
+
+ scoped_refptr<IOBuffer> write_buffer(
+ new StringIOBuffer(std::string(write_data_size, 'd')));
+ EXPECT_EQ(write_data_size,
+ client_socket.Write(write_buffer, write_data_size,
+ GetCompletionCallback(OK)));
+
+ base::RunLoop().RunUntilIdle();
+ // First read completed.
+
+ // Reads remaining data.
+ EXPECT_EQ(write_data_size - small_read_buffer_size,
+ accepted_socket->Read(read_buffer, read_buffer_size,
+ GetCompletionCallback(OK)));
+ // No more data.
+ EXPECT_EQ(ERR_IO_PENDING,
+ accepted_socket->Read(
+ read_buffer, read_buffer_size,
+ GetCompletionCallback(ERR_CONNECTION_CLOSED)));
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698