Chromium Code Reviews| 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..ed83af1e4b46a3ce3628bd93453427e1c374f1a4 |
| --- /dev/null |
| +++ b/net/socket/unix_domain_server_socket_posix_unittest.cc |
| @@ -0,0 +1,118 @@ |
| +// 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 "net/socket/unix_domain_server_socket_posix.h" |
| + |
| +#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 "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +namespace { |
| + |
| +const char kSocketFilename[] = "unix_domain_socket_for_testing"; |
| +const char kInvalidSocketPath[] = "/invalid/path"; |
| + |
| +bool UserCanConnectCallback(bool allow_user, uid_t uid, gid_t gid) { |
|
mmenke
2014/07/11 20:44:04
Can we validate uid and gid here?
byungchul
2014/07/14 17:49:23
I don't understand what you want to validate? Vali
mmenke
2014/07/14 19:41:28
Yes - mostly just to make sure they're populated,
byungchul
2014/07/15 17:30:44
Done.
|
| + return allow_user; |
| +} |
| + |
| +UnixDomainServerSocket::AuthCallback CreateAuthCallback(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 { |
| + // Builds a socket path under a temp directory created newly. |
| + base::FilePath temp_dir; |
| + base::CreateNewTempDirectory("", &temp_dir); |
| + socket_path_ = temp_dir.Append(kSocketFilename).value(); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + // Deletes the temp dir created by SetUp(). |
| + base::DeleteFile(base::FilePath(socket_path_).DirName(), true); |
| + } |
| + |
| + std::string socket_path_; |
| +}; |
| + |
| +TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPath) { |
| + const bool kUseAbstractNamespace = false; |
| + UnixDomainServerSocket server_socket(CreateAuthCallback(true), |
| + kUseAbstractNamespace); |
| + EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| + server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); |
| +} |
| + |
| +TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { |
| + const bool kUseAbstractNamespace = true; |
| + UnixDomainServerSocket server_socket(CreateAuthCallback(true), |
| + kUseAbstractNamespace); |
| +#if defined(OS_ANDROID) || defined(OS_LINUX) |
| + EXPECT_EQ(OK, |
| + server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); |
| +#else |
| + EXPECT_EQ(ERR_ADDRESS_INVALID, |
| + server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); |
| +#endif |
| +} |
| + |
| +TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { |
| + const bool kUseAbstractNamespace = false; |
| + |
| + UnixDomainServerSocket server_socket(CreateAuthCallback(false), |
| + kUseAbstractNamespace); |
| + 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_, kUseAbstractNamespace); |
| + 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 |