Index: net/socket/unix_domain_server_socket_posix_unittest.cc |
diff --git a/net/socket/unix_domain_server_socket_posix_unittest.cc b/net/socket/unix_domain_server_socket_posix_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..86dd602d47afc5d65c55bff4226e8ca49460a9a1 |
--- /dev/null |
+++ b/net/socket/unix_domain_server_socket_posix_unittest.cc |
@@ -0,0 +1,123 @@ |
+// 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 <vector> |
+ |
+#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 "base/stl_util.h" |
+#include "net/base/io_buffer.h" |
+#include "net/base/net_errors.h" |
+#include "net/socket/unix_domain_client_socket_posix.h" |
+#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"; |
+const char kInvalidSocketPath[] = "/invalid/path"; |
+ |
+std::string MakeSocketPathForTesting() { |
+ base::FilePath temp_dir; |
+ base::GetTempDir(&temp_dir); |
+ return temp_dir.Append(kSocketFilename).value(); |
+} |
+ |
+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) { |
+ return base::Bind(&CallbackWithExpectedResultValue, expected_rv); |
+} |
+ |
+} // namespace |
+ |
+class UnixDomainServerSocketTest : public testing::Test { |
+ protected: |
+ virtual void SetUp() OVERRIDE { |
+ DeleteSocketPathForTesting(); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ DeleteSocketPathForTesting(); |
+ } |
+ |
+ void DeleteSocketPathForTesting() { |
+ base::DeleteFile(base::FilePath(MakeSocketPathForTesting()), false); |
+ } |
+}; |
+ |
+TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPath) { |
+ const std::string socket_path(kInvalidSocketPath); |
+ const bool use_abstract_namespace = false; |
+ UnixDomainServerSocket server_socket(GetAuthCallback(true), |
+ use_abstract_namespace); |
+ EXPECT_EQ(ERR_FILE_NOT_FOUND, |
+ server_socket.ListenWithAddressAndPort(socket_path, 0, 1)); |
+} |
+ |
+TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { |
+ const std::string socket_path(kInvalidSocketPath); |
+ const bool use_abstract_namespace = true; |
+ UnixDomainServerSocket server_socket(GetAuthCallback(true), |
+ use_abstract_namespace); |
+#if defined(OS_ANDROID) || defined(OS_LINUX) |
+ EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1)); |
+#else |
+ EXPECT_EQ(ERR_ADDRESS_INVALID, |
+ server_socket.ListenWithAddressAndPort(socket_path, 0, 1)); |
+#endif |
+} |
+ |
+TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { |
+ const std::string socket_path(MakeSocketPathForTesting()); |
+ const bool use_abstract_namespace = false; |
+ |
+ UnixDomainServerSocket server_socket(GetAuthCallback(false), |
+ 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(ERR_CONNECTION_CLOSED))); |
+ EXPECT_TRUE(!accepted_socket); |
+ |
+ UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace); |
+ EXPECT_FALSE(client_socket.IsConnected()); |
+ |
+ // Return success first because server socket accepts it. |
+ EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK))); |
+ EXPECT_TRUE(client_socket.IsConnected()); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ // Server disconnects the connection. |
+ EXPECT_FALSE(client_socket.IsConnected()); |
+ // But, server didn't create the accepted socket. |
+ EXPECT_TRUE(!accepted_socket); |
+ |
+ const int read_buffer_size = 10; |
+ scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size)); |
+ EXPECT_EQ(0, /* EOF */ |
+ client_socket.Read(read_buffer, read_buffer_size, |
+ GetCompletionCallback(OK))); |
+} |
+ |
+// Normal cases including read/write are tested by UnixDomainClientSocketTest. |
+ |
+} // namespace net |