18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import 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 ;
21
22
import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .CA_PEM_FILE ;
22
23
import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .CLIENT_PEM_FILE ;
23
24
import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .CLIENT_SPIFFE_PEM_FILE ;
25
+ import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .SERVER_0_PEM_FILE ;
24
26
import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .SERVER_1_PEM_FILE ;
25
27
import static io .grpc .xds .internal .security .CommonTlsContextTestsUtil .SERVER_1_SPIFFE_PEM_FILE ;
26
28
import static org .junit .Assert .fail ;
42
44
import java .security .cert .CertificateException ;
43
45
import java .security .cert .X509Certificate ;
44
46
import java .util .Arrays ;
47
+ import java .util .Collection ;
45
48
import java .util .Collections ;
46
49
import java .util .List ;
47
50
import javax .net .ssl .SSLEngine ;
52
55
import org .junit .Rule ;
53
56
import org .junit .Test ;
54
57
import org .junit .runner .RunWith ;
55
- import org .junit .runners .JUnit4 ;
58
+ import org .junit .runners .Parameterized ;
59
+ import org .junit .runners .Parameterized .Parameters ;
56
60
import org .mockito .Mock ;
57
61
import org .mockito .junit .MockitoJUnit ;
58
62
import org .mockito .junit .MockitoRule ;
59
63
60
64
/**
61
65
* Unit tests for {@link XdsX509TrustManager}.
62
66
*/
63
- @ RunWith (JUnit4 .class )
67
+ @ RunWith (Parameterized .class )
64
68
public class XdsX509TrustManagerTest {
65
69
66
70
@ Rule
@@ -74,6 +78,12 @@ public class XdsX509TrustManagerTest {
74
78
75
79
private XdsX509TrustManager trustManager ;
76
80
81
+ private final TestParam testParam ;
82
+
83
+ public XdsX509TrustManagerTest (TestParam testParam ) {
84
+ this .testParam = testParam ;
85
+ }
86
+
77
87
@ Test
78
88
public void nullCertContextTest () throws CertificateException , IOException {
79
89
trustManager = new XdsX509TrustManager (null , mockDelegate );
@@ -691,6 +701,52 @@ public void unsupportedAltNameType() throws CertificateException, IOException {
691
701
}
692
702
}
693
703
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
+
694
750
private TestSslEngine buildTrustManagerAndGetSslEngine ()
695
751
throws CertificateException , IOException , CertStoreException {
696
752
SSLParameters sslParams = buildTrustManagerAndGetSslParameters ();
@@ -754,4 +810,18 @@ public void setSSLParameters(SSLParameters sslParameters) {
754
810
755
811
private SSLParameters sslParameters ;
756
812
}
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
+ }
757
827
}
0 commit comments