public void ShouldResolveExactMatchWithAlias()
        {
            string testExpectedHostname = "example.com";
            string testalias = "IIS1";
            string testExpectedResult = "iis1.example.com";

            DnsRuleGroup group = new DnsRuleGroup();
            group.AddRule(new DnsRule
                              {
                                  Hostnames = new[] { (Hostname)testExpectedHostname },
                                  RecordType = DnsRecordType.CNAME,
                                  ValueAlias = testalias
                              });

            DnsValueAlias alias = new DnsValueAlias { Name = testalias, Value = testExpectedResult };

            RuleResolver resolver = new RuleResolver(new DnsRuleGroup[] { group },
                                                     new DnsValueAlias[] { alias });

            QueryResult result = resolver.Resolve(new Query
                                                      {
                                                          QueryType = DnsRecordType.CNAME,
                                                          Question = testExpectedHostname
                                                      });

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Result, Is.True);
            Assert.That(result.RecordType, Is.EqualTo(DnsRecordType.CNAME));
            Assert.That(result.RecordValue, Is.EqualTo(testExpectedResult));
        }
        public void ShouldResolveMorePreciseHostnameInGroup()
        {
            string testHostnameMatch = "www.example.com";
            string testExpectedResult = "iis3.example.com";

            string testHostnameEntry1 = "*.example.com";
            string testHostnameReturnValue1 = "iis1.example.com";

            string testHostnameEntry2 = testHostnameMatch;
            string testHostnameReturnValue2 = testExpectedResult;

            DnsRuleGroup group = new DnsRuleGroup();
            group.AddRule(new DnsRule
                              {
                                  Hostnames = new[] { (Hostname)testHostnameEntry1 },
                                  RecordType = DnsRecordType.CNAME,
                                  Value = testHostnameReturnValue1
                              });

            group.AddRule(new DnsRule
                              {
                                  Hostnames = new[] { (Hostname)testHostnameEntry2 },
                                  RecordType = DnsRecordType.CNAME,
                                  Value = testHostnameReturnValue2
                              });

            RuleResolver resolver = new RuleResolver(new DnsRuleGroup[] { group },
                                                     new DnsValueAlias[] { });

            QueryResult result = resolver.Resolve(new Query
                                                      {
                                                          QueryType = DnsRecordType.CNAME,
                                                          Question = testHostnameMatch
                                                      });

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Result, Is.True);
            Assert.That(result.RecordType, Is.EqualTo(DnsRecordType.CNAME));
            Assert.That(result.RecordValue, Is.EqualTo(testExpectedResult));
        }
Beispiel #3
0
 public RuleResolver(DnsRuleGroup[] dnsRuleGroups, DnsValueAlias[] dnsValueAliases)
 {
     _dnsRuleGroups = dnsRuleGroups ?? new DnsRuleGroup[] { };
     _dnsValueAliases = dnsValueAliases ?? new DnsValueAlias[] { };
 }
        public void ShouldReturnResolveUpstreamForAnyHostMatch()
        {
            string testHostnameMatch = "www.example.com";
            string testHostnameEntry = "*.example.com";

            DnsRuleGroup group = new DnsRuleGroup();
            group.AddRule(new DnsRule
            {
                Hostnames = new[] { (Hostname)testHostnameEntry },
                RuleType = DnsRuleType.ResolveUpstream
            });

            RuleResolver resolver = new RuleResolver(new DnsRuleGroup[] { group },
                                                     new DnsValueAlias[] { });

            QueryResult result = resolver.Resolve(new Query
            {
                QueryType = DnsRecordType.CNAME,
                Question = testHostnameMatch
            });

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Result, Is.True);
            Assert.That(result.RuleResult, Is.EqualTo(DnsRuleType.ResolveUpstream));
        }
Beispiel #5
0
        private RuleMatchResult GetRuleMatch(Query query, DnsRuleGroup group)
        {
            // Examine the group's rules and give a result.

            var similarRules = group.Rules
                .Select(rule => new
                                    {
                                        HostnameSimilarity = FindHostnameSimilarity(query, rule),
                                        TypeSimilarity = SimilarRuleTypes(query, rule),
                                        Rule = rule
                                    })
                .Where(result => result.HostnameSimilarity > 0 && result.TypeSimilarity > 0)
                .OrderByDescending(rule => rule.HostnameSimilarity)
                .ThenByDescending(rule => rule.TypeSimilarity)
                .ToArray();

            if (!similarRules.Any())
            {
                return new RuleMatchResult { Match = false };
            }

            return
                similarRules
                    .Select(
                        rule =>
                        new RuleMatchResult
                            {
                                HostnameSimilarity = rule.HostnameSimilarity,
                                Match = true,
                                Rule = rule.Rule
                            })
                    .FirstOrDefault();
        }