@@ -2740,6 +2740,70 @@ test_casing_options (void)
2740
2740
mongoc_uri_destroy (uri );
2741
2741
}
2742
2742
2743
+ static void
2744
+ test_parses_long_ipv6 (void )
2745
+ {
2746
+ // Test parsing long malformed IPv6 literals. This is a regression test for
2747
+ // CDRIVER-4816.
2748
+ bson_error_t error ;
2749
+
2750
+ // Test the largest permitted IPv6 literal.
2751
+ {
2752
+ // Construct a string of repeating `:`.
2753
+ bson_string_t * host = bson_string_new (NULL );
2754
+ for (int i = 0 ; i < BSON_HOST_NAME_MAX - 2 ; i ++ ) {
2755
+ // Max IPv6 literal is two less due to including `[` and `]`.
2756
+ bson_string_append (host , ":" );
2757
+ }
2758
+
2759
+ char * host_and_port = bson_strdup_printf ("[%s]:27017" , host -> str );
2760
+ char * uri_string = bson_strdup_printf ("mongodb://%s" , host_and_port );
2761
+ mongoc_uri_t * uri = mongoc_uri_new_with_error (uri_string , & error );
2762
+ ASSERT_OR_PRINT (uri , error );
2763
+ const mongoc_host_list_t * hosts = mongoc_uri_get_hosts (uri );
2764
+ ASSERT_CMPSTR (hosts -> host , host -> str );
2765
+ ASSERT_CMPSTR (hosts -> host_and_port , host_and_port );
2766
+ ASSERT_CMPUINT16 (hosts -> port , = = , 27017 );
2767
+ ASSERT (!hosts -> next );
2768
+
2769
+ mongoc_uri_destroy (uri );
2770
+ bson_free (uri_string );
2771
+ bson_free (host_and_port );
2772
+ bson_string_free (host , true /* free_segment */ );
2773
+ }
2774
+
2775
+ // Test one character more than the largest IPv6 literal.
2776
+ {
2777
+ // Construct a string of repeating `:`.
2778
+ bson_string_t * host = bson_string_new (NULL );
2779
+ for (int i = 0 ; i < BSON_HOST_NAME_MAX - 2 + 1 ; i ++ ) {
2780
+ bson_string_append (host , ":" );
2781
+ }
2782
+
2783
+ char * host_and_port = bson_strdup_printf ("[%s]:27017" , host -> str );
2784
+ char * uri_string = bson_strdup_printf ("mongodb://%s" , host_and_port );
2785
+ capture_logs (true);
2786
+ mongoc_uri_t * uri = mongoc_uri_new_with_error (uri_string , & error );
2787
+ // Expect error parsing IPv6 literal is logged.
2788
+ ASSERT_CAPTURED_LOG ("parsing IPv6" ,
2789
+ MONGOC_LOG_LEVEL_ERROR ,
2790
+ "IPv6 literal provided in URI is too long" );
2791
+ capture_logs (false);
2792
+
2793
+ // Expect a generic parsing error is also returned.
2794
+ ASSERT (!uri );
2795
+ ASSERT_ERROR_CONTAINS (error ,
2796
+ MONGOC_ERROR_COMMAND ,
2797
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
2798
+ "Invalid host string in URI" );
2799
+
2800
+ mongoc_uri_destroy (uri );
2801
+ bson_free (uri_string );
2802
+ bson_free (host_and_port );
2803
+ bson_string_free (host , true /* free_segment */ );
2804
+ }
2805
+ }
2806
+
2743
2807
void
2744
2808
test_uri_install (TestSuite * suite )
2745
2809
{
@@ -2774,4 +2838,5 @@ test_uri_install (TestSuite *suite)
2774
2838
"/Uri/one_tls_option_enables_tls" ,
2775
2839
test_one_tls_option_enables_tls );
2776
2840
TestSuite_Add (suite , "/Uri/options_casing" , test_casing_options );
2841
+ TestSuite_Add (suite , "/Uri/parses_long_ipv6" , test_parses_long_ipv6 );
2777
2842
}
0 commit comments