Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / sandbox / win / src / app_container_test.cc
blobced5cbde7c1e92cae047b34cb52078ef88dadcca
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 #include <windows.h>
7 #define _ATL_NO_EXCEPTIONS
8 #include <atlbase.h>
9 #include <atlsecurity.h>
11 #include "base/strings/string16.h"
12 #include "base/win/scoped_handle.h"
13 #include "base/win/windows_version.h"
14 #include "sandbox/win/src/sync_policy_test.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace {
19 const wchar_t kAppContainerName[] = L"sbox_test";
20 const wchar_t kAppContainerSid[] =
21 L"S-1-15-2-3251537155-1984446955-2931258699-841473695-1938553385-"
22 L"924012148-2839372144";
24 const ULONG kSharing = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
26 HANDLE CreateTaggedEvent(const base::string16& name,
27 const base::string16& sid) {
28 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, name.c_str()));
29 if (!event.IsValid())
30 return NULL;
32 wchar_t file_name[MAX_PATH] = {};
33 wchar_t temp_directory[MAX_PATH] = {};
34 GetTempPath(MAX_PATH, temp_directory);
35 GetTempFileName(temp_directory, L"test", 0, file_name);
37 base::win::ScopedHandle file;
38 file.Set(CreateFile(file_name, GENERIC_READ | STANDARD_RIGHTS_READ, kSharing,
39 NULL, OPEN_EXISTING, 0, NULL));
40 DeleteFile(file_name);
41 if (!file.IsValid())
42 return NULL;
44 CSecurityDesc sd;
45 if (!AtlGetSecurityDescriptor(file.Get(), SE_FILE_OBJECT, &sd,
46 OWNER_SECURITY_INFORMATION |
47 GROUP_SECURITY_INFORMATION |
48 DACL_SECURITY_INFORMATION)) {
49 return NULL;
52 PSID local_sid;
53 if (!ConvertStringSidToSid(sid.c_str(), &local_sid))
54 return NULL;
56 CDacl new_dacl;
57 sd.GetDacl(&new_dacl);
58 CSid csid(reinterpret_cast<SID*>(local_sid));
59 new_dacl.AddAllowedAce(csid, EVENT_ALL_ACCESS);
60 if (!AtlSetDacl(event.Get(), SE_KERNEL_OBJECT, new_dacl))
61 event.Close();
63 LocalFree(local_sid);
64 return event.IsValid() ? event.Take() : NULL;
67 } // namespace
69 namespace sandbox {
71 TEST(AppContainerTest, AllowOpenEvent) {
72 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8)
73 return;
75 TestRunner runner(JOB_UNPROTECTED, USER_UNPROTECTED, USER_UNPROTECTED);
77 const wchar_t capability[] = L"S-1-15-3-12345678-87654321";
78 base::win::ScopedHandle handle(CreateTaggedEvent(L"test", capability));
79 ASSERT_TRUE(handle.IsValid());
81 EXPECT_EQ(SBOX_ALL_OK,
82 runner.broker()->InstallAppContainer(kAppContainerSid,
83 kAppContainerName));
84 EXPECT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetCapability(capability));
85 EXPECT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetAppContainer(kAppContainerSid));
87 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_Open f test"));
89 runner.SetTestState(BEFORE_REVERT);
90 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Event_Open f test"));
91 EXPECT_EQ(SBOX_ALL_OK,
92 runner.broker()->UninstallAppContainer(kAppContainerSid));
95 TEST(AppContainerTest, DenyOpenEvent) {
96 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8)
97 return;
99 TestRunner runner(JOB_UNPROTECTED, USER_UNPROTECTED, USER_UNPROTECTED);
101 const wchar_t capability[] = L"S-1-15-3-12345678-87654321";
102 base::win::ScopedHandle handle(CreateTaggedEvent(L"test", capability));
103 ASSERT_TRUE(handle.IsValid());
105 EXPECT_EQ(SBOX_ALL_OK,
106 runner.broker()->InstallAppContainer(kAppContainerSid,
107 kAppContainerName));
108 EXPECT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetAppContainer(kAppContainerSid));
110 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_Open f test"));
112 runner.SetTestState(BEFORE_REVERT);
113 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_Open f test"));
114 EXPECT_EQ(SBOX_ALL_OK,
115 runner.broker()->UninstallAppContainer(kAppContainerSid));
118 TEST(AppContainerTest, NoImpersonation) {
119 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8)
120 return;
122 TestRunner runner(JOB_UNPROTECTED, USER_LIMITED, USER_LIMITED);
123 EXPECT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetAppContainer(kAppContainerSid));
126 TEST(AppContainerTest, WantsImpersonation) {
127 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8)
128 return;
130 TestRunner runner(JOB_UNPROTECTED, USER_UNPROTECTED, USER_NON_ADMIN);
131 EXPECT_EQ(SBOX_ERROR_CANNOT_INIT_APPCONTAINER,
132 runner.GetPolicy()->SetAppContainer(kAppContainerSid));
135 TEST(AppContainerTest, RequiresImpersonation) {
136 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8)
137 return;
139 TestRunner runner(JOB_UNPROTECTED, USER_RESTRICTED, USER_RESTRICTED);
140 EXPECT_EQ(SBOX_ERROR_CANNOT_INIT_APPCONTAINER,
141 runner.GetPolicy()->SetAppContainer(kAppContainerSid));
144 TEST(AppContainerTest, DenyOpenEventForLowBox) {
145 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8)
146 return;
148 TestRunner runner(JOB_UNPROTECTED, USER_UNPROTECTED, USER_UNPROTECTED);
150 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, L"test"));
151 ASSERT_TRUE(event.IsValid());
153 EXPECT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetLowBox(kAppContainerSid));
155 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Event_Open f test"));
158 // TODO(shrikant): Please add some tests to prove usage of lowbox token like
159 // socket connection to local server in lock down mode.
161 } // namespace sandbox