Windows should animate when they are about to get docked at screen edges.
[chromium-blink-merge.git] / sandbox / win / tests / validation_tests / suite.cc
blob95209b7afe00deecffaf17c89266208268f34315
1 // Copyright (c) 2012 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.
5 // This file contains the validation tests for the sandbox.
6 // It includes the tests that need to be performed inside the
7 // sandbox.
9 #include <shlwapi.h>
11 #include "base/win/windows_version.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "sandbox/win/tests/common/controller.h"
15 #pragma comment(lib, "shlwapi.lib")
17 namespace {
19 void TestProcessAccess(sandbox::TestRunner* runner, DWORD target) {
20 const wchar_t *kCommandTemplate = L"OpenProcessCmd %d %d";
21 wchar_t command[1024] = {0};
23 // Test all the scary process permissions.
24 wsprintf(command, kCommandTemplate, target, PROCESS_CREATE_THREAD);
25 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
26 wsprintf(command, kCommandTemplate, target, PROCESS_DUP_HANDLE);
27 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
28 wsprintf(command, kCommandTemplate, target, PROCESS_SET_INFORMATION);
29 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
30 wsprintf(command, kCommandTemplate, target, PROCESS_VM_OPERATION);
31 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
32 wsprintf(command, kCommandTemplate, target, PROCESS_VM_READ);
33 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
34 wsprintf(command, kCommandTemplate, target, PROCESS_VM_WRITE);
35 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
36 wsprintf(command, kCommandTemplate, target, PROCESS_QUERY_INFORMATION);
37 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
38 wsprintf(command, kCommandTemplate, target, WRITE_DAC);
39 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
40 wsprintf(command, kCommandTemplate, target, WRITE_OWNER);
41 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
42 wsprintf(command, kCommandTemplate, target, READ_CONTROL);
43 EXPECT_EQ(sandbox::SBOX_TEST_DENIED, runner->RunTest(command));
46 } // namespace
48 namespace sandbox {
50 // Returns true if the volume that contains any_path supports ACL security. The
51 // input path can contain unexpanded environment strings. Returns false on any
52 // failure or if the file system does not support file security (such as FAT).
53 bool VolumeSupportsACLs(const wchar_t* any_path) {
54 wchar_t expand[MAX_PATH +1];
55 DWORD len =::ExpandEnvironmentStringsW(any_path, expand, _countof(expand));
56 if (0 == len) return false;
57 if (len > _countof(expand)) return false;
58 if (!::PathStripToRootW(expand)) return false;
59 DWORD fs_flags = 0;
60 if (!::GetVolumeInformationW(expand, NULL, 0, 0, NULL, &fs_flags, NULL, 0))
61 return false;
62 if (fs_flags & FILE_PERSISTENT_ACLS) return true;
63 return false;
66 // Tests if the suite is working properly.
67 TEST(ValidationSuite, TestSuite) {
68 TestRunner runner;
69 ASSERT_EQ(SBOX_TEST_PING_OK, runner.RunTest(L"ping"));
72 // Tests if the file system is correctly protected by the sandbox.
73 TEST(ValidationSuite, TestFileSystem) {
74 // Do not perform the test if the system is using FAT or any other
75 // file system that does not have file security.
76 ASSERT_TRUE(VolumeSupportsACLs(L"%SystemDrive%\\"));
77 ASSERT_TRUE(VolumeSupportsACLs(L"%SystemRoot%\\"));
78 ASSERT_TRUE(VolumeSupportsACLs(L"%ProgramFiles%\\"));
79 ASSERT_TRUE(VolumeSupportsACLs(L"%Temp%\\"));
80 ASSERT_TRUE(VolumeSupportsACLs(L"%AppData%\\"));
82 TestRunner runner;
83 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenFile %SystemDrive%"));
84 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenFile %SystemRoot%"));
85 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenFile %ProgramFiles%"));
86 EXPECT_EQ(SBOX_TEST_DENIED,
87 runner.RunTest(L"OpenFile %SystemRoot%\\System32"));
88 EXPECT_EQ(SBOX_TEST_DENIED,
89 runner.RunTest(L"OpenFile %SystemRoot%\\explorer.exe"));
90 EXPECT_EQ(SBOX_TEST_DENIED,
91 runner.RunTest(L"OpenFile %SystemRoot%\\Cursors\\arrow_i.cur"));
92 EXPECT_EQ(SBOX_TEST_DENIED,
93 runner.RunTest(L"OpenFile %AllUsersProfile%"));
94 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenFile %Temp%"));
95 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenFile %AppData%"));
98 // Tests if the registry is correctly protected by the sandbox.
99 TEST(ValidationSuite, TestRegistry) {
100 TestRunner runner;
101 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenKey HKLM"));
102 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenKey HKCU"));
103 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenKey HKU"));
104 EXPECT_EQ(SBOX_TEST_DENIED,
105 runner.RunTest(
106 L"OpenKey HKLM "
107 L"\"Software\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon\""));
110 // Tests that the permissions on the Windowstation does not allow the sandbox
111 // to get to the interactive desktop or to make the sbox desktop interactive.
112 TEST(ValidationSuite, TestDesktop) {
113 TestRunner runner;
114 runner.GetPolicy()->SetAlternateDesktop(false);
115 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"OpenInteractiveDesktop NULL"));
116 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"SwitchToSboxDesktop NULL"));
119 // Tests if the windows are correctly protected by the sandbox.
120 TEST(ValidationSuite, TestWindows) {
121 TestRunner runner;
122 wchar_t command[1024] = {0};
124 wsprintf(command, L"ValidWindow %d", ::GetDesktopWindow());
125 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(command));
127 wsprintf(command, L"ValidWindow %d", ::FindWindow(NULL, NULL));
128 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(command));
131 // Tests that a locked-down process cannot open another locked-down process.
132 TEST(ValidationSuite, TestProcessDenyLockdown) {
133 TestRunner runner;
134 TestRunner target;
135 wchar_t command[1024] = {0};
137 target.SetAsynchronous(true);
139 EXPECT_EQ(SBOX_TEST_SUCCEEDED, target.RunTest(L"SleepCmd 30000"));
141 TestProcessAccess(&runner, target.process_id());
144 // Tests that a low-integrity process cannot open a locked-down process (due
145 // to the integrity label changing after startup via SetDelayedIntegrityLevel).
146 TEST(ValidationSuite, TestProcessDenyLowIntegrity) {
147 // This test applies only to Vista and above.
148 if (base::win::Version() < base::win::VERSION_VISTA)
149 return;
151 TestRunner runner;
152 TestRunner target;
153 wchar_t command[1024] = {0};
155 target.SetAsynchronous(true);
156 target.GetPolicy()->SetDelayedIntegrityLevel(INTEGRITY_LEVEL_LOW);
158 runner.GetPolicy()->SetIntegrityLevel(INTEGRITY_LEVEL_LOW);
159 runner.GetPolicy()->SetTokenLevel(USER_RESTRICTED_SAME_ACCESS,
160 USER_INTERACTIVE);
162 EXPECT_EQ(SBOX_TEST_SUCCEEDED, target.RunTest(L"SleepCmd 30000"));
164 TestProcessAccess(&runner, target.process_id());
167 // Tests that a locked-down process cannot open a low-integrity process.
168 TEST(ValidationSuite, TestProcessDenyBelowLowIntegrity) {
169 // This test applies only to Vista and above.
170 if (base::win::Version() < base::win::VERSION_VISTA)
171 return;
173 TestRunner runner;
174 TestRunner target;
175 wchar_t command[1024] = {0};
177 target.SetAsynchronous(true);
178 target.GetPolicy()->SetIntegrityLevel(INTEGRITY_LEVEL_LOW);
179 target.GetPolicy()->SetTokenLevel(USER_RESTRICTED_SAME_ACCESS,
180 USER_INTERACTIVE);
182 runner.GetPolicy()->SetDelayedIntegrityLevel(INTEGRITY_LEVEL_UNTRUSTED);
183 runner.GetPolicy()->SetTokenLevel(USER_RESTRICTED_SAME_ACCESS,
184 USER_INTERACTIVE);
186 EXPECT_EQ(SBOX_TEST_SUCCEEDED, target.RunTest(L"SleepCmd 30000"));
188 TestProcessAccess(&runner, target.process_id());
191 // Tests if the threads are correctly protected by the sandbox.
192 TEST(ValidationSuite, TestThread) {
193 TestRunner runner;
194 wchar_t command[1024] = {0};
196 wsprintf(command, L"OpenThreadCmd %d", ::GetCurrentThreadId());
197 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(command));
200 } // namespace sandbox