blob: 60c92152c3b4adb963ba8a9654ab869c71d710ca [file] [log] [blame]
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:211// 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 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
11#if defined(OS_WIN)
12#include <windows.h>
Hans Wennborgc3cffa62020-04-27 10:09:1213#include "base/logging.h"
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2114#endif // defined(OS_WIN)
15
16namespace base {
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2117
18TEST(ScopedClearLastError, TestNoError) {
19 errno = 1;
20 {
21 ScopedClearLastError clear_error;
22 EXPECT_EQ(0, errno);
23 }
24 EXPECT_EQ(1, errno);
25}
26
27TEST(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
38TEST(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
47TEST(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-Dorayd120ebf2018-09-14 23:38:2158} // namespace base