diff options
-rw-r--r-- | chrome/chrome.xcodeproj/project.pbxproj | 4 | ||||
-rw-r--r-- | chrome/common/file_descriptor_set_unittest.cc | 154 | ||||
-rw-r--r-- | chrome/test/unit/unit_tests.scons | 6 |
3 files changed, 164 insertions, 0 deletions
diff --git a/chrome/chrome.xcodeproj/project.pbxproj b/chrome/chrome.xcodeproj/project.pbxproj index d03152b..396c8751 100644 --- a/chrome/chrome.xcodeproj/project.pbxproj +++ b/chrome/chrome.xcodeproj/project.pbxproj @@ -286,6 +286,7 @@ 8CB218DCFAC761AC876C6531 /* ssl_blocking_page.cc in Sources */ = {isa = PBXBuildFile; fileRef = B5D16ECB0F21451600861FAC /* ssl_blocking_page.cc */; }; 8F51B73AAAF1772ECF9BD180 /* url_fetcher.cc in Sources */ = {isa = PBXBuildFile; fileRef = 778D7927798B7E3FAA498D3D /* url_fetcher.cc */; }; 94542322A5E5A8F4FDDAB7F0 /* render_view_host_manager.cc in Sources */ = {isa = PBXBuildFile; fileRef = A76E42AD0F28EDB5009A7E88 /* render_view_host_manager.cc */; }; + 97DD178B77011735FE4399E9 /* file_descriptor_set_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = D74DA6A9031CAE292790BD5E /* file_descriptor_set_unittest.cc */; }; 9E85B39CA40439D93CE52E60 /* fav_icon_helper.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BF8760E9D4839009A6919 /* fav_icon_helper.cc */; }; A0BC9272AD709E86D492DAEC /* bookmark_storage.cc in Sources */ = {isa = PBXBuildFile; fileRef = D941DBEFD8B8B537DE8A4DAE /* bookmark_storage.cc */; }; A0EB956531B9DB1E40DAE980 /* user_script_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 37521A11B07C479E93A39D52 /* user_script_unittest.cc */; }; @@ -2567,6 +2568,7 @@ BADB8BD20F3A4E4900989B26 /* resource_dispatcher_host.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = resource_dispatcher_host.h; path = renderer_host/resource_dispatcher_host.h; sourceTree = "<group>"; }; C18F2A0A6FB6BEF75406511D /* chrome_paths_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = chrome_paths_mac.cc; sourceTree = "<group>"; }; C8D26D9EC81E03E91E270463 /* session_service.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = session_service.cc; path = sessions/session_service.cc; sourceTree = "<group>"; }; + D74DA6A9031CAE292790BD5E /* file_descriptor_set_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = file_descriptor_set_unittest.cc; sourceTree = "<group>"; }; D941DBEFD8B8B537DE8A4DAE /* bookmark_storage.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = bookmark_storage.cc; path = bookmarks/bookmark_storage.cc; sourceTree = "<group>"; }; E40CC5E10F2E348900708647 /* history_contents_provider.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = history_contents_provider.cc; path = autocomplete/history_contents_provider.cc; sourceTree = "<group>"; }; E40CC5E20F2E348900708647 /* history_contents_provider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = history_contents_provider.h; path = autocomplete/history_contents_provider.h; sourceTree = "<group>"; }; @@ -3621,6 +3623,7 @@ 4D7BFBA50E9D4C9F009A6919 /* env_vars.cc */, 4D7BFBA60E9D4C9F009A6919 /* env_vars.h */, 4590B61B84978823B2BADA68 /* file_descriptor_set_posix.cc */, + D74DA6A9031CAE292790BD5E /* file_descriptor_set_unittest.cc */, 4D7BFBA70E9D4C9F009A6919 /* filter_policy.h */, 4D7BFBA80E9D4C9F009A6919 /* gears_api.h */, 4D7BFBAA0E9D4C9F009A6919 /* ipc_channel.h */, @@ -5470,6 +5473,7 @@ E4F324950EE5D758002533CE /* extension_unittest.cc in Sources */, A54612DC0EE9958600A8EE5D /* extensions_service_unittest.cc in Sources */, 331218300F3C010A006CB2B0 /* external_js_object.cc in Sources */, + 97DD178B77011735FE4399E9 /* file_descriptor_set_unittest.cc in Sources */, 0EE123B79B750A2FCEFB4569 /* history_backend_unittest.cc in Sources */, 71784BA931A610A94A7FEDC9 /* history_querying_unittest.cc in Sources */, 4D7BFB3C0E9D4C25009A6919 /* history_types_unittest.cc in Sources */, 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<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()); + ASSERT_TRUE(set->AddAndAutoClose(kFDBase)); + ASSERT_EQ(set->size(), 1u); + ASSERT_TRUE(!set->empty()); + + set->CommitAll(); +} + +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()); + + 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<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 = 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<FileDescriptorSet> 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); +} diff --git a/chrome/test/unit/unit_tests.scons b/chrome/test/unit/unit_tests.scons index 394df33..2a37fa6 100644 --- a/chrome/test/unit/unit_tests.scons +++ b/chrome/test/unit/unit_tests.scons @@ -367,6 +367,12 @@ if not env.Bit('windows'): '$TARGET_ROOT/grit_derived_sources/webkit_resources.res', ) +if env.Bit('posix'): + # POSIX only unittests + input_files.Append( + '$CHROME_DIR/common/file_descriptor_set_unittest.cc' + ) + if not env.Bit('windows'): # TODO(port): Port these. input_files.Remove( |