Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / file_descriptor_info_impl_unittest.cc
blob9724a7cff64871be4432138c26c42460ee681804
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.
5 #include "content/browser/file_descriptor_info_impl.h"
7 #include <fcntl.h>
8 #include <unistd.h>
10 #include "base/basictypes.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace {
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);
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;
31 return true;
34 } // namespace
36 namespace content {
38 typedef testing::Test FileDescriptorInfoTest;
40 TEST_F(FileDescriptorInfoTest, Transfer) {
41 int testingId = 42;
42 scoped_ptr<FileDescriptorInfo> target(FileDescriptorInfoImpl::Create());
43 base::ScopedFD fd(GetSafeFd());
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);
51 target.reset();
53 ASSERT_TRUE(VerifyClosed(raw_fd));
56 TEST_F(FileDescriptorInfoTest, Share) {
57 int testingId = 42;
58 scoped_ptr<FileDescriptorInfo> target(FileDescriptorInfoImpl::Create());
59 base::ScopedFD fd(GetSafeFd());
61 int raw_fd = fd.get();
62 target->Share(testingId, fd.get());
63 ASSERT_EQ(1U, target->GetMappingSize());
64 ASSERT_EQ(target->GetFDAt(0), raw_fd);
65 ASSERT_EQ(target->GetIDAt(0), testingId);
67 target.reset();
69 ASSERT_TRUE(!VerifyClosed(fd.release()));
72 TEST_F(FileDescriptorInfoTest, GetMappingWithIDAdjustment) {
73 int testingId1 = 42;
74 int testingId2 = 43;
75 scoped_ptr<FileDescriptorInfo> target(FileDescriptorInfoImpl::Create());
77 target->Transfer(testingId1, base::ScopedFD(GetSafeFd()));
78 target->Transfer(testingId2, base::ScopedFD(GetSafeFd()));
80 base::FileHandleMappingVector mapping =
81 target->GetMappingWithIDAdjustment(100);
82 ASSERT_EQ(mapping[0].second, 142);
83 ASSERT_EQ(mapping[1].second, 143);
86 } // namespace content