Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/socket/unix_domain_server_socket_posix.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/stl_util.h" | |
| 15 #include "net/base/io_buffer.h" | |
| 16 #include "net/base/net_errors.h" | |
| 17 #include "net/socket/unix_domain_client_socket_posix.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace net { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const char kSocketFilename[] = "unix_domain_socket_for_testing"; | |
| 25 const char kInvalidSocketPath[] = "/invalid/path"; | |
| 26 | |
| 27 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.
| |
| 28 return allow_user; | |
| 29 } | |
| 30 | |
| 31 UnixDomainServerSocket::AuthCallback CreateAuthCallback(bool allow_user) { | |
| 32 return base::Bind(&UserCanConnectCallback, allow_user); | |
| 33 } | |
| 34 | |
| 35 void CallbackWithExpectedResultValue(int expected_rv, int rv) { | |
| 36 EXPECT_EQ(expected_rv, rv); | |
| 37 } | |
| 38 | |
| 39 CompletionCallback GetCompletionCallback(int expected_rv) { | |
| 40 return base::Bind(&CallbackWithExpectedResultValue, expected_rv); | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 class UnixDomainServerSocketTest : public testing::Test { | |
| 46 protected: | |
| 47 virtual void SetUp() OVERRIDE { | |
| 48 // Builds a socket path under a temp directory created newly. | |
| 49 base::FilePath temp_dir; | |
| 50 base::CreateNewTempDirectory("", &temp_dir); | |
| 51 socket_path_ = temp_dir.Append(kSocketFilename).value(); | |
| 52 } | |
| 53 | |
| 54 virtual void TearDown() OVERRIDE { | |
| 55 // Deletes the temp dir created by SetUp(). | |
| 56 base::DeleteFile(base::FilePath(socket_path_).DirName(), true); | |
| 57 } | |
| 58 | |
| 59 std::string socket_path_; | |
| 60 }; | |
| 61 | |
| 62 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPath) { | |
| 63 const bool kUseAbstractNamespace = false; | |
| 64 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | |
| 65 kUseAbstractNamespace); | |
| 66 EXPECT_EQ(ERR_FILE_NOT_FOUND, | |
| 67 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); | |
| 68 } | |
| 69 | |
| 70 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { | |
| 71 const bool kUseAbstractNamespace = true; | |
| 72 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | |
| 73 kUseAbstractNamespace); | |
| 74 #if defined(OS_ANDROID) || defined(OS_LINUX) | |
| 75 EXPECT_EQ(OK, | |
| 76 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); | |
| 77 #else | |
| 78 EXPECT_EQ(ERR_ADDRESS_INVALID, | |
| 79 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); | |
| 80 #endif | |
| 81 } | |
| 82 | |
| 83 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { | |
| 84 const bool kUseAbstractNamespace = false; | |
| 85 | |
| 86 UnixDomainServerSocket server_socket(CreateAuthCallback(false), | |
| 87 kUseAbstractNamespace); | |
| 88 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); | |
| 89 | |
| 90 scoped_ptr<StreamSocket> accepted_socket; | |
| 91 EXPECT_EQ(ERR_IO_PENDING, | |
| 92 server_socket.Accept(&accepted_socket, | |
| 93 GetCompletionCallback(ERR_CONNECTION_CLOSED))); | |
| 94 EXPECT_TRUE(!accepted_socket); | |
| 95 | |
| 96 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); | |
| 97 EXPECT_FALSE(client_socket.IsConnected()); | |
| 98 | |
| 99 // Return success first because server socket accepts it. | |
| 100 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK))); | |
| 101 EXPECT_TRUE(client_socket.IsConnected()); | |
| 102 | |
| 103 base::RunLoop().RunUntilIdle(); | |
| 104 // Server disconnects the connection. | |
| 105 EXPECT_FALSE(client_socket.IsConnected()); | |
| 106 // But, server didn't create the accepted socket. | |
| 107 EXPECT_TRUE(!accepted_socket); | |
| 108 | |
| 109 const int read_buffer_size = 10; | |
| 110 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size)); | |
| 111 EXPECT_EQ(0, /* EOF */ | |
| 112 client_socket.Read(read_buffer, read_buffer_size, | |
| 113 GetCompletionCallback(OK))); | |
| 114 } | |
| 115 | |
| 116 // Normal cases including read/write are tested by UnixDomainClientSocketTest. | |
| 117 | |
| 118 } // namespace net | |
| OLD | NEW |