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

Unified Diff: net/socket/unix_domain_server_socket_posix_unittest.cc

Issue 376323002: Refactor unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Checked uid and gid in unittests. 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_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..5aa3acada30f4bc3477ee80620acf08bf6d11876
--- /dev/null
+++ b/net/socket/unix_domain_server_socket_posix_unittest.cc
@@ -0,0 +1,115 @@
+// 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/files/file_path.h"
+#include "base/files/scoped_temp_dir.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/base/test_completion_callback.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) {
+ // Here peers are running in same process. Check if they are expected.
+ EXPECT_EQ(getuid(), uid);
+ EXPECT_EQ(getgid(), gid);
+ return allow_user;
+}
+
+UnixDomainServerSocket::AuthCallback CreateAuthCallback(bool allow_user) {
+ return base::Bind(&UserCanConnectCallback, allow_user);
+}
+
+} // namespace
+
+class UnixDomainServerSocketTest : public testing::Test {
+ protected:
+ UnixDomainServerSocketTest() {
+ EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
+ socket_path_ = temp_dir_.path().Append(kSocketFilename).value();
+ }
+
+ base::ScopedTempDir temp_dir_;
+ 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;
+ TestCompletionCallback accept_callback;
+ EXPECT_EQ(ERR_IO_PENDING,
+ server_socket.Accept(&accepted_socket, accept_callback.callback()));
+ EXPECT_FALSE(accepted_socket);
+
+ UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace);
+ EXPECT_FALSE(client_socket.IsConnected());
+
+ // Return success first because server socket accepts it.
+ TestCompletionCallback connect_callback;
+ int rv = client_socket.Connect(connect_callback.callback());
+ if (rv == ERR_IO_PENDING)
+ rv = connect_callback.WaitForResult();
mmenke 2014/07/15 15:25:56 If this case ever happens, I think the IsConnected
byungchul 2014/07/15 17:30:45 Sorry, I don't understand. How could IsConnect() f
mmenke 2014/07/15 17:44:41 If in WaitForResult, the accept callback is also c
byungchul 2014/07/15 18:07:01 Even if this happens, accept_callback will not be
mmenke 2014/07/15 19:12:57 I'm not concerned about accept_socket not being NU
byungchul 2014/07/15 22:01:18 Oh, now I understand what you meant. Done.
+ EXPECT_EQ(OK, rv);
+ EXPECT_TRUE(client_socket.IsConnected());
+
+ // Cannot use accept_callback.WaitForResult() because authentication error is
+ // invisible to the caller.
+ base::RunLoop().RunUntilIdle();
+ // Server disconnects the connection.
+ EXPECT_FALSE(client_socket.IsConnected());
+ // But, server didn't create the accepted socket.
+ EXPECT_FALSE(accepted_socket);
+
+ const int read_buffer_size = 10;
+ scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size));
+ TestCompletionCallback read_callback;
+ EXPECT_EQ(0, /* EOF */
+ client_socket.Read(read_buffer, read_buffer_size,
+ read_callback.callback()));
+}
+
+// Normal cases including read/write are tested by UnixDomainClientSocketTest.
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698