scoped_clear_last_error_unittest.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // 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. #include "base/scoped_clear_last_error.h"
  5. #include "base/logging.h"
  6. #include "build/build_config.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #if BUILDFLAG(IS_WIN)
  9. #include <windows.h>
  10. #include "base/logging.h"
  11. #endif // BUILDFLAG(IS_WIN)
  12. namespace base {
  13. TEST(ScopedClearLastError, TestNoError) {
  14. errno = 1;
  15. {
  16. ScopedClearLastError clear_error;
  17. EXPECT_EQ(0, errno);
  18. }
  19. EXPECT_EQ(1, errno);
  20. }
  21. TEST(ScopedClearLastError, TestError) {
  22. errno = 1;
  23. {
  24. ScopedClearLastError clear_error;
  25. errno = 2;
  26. }
  27. EXPECT_EQ(1, errno);
  28. }
  29. #if BUILDFLAG(IS_WIN)
  30. TEST(ScopedClearLastError, TestNoErrorWin) {
  31. ::SetLastError(1);
  32. {
  33. ScopedClearLastError clear_error;
  34. EXPECT_EQ(logging::SystemErrorCode(0), ::GetLastError());
  35. }
  36. EXPECT_EQ(logging::SystemErrorCode(1), ::GetLastError());
  37. }
  38. TEST(ScopedClearLastError, TestErrorWin) {
  39. ::SetLastError(1);
  40. {
  41. ScopedClearLastError clear_error;
  42. ::SetLastError(2);
  43. }
  44. EXPECT_EQ(logging::SystemErrorCode(1), ::GetLastError());
  45. }
  46. #endif // BUILDFLAG(IS_WIN)
  47. } // namespace base