Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 1 | // Copyright (c) 2018 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 "base/scoped_clear_last_error.h" |
| 6 | |
Hans Wennborg | afeb390 | 2020-06-17 14:42:29 | [diff] [blame^] | 7 | #include "base/logging.h" |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | #if defined(OS_WIN) |
| 12 | #include <windows.h> |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 13 | #include "base/logging.h" |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 14 | #endif // defined(OS_WIN) |
| 15 | |
| 16 | namespace base { |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 17 | |
| 18 | TEST(ScopedClearLastError, TestNoError) { |
| 19 | errno = 1; |
| 20 | { |
| 21 | ScopedClearLastError clear_error; |
| 22 | EXPECT_EQ(0, errno); |
| 23 | } |
| 24 | EXPECT_EQ(1, errno); |
| 25 | } |
| 26 | |
| 27 | TEST(ScopedClearLastError, TestError) { |
| 28 | errno = 1; |
| 29 | { |
| 30 | ScopedClearLastError clear_error; |
| 31 | errno = 2; |
| 32 | } |
| 33 | EXPECT_EQ(1, errno); |
| 34 | } |
| 35 | |
| 36 | #if defined(OS_WIN) |
| 37 | |
| 38 | TEST(ScopedClearLastError, TestNoErrorWin) { |
| 39 | ::SetLastError(1); |
| 40 | { |
| 41 | ScopedClearLastError clear_error; |
| 42 | EXPECT_EQ(logging::SystemErrorCode(0), ::GetLastError()); |
| 43 | } |
| 44 | EXPECT_EQ(logging::SystemErrorCode(1), ::GetLastError()); |
| 45 | } |
| 46 | |
| 47 | TEST(ScopedClearLastError, TestErrorWin) { |
| 48 | ::SetLastError(1); |
| 49 | { |
| 50 | ScopedClearLastError clear_error; |
| 51 | ::SetLastError(2); |
| 52 | } |
| 53 | EXPECT_EQ(logging::SystemErrorCode(1), ::GetLastError()); |
| 54 | } |
| 55 | |
| 56 | #endif // defined(OS_WIN) |
| 57 | |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 58 | } // namespace base |