• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 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 <windows.h>
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "base/registry.h"
9 #include "base/string_util.h"
10 #include "base/win_util.h"
11 
12 // The test is somewhat silly, because the Vista bots some have UAC enabled
13 // and some have it disabled. At least we check that it does not crash.
TEST(BaseWinUtilTest,TestIsUACEnabled)14 TEST(BaseWinUtilTest, TestIsUACEnabled) {
15   if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) {
16     win_util::UserAccountControlIsEnabled();
17   } else {
18     EXPECT_TRUE(win_util::UserAccountControlIsEnabled());
19   }
20 }
21 
TEST(BaseWinUtilTest,TestGetUserSidString)22 TEST(BaseWinUtilTest, TestGetUserSidString) {
23   std::wstring user_sid;
24   EXPECT_TRUE(win_util::GetUserSidString(&user_sid));
25   EXPECT_TRUE(!user_sid.empty());
26 }
27 
TEST(BaseWinUtilTest,TestGetNonClientMetrics)28 TEST(BaseWinUtilTest, TestGetNonClientMetrics) {
29   NONCLIENTMETRICS metrics = {0};
30   win_util::GetNonClientMetrics(&metrics);
31   EXPECT_TRUE(metrics.cbSize > 0);
32   EXPECT_TRUE(metrics.iScrollWidth > 0);
33   EXPECT_TRUE(metrics.iScrollHeight > 0);
34 }
35 
36 namespace {
37 
38 // Saves the current thread's locale ID when initialized, and restores it when
39 // the instance is going out of scope.
40 class ThreadLocaleSaver {
41  public:
ThreadLocaleSaver()42   ThreadLocaleSaver() : original_locale_id_(GetThreadLocale()) {}
~ThreadLocaleSaver()43   ~ThreadLocaleSaver() { SetThreadLocale(original_locale_id_); }
44 
45  private:
46   LCID original_locale_id_;
47 
48   DISALLOW_COPY_AND_ASSIGN(ThreadLocaleSaver);
49 };
50 
51 }  // namespace
52 
TEST(BaseWinUtilTest,FormatMessage)53 TEST(BaseWinUtilTest, FormatMessage) {
54   // Because we cannot write tests of every language, we only test the message
55   // of en-US locale. Here, we change the current locale temporarily.
56   ThreadLocaleSaver thread_locale_saver;
57   WORD language_id = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
58   LCID locale_id = MAKELCID(language_id, SORT_DEFAULT);
59   ASSERT_TRUE(SetThreadLocale(locale_id));
60 
61   const int kAccessDeniedErrorCode = 5;
62   SetLastError(kAccessDeniedErrorCode);
63   ASSERT_EQ(GetLastError(), kAccessDeniedErrorCode);
64   std::wstring value;
65   TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value);
66   EXPECT_EQ(std::wstring(L"Access is denied."), value);
67 
68   // Manually call the OS function
69   wchar_t * string_buffer = NULL;
70   unsigned string_length =
71       ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
72                       FORMAT_MESSAGE_FROM_SYSTEM |
73                       FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
74                       kAccessDeniedErrorCode, 0,
75                       reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL);
76 
77   // Verify the call succeeded
78   ASSERT_TRUE(string_length);
79   ASSERT_TRUE(string_buffer);
80 
81   // Verify the string is the same by different calls
82   EXPECT_EQ(win_util::FormatLastWin32Error(), std::wstring(string_buffer));
83   EXPECT_EQ(win_util::FormatMessage(kAccessDeniedErrorCode),
84             std::wstring(string_buffer));
85 
86   // Done with the buffer allocated by ::FormatMessage()
87   LocalFree(string_buffer);
88 }
89