OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/win/scoped_handle.h" | 9 #include "base/win/scoped_handle.h" |
10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
11 #include "sandbox/win/tests/common/controller.h" | 11 #include "sandbox/win/tests/common/controller.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace sandbox { | 14 namespace sandbox { |
15 | 15 |
16 SBOX_TESTS_COMMAND int HandleInheritanceTests_PrintToStdout(int argc, | 16 SBOX_TESTS_COMMAND int HandleInheritanceTests_PrintToStdout(int argc, |
17 wchar_t** argv) { | 17 wchar_t** argv) { |
18 printf("Example output to stdout\n"); | 18 printf("Example output to stdout\n"); |
19 return SBOX_TEST_SUCCEEDED; | 19 return SBOX_TEST_SUCCEEDED; |
20 } | 20 } |
21 | 21 |
22 TEST(HandleInheritanceTests, TestStdoutInheritance) { | 22 TEST(HandleInheritanceTests, TestStdoutInheritance) { |
23 base::ScopedTempDir temp_directory; | 23 base::ScopedTempDir temp_directory; |
24 base::FilePath temp_file_name; | 24 base::FilePath temp_file_name; |
25 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 25 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
26 ASSERT_TRUE(CreateTemporaryFileInDir(temp_directory.path(), &temp_file_name)); | 26 ASSERT_TRUE( |
| 27 CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file_name)); |
27 | 28 |
28 SECURITY_ATTRIBUTES attrs = {}; | 29 SECURITY_ATTRIBUTES attrs = {}; |
29 attrs.nLength = sizeof(attrs); | 30 attrs.nLength = sizeof(attrs); |
30 attrs.bInheritHandle = TRUE; | 31 attrs.bInheritHandle = TRUE; |
31 base::win::ScopedHandle tmp_handle( | 32 base::win::ScopedHandle tmp_handle( |
32 CreateFile(temp_file_name.value().c_str(), GENERIC_WRITE, | 33 CreateFile(temp_file_name.value().c_str(), GENERIC_WRITE, |
33 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, | 34 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, |
34 &attrs, OPEN_EXISTING, 0, NULL)); | 35 &attrs, OPEN_EXISTING, 0, NULL)); |
35 ASSERT_TRUE(tmp_handle.IsValid()); | 36 ASSERT_TRUE(tmp_handle.IsValid()); |
36 | 37 |
37 TestRunner runner; | 38 TestRunner runner; |
38 ASSERT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStdoutHandle(tmp_handle.Get())); | 39 ASSERT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStdoutHandle(tmp_handle.Get())); |
39 int result = runner.RunTest(L"HandleInheritanceTests_PrintToStdout"); | 40 int result = runner.RunTest(L"HandleInheritanceTests_PrintToStdout"); |
40 ASSERT_EQ(SBOX_TEST_SUCCEEDED, result); | 41 ASSERT_EQ(SBOX_TEST_SUCCEEDED, result); |
41 | 42 |
42 std::string data; | 43 std::string data; |
43 ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); | 44 ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); |
44 // Redirection uses a feature that was added in Windows Vista. | 45 // Redirection uses a feature that was added in Windows Vista. |
45 ASSERT_EQ("Example output to stdout\r\n", data); | 46 ASSERT_EQ("Example output to stdout\r\n", data); |
46 } | 47 } |
47 | 48 |
48 } // namespace sandbox | 49 } // namespace sandbox |
OLD | NEW |