OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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/environment.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/win/windows_version.h" |
| 11 #include "sandbox/win/tests/common/controller.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace sandbox { |
| 15 |
| 16 class AddressSanitizerTests : public ::testing::Test { |
| 17 public: |
| 18 void SetUp() { |
| 19 env_.reset(base::Environment::Create()); |
| 20 had_asan_options_ = env_->GetVar("ASAN_OPTIONS", &old_asan_options_); |
| 21 } |
| 22 |
| 23 void TearDown() { |
| 24 if (had_asan_options_) |
| 25 ASSERT_TRUE(env_->SetVar("ASAN_OPTIONS", old_asan_options_)); |
| 26 else |
| 27 env_->UnSetVar("ASAN_OPTIONS"); |
| 28 } |
| 29 |
| 30 protected: |
| 31 scoped_ptr<base::Environment> env_; |
| 32 bool had_asan_options_; |
| 33 std::string old_asan_options_; |
| 34 }; |
| 35 |
| 36 SBOX_TESTS_COMMAND int AddressSanitizerTests_Report(int argc, wchar_t** argv) { |
| 37 volatile int idx = 42; |
| 38 int *blah = new int[42]; |
| 39 blah[idx] = 42; |
| 40 return SBOX_TEST_FAILED; |
| 41 } |
| 42 |
| 43 #if defined(ADDRESS_SANITIZER) |
| 44 #define MAYBE_TestAddressSanitizer TestAddressSanitizer |
| 45 #else |
| 46 #define MAYBE_TestAddressSanitizer DISABLED_TestAddressSanitizer |
| 47 #endif |
| 48 TEST_F(AddressSanitizerTests, MAYBE_TestAddressSanitizer) { |
| 49 wchar_t temp_directory[MAX_PATH]; |
| 50 wchar_t temp_file_name[MAX_PATH]; |
| 51 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
| 52 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); |
| 53 |
| 54 SECURITY_ATTRIBUTES attrs = {}; |
| 55 attrs.nLength = sizeof(attrs); |
| 56 attrs.bInheritHandle = TRUE; |
| 57 HANDLE file_handle = CreateFile( |
| 58 temp_file_name, GENERIC_WRITE, |
| 59 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, |
| 60 &attrs, OPEN_EXISTING, 0, NULL); |
| 61 ASSERT_NE(file_handle, INVALID_HANDLE_VALUE); |
| 62 |
| 63 TestRunner runner; |
| 64 ASSERT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStderrHandle(file_handle)); |
| 65 |
| 66 wchar_t main_module_path[MAX_PATH]; |
| 67 DWORD ret = ::GetModuleFileName(NULL, main_module_path, MAX_PATH); |
| 68 ASSERT_TRUE(0 != ret && ret < MAX_PATH); |
| 69 base::FilePath pdb_path = |
| 70 base::FilePath(main_module_path).DirName().Append(L"*.pdb"); |
| 71 ASSERT_TRUE(runner.AddFsRule(sandbox::TargetPolicy::FILES_ALLOW_READONLY, |
| 72 pdb_path.value().c_str())); |
| 73 |
| 74 env_->SetVar("ASAN_OPTIONS", "exitcode=123"); |
| 75 int result = runner.RunTest(L"AddressSanitizerTests_Report"); |
| 76 EXPECT_EQ(123, result); |
| 77 EXPECT_TRUE(::CloseHandle(file_handle)); |
| 78 |
| 79 std::string data; |
| 80 ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); |
| 81 // Redirection uses a feature that was added in Windows Vista. |
| 82 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 83 ASSERT_TRUE(strstr(data.c_str(), "ERROR: AddressSanitizer")) |
| 84 << "There doesn't seem to be an ASan report:\n" << data; |
| 85 ASSERT_TRUE(strstr(data.c_str(), "AddressSanitizerTests_Report")) |
| 86 << "The ASan report doesn't appear to be symbolized:\n" << data; |
| 87 ASSERT_TRUE(strstr(data.c_str(), strrchr(__FILE__, '\\'))) |
| 88 << "The stack trace doesn't have a correct filename:\n" << data; |
| 89 } else { |
| 90 EXPECT_EQ("", data); |
| 91 } |
| 92 |
| 93 EXPECT_TRUE(::DeleteFile(temp_file_name)); |
| 94 } |
| 95 |
| 96 } |
OLD | NEW |