Exemple #1
0
        public void Handle_Should_Returns_SearchResponse()
        {
            var native         = new Mock <LdapNative>();
            var requestHandler = CreateRequestHandler(native);
            var msg            = IntPtr.Zero;
            var ldapHandle     = new LdapHandle(IntPtr.Zero);
            var entry          = new IntPtr(1);
            var dn             = "cn=admin,dc=example,dc=com";
            var attribute      = new KeyValuePair <string, List <string> >("cn", new List <string>(2)
            {
                "admin", null
            });
            var attributeNamePtr = Marshal.StringToHGlobalAnsi(attribute.Key);
            var dnPtr            = Marshal.StringToHGlobalAnsi(dn);
            var valuesPtr        = Marshal.AllocHGlobal(IntPtr.Size * attribute.Value.Count);

            MarshalUtils.StringArrayToPtr(attribute.Value, valuesPtr);

            native.Setup(_ => _.ldap_first_entry(ldapHandle, msg))
            .Returns(entry);
            native.Setup(_ => _.ldap_next_entry(ldapHandle, msg))
            .Returns(IntPtr.Zero);
            native.Setup(_ => _.ldap_get_dn(ldapHandle, entry))
            .Returns(dnPtr);
            native.Setup(_ => _.ldap_memfree(It.IsAny <IntPtr>()))
            .Callback((IntPtr ptr) => Marshal.FreeHGlobal(ptr));
            native.Setup(_ => _.ldap_first_attribute(ldapHandle, entry, ref It.Ref <IntPtr> .IsAny))
            .Returns(attributeNamePtr);
            native.Setup(_ => _.ldap_next_attribute(ldapHandle, entry, It.IsAny <IntPtr>()))
            .Returns(IntPtr.Zero);
            native.Setup(_ => _.ldap_get_values(ldapHandle, entry, attributeNamePtr))
            .Returns(valuesPtr);
            native.Setup(_ => _.ldap_value_free(It.IsAny <IntPtr>()))
            .Callback((IntPtr ptr) => Marshal.FreeHGlobal(ptr));

            var status = requestHandler.Handle(ldapHandle,
                                               Native.LdapResultType.LDAP_RES_SEARCH_ENTRY, msg, out _);

            Assert.Equal(LdapResultCompleteStatus.Partial, status);

            status = requestHandler.Handle(ldapHandle,
                                           Native.LdapResultType.LDAP_RES_SEARCH_RESULT, msg, out var actual);
            Assert.Equal(LdapResultCompleteStatus.Complete, status);
            Assert.IsType <SearchResponse>(actual);
            var searchResult = actual as SearchResponse;

            Assert.NotNull(searchResult);
            Assert.Single(searchResult.Entries);
            Assert.Equal(dn, searchResult.Entries[0].Dn);
            Assert.Single(searchResult.Entries[0].Attributes);
            Assert.True(searchResult.Entries[0].Attributes.ContainsKey(attribute.Key));
            Assert.Equal(attribute.Value[0], searchResult.Entries[0].Attributes[attribute.Key][0]);
        }
Exemple #2
0
        internal override int SetClientCertificate(SafeHandle ld, X509Certificate2 certificate)
        {
            var certFile = Path.GetTempFileName();
            var keyFile = Path.GetTempFileName();
            File.WriteAllText(certFile, CertificateToPem(certificate));
            File.WriteAllText(keyFile, RsaKeyToPem(certificate));
            _tempFiles.Add(certFile);
            _tempFiles.Add(keyFile);

            var globalHandle = new LdapHandle(IntPtr.Zero);
            ThrowIfError(ldap_set_option(globalHandle, (int) Native.LdapOption.LDAP_OPT_X_TLS_CERTFILE, certFile),
                nameof(ldap_set_option));
            return ldap_set_option(globalHandle, (int) Native.LdapOption.LDAP_OPT_X_TLS_KEYFILE, keyFile);
        }
Exemple #3
0
 public LdapMessage(LdapHandle ldapHandle, IntPtr ldapMessage)
 {
     _ldapHandle  = ldapHandle;
     _ldapMessage = ldapMessage;
     _dircontext  = null;
 }