@@ -471,4 +471,80 @@ void testMatchDNSName() throws Exception {
471471 publicSuffixMatcher ));
472472 }
473473
474+ @ Test
475+ void testMatchIdentity () {
476+ // Test 1: IDN matching punycode
477+ final String unicodeHost1 = "поиск-слов.рф" ;
478+ final String punycodeHost1 = "xn----dtbqigoecuc.xn--p1ai" ;
479+
480+ // These should now match, thanks to IDN.toASCII():
481+ Assertions .assertTrue (
482+ DefaultHostnameVerifier .matchIdentity (unicodeHost1 , punycodeHost1 ),
483+ "Expected the Unicode host and its punycode to match"
484+ );
485+
486+ // ‘example.com’ vs. an unrelated punycode domain should fail:
487+ Assertions .assertFalse (
488+ DefaultHostnameVerifier .matchIdentity ("example.com" , punycodeHost1 ),
489+ "Expected mismatch between example.com and xn----dtbqigoecuc.xn--p1ai"
490+ );
491+
492+ // Test 2: Unicode host and Unicode identity
493+ final String unicodeHost2 = "пример.рф" ;
494+ final String unicodeIdentity2 = "пример.рф" ;
495+ Assertions .assertTrue (
496+ DefaultHostnameVerifier .matchIdentity (unicodeHost2 , unicodeIdentity2 ),
497+ "Expected Unicode host and Unicode identity to match"
498+ );
499+
500+ // Test 3: Punycode host and Unicode identity
501+ final String unicodeHost3 = "пример.рф" ;
502+ final String punycodeIdentity3 = "xn--e1afmkfd.xn--p1ai" ;
503+ Assertions .assertTrue (
504+ DefaultHostnameVerifier .matchIdentity (unicodeHost3 , punycodeIdentity3 ),
505+ "Expected Unicode host and punycode identity to match"
506+ );
507+
508+ // Test 4: Wildcard matching in the left-most label
509+ final String unicodeHost4 = "sub.пример.рф" ;
510+ final String unicodeIdentity4 = "*.пример.рф" ;
511+ Assertions .assertTrue (
512+ DefaultHostnameVerifier .matchIdentity (unicodeHost4 , unicodeIdentity4 ),
513+ "Expected wildcard to match subdomain"
514+ );
515+
516+ // Test 5: Invalid host
517+ final String invalidHost = "invalid_host" ;
518+ final String unicodeIdentity5 = "пример.рф" ;
519+ Assertions .assertFalse (
520+ DefaultHostnameVerifier .matchIdentity (invalidHost , unicodeIdentity5 ),
521+ "Expected invalid host to not match"
522+ );
523+
524+ // Test 6: Invalid identity
525+ final String unicodeHost4b = "пример.рф" ;
526+ final String invalidIdentity = "xn--invalid-punycode" ;
527+ Assertions .assertFalse (
528+ DefaultHostnameVerifier .matchIdentity (unicodeHost4b , invalidIdentity ),
529+ "Expected invalid identity to not match"
530+ );
531+
532+ // Test 7: Mixed case comparison
533+ final String unicodeHost5 = "ПрИмеР.рф" ;
534+ final String unicodeIdentity6 = "пример.рф" ;
535+ Assertions .assertTrue (
536+ DefaultHostnameVerifier .matchIdentity (unicodeHost5 , unicodeIdentity6 ),
537+ "Expected case-insensitive Unicode comparison to match"
538+ );
539+
540+
541+ // Test 8: Wildcard in the middle label (per RFC 2818, should match)
542+ final String unicodeHost6 = "sub.пример.рф" ;
543+ final String unicodeIdentity8 = "sub.*.рф" ;
544+ Assertions .assertTrue (
545+ DefaultHostnameVerifier .matchIdentity (unicodeHost6 , unicodeIdentity8 ),
546+ "Expected wildcard in the middle label to match"
547+ );
548+ }
549+
474550}
0 commit comments