From c792d8102ca50178f6084be3bd251ded7a3110f8 Mon Sep 17 00:00:00 2001 From: "agl@chromium.org" Date: Fri, 13 Feb 2009 02:36:08 +0000 Subject: POSIX: Add FileDescriptorSet unittest Review URL: http://codereview.chromium.org/21296 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9736 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/common/file_descriptor_set_unittest.cc | 154 ++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 chrome/common/file_descriptor_set_unittest.cc (limited to 'chrome/common') diff --git a/chrome/common/file_descriptor_set_unittest.cc b/chrome/common/file_descriptor_set_unittest.cc new file mode 100644 index 0000000..061103a --- /dev/null +++ b/chrome/common/file_descriptor_set_unittest.cc @@ -0,0 +1,154 @@ +// 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 "base/basictypes.h" +#include "chrome/common/file_descriptor_set_posix.h" +#include "testing/gtest/include/gtest/gtest.h" + +// 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 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 set = new FileDescriptorSet; + + ASSERT_EQ(set->size(), 0u); + ASSERT_TRUE(set->empty()); + ASSERT_TRUE(set->AddAndAutoClose(kFDBase)); + ASSERT_EQ(set->size(), 1u); + ASSERT_TRUE(!set->empty()); + + set->CommitAll(); +} + +TEST(FileDescriptorSet, MaxSize) { + scoped_refptr 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 set = new FileDescriptorSet; + + ASSERT_TRUE(set->empty()); + set->SetDescriptors(NULL, 0); + ASSERT_TRUE(set->empty()); + + static const int fds[] = {kFDBase}; + set->SetDescriptors(fds, 1); + ASSERT_TRUE(!set->empty()); + ASSERT_EQ(set->size(), 1u); + + set->CommitAll(); +} + +TEST(FileDescriptorSet, GetDescriptors) { + scoped_refptr 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 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 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 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 set = new FileDescriptorSet; + + const int fd = open("/dev/null", O_RDONLY); + ASSERT_TRUE(set->Add(fd)); + set->CommitAll(); + + const int duped = dup(fd); + ASSERT_GE(duped, 0); + close(duped); + close(fd); +} + +TEST(FileDescriptorSet, DoClose) { + scoped_refptr set = new FileDescriptorSet; + + const int fd = open("/dev/null", O_RDONLY); + ASSERT_TRUE(set->AddAndAutoClose(fd)); + set->CommitAll(); + + const int duped = dup(fd); + ASSERT_EQ(duped, -1); + close(fd); +} -- cgit v1.1