Fix NRE when context value is null in method Save from Principal class (dotnet/corefx...
authorRoman Marusyk <Marusyk@users.noreply.github.com>
Wed, 9 Oct 2019 23:37:13 +0000 (02:37 +0300)
committerSantiago Fernandez Madero <safern@microsoft.com>
Wed, 9 Oct 2019 23:37:13 +0000 (16:37 -0700)
* Fix NRE when context value is null in method Save from Principal class

* Add unit test

Commit migrated from https://github.com/dotnet/corefx/commit/30280e07adc3c1f0776ef9531b47a769e2b45420

src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/Principal.cs
src/libraries/System.DirectoryServices.AccountManagement/tests/PrincipalTest.cs

index 98a021c..040de0d 100644 (file)
@@ -337,10 +337,6 @@ namespace System.DirectoryServices.AccountManagement
             // Make sure we're not a fake principal
             CheckFakePrincipal();
 
-            if (context.ContextType == ContextType.Machine || _ctx.ContextType == ContextType.Machine)
-            {
-                throw new InvalidOperationException(SR.SaveToNotSupportedAgainstMachineStore);
-            }
             // We must have a PrincipalContext to save into.  This should always be the case, unless we're unpersisted
             // and they never set a PrincipalContext.
             if (context == null)
@@ -349,6 +345,11 @@ namespace System.DirectoryServices.AccountManagement
                 throw new InvalidOperationException(SR.NullArguments);
             }
 
+            if (context.ContextType == ContextType.Machine || _ctx.ContextType == ContextType.Machine)
+            {
+                throw new InvalidOperationException(SR.SaveToNotSupportedAgainstMachineStore);
+            }
+
             // If the user is trying to save to the same context we are already set to then just save the changes
             if (context == _ctx)
             {
index acdfd1a..893ab0d 100644 (file)
@@ -2,6 +2,7 @@
 // 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.Collections.Generic;
 using System.Security.Principal;
 using Xunit;
 
@@ -107,6 +108,27 @@ namespace System.DirectoryServices.AccountManagement.Tests
             }
         }
 
+        [Theory]
+        [MemberData(nameof(TestDebuggerAttributes_Inputs))]
+        public void Save_ThrowsInvalidOperationException(PrincipalContext context)
+        {
+            if (DomainContext == null)
+            {
+                return;
+            }
+
+            using (Principal principal = CreateExtendedPrincipal(DomainContext, Guid.NewGuid().ToString()))
+            {
+                Assert.Throws<InvalidOperationException>(() => principal.Save(context));
+            }
+        }
+
+        public static IEnumerable<object[]> TestDebuggerAttributes_Inputs()
+        {
+            yield return new object[] { null };
+            yield return new object[] { new PrincipalContext(ContextType.Machine) };
+        }
+
         public abstract Principal CreatePrincipal(PrincipalContext context, string name);
 
         public abstract Principal CreateExtendedPrincipal(PrincipalContext context, string name);