| OLD | NEW |
| (Empty) |
| 1 // Copyright 2007-2010 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/base/constants.h" | |
| 17 #include "omaha/base/scoped_any.h" | |
| 18 #include "omaha/base/scoped_impersonation.h" | |
| 19 #include "omaha/base/user_info.h" | |
| 20 #include "omaha/testing/unit_test.h" | |
| 21 | |
| 22 // We can't make any assumption about the context the unit test runs, however | |
| 23 // we expect the calls to succeed. | |
| 24 namespace omaha { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const TCHAR kNtNonUniqueIdPrefix[] = _T("S-1-5-21-"); | |
| 29 const int kNtNonUniqueIdPrefixLength = arraysize(kNtNonUniqueIdPrefix) - 1; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 TEST(UserInfoTest, GetProcessUser) { | |
| 34 CString name, domain, sid; | |
| 35 EXPECT_HRESULT_SUCCEEDED(user_info::GetProcessUser(&name, &domain, &sid)); | |
| 36 | |
| 37 EXPECT_FALSE(name.IsEmpty()); | |
| 38 EXPECT_FALSE(domain.IsEmpty()); | |
| 39 if (user_info::IsRunningAsSystem()) { | |
| 40 EXPECT_STREQ(kLocalSystemSid, sid); | |
| 41 } else { | |
| 42 EXPECT_STREQ(kNtNonUniqueIdPrefix, sid.Left(kNtNonUniqueIdPrefixLength)); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 TEST(UserInfoTest, GetProcessUser_SidOnly) { | |
| 47 CString name, domain, sid1; | |
| 48 EXPECT_HRESULT_SUCCEEDED(user_info::GetProcessUser(&name, &domain, &sid1)); | |
| 49 | |
| 50 if (user_info::IsRunningAsSystem()) { | |
| 51 EXPECT_STREQ(kLocalSystemSid, sid1); | |
| 52 } else { | |
| 53 EXPECT_STREQ(kNtNonUniqueIdPrefix, sid1.Left(kNtNonUniqueIdPrefixLength)); | |
| 54 } | |
| 55 | |
| 56 CString sid2; | |
| 57 EXPECT_HRESULT_SUCCEEDED(user_info::GetProcessUser(NULL, NULL, &sid2)); | |
| 58 EXPECT_STREQ(sid1, sid2); | |
| 59 } | |
| 60 | |
| 61 | |
| 62 TEST(UserInfoTest, GetProcessUserSid) { | |
| 63 CSid sid; | |
| 64 EXPECT_HRESULT_SUCCEEDED(user_info::GetProcessUserSid(&sid)); | |
| 65 | |
| 66 const CString name = sid.AccountName(); | |
| 67 EXPECT_FALSE(name.IsEmpty()); | |
| 68 const CString sid_string = sid.Sid(); | |
| 69 if (user_info::IsRunningAsSystem()) { | |
| 70 EXPECT_STREQ(kLocalSystemSid, sid_string); | |
| 71 } else { | |
| 72 EXPECT_STREQ(kNtNonUniqueIdPrefix, | |
| 73 sid_string.Left(kNtNonUniqueIdPrefixLength)); | |
| 74 } | |
| 75 | |
| 76 EXPECT_EQ(1, sid.GetPSID()->Revision); | |
| 77 | |
| 78 const SID_IDENTIFIER_AUTHORITY kNtAuthority = SECURITY_NT_AUTHORITY; | |
| 79 const SID_IDENTIFIER_AUTHORITY* authority = | |
| 80 sid.GetPSID_IDENTIFIER_AUTHORITY(); | |
| 81 for (int i = 0; i < arraysize(authority->Value); ++i) { | |
| 82 EXPECT_EQ(kNtAuthority.Value[i], authority->Value[i]); | |
| 83 } | |
| 84 | |
| 85 UCHAR expected_auth_count = user_info::IsRunningAsSystem() ? 1 : 5; | |
| 86 EXPECT_EQ(expected_auth_count, sid.GetSubAuthorityCount()); | |
| 87 | |
| 88 DWORD expected_authority = user_info::IsRunningAsSystem() ? | |
| 89 SECURITY_LOCAL_SYSTEM_RID : | |
| 90 SECURITY_NT_NON_UNIQUE; | |
| 91 EXPECT_EQ(expected_authority, sid.GetSubAuthority(0)); | |
| 92 EXPECT_LT(static_cast<DWORD>(DOMAIN_USER_RID_MAX), sid.GetSubAuthority(4)); | |
| 93 } | |
| 94 | |
| 95 // Expect the unit tests do not run impersonated. | |
| 96 TEST(UserInfoTest, GetThreadUserSid) { | |
| 97 CString thread_sid; | |
| 98 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_NO_TOKEN), | |
| 99 user_info::GetThreadUserSid(&thread_sid)); | |
| 100 } | |
| 101 | |
| 102 // Expect the unit tests do not run impersonated. | |
| 103 // TODO(omaha3): Assuming we are running as admin, is there anything we can | |
| 104 // impersonate so that this important path gets tested? | |
| 105 TEST(UserInfoTest, GetEffectiveUserSid) { | |
| 106 CString thread_sid; | |
| 107 EXPECT_HRESULT_SUCCEEDED(user_info::GetEffectiveUserSid(&thread_sid)); | |
| 108 CSid process_sid; | |
| 109 EXPECT_HRESULT_SUCCEEDED(user_info::GetProcessUserSid(&process_sid)); | |
| 110 EXPECT_STREQ(process_sid.Sid(), thread_sid); | |
| 111 } | |
| 112 | |
| 113 TEST(UserInfoTest, IsLocalSystemUser) { | |
| 114 bool is_system = false; | |
| 115 CString sid; | |
| 116 EXPECT_HRESULT_SUCCEEDED(user_info::IsLocalSystemUser(&is_system, &sid)); | |
| 117 } | |
| 118 | |
| 119 TEST(UserInfoTest, IsThreadImpersonating) { | |
| 120 EXPECT_FALSE(user_info::IsThreadImpersonating()); | |
| 121 | |
| 122 scoped_handle process_token; | |
| 123 EXPECT_NE(0, ::OpenProcessToken(GetCurrentProcess(), | |
| 124 TOKEN_ALL_ACCESS, | |
| 125 address(process_token))); | |
| 126 | |
| 127 scoped_handle restricted_token; | |
| 128 EXPECT_NE(0, ::CreateRestrictedToken(get(process_token), | |
| 129 DISABLE_MAX_PRIVILEGE, | |
| 130 0, NULL, | |
| 131 0, NULL, | |
| 132 0, NULL, | |
| 133 address(restricted_token))); | |
| 134 | |
| 135 scoped_impersonation impersonate_user(get(restricted_token)); | |
| 136 | |
| 137 EXPECT_TRUE(user_info::IsThreadImpersonating()); | |
| 138 } | |
| 139 | |
| 140 } // namespace omaha | |
| OLD | NEW |