michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include michael@0: michael@0: #include "base/file_util.h" michael@0: #include "base/win/windows_version.h" michael@0: #include "sandbox/win/tests/common/controller.h" michael@0: #include "testing/gtest/include/gtest/gtest.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: SBOX_TESTS_COMMAND int HandleInheritanceTests_PrintToStdout(int argc, michael@0: wchar_t** argv) { michael@0: printf("Example output to stdout\n"); michael@0: return SBOX_TEST_SUCCEEDED; michael@0: } michael@0: michael@0: TEST(HandleInheritanceTests, TestStdoutInheritance) { michael@0: wchar_t temp_directory[MAX_PATH]; michael@0: wchar_t temp_file_name[MAX_PATH]; michael@0: ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); michael@0: ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); michael@0: michael@0: SECURITY_ATTRIBUTES attrs = {}; michael@0: attrs.nLength = sizeof(attrs); michael@0: attrs.lpSecurityDescriptor = NULL; michael@0: attrs.bInheritHandle = TRUE; michael@0: HANDLE file_handle = CreateFile( michael@0: temp_file_name, GENERIC_WRITE, michael@0: FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, michael@0: &attrs, OPEN_EXISTING, 0, NULL); michael@0: EXPECT_NE(file_handle, INVALID_HANDLE_VALUE); michael@0: michael@0: TestRunner runner; michael@0: EXPECT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStdoutHandle(file_handle)); michael@0: int result = runner.RunTest(L"HandleInheritanceTests_PrintToStdout"); michael@0: EXPECT_EQ(SBOX_TEST_SUCCEEDED, result); michael@0: EXPECT_TRUE(::CloseHandle(file_handle)); michael@0: michael@0: std::string data; michael@0: EXPECT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); michael@0: // Redirection uses a feature that was added in Windows Vista. michael@0: if (base::win::GetVersion() >= base::win::VERSION_VISTA) { michael@0: EXPECT_EQ("Example output to stdout\r\n", data); michael@0: } else { michael@0: EXPECT_EQ("", data); michael@0: } michael@0: michael@0: EXPECT_TRUE(::DeleteFile(temp_file_name)); michael@0: } michael@0: michael@0: }