xref: /aosp_15_r20/external/cronet/base/scoped_clear_last_error_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
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 
7 #include "base/logging.h"
8 #include "build/build_config.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 #if BUILDFLAG(IS_WIN)
12 #include <windows.h>
13 #endif  // BUILDFLAG(IS_WIN)
14 
15 namespace base {
16 
TEST(ScopedClearLastError,TestNoError)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 
TEST(ScopedClearLastError,TestError)26 TEST(ScopedClearLastError, TestError) {
27   errno = 1;
28   {
29     ScopedClearLastError clear_error;
30     errno = 2;
31   }
32   EXPECT_EQ(1, errno);
33 }
34 
35 #if BUILDFLAG(IS_WIN)
36 
TEST(ScopedClearLastError,TestNoErrorWin)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 
TEST(ScopedClearLastError,TestErrorWin)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 
55 #endif  // BUILDFLAG(IS_WIN)
56 
57 }  // namespace base
58