This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Optimize performance of SecurityIdentifier.ToString() #33579
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d448be5
optimize performance of SecurityIdentifier.ToString()
Wraith2 53668f9
address feedback
Wraith2 e3d0cee
fix silly mistakes and address feedback
Wraith2 a4ff1cc
add in compatibility ifdefs
Wraith2 a204321
add SecurityIdentifier.ToString() tests
Wraith2 a2458d9
address test feedback
Wraith2 d336010
uncomplicate test
Wraith2 3d34c89
configuration changes
Wraith2 8e4cd63
configuration updates
Wraith2 0a10e32
added nano and core server test exclusions
Wraith2 ab4b302
added netcoreapp2.1 pinvoke exception list
Wraith2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...em.Security.Principal.Windows/src/PinvokeAnalyzerExceptionList.analyzerdata.netcoreapp2.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| advapi32.dll!LsaNtStatusToWinError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/System.Security.Principal.Windows/tests/SecurityIdentifierTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Security.Principal; | ||
| using Xunit; | ||
|
|
||
| public class SecurityIdentifierTests | ||
| { | ||
| [Fact] | ||
| public void ValidateGetCurrentUser() | ||
| { | ||
| using (WindowsIdentity identity = WindowsIdentity.GetCurrent(false)) | ||
| { | ||
| Assert.NotNull(identity.User); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateToString() | ||
| { | ||
| string sddl = null; | ||
| using (WindowsIdentity me = WindowsIdentity.GetCurrent(false)) | ||
| { | ||
| sddl = me.User.ToString(); | ||
| } | ||
|
|
||
| Assert.NotNull(sddl); | ||
| Assert.NotEmpty(sddl); | ||
| Assert.StartsWith("S-1-5-", sddl); // sid prefix, version 1, user account type | ||
| Assert.NotEqual('-', sddl[sddl.Length - 1]); | ||
|
|
||
| string[] parts = sddl.Substring(6).Split('-'); | ||
Wraith2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Assert.NotNull(parts); | ||
| Assert.NotEmpty(parts); | ||
|
|
||
| Assert.All(parts, part => uint.TryParse(part, out uint _)); | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer), nameof(PlatformDetection.IsNotWindowsServerCore))] | ||
| public void ValidateToStringUsingWhoami() | ||
| { | ||
| string librarySid = null; | ||
| using (WindowsIdentity me = WindowsIdentity.GetCurrent(false)) | ||
| { | ||
| librarySid = me.User.ToString(); | ||
| } | ||
|
|
||
| string windowsSid = null; | ||
| string processArguments = " /user"; | ||
| // Call whois to get current sid | ||
| Process whoamiProcess = new Process(); | ||
| whoamiProcess.StartInfo.FileName = "whoami.exe"; // whoami.exe is in system32, should already be on path | ||
Wraith2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| whoamiProcess.StartInfo.Arguments = " " + processArguments; | ||
| whoamiProcess.StartInfo.CreateNoWindow = true; | ||
| whoamiProcess.StartInfo.UseShellExecute = false; | ||
| whoamiProcess.StartInfo.RedirectStandardOutput = true; | ||
| whoamiProcess.Start(); | ||
| string output = whoamiProcess.StandardOutput.ReadToEnd(); | ||
| whoamiProcess.WaitForExit(); | ||
|
|
||
| if (whoamiProcess.ExitCode == 0) | ||
| { | ||
| int startSid = output.IndexOf("S-1-5-"); | ||
| int endSid = 0; | ||
| if (startSid >= 0) | ||
| { | ||
| int length = Math.Min(output.Length - startSid, librarySid.Length); | ||
| windowsSid = output.Substring(startSid, length); | ||
|
|
||
| if (output.Length > startSid + length) | ||
| { | ||
| Assert.True(char.IsWhiteSpace(output[startSid + length])); | ||
| } | ||
| } | ||
| if (endSid > startSid) | ||
| { | ||
| windowsSid = output.Substring(startSid, (endSid - startSid)); | ||
| } | ||
| } | ||
| Assert.NotNull(windowsSid); | ||
| Assert.NotEmpty(windowsSid); | ||
| Assert.NotNull(librarySid); | ||
| Assert.NotEmpty(librarySid); | ||
|
|
||
| Assert.Equal(windowsSid, librarySid); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.