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.
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/shared_memory.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/win/scoped_handle.h"
13 #include "base/win/windows_version.h"
14 #include "sandbox/win/tests/common/controller.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 SBOX_TESTS_COMMAND
int HandleInheritanceTests_PrintToStdout(int argc
,
21 printf("Example output to stdout\n");
22 return SBOX_TEST_SUCCEEDED
;
25 TEST(HandleInheritanceTests
, TestStdoutInheritance
) {
26 base::ScopedTempDir temp_directory
;
27 base::FilePath temp_file_name
;
28 ASSERT_TRUE(temp_directory
.CreateUniqueTempDir());
29 ASSERT_TRUE(CreateTemporaryFileInDir(temp_directory
.path(), &temp_file_name
));
31 SECURITY_ATTRIBUTES attrs
= {};
32 attrs
.nLength
= sizeof(attrs
);
33 attrs
.bInheritHandle
= TRUE
;
34 base::win::ScopedHandle
tmp_handle(
35 CreateFile(temp_file_name
.value().c_str(), GENERIC_WRITE
,
36 FILE_SHARE_WRITE
| FILE_SHARE_READ
| FILE_SHARE_DELETE
,
37 &attrs
, OPEN_EXISTING
, 0, NULL
));
38 ASSERT_TRUE(tmp_handle
.IsValid());
41 ASSERT_EQ(SBOX_ALL_OK
, runner
.GetPolicy()->SetStdoutHandle(tmp_handle
.Get()));
42 int result
= runner
.RunTest(L
"HandleInheritanceTests_PrintToStdout");
43 ASSERT_EQ(SBOX_TEST_SUCCEEDED
, result
);
46 ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name
), &data
));
47 // Redirection uses a feature that was added in Windows Vista.
48 if (base::win::GetVersion() >= base::win::VERSION_VISTA
) {
49 ASSERT_EQ("Example output to stdout\r\n", data
);
55 SBOX_TESTS_COMMAND
int
56 HandleInheritanceTests_ValidInheritedHandle(int argc
, wchar_t **argv
) {
58 return SBOX_TEST_FAILED_TO_RUN_TEST
;
59 base::SharedMemoryHandle handle
= nullptr;
60 base::StringToUint(argv
[0], reinterpret_cast<unsigned int *>(&handle
));
62 // This handle we inherited must be both valid and closeable.
64 if (!GetHandleInformation(handle
, &flags
))
65 return SBOX_TEST_FAILED
;
66 if ((flags
& HANDLE_FLAG_PROTECT_FROM_CLOSE
) != 0)
67 return SBOX_TEST_FAILED
;
69 return SBOX_TEST_SUCCEEDED
;
72 TEST(HandleInheritanceTests
, InheritByValue
) {
73 // Handle inheritance doesn't work on XP.
74 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
77 base::SharedMemory test_shared_memory
;
78 ASSERT_TRUE(test_shared_memory
.CreateAnonymous(1000));
79 ASSERT_TRUE(test_shared_memory
.Map(0));
83 runner
.GetPolicy()->AddHandleToShare(test_shared_memory
.handle());
85 std::string command_line
=
86 "HandleInheritanceTests_ValidInheritedHandle " +
87 base::UintToString(reinterpret_cast<unsigned int>(shared_handle
));
88 int result
= runner
.RunTest(base::UTF8ToUTF16(command_line
).c_str());
89 ASSERT_EQ(SBOX_TEST_SUCCEEDED
, result
);
92 } // namespace sandbox