<commit_before>﻿using System;

namespace Novell.Directory.Ldap.NETStandard.FunctionalTests.Helpers
{
    public static class TestHelper
    {
        public static void WithLdapConnection(Action<ILdapConnection> actionOnConnectedLdapConnection, bool useSsl = false)
        {
            using (var ldapConnection = new LdapConnection())
            {
                ldapConnection.UserDefinedServerCertValidationDelegate += (sender, certificate, chain, errors) => true;
                if (useSsl)
                    ldapConnection.SecureSocketLayer = true;
                ldapConnection.Connect(TestsConfig.LdapServer.ServerAddress, useSsl ? TestsConfig.LdapServer.ServerPortSsl : TestsConfig.LdapServer.ServerPort);
                actionOnConnectedLdapConnection(ldapConnection);
            }
        }

        public static void WithAuthenticatedLdapConnection(Action<ILdapConnection> actionOnAuthenticatedLdapConnection)
        {
            WithLdapConnection(ldapConnection =>
            {
                ldapConnection.Bind(TestsConfig.LdapServer.RootUserDn, TestsConfig.LdapServer.RootUserPassword);
                actionOnAuthenticatedLdapConnection(ldapConnection);
            });
        }

        public static T WithLdapConnection<T>(Func<ILdapConnection, T> funcOnConnectedLdapConnection)
        {
            using (var ldapConnection = new LdapConnection())
            {
                ldapConnection.Connect(TestsConfig.LdapServer.ServerAddress, TestsConfig.LdapServer.ServerPort);
                return funcOnConnectedLdapConnection(ldapConnection);
            }
        }

        public static T WithAuthenticatedLdapConnection<T>(Func<ILdapConnection, T> funcOnAuthenticatedLdapConnection)
        {
            return WithLdapConnection(ldapConnection =>
            {
                ldapConnection.Bind(TestsConfig.LdapServer.RootUserDn, TestsConfig.LdapServer.RootUserPassword);
                return funcOnAuthenticatedLdapConnection(ldapConnection);
            });
        }

        public static string BuildDn(string cn)
        {
            return $"cn={cn}," + TestsConfig.LdapServer.BaseDn;
        }
    }
}<commit_msg>better test helper<commit_after>﻿using System;

namespace Novell.Directory.Ldap.NETStandard.FunctionalTests.Helpers
{
    public static class TestHelper
    {
        public static void WithLdapConnection(Action<ILdapConnection> actionOnConnectedLdapConnection, bool useSsl = false)
        {
            WithLdapConnectionImpl<object>((ldapConnection) =>
            {
                actionOnConnectedLdapConnection(ldapConnection);
                return null;
            }, useSsl);
        }

        public static void WithAuthenticatedLdapConnection(Action<ILdapConnection> actionOnAuthenticatedLdapConnection)
        {
            WithLdapConnection(ldapConnection =>
            {
                ldapConnection.Bind(TestsConfig.LdapServer.RootUserDn, TestsConfig.LdapServer.RootUserPassword);
                actionOnAuthenticatedLdapConnection(ldapConnection);
            });
        }

        public static T WithLdapConnection<T>(Func<ILdapConnection, T> funcOnConnectedLdapConnection, bool useSsl = false)
        {
            return WithLdapConnectionImpl(funcOnConnectedLdapConnection);
        }

        public static T WithAuthenticatedLdapConnection<T>(Func<ILdapConnection, T> funcOnAuthenticatedLdapConnection)
        {
            return WithLdapConnection(ldapConnection =>
            {
                ldapConnection.Bind(TestsConfig.LdapServer.RootUserDn, TestsConfig.LdapServer.RootUserPassword);
                return funcOnAuthenticatedLdapConnection(ldapConnection);
            });
        }

        private static T WithLdapConnectionImpl<T>(Func<ILdapConnection, T> funcOnConnectedLdapConnection, bool useSsl = false)
        {
            using (var ldapConnection = new LdapConnection())
            {
                ldapConnection.UserDefinedServerCertValidationDelegate += (sender, certificate, chain, errors) => true;
                if (useSsl)
                {                    
                    ldapConnection.SecureSocketLayer = true;
                }
                ldapConnection.Connect(TestsConfig.LdapServer.ServerAddress, useSsl ? TestsConfig.LdapServer.ServerPortSsl : TestsConfig.LdapServer.ServerPort);
                return funcOnConnectedLdapConnection(ldapConnection);
            }
        }

        public static string BuildDn(string cn)
        {
            return $"cn={cn}," + TestsConfig.LdapServer.BaseDn;
        }
    }
}
{'fraction_non_alphanumeric': 0.07183406113537118, 'fraction_numerical': 0.0, 'mean_word_length': 2.428892215568862, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1568188', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1220, 'n_words': 173}