Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: sandbox/win/src/lpc_policy_test.cc

Issue 1419483002: Windows sbox: Warmup locales before sandbox lockdown (and tests) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use decltype in function ptr typedef of GetUserDefaultLocaleName Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sandbox/win/sandbox_win.gypi ('k') | sandbox/win/src/sandbox_policy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 // These tests have been added to specifically tests issues arising from (A)LPC
6 // lock down.
7
8 #include <algorithm>
9 #include <cctype>
10
11 #include <windows.h>
12 #include <winioctl.h>
13
14 #include "base/win/windows_version.h"
15 #include "sandbox/win/src/sandbox.h"
16 #include "sandbox/win/src/sandbox_factory.h"
17 #include "sandbox/win/src/sandbox_policy.h"
18 #include "sandbox/win/tests/common/controller.h"
19 #include "sandbox/win/tests/common/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace sandbox {
23
24 // Converts LCID to std::wstring for passing to sbox tests.
25 std::wstring LcidToWString(LCID lcid) {
26 wchar_t buff[10] = {0};
27 int res = swprintf_s(buff, sizeof(buff) / sizeof(buff[0]), L"%08x", lcid);
28 if (-1 != res) {
29 return std::wstring(buff);
30 }
31 return std::wstring();
32 }
33
34 // Converts LANGID to std::wstring for passing to sbox tests.
35 std::wstring LangidToWString(LANGID langid) {
36 wchar_t buff[10] = {0};
37 int res = swprintf_s(buff, sizeof(buff) / sizeof(buff[0]), L"%04x", langid);
38 if (-1 != res) {
39 return std::wstring(buff);
40 }
41 return std::wstring();
42 }
43
44 SBOX_TESTS_COMMAND int Lpc_GetUserDefaultLangID(int argc, wchar_t** argv) {
45 if (argc != 1)
46 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
47 std::wstring expected_langid_string(argv[0]);
48
49 // This will cause an exception if not warmed up suitably.
50 LANGID langid = ::GetUserDefaultLangID();
51
52 std::wstring langid_string = LangidToWString(langid);
53 if (0 == wcsncmp(langid_string.c_str(), expected_langid_string.c_str(), 4)) {
54 return SBOX_TEST_SUCCEEDED;
55 }
56 return SBOX_TEST_FAILED;
57 }
58
59 TEST(LpcPolicyTest, GetUserDefaultLangID) {
60 LANGID langid = ::GetUserDefaultLangID();
61 std::wstring cmd = L"Lpc_GetUserDefaultLangID " + LangidToWString(langid);
62 TestRunner runner;
63 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str()));
64 }
65
66 SBOX_TESTS_COMMAND int Lpc_GetUserDefaultLCID(int argc, wchar_t** argv) {
67 if (argc != 1)
68 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
69 std::wstring expected_lcid_string(argv[0]);
70
71 // This will cause an exception if not warmed up suitably.
72 LCID lcid = ::GetUserDefaultLCID();
73
74 std::wstring lcid_string = LcidToWString(lcid);
75 if (0 == wcsncmp(lcid_string.c_str(), expected_lcid_string.c_str(), 8)) {
76 return SBOX_TEST_SUCCEEDED;
77 }
78 return SBOX_TEST_FAILED;
79 }
80
81 TEST(LpcPolicyTest, GetUserDefaultLCID) {
82 LCID lcid = ::GetUserDefaultLCID();
83 std::wstring cmd = L"Lpc_GetUserDefaultLCID " + LcidToWString(lcid);
84 TestRunner runner;
85 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str()));
86 }
87
88 // GetUserDefaultLocaleName is not available on WIN XP. So we'll
89 // load it on-the-fly.
90 const wchar_t kKernel32DllName[] = L"kernel32.dll";
91 typedef int(WINAPI* GetUserDefaultLocaleNameFunction)(LPWSTR lpLocaleName,
92 int cchLocaleName);
93
94 SBOX_TESTS_COMMAND int Lpc_GetUserDefaultLocaleName(int argc, wchar_t** argv) {
95 if (argc != 1)
96 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
97 std::wstring expected_locale_name(argv[0]);
98 static GetUserDefaultLocaleNameFunction GetUserDefaultLocaleName_func = NULL;
99 if (!GetUserDefaultLocaleName_func) {
100 // GetUserDefaultLocaleName is not available on WIN XP. So we'll
101 // load it on-the-fly.
102 HMODULE kernel32_dll = ::GetModuleHandle(kKernel32DllName);
103 if (!kernel32_dll) {
104 return SBOX_TEST_FAILED;
105 }
106 GetUserDefaultLocaleName_func =
107 reinterpret_cast<GetUserDefaultLocaleNameFunction>(
108 GetProcAddress(kernel32_dll, "GetUserDefaultLocaleName"));
109 if (!GetUserDefaultLocaleName_func) {
110 return SBOX_TEST_FAILED;
111 }
112 }
113 wchar_t locale_name[LOCALE_NAME_MAX_LENGTH] = {0};
114 // This will cause an exception if not warmed up suitably.
115 int ret = GetUserDefaultLocaleName_func(
116 locale_name, LOCALE_NAME_MAX_LENGTH * sizeof(wchar_t));
117 if (!ret) {
118 return SBOX_TEST_FAILED;
119 }
120 if (!wcsnlen(locale_name, LOCALE_NAME_MAX_LENGTH)) {
121 return SBOX_TEST_FAILED;
122 }
123 if (0 == wcsncmp(locale_name, expected_locale_name.c_str(),
124 LOCALE_NAME_MAX_LENGTH)) {
125 return SBOX_TEST_SUCCEEDED;
126 }
127 return SBOX_TEST_FAILED;
128 }
129
130 TEST(LpcPolicyTest, GetUserDefaultLocaleName) {
131 // GetUserDefaultLocaleName is not available before Vista.
132 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
133 return;
134 }
135 static GetUserDefaultLocaleNameFunction GetUserDefaultLocaleName_func = NULL;
136 if (!GetUserDefaultLocaleName_func) {
137 // GetUserDefaultLocaleName is not available on WIN XP. So we'll
138 // load it on-the-fly.
139 HMODULE kernel32_dll = ::GetModuleHandle(kKernel32DllName);
140 EXPECT_NE(NULL, int(kernel32_dll));
141 GetUserDefaultLocaleName_func =
142 reinterpret_cast<GetUserDefaultLocaleNameFunction>(
143 GetProcAddress(kernel32_dll, "GetUserDefaultLocaleName"));
144 EXPECT_NE(NULL, int(GetUserDefaultLocaleName_func));
145 }
146 wchar_t locale_name[LOCALE_NAME_MAX_LENGTH] = {0};
147 EXPECT_NE(0, GetUserDefaultLocaleName_func(
148 locale_name, LOCALE_NAME_MAX_LENGTH * sizeof(wchar_t)));
149 EXPECT_NE(0U, wcsnlen(locale_name, LOCALE_NAME_MAX_LENGTH));
150 std::wstring cmd =
151 L"Lpc_GetUserDefaultLocaleName " + std::wstring(locale_name);
152 TestRunner runner;
153 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str()));
154 }
155
156 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/win/sandbox_win.gypi ('k') | sandbox/win/src/sandbox_policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698