OLD | NEW |
| (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 static GetUserDefaultLocaleNameFunction GetUserDefaultLocaleName_func = NULL; | |
132 if (!GetUserDefaultLocaleName_func) { | |
133 // GetUserDefaultLocaleName is not available on WIN XP. So we'll | |
134 // load it on-the-fly. | |
135 HMODULE kernel32_dll = ::GetModuleHandle(kKernel32DllName); | |
136 EXPECT_NE(nullptr, kernel32_dll); | |
137 GetUserDefaultLocaleName_func = | |
138 reinterpret_cast<GetUserDefaultLocaleNameFunction>( | |
139 GetProcAddress(kernel32_dll, "GetUserDefaultLocaleName")); | |
140 EXPECT_NE(nullptr, GetUserDefaultLocaleName_func); | |
141 } | |
142 wchar_t locale_name[LOCALE_NAME_MAX_LENGTH] = {0}; | |
143 EXPECT_NE(0, GetUserDefaultLocaleName_func( | |
144 locale_name, LOCALE_NAME_MAX_LENGTH * sizeof(wchar_t))); | |
145 EXPECT_NE(0U, wcsnlen(locale_name, LOCALE_NAME_MAX_LENGTH)); | |
146 std::wstring cmd = | |
147 L"Lpc_GetUserDefaultLocaleName " + std::wstring(locale_name); | |
148 TestRunner runner; | |
149 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str())); | |
150 } | |
151 | |
152 } // namespace sandbox | |
OLD | NEW |