< Summary

Class:SharpHoundCommonLib.LdapConnectionWrapper
Assembly:SharpHoundCommonLib
File(s):D:\a\SharpHoundCommon\SharpHoundCommon\src\CommonLib\LdapConnectionWrapper.cs
Covered lines:0
Uncovered lines:62
Coverable lines:62
Total lines:97
Line coverage:0% (0 of 62)
Covered branches:0
Total branches:26
Branch coverage:0% (0 of 26)

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%100%
GetServer()0%200%
GetSearchBase(...)0%800%
GetSavedContext(...)0%400%
SaveContext(...)0%400%
Equals(...)100%100%
Equals(...)0%600%
GetHashCode()0%200%

File(s)

D:\a\SharpHoundCommon\SharpHoundCommon\src\CommonLib\LdapConnectionWrapper.cs

#LineLine coverage
 1using System;
 2using System.DirectoryServices.Protocols;
 3using SharpHoundCommonLib.Enums;
 4
 5namespace SharpHoundCommonLib {
 6    public class LdapConnectionWrapper {
 07        public LdapConnection Connection { get; private set; }
 8        private readonly IDirectoryObject _rootDseEntry;
 9        private string _domainSearchBase;
 10        private string _configurationSearchBase;
 11        private string _schemaSearchBase;
 12        private string _server;
 013        private string Guid { get; set; }
 14        public readonly bool GlobalCatalog;
 15        public readonly string PoolIdentifier;
 16
 017        public LdapConnectionWrapper(LdapConnection connection, IDirectoryObject rootDseEntry, bool globalCatalog,
 018            string poolIdentifier) {
 019            Connection = connection;
 020            _rootDseEntry = rootDseEntry;
 021            Guid = new Guid().ToString();
 022            GlobalCatalog = globalCatalog;
 023            PoolIdentifier = poolIdentifier;
 024        }
 25
 026        public string GetServer() {
 027            if (_server != null) {
 028                return _server;
 29            }
 30
 031            _server = _rootDseEntry.GetProperty(LDAPProperties.DNSHostName);
 032            return _server;
 033        }
 34
 035        public bool GetSearchBase(NamingContext context, out string searchBase) {
 036            searchBase = GetSavedContext(context);
 037            if (searchBase != null) {
 038                return true;
 39            }
 40
 041            searchBase = context switch {
 042                NamingContext.Default => _rootDseEntry.GetProperty(LDAPProperties.DefaultNamingContext),
 043                NamingContext.Configuration =>
 044                    _rootDseEntry.GetProperty(LDAPProperties.ConfigurationNamingContext),
 045                NamingContext.Schema => _rootDseEntry.GetProperty(LDAPProperties.SchemaNamingContext),
 046                _ => throw new ArgumentOutOfRangeException(nameof(context), context, null)
 047            };
 48
 049            if (searchBase != null) {
 050                SaveContext(context, searchBase);
 051                return true;
 52            }
 53
 054            return false;
 055        }
 56
 057        private string GetSavedContext(NamingContext context) {
 058            return context switch {
 059                NamingContext.Configuration => _configurationSearchBase,
 060                NamingContext.Default => _domainSearchBase,
 061                NamingContext.Schema => _schemaSearchBase,
 062                _ => throw new ArgumentOutOfRangeException(nameof(context), context, null)
 063            };
 064        }
 65
 066        public void SaveContext(NamingContext context, string searchBase) {
 067            switch (context) {
 68                case NamingContext.Default:
 069                    _domainSearchBase = searchBase;
 070                    break;
 71                case NamingContext.Configuration:
 072                    _configurationSearchBase = searchBase;
 073                    break;
 74                case NamingContext.Schema:
 075                    _schemaSearchBase = searchBase;
 076                    break;
 77                default:
 078                    throw new ArgumentOutOfRangeException(nameof(context), context, null);
 79            }
 080        }
 81
 082        protected bool Equals(LdapConnectionWrapper other) {
 083            return Guid == other.Guid;
 084        }
 85
 086        public override bool Equals(object obj) {
 087            if (ReferenceEquals(null, obj)) return false;
 088            if (ReferenceEquals(this, obj)) return true;
 089            if (obj.GetType() != this.GetType()) return false;
 090            return Equals((LdapConnectionWrapper)obj);
 091        }
 92
 093        public override int GetHashCode() {
 094            return (Guid != null ? Guid.GetHashCode() : 0);
 095        }
 96    }
 97}