Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 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 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 11 | #if BUILDFLAG(IS_WIN) |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 12 | #include <windows.h> |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 13 | #endif // BUILDFLAG(IS_WIN) |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 14 | |
| 15 | namespace base { |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 16 | |
| 17 | TEST(ScopedClearLastError, TestNoError) { |
| 18 | errno = 1; |
| 19 | { |
| 20 | ScopedClearLastError clear_error; |
| 21 | EXPECT_EQ(0, errno); |
| 22 | } |
| 23 | EXPECT_EQ(1, errno); |
| 24 | } |
| 25 | |
| 26 | TEST(ScopedClearLastError, TestError) { |
| 27 | errno = 1; |
| 28 | { |
| 29 | ScopedClearLastError clear_error; |
| 30 | errno = 2; |
| 31 | } |
| 32 | EXPECT_EQ(1, errno); |
| 33 | } |
| 34 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 35 | #if BUILDFLAG(IS_WIN) |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 36 | |
| 37 | TEST(ScopedClearLastError, TestNoErrorWin) { |
| 38 | ::SetLastError(1); |
| 39 | { |
| 40 | ScopedClearLastError clear_error; |
| 41 | EXPECT_EQ(logging::SystemErrorCode(0), ::GetLastError()); |
| 42 | } |
| 43 | EXPECT_EQ(logging::SystemErrorCode(1), ::GetLastError()); |
| 44 | } |
| 45 | |
| 46 | TEST(ScopedClearLastError, TestErrorWin) { |
| 47 | ::SetLastError(1); |
| 48 | { |
| 49 | ScopedClearLastError clear_error; |
| 50 | ::SetLastError(2); |
| 51 | } |
| 52 | EXPECT_EQ(logging::SystemErrorCode(1), ::GetLastError()); |
| 53 | } |
| 54 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 55 | #endif // BUILDFLAG(IS_WIN) |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 56 | |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 57 | } // namespace base |