Ejemplo n.º 1
0
        public void MarshalUtils_ByteArraysToBerValueArray_Returns_Ptr_To_BerValueArrays(params byte[][] sourceData)
        {
            var ptr = Marshal.AllocCoTaskMem((sourceData.Length + 1) * IntPtr.Size);

            MarshalUtils.ByteArraysToBerValueArray(sourceData, ptr);
            var actual = MarshalUtils.BerValArrayToByteArrays(ptr);

            Assert.Equal(sourceData, actual);
            Marshal.FreeCoTaskMem(ptr);
        }
Ejemplo n.º 2
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, byte[][]>("cn", new[] { Encoder.Instance.GetBytes("admin") });
            var attributeNamePtr = Encoder.Instance.StringToPtr(attribute.Key);
            var dnPtr            = Encoder.Instance.StringToPtr(dn);
            var valuesPtr        = Marshal.AllocHGlobal(IntPtr.Size * (attribute.Value.Length + 1));

            MarshalUtils.ByteArraysToBerValueArray(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_len(ldapHandle, entry, attributeNamePtr))
            .Returns(valuesPtr);
            native.Setup(_ => _.ldap_value_free_len(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.Contains(attribute.Key));
            Assert.Equal(attribute.Value[0],
                         searchResult.Entries[0].Attributes[attribute.Key].GetValues <byte[]>().First());
        }
Ejemplo n.º 3
0
        private static Native.Native.LDAPMod ToLdapMod(LdapModifyAttribute attribute)
        {
            var modValue    = attribute.Values ?? new List <string>();
            var modValuePtr = MarshalUtils.AllocHGlobalIntPtrArray(modValue.Count + 1);

            MarshalUtils.ByteArraysToBerValueArray(modValue.Select(GetModValue).ToArray(), modValuePtr);
            return(new Native.Native.LDAPMod
            {
                mod_op = (int)attribute.LdapModOperation | (int)LdapForNet.Native.Native.LdapModOperation.LDAP_MOD_BVALUES,
                mod_type = Encoder.Instance.StringToPtr(attribute.Type),
                mod_vals_u = new Native.Native.LDAPMod.mod_vals
                {
                    modv_bvals = modValuePtr
                },
                mod_next = IntPtr.Zero
            });
        }
Ejemplo n.º 4
0
        private static Native.Native.LDAPMod ToLdapMod(DirectoryAttribute attribute,
                                                       Native.Native.LdapModOperation operation)
        {
            var modValue    = attribute.GetValues <byte[]>()?.ToList() ?? new List <byte[]>();
            var modValuePtr = MarshalUtils.AllocHGlobalIntPtrArray(modValue.Count + 1);

            MarshalUtils.ByteArraysToBerValueArray(modValue.Select(_ => _ ?? new byte[0]).ToArray(), modValuePtr);
            return(new Native.Native.LDAPMod
            {
                mod_op = (int)operation | (int)LdapForNet.Native.Native.LdapModOperation.LDAP_MOD_BVALUES,
                mod_type = Encoder.Instance.StringToPtr(attribute.Name),
                mod_vals_u = new Native.Native.LDAPMod.mod_vals
                {
                    modv_bvals = modValuePtr
                },
                mod_next = IntPtr.Zero
            });
        }