handle_inheritance_test.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include <stdio.h>
  5. #include "base/files/file_util.h"
  6. #include "base/files/scoped_temp_dir.h"
  7. #include "base/win/scoped_handle.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. namespace sandbox {
  12. SBOX_TESTS_COMMAND int HandleInheritanceTests_PrintToStdout(int argc,
  13. wchar_t** argv) {
  14. printf("Example output to stdout\n");
  15. return SBOX_TEST_SUCCEEDED;
  16. }
  17. TEST(HandleInheritanceTests, TestStdoutInheritance) {
  18. base::ScopedTempDir temp_directory;
  19. base::FilePath temp_file_name;
  20. ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  21. ASSERT_TRUE(
  22. CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file_name));
  23. SECURITY_ATTRIBUTES attrs = {};
  24. attrs.nLength = sizeof(attrs);
  25. attrs.bInheritHandle = true;
  26. base::win::ScopedHandle tmp_handle(
  27. CreateFile(temp_file_name.value().c_str(), GENERIC_WRITE,
  28. FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, &attrs,
  29. OPEN_EXISTING, 0, nullptr));
  30. ASSERT_TRUE(tmp_handle.IsValid());
  31. TestRunner runner;
  32. ASSERT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStdoutHandle(tmp_handle.Get()));
  33. int result = runner.RunTest(L"HandleInheritanceTests_PrintToStdout");
  34. ASSERT_EQ(SBOX_TEST_SUCCEEDED, result);
  35. std::string data;
  36. ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data));
  37. // Redirection uses a feature that was added in Windows Vista.
  38. ASSERT_EQ("Example output to stdout\r\n", data);
  39. }
  40. } // namespace sandbox