1818
1919import static com .google .common .truth .Truth .assertThat ;
2020import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .BAD_SERVER_PEM_FILE ;
21+ import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .BAD_WILDCARD_DNS_PEM_FILE ;
2122import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .CA_PEM_FILE ;
2223import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .CLIENT_PEM_FILE ;
2324import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .CLIENT_SPIFFE_PEM_FILE ;
25+ import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .SERVER_0_PEM_FILE ;
2426import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .SERVER_1_PEM_FILE ;
2527import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .SERVER_1_SPIFFE_PEM_FILE ;
2628import static org .junit .Assert .fail ;
4244import java .security .cert .CertificateException ;
4345import java .security .cert .X509Certificate ;
4446import java .util .Arrays ;
47+ import java .util .Collection ;
4548import java .util .Collections ;
4649import java .util .List ;
4750import javax .net .ssl .SSLEngine ;
5255import org .junit .Rule ;
5356import org .junit .Test ;
5457import org .junit .runner .RunWith ;
55- import org .junit .runners .JUnit4 ;
58+ import org .junit .runners .Parameterized ;
59+ import org .junit .runners .Parameterized .Parameters ;
5660import org .mockito .Mock ;
5761import org .mockito .junit .MockitoJUnit ;
5862import org .mockito .junit .MockitoRule ;
5963
6064/**
6165 * Unit tests for {@link XdsX509TrustManager}.
6266 */
63- @ RunWith (JUnit4 .class )
67+ @ RunWith (Parameterized .class )
6468public class XdsX509TrustManagerTest {
6569
6670 @ Rule
@@ -74,6 +78,12 @@ public class XdsX509TrustManagerTest {
7478
7579 private XdsX509TrustManager trustManager ;
7680
81+ private final TestParam testParam ;
82+
83+ public XdsX509TrustManagerTest (TestParam testParam ) {
84+ this .testParam = testParam ;
85+ }
86+
7787 @ Test
7888 public void nullCertContextTest () throws CertificateException , IOException {
7989 trustManager = new XdsX509TrustManager (null , mockDelegate );
@@ -691,6 +701,52 @@ public void unsupportedAltNameType() throws CertificateException, IOException {
691701 }
692702 }
693703
704+ @ Test
705+ public void testDnsWildcardPatterns ()
706+ throws CertificateException , IOException {
707+ StringMatcher stringMatcher =
708+ StringMatcher .newBuilder ()
709+ .setExact (testParam .sanPattern )
710+ .setIgnoreCase (testParam .ignoreCase )
711+ .build ();
712+ @ SuppressWarnings ("deprecation" )
713+ CertificateValidationContext certContext =
714+ CertificateValidationContext .newBuilder ()
715+ .addMatchSubjectAltNames (stringMatcher )
716+ .build ();
717+ trustManager = new XdsX509TrustManager (certContext , mockDelegate );
718+ X509Certificate [] certs =
719+ CertificateUtils .toX509Certificates (TlsTesting .loadCert (testParam .certFile ));
720+ try {
721+ trustManager .verifySubjectAltNameInChain (certs );
722+ assertThat (testParam .expected ).isTrue ();
723+ } catch (CertificateException certException ) {
724+ assertThat (testParam .expected ).isFalse ();
725+ assertThat (certException ).hasMessageThat ().isEqualTo ("Peer certificate SAN check failed" );
726+ }
727+ }
728+
729+ @ Parameters (name = "{index}: {0}" )
730+ public static Collection <Object []> getParameters () {
731+ return Arrays .asList (new Object [][] {
732+ {new TestParam ("*.test.google.fr" , SERVER_1_PEM_FILE , false , true )},
733+ {new TestParam ("*.test.youtube.com" , SERVER_1_PEM_FILE , false , true )},
734+ {new TestParam ("waterzooi.test.google.be" , SERVER_1_PEM_FILE , false , true )},
735+ {new TestParam ("192.168.1.3" , SERVER_1_PEM_FILE , false , true )},
736+ {new TestParam ("*.TEST.YOUTUBE.com" , SERVER_1_PEM_FILE , true , true )},
737+ {new TestParam ("w*i.test.google.be" , SERVER_1_PEM_FILE , false , true )},
738+ {new TestParam ("w*a.test.google.be" , SERVER_1_PEM_FILE , false , false )},
739+ {new TestParam ("*.test.google.com.au" , SERVER_0_PEM_FILE , false , false )},
740+ {new TestParam ("*.TEST.YOUTUBE.com" , SERVER_1_PEM_FILE , false , false )},
741+ {new TestParam ("*waterzooi" , SERVER_1_PEM_FILE , false , false )},
742+ {new TestParam ("*.lyft.com" , BAD_WILDCARD_DNS_PEM_FILE , false , false )},
743+ {new TestParam ("ly**ft.com" , BAD_WILDCARD_DNS_PEM_FILE , false , false )},
744+ {new TestParam ("*yft.c*m" , BAD_WILDCARD_DNS_PEM_FILE , false , false )},
745+ {new TestParam ("xn--*.lyft.com" , BAD_WILDCARD_DNS_PEM_FILE , false , false )},
746+ {new TestParam ("" , BAD_WILDCARD_DNS_PEM_FILE , false , false )},
747+ });
748+ }
749+
694750 private TestSslEngine buildTrustManagerAndGetSslEngine ()
695751 throws CertificateException , IOException , CertStoreException {
696752 SSLParameters sslParams = buildTrustManagerAndGetSslParameters ();
@@ -754,4 +810,18 @@ public void setSSLParameters(SSLParameters sslParameters) {
754810
755811 private SSLParameters sslParameters ;
756812 }
813+
814+ private static class TestParam {
815+ final String sanPattern ;
816+ final String certFile ;
817+ final boolean ignoreCase ;
818+ final boolean expected ;
819+
820+ TestParam (String sanPattern , String certFile , boolean ignoreCase , boolean expected ) {
821+ this .sanPattern = sanPattern ;
822+ this .certFile = certFile ;
823+ this .ignoreCase = ignoreCase ;
824+ this .expected = expected ;
825+ }
826+ }
757827}
0 commit comments