@@ -40,6 +40,73 @@ _reset_metadata (void)
4040 _mongoc_metadata_init ();
4141}
4242
43+ static void
44+ test_mongoc_metadata_appname_in_uri (void )
45+ {
46+ char long_string [MONGOC_METADATA_APPNAME_MAX + 2 ];
47+ char * uri_str ;
48+ const char * good_uri = "mongodb://host/?appname=mongodump" ;
49+ mongoc_uri_t * uri ;
50+ const char * appname = "mongodump" ;
51+
52+ memset (long_string , 'a' , MONGOC_METADATA_APPNAME_MAX + 1 );
53+ long_string [MONGOC_METADATA_APPNAME_MAX + 1 ] = '\0' ;
54+
55+ /* Shouldn't be able to set with appname really long */
56+ capture_logs (true);
57+ uri_str = bson_strdup_printf ("mongodb://a/?appname=%s" , long_string );
58+ ASSERT (!mongoc_client_new (uri_str ));
59+ capture_logs (false);
60+
61+ uri = mongoc_uri_new (good_uri );
62+ ASSERT (uri );
63+ appname = mongoc_uri_get_appname (uri );
64+ ASSERT (appname );
65+ ASSERT (!strcmp (appname , appname ));
66+ mongoc_uri_destroy (uri );
67+
68+ uri = mongoc_uri_new (NULL );
69+ ASSERT (uri );
70+ ASSERT (!mongoc_uri_set_appname (uri , long_string ));
71+ ASSERT (mongoc_uri_set_appname (uri , appname ));
72+ appname = mongoc_uri_get_appname (uri );
73+ ASSERT (appname );
74+ ASSERT (!strcmp (appname , appname ));
75+ mongoc_uri_destroy (uri );
76+
77+ bson_free (uri_str );
78+ }
79+
80+ static void
81+ test_mongoc_metadata_appname_frozen_single (void )
82+ {
83+ mongoc_client_t * client ;
84+ const char * good_uri = "mongodb://host/?appname=mongodump" ;
85+
86+ client = mongoc_client_new (good_uri );
87+
88+ /* Shouldn't be able to set appname again */
89+ ASSERT (!mongoc_client_set_appname (client , "a" ));
90+
91+ mongoc_client_destroy (client );
92+ }
93+
94+ static void
95+ test_mongoc_metadata_appname_frozen_pooled (void )
96+ {
97+ mongoc_client_pool_t * pool ;
98+ const char * good_uri = "mongodb://host/?appname=mongodump" ;
99+ mongoc_uri_t * uri ;
100+
101+ uri = mongoc_uri_new (good_uri );
102+
103+ pool = mongoc_client_pool_new (uri );
104+ ASSERT (!mongoc_client_pool_set_appname (pool , "test" ));
105+
106+ mongoc_client_pool_destroy (pool );
107+ mongoc_uri_destroy (uri );
108+ }
109+
43110static void
44111test_mongoc_metadata_append_success (void )
45112{
@@ -71,6 +138,7 @@ test_mongoc_metadata_append_success (void)
71138 mock_server_run (server );
72139 uri = mongoc_uri_copy (mock_server_get_uri (server ));
73140 mongoc_uri_set_option_as_int32 (uri , "heartbeatFrequencyMS" , 500 );
141+ mongoc_uri_set_option_as_utf8 (uri , "appname" , "testapp" );
74142 pool = mongoc_client_pool_new (uri );
75143
76144 /* Force topology scanner to start */
@@ -86,6 +154,14 @@ test_mongoc_metadata_append_success (void)
86154 ASSERT (bson_iter_init_find (& iter , request_doc , METADATA_FIELD ));
87155 ASSERT (bson_iter_recurse (& iter , & md_iter ));
88156
157+ ASSERT (bson_iter_find (& md_iter , "application" ));
158+ ASSERT (BSON_ITER_HOLDS_DOCUMENT (& md_iter ));
159+ ASSERT (bson_iter_recurse (& md_iter , & inner_iter ));
160+ ASSERT (bson_iter_find (& inner_iter , "name" ));
161+ val = bson_iter_utf8 (& inner_iter , NULL );
162+ ASSERT (val );
163+ ASSERT (!strcmp (val , "testapp" ));
164+
89165 /* Make sure driver.name and driver.version and platform are all right */
90166 ASSERT (bson_iter_find (& md_iter , "driver" ));
91167 ASSERT (BSON_ITER_HOLDS_DOCUMENT (& md_iter ));
@@ -321,6 +397,13 @@ test_mongoc_metadata_cannot_send (void)
321397void
322398test_metadata_install (TestSuite * suite )
323399{
400+ TestSuite_Add (suite , "/ClientMetadata/appname_in_uri" ,
401+ test_mongoc_metadata_appname_in_uri );
402+ TestSuite_Add (suite , "/ClientMetadata/appname_frozen_single" ,
403+ test_mongoc_metadata_appname_frozen_single );
404+ TestSuite_Add (suite , "/ClientMetadata/appname_frozen_pooled" ,
405+ test_mongoc_metadata_appname_frozen_pooled );
406+
324407 TestSuite_Add (suite , "/ClientMetadata/success" ,
325408 test_mongoc_metadata_append_success );
326409 TestSuite_Add (suite , "/ClientMetadata/failure" ,
0 commit comments