Fixing regression for AuthType.Anonymous which leads to a NullReferenceException...
authorJose Perez Rodriguez <joperezr@microsoft.com>
Tue, 14 Dec 2021 23:25:55 +0000 (15:25 -0800)
committerGitHub <noreply@github.com>
Tue, 14 Dec 2021 23:25:55 +0000 (15:25 -0800)
src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs
src/libraries/System.DirectoryServices.Protocols/tests/LdapConnectionTests.cs

index 4197abb..329237f 100644 (file)
@@ -111,7 +111,7 @@ namespace System.DirectoryServices.Protocols
                 passwordPtr = LdapPal.StringToPtr(passwd);
                 BerVal passwordBerval = new BerVal
                 {
-                    bv_len = passwd.Length,
+                    bv_len = passwd?.Length ?? 0,
                     bv_val = passwordPtr,
                 };
 
index 9a8074b..a0522a6 100644 (file)
@@ -112,6 +112,16 @@ namespace System.DirectoryServices.Protocols.Tests
             Assert.Equal(AuthType.Basic, connection.AuthType);
         }
 
+        [Fact]
+        public void AuthType_Anonymous_DoesNotThrowNull()
+        {
+            var connection = new LdapConnection("server");
+            connection.AuthType = AuthType.Anonymous;
+            // When calling Bind we make sure that the exception thrown is not that there was a NullReferenceException
+            // trying to retrive a null password's lenght, but instead an LdapException given the server cannot be reached.
+            Assert.Throws<LdapException>(() => connection.Bind());
+        }
+
         [Theory]
         [InlineData(AuthType.Anonymous - 1)]
         [InlineData(AuthType.Kerberos + 1)]