Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "content/browser/file_descriptor_info_impl.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <unistd.h> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/posix/eintr_wrapper.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Get a safe file descriptor for test purposes. | |
| 17 // TODO(morrita) Merge with things in file_descriptor_set_posix_unittest.cc | |
| 18 int GetSafeFd() { | |
| 19 return open("/dev/null", O_RDONLY); | |
| 20 } | |
| 21 | |
| 22 // Returns true if fd was already closed. Closes fd if not closed. | |
| 23 // TODO(morrita) Merge with things in file_descriptor_set_posix_unittest.cc | |
| 24 bool VerifyClosed(int fd) { | |
| 25 const int duped = dup(fd); | |
| 26 if (duped != -1) { | |
| 27 EXPECT_NE(IGNORE_EINTR(close(duped)), -1); | |
| 28 EXPECT_NE(IGNORE_EINTR(close(fd)), -1); | |
| 29 return false; | |
| 30 } | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 namespace content { | |
| 37 | |
| 38 typedef testing::Test FileDescriptorInfoTest; | |
| 39 | |
| 40 TEST_F(FileDescriptorInfoTest, Transfer) { | |
| 41 int testingId = 42; | |
| 42 scoped_ptr<FileDescriptorInfo> target(FileDescriptorInfoImpl::Create()); | |
| 43 base::ScopedFD fd(GetSafeFd()); | |
| 44 | |
| 45 int raw_fd = fd.get(); | |
| 46 target->Transfer(testingId, fd.Pass()); | |
| 47 ASSERT_EQ(1U, target->GetMappingSize()); | |
| 48 ASSERT_EQ(target->GetFDAt(0), raw_fd); | |
| 49 ASSERT_EQ(target->GetIDAt(0), testingId); | |
| 50 | |
| 51 target.reset(); | |
| 52 | |
| 53 ASSERT_TRUE(VerifyClosed(raw_fd)); | |
|
mdempsky
2014/09/24 21:16:44
FYI, a slightly more robust way to test that a FD
Hajime Morrita
2014/09/24 23:08:56
Acknowledged.
| |
| 54 } | |
| 55 | |
| 56 TEST_F(FileDescriptorInfoTest, TransferWithoutMapping) { | |
| 57 scoped_ptr<FileDescriptorInfo> target(FileDescriptorInfoImpl::Create()); | |
| 58 base::ScopedFD fd(GetSafeFd()); | |
| 59 | |
| 60 int raw_fd = fd.get(); | |
| 61 target->TransferWithoutMapping(fd.Pass()); | |
| 62 ASSERT_EQ(0U, target->GetMappingSize()); | |
| 63 | |
| 64 target.reset(); | |
| 65 | |
| 66 ASSERT_TRUE(VerifyClosed(raw_fd)); | |
| 67 } | |
| 68 | |
| 69 TEST_F(FileDescriptorInfoTest, Share) { | |
| 70 int testingId = 42; | |
| 71 scoped_ptr<FileDescriptorInfo> target(FileDescriptorInfoImpl::Create()); | |
| 72 base::ScopedFD fd(GetSafeFd()); | |
| 73 | |
| 74 int raw_fd = fd.get(); | |
| 75 target->Share(testingId, fd.get()); | |
| 76 ASSERT_EQ(1U, target->GetMappingSize()); | |
| 77 ASSERT_EQ(target->GetFDAt(0), raw_fd); | |
| 78 ASSERT_EQ(target->GetIDAt(0), testingId); | |
| 79 | |
| 80 target.reset(); | |
| 81 | |
| 82 ASSERT_TRUE(!VerifyClosed(fd.release())); | |
| 83 } | |
| 84 | |
| 85 TEST_F(FileDescriptorInfoTest, GetMappingWithIDAdjustment) { | |
| 86 int testingId1 = 42; | |
| 87 int testingId2 = 43; | |
| 88 scoped_ptr<FileDescriptorInfo> target(FileDescriptorInfoImpl::Create()); | |
| 89 | |
| 90 target->Transfer(testingId1, base::ScopedFD(GetSafeFd())); | |
| 91 target->Transfer(testingId2, base::ScopedFD(GetSafeFd())); | |
| 92 | |
| 93 base::FileHandleMappingVector mapping = | |
| 94 target->GetMappingWithIDAdjustment(100); | |
| 95 ASSERT_EQ(mapping[0].second, 142); | |
| 96 ASSERT_EQ(mapping[1].second, 143); | |
| 97 } | |
| 98 | |
| 99 } // namespace content | |
| OLD | NEW |