Skip to content

Commit

Permalink
Added tests for dns resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
chkead committed Jan 25, 2024
1 parent 3e95e09 commit 579d5fc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 29 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/hivemq/security/ssl/DnsResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hivemq.security.ssl;

import java.util.Map;

public class DnsResolver {

private final Map<String, String> dnsMap;

DnsResolver(final Map<String, String> dnsMap) {
this.dnsMap = dnsMap;
}

String resolve(final String domain) {
String alias = dnsMap.get(domain);
if (alias != null) {
return alias;
}

int index = domain.indexOf('.');
while (index >= 0) {
final String wildcardDomain = "*" + domain.substring(index);
alias = dnsMap.get(wildcardDomain);
if (alias != null) {
return alias;
}
index = domain.indexOf('.', index + 1);
}

return null;
}

}
29 changes: 0 additions & 29 deletions src/main/java/com/hivemq/security/ssl/SslContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,33 +242,4 @@ private static Set<String> getDnsHostnamesFromCertificate(final X509Certificate
throw new SslException("Invalid auth mode: " + clientAuthMode);
}

private static class DnsResolver {

private final Map<String, String> dnsMap;

DnsResolver(final Map<String, String> dnsMap) {
this.dnsMap = dnsMap;
}

String resolve(final String domain) {
String alias = dnsMap.get(domain);
if (alias != null) {
return alias;
}

int index = domain.indexOf('.');
while (index >= 0) {
final String wildcardDomain = "*" + domain.substring(index);
alias = dnsMap.get(wildcardDomain);
if (alias != null) {
return alias;
}
index = domain.indexOf('.', index + 1);
}

return null;
}

}

}
51 changes: 51 additions & 0 deletions src/test/java/com/hivemq/security/ssl/DnsResolverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.hivemq.security.ssl;

import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.*;

public class DnsResolverTest {
public static final String ALIAS_1 = "alias1";
public static final String TEST_EXAMPLE_COM = "test.example.com";

@Test
public void test_resolve_simple_dns_name() {
final DnsResolver dnsResolver = new DnsResolver(Map.of(TEST_EXAMPLE_COM, ALIAS_1));

final String resolve = dnsResolver.resolve(TEST_EXAMPLE_COM);

assertNotNull(resolve);
assertEquals(ALIAS_1, resolve);
}

@Test
public void test_resolve_non_matching_dns_name() {
final DnsResolver dnsResolver = new DnsResolver(Map.of(TEST_EXAMPLE_COM, ALIAS_1));

final String resolve = dnsResolver.resolve("other.example.com");

assertNull(resolve);
}

@Test
public void test_resolve_wildcard_dns_name() {
final DnsResolver dnsResolver = new DnsResolver(Map.of("*.example.com", ALIAS_1));

final String resolve = dnsResolver.resolve(TEST_EXAMPLE_COM);

assertNotNull(resolve);
assertEquals(ALIAS_1, resolve);
}

@Test
public void test_resolve_nested_wildcard_dns_name() {
final DnsResolver dnsResolver = new DnsResolver(Map.of("*.example.com", ALIAS_1));

final String resolve = dnsResolver.resolve("sub.test.example.com");

assertNotNull(resolve);
assertEquals(ALIAS_1, resolve);
}
}

0 comments on commit 579d5fc

Please sign in to comment.