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 <stdio.h> 6 7 #include "base/file_util.h" 8 #include "base/win/windows_version.h" 9 #include "sandbox/win/tests/common/controller.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace sandbox { 13 14 SBOX_TESTS_COMMAND int HandleInheritanceTests_PrintToStdout(int argc, 15 wchar_t** argv) { 16 printf("Example output to stdout\n"); 17 return SBOX_TEST_SUCCEEDED; 18 } 19 20 TEST(HandleInheritanceTests, TestStdoutInheritance) { 21 wchar_t temp_directory[MAX_PATH]; 22 wchar_t temp_file_name[MAX_PATH]; 23 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); 24 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); 25 26 SECURITY_ATTRIBUTES attrs = {}; 27 attrs.nLength = sizeof(attrs); 28 attrs.lpSecurityDescriptor = NULL; 29 attrs.bInheritHandle = TRUE; 30 HANDLE file_handle = CreateFile( 31 temp_file_name, GENERIC_WRITE, 32 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, 33 &attrs, OPEN_EXISTING, 0, NULL); 34 EXPECT_NE(file_handle, INVALID_HANDLE_VALUE); 35 36 TestRunner runner; 37 EXPECT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStdoutHandle(file_handle)); 38 int result = runner.RunTest(L"HandleInheritanceTests_PrintToStdout"); 39 EXPECT_EQ(SBOX_TEST_SUCCEEDED, result); 40 EXPECT_TRUE(::CloseHandle(file_handle)); 41 42 std::string data; 43 EXPECT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); 44 // Redirection uses a feature that was added in Windows Vista. 45 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { 46 EXPECT_EQ("Example output to stdout\r\n", data); 47 } else { 48 EXPECT_EQ("", data); 49 } 50 51 EXPECT_TRUE(::DeleteFile(temp_file_name)); 52 } 53 54 } 55