blob: 4604c25138049d9fe72c763d7bb583407143dfd8 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:212// 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 Wennborgafeb3902020-06-17 14:42:297#include "base/logging.h"
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:218#include "build/build_config.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
Xiaohan Wang38e4ebb2022-01-19 06:57:4311#if BUILDFLAG(IS_WIN)
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2112#include <windows.h>
Xiaohan Wang38e4ebb2022-01-19 06:57:4313#endif // BUILDFLAG(IS_WIN)
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2114
15namespace base {
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2116
17TEST(ScopedClearLastError, TestNoError) {
18 errno = 1;
19 {
20 ScopedClearLastError clear_error;
21 EXPECT_EQ(0, errno);
22 }
23 EXPECT_EQ(1, errno);
24}
25
26TEST(ScopedClearLastError, TestError) {
27 errno = 1;
28 {
29 ScopedClearLastError clear_error;
30 errno = 2;
31 }
32 EXPECT_EQ(1, errno);
33}
34
Xiaohan Wang38e4ebb2022-01-19 06:57:4335#if BUILDFLAG(IS_WIN)
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2136
37TEST(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
46TEST(ScopedClearLastError, TestErrorWin) {
47 ::SetLastError(1);
48 {
49 ScopedClearLastError clear_error;
50 ::SetLastError(2);
51 }
52 EXPECT_EQ(logging::SystemErrorCode(1), ::GetLastError());
53}
54
Xiaohan Wang38e4ebb2022-01-19 06:57:4355#endif // BUILDFLAG(IS_WIN)
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2156
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2157} // namespace base