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

Unified Diff: ipc/file_descriptor_set_unittest.cc

Issue 155905: Separates ipc code from common (http://crbug.com/16829) (Closed)
Patch Set: Fixes reference to 'common_message_traits' it's actually 'common_param_traits' Created 11 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
« no previous file with comments | « ipc/file_descriptor_set_posix.cc ('k') | ipc/ipc.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ipc/file_descriptor_set_unittest.cc
diff --git a/ipc/file_descriptor_set_unittest.cc b/ipc/file_descriptor_set_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..243d29e28ebbb2994a140da3bf3943b039ee2fbc
--- /dev/null
+++ b/ipc/file_descriptor_set_unittest.cc
@@ -0,0 +1,178 @@
+// Copyright (c) 2006-2009 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.
+
+// This test is POSIX only.
+
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "base/basictypes.h"
+#include "base/eintr_wrapper.h"
+#include "ipc/file_descriptor_set_posix.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// Get a safe file descriptor for test purposes.
+int GetSafeFd() {
+ return open("/dev/null", O_RDONLY);
+}
+
+// Returns true if fd was already closed. Closes fd if not closed.
+bool VerifyClosed(int fd) {
+ const int duped = dup(fd);
+ if (duped != -1) {
+ HANDLE_EINTR(close(duped));
+ HANDLE_EINTR(close(fd));
+ return false;
+ }
+ return true;
+}
+
+// The FileDescriptorSet will try and close some of the descriptor numbers
+// which we given it. This is the base descriptor value. It's great enough such
+// that no real descriptor will accidently be closed.
+static const int kFDBase = 50000;
+
+TEST(FileDescriptorSet, BasicAdd) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ ASSERT_EQ(set->size(), 0u);
+ ASSERT_TRUE(set->empty());
+ ASSERT_TRUE(set->Add(kFDBase));
+ ASSERT_EQ(set->size(), 1u);
+ ASSERT_TRUE(!set->empty());
+
+ // Empties the set and stops a warning about deleting a set with unconsumed
+ // descriptors
+ set->CommitAll();
+}
+
+TEST(FileDescriptorSet, BasicAddAndClose) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ ASSERT_EQ(set->size(), 0u);
+ ASSERT_TRUE(set->empty());
+ const int fd = GetSafeFd();
+ ASSERT_TRUE(set->AddAndAutoClose(fd));
+ ASSERT_EQ(set->size(), 1u);
+ ASSERT_TRUE(!set->empty());
+
+ set->CommitAll();
+
+ ASSERT_TRUE(VerifyClosed(fd));
+}
+TEST(FileDescriptorSet, MaxSize) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ for (unsigned i = 0;
+ i < FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE; ++i) {
+ ASSERT_TRUE(set->Add(kFDBase + 1 + i));
+ }
+
+ ASSERT_TRUE(!set->Add(kFDBase));
+
+ set->CommitAll();
+}
+
+TEST(FileDescriptorSet, SetDescriptors) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ ASSERT_TRUE(set->empty());
+ set->SetDescriptors(NULL, 0);
+ ASSERT_TRUE(set->empty());
+
+ const int fd = GetSafeFd();
+ static const int fds[] = {fd};
+ set->SetDescriptors(fds, 1);
+ ASSERT_TRUE(!set->empty());
+ ASSERT_EQ(set->size(), 1u);
+
+ set->CommitAll();
+
+ ASSERT_TRUE(VerifyClosed(fd));
+}
+
+TEST(FileDescriptorSet, GetDescriptors) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ set->GetDescriptors(NULL);
+ ASSERT_TRUE(set->Add(kFDBase));
+
+ int fds[1];
+ fds[0] = 0;
+ set->GetDescriptors(fds);
+ ASSERT_EQ(fds[0], kFDBase);
+ set->CommitAll();
+ ASSERT_TRUE(set->empty());
+}
+
+TEST(FileDescriptorSet, WalkInOrder) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ ASSERT_TRUE(set->Add(kFDBase));
+ ASSERT_TRUE(set->Add(kFDBase + 1));
+ ASSERT_TRUE(set->Add(kFDBase + 2));
+
+ ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
+ ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
+ ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
+
+ set->CommitAll();
+}
+
+TEST(FileDescriptorSet, WalkWrongOrder) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ ASSERT_TRUE(set->Add(kFDBase));
+ ASSERT_TRUE(set->Add(kFDBase + 1));
+ ASSERT_TRUE(set->Add(kFDBase + 2));
+
+ ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
+ ASSERT_EQ(set->GetDescriptorAt(2), -1);
+
+ set->CommitAll();
+}
+
+TEST(FileDescriptorSet, WalkCycle) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ ASSERT_TRUE(set->Add(kFDBase));
+ ASSERT_TRUE(set->Add(kFDBase + 1));
+ ASSERT_TRUE(set->Add(kFDBase + 2));
+
+ ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
+ ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
+ ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
+ ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
+ ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
+ ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
+ ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
+ ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
+ ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
+
+ set->CommitAll();
+}
+
+TEST(FileDescriptorSet, DontClose) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ const int fd = GetSafeFd();
+ ASSERT_TRUE(set->Add(fd));
+ set->CommitAll();
+
+ ASSERT_FALSE(VerifyClosed(fd));
+}
+
+TEST(FileDescriptorSet, DoClose) {
+ scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
+
+ const int fd = GetSafeFd();
+ ASSERT_TRUE(set->AddAndAutoClose(fd));
+ set->CommitAll();
+
+ ASSERT_TRUE(VerifyClosed(fd));
+}
+
+} // namespace
« no previous file with comments | « ipc/file_descriptor_set_posix.cc ('k') | ipc/ipc.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698