blob: 3ae6b39410041c78f2df8f6408324f64769c4f8a [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
7#include "base/logging.h"
8#include "build/build_config.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11#if defined(OS_WIN)
12#include <windows.h>
13#endif // defined(OS_WIN)
14
15namespace base {
16namespace internal {
17
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
58} // namespace internal
59} // namespace base