1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file contains unit tests for the sid class.
#include "sandbox/win/src/sid.h"
#include <sddl.h>
#include "base/win/atl.h"
#include "base/win/windows_version.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace sandbox {
namespace {
bool EqualSid(const Sid& sid, const ATL::CSid& compare_sid) {
if (!sid.IsValid())
return false;
return !!::EqualSid(sid.GetPSID(), const_cast<SID*>(compare_sid.GetPSID()));
}
bool EqualSid(const Sid& sid, const wchar_t* sddl_sid) {
PSID compare_sid;
if (!sid.IsValid())
return false;
if (!::ConvertStringSidToSid(sddl_sid, &compare_sid))
return false;
bool equal = !!::EqualSid(sid.GetPSID(), compare_sid);
::LocalFree(compare_sid);
return equal;
}
struct KnownCapabilityTestEntry {
WellKnownCapabilities capability;
const wchar_t* sddl_sid;
};
struct NamedCapabilityTestEntry {
const wchar_t* capability_name;
const wchar_t* sddl_sid;
};
} // namespace
// Tests the creation of a Sid.
TEST(SidTest, Constructors) {
ATL::CSid sid_world = ATL::Sids::World();
PSID sid_world_pointer = const_cast<SID*>(sid_world.GetPSID());
// Check the SID* constructor.
Sid sid_sid_star(sid_world_pointer);
ASSERT_TRUE(EqualSid(sid_sid_star, sid_world));
// Check the copy constructor.
Sid sid_copy(sid_sid_star);
ASSERT_TRUE(EqualSid(sid_copy, sid_world));
Sid sid_sddl = Sid::FromSddlString(L"S-1-1-0");
ASSERT_TRUE(sid_sddl.IsValid());
ASSERT_TRUE(EqualSid(sid_sddl, sid_world));
Sid sid_sddl_invalid = Sid::FromSddlString(L"X-1-1-0");
ASSERT_FALSE(sid_sddl_invalid.IsValid());
Sid sid_sddl_empty = Sid::FromSddlString(L"");
ASSERT_FALSE(sid_sddl_empty.IsValid());
// Note that the WELL_KNOWN_SID_TYPE constructor is tested in the GetPSID
// test. AppContainer related constructors are tested in AppContainer.
}
// Tests the method GetPSID
TEST(SidTest, GetPSID) {
// Check for non-null result;
ASSERT_NE(nullptr, Sid(::WinLocalSid).GetPSID());
ASSERT_NE(nullptr, Sid(::WinCreatorOwnerSid).GetPSID());
ASSERT_NE(nullptr, Sid(::WinBatchSid).GetPSID());
ASSERT_TRUE(EqualSid(Sid(::WinNullSid), ATL::Sids::Null()));
ASSERT_TRUE(EqualSid(Sid(::WinWorldSid), ATL::Sids::World()));
ASSERT_TRUE(EqualSid(Sid(::WinDialupSid), ATL::Sids::Dialup()));
ASSERT_TRUE(EqualSid(Sid(::WinNetworkSid), ATL::Sids::Network()));
ASSERT_TRUE(
EqualSid(Sid(::WinBuiltinAdministratorsSid), ATL::Sids::Admins()));
ASSERT_TRUE(EqualSid(Sid(::WinBuiltinUsersSid), ATL::Sids::Users()));
ASSERT_TRUE(EqualSid(Sid(::WinBuiltinGuestsSid), ATL::Sids::Guests()));
ASSERT_TRUE(EqualSid(Sid(::WinProxySid), ATL::Sids::Proxy()));
}
TEST(SidTest, KnownCapability) {
if (base::win::GetVersion() < base::win::Version::WIN8)
return;
Sid sid_invalid_well_known =
Sid::FromKnownCapability(kMaxWellKnownCapability);
EXPECT_FALSE(sid_invalid_well_known.IsValid());
const KnownCapabilityTestEntry capabilities[] = {
{kInternetClient, L"S-1-15-3-1"},
{kInternetClientServer, L"S-1-15-3-2"},
{kPrivateNetworkClientServer, L"S-1-15-3-3"},
{kPicturesLibrary, L"S-1-15-3-4"},
{kVideosLibrary, L"S-1-15-3-5"},
{kMusicLibrary, L"S-1-15-3-6"},
{kDocumentsLibrary, L"S-1-15-3-7"},
{kEnterpriseAuthentication, L"S-1-15-3-8"},
{kSharedUserCertificates, L"S-1-15-3-9"},
{kRemovableStorage, L"S-1-15-3-10"},
{kAppointments, L"S-1-15-3-11"},
{kContacts, L"S-1-15-3-12"},
};
for (auto capability : capabilities) {
EXPECT_TRUE(EqualSid(Sid::FromKnownCapability(capability.capability),
capability.sddl_sid))
<< "Known Capability: " << capability.sddl_sid;
}
}
TEST(SidTest, NamedCapability) {
if (base::win::GetVersion() < base::win::Version::WIN10)
return;
Sid sid_nullptr = Sid::FromNamedCapability(nullptr);
EXPECT_FALSE(sid_nullptr.IsValid());
Sid sid_empty = Sid::FromNamedCapability(L"");
EXPECT_FALSE(sid_empty.IsValid());
const NamedCapabilityTestEntry capabilities[] = {
{L"internetClient", L"S-1-15-3-1"},
{L"internetClientServer", L"S-1-15-3-2"},
{L"registryRead",
L"S-1-15-3-1024-1065365936-1281604716-3511738428-"
"1654721687-432734479-3232135806-4053264122-3456934681"},
{L"lpacCryptoServices",
L"S-1-15-3-1024-3203351429-2120443784-2872670797-"
"1918958302-2829055647-4275794519-765664414-2751773334"},
{L"enterpriseAuthentication", L"S-1-15-3-8"},
{L"privateNetworkClientServer", L"S-1-15-3-3"}};
for (auto capability : capabilities) {
EXPECT_TRUE(EqualSid(Sid::FromNamedCapability(capability.capability_name),
capability.sddl_sid))
<< "Named Capability: " << capability.sddl_sid;
}
}
TEST(SidTest, Sddl) {
Sid sid_sddl = Sid::FromSddlString(L"S-1-1-0");
ASSERT_TRUE(sid_sddl.IsValid());
std::wstring sddl_str;
ASSERT_TRUE(sid_sddl.ToSddlString(&sddl_str));
ASSERT_EQ(L"S-1-1-0", sddl_str);
}
TEST(SidTest, SubAuthorities) {
DWORD world_subauthorities[] = {0};
SID_IDENTIFIER_AUTHORITY world_authority = {SECURITY_WORLD_SID_AUTHORITY};
Sid sid_world =
Sid::FromSubAuthorities(&world_authority, 1, world_subauthorities);
ASSERT_TRUE(EqualSid(sid_world, ATL::Sids::World()));
ASSERT_TRUE(Sid::FromSubAuthorities(&world_authority, 0, nullptr).IsValid());
DWORD admin_subauthorities[] = {32, 544};
SID_IDENTIFIER_AUTHORITY nt_authority = {SECURITY_NT_AUTHORITY};
Sid sid_admin =
Sid::FromSubAuthorities(&nt_authority, 2, admin_subauthorities);
ASSERT_TRUE(EqualSid(sid_admin, ATL::Sids::Admins()));
}
} // namespace sandbox
|