Skip to content

Commit aad984f

Browse files
ian borosajdavis
ian boros
authored andcommitted
CDRIVER-1419 metadata functions now log errors
# Conflicts: # doc/mongoc_client_set_appname.page # doc/mongoc_metadata_append.page # tests/test-mongoc-metadata.c
1 parent 52a3f66 commit aad984f

8 files changed

+60
-10
lines changed

src/mongoc/mongoc-client.c

+1
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,7 @@ mongoc_client_set_appname (mongoc_client_t *client,
19281928
const char *appname)
19291929
{
19301930
if (!client->topology->single_threaded) {
1931+
MONGOC_ERROR ("Cannot call set_appname on a client from a pool");
19311932
return false;
19321933
}
19331934

src/mongoc/mongoc-metadata.c

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ mongoc_handshake_data_append (const char *driver_name,
287287
int max_size = 0;
288288

289289
if (gMongocMetadata.frozen) {
290+
MONGOC_ERROR ("Cannot set metadata more than once");
290291
return false;
291292
}
292293

src/mongoc/mongoc-topology-scanner.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,12 @@ _mongoc_topology_scanner_set_appname (mongoc_topology_scanner_t *ts,
764764
const char *appname)
765765
{
766766
if (!_mongoc_metadata_appname_is_valid (appname)) {
767+
MONGOC_ERROR ("Cannot set appname: %s is invalid", appname);
767768
return false;
768769
}
769770

770771
if (ts->appname != NULL) {
771-
/* We've already set it */
772+
MONGOC_ERROR ("Cannot set appname more than once");
772773
return false;
773774
}
774775

src/mongoc/mongoc-topology.c

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#endif
2323

2424
#include "mongoc-error.h"
25+
#include "mongoc-log.h"
2526
#include "mongoc-topology-private.h"
2627
#include "mongoc-client-private.h"
2728
#include "mongoc-util-private.h"
@@ -963,6 +964,8 @@ _mongoc_topology_set_appname (mongoc_topology_t *topology,
963964
if (topology->scanner_state == MONGOC_TOPOLOGY_SCANNER_OFF) {
964965
ret = _mongoc_topology_scanner_set_appname (topology->scanner,
965966
appname);
967+
} else {
968+
MONGOC_ERROR ("Cannot set appname after handshake initiated");
966969
}
967970
mongoc_mutex_unlock (&topology->mutex);
968971
return ret;

src/mongoc/mongoc-uri.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ mongoc_uri_parse_option (mongoc_uri_t *uri,
640640
#ifdef MONGOC_EXPERIMENTAL_FEATURES
641641
} else if (!strcasecmp (key, "appname")) {
642642
if (!mongoc_uri_set_appname (uri, value)) {
643-
MONGOC_WARNING ("appname is invalid [appname=%s]", value);
643+
MONGOC_WARNING ("Cannot set appname: %s is invalid", value);
644644
goto CLEANUP;
645645
}
646646
#endif

tests/test-mongoc-client-pool.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ test_mongoc_client_pool_metadata (void)
224224

225225
ASSERT (mongoc_client_pool_set_appname (pool, "some application"));
226226
/* Be sure we can't set it twice */
227+
capture_logs (true);
227228
ASSERT (!mongoc_client_pool_set_appname (pool, "a"));
229+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
230+
MONGOC_LOG_LEVEL_ERROR,
231+
"Cannot set appname more than once");
232+
capture_logs (false);
233+
228234
mongoc_client_pool_destroy (pool);
229235

230236
/* Make sure that after we pop a client we can't set metadata anymore */
@@ -233,13 +239,23 @@ test_mongoc_client_pool_metadata (void)
233239
client = mongoc_client_pool_pop (pool);
234240

235241
/* Be sure a client can't set it now that we've popped them */
242+
capture_logs (true);
236243
ASSERT (!mongoc_client_set_appname (client, "a"));
244+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
245+
MONGOC_LOG_LEVEL_ERROR,
246+
"Cannot call set_appname on a client from a pool");
247+
capture_logs (false);
237248

238249
mongoc_client_pool_push (pool, client);
239250

240251
/* even now that we pushed the client back we shouldn't be able to set
241-
the metadata */
252+
* the metadata */
253+
capture_logs (true);
242254
ASSERT (!mongoc_client_pool_set_appname (pool, "a"));
255+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
256+
MONGOC_LOG_LEVEL_ERROR,
257+
"Cannot set appname after handshake initiated");
258+
capture_logs (false);
243259

244260
mongoc_uri_destroy(uri);
245261
mongoc_client_pool_destroy(pool);

tests/test-mongoc-client.c

+9
Original file line numberDiff line numberDiff line change
@@ -1805,13 +1805,22 @@ test_mongoc_client_application_metadata (void)
18051805
big_string[BUFFER_SIZE - 1] = '\0';
18061806

18071807
/* Check that setting too long a name causes failure */
1808+
capture_logs (true);
18081809
ASSERT (!mongoc_client_set_appname (client, big_string));
1810+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
1811+
MONGOC_LOG_LEVEL_ERROR,
1812+
"is invalid");
1813+
clear_captured_logs ();
18091814

18101815
/* Success case */
18111816
ASSERT (mongoc_client_set_appname (client, short_string));
18121817

18131818
/* Make sure we can't set it twice */
18141819
ASSERT (!mongoc_client_set_appname (client, "a"));
1820+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
1821+
MONGOC_LOG_LEVEL_ERROR,
1822+
"Cannot set appname more than once");
1823+
capture_logs (false);
18151824

18161825
mongoc_client_destroy (client);
18171826
mongoc_uri_destroy (uri);

tests/test-mongoc-metadata.c

+26-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ test_mongoc_metadata_appname_in_uri (void)
4848
const char *good_uri = "mongodb://host/?appname=mongodump";
4949
mongoc_uri_t *uri;
5050
const char *appname = "mongodump";
51+
const char *value;
5152

5253
memset (long_string, 'a', MONGOC_METADATA_APPNAME_MAX + 1);
5354
long_string[MONGOC_METADATA_APPNAME_MAX + 1] = '\0';
@@ -56,22 +57,25 @@ test_mongoc_metadata_appname_in_uri (void)
5657
capture_logs (true);
5758
uri_str = bson_strdup_printf ("mongodb://a/?appname=%s", long_string);
5859
ASSERT (!mongoc_client_new (uri_str));
60+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
61+
MONGOC_LOG_LEVEL_WARNING,
62+
"is invalid");
5963
capture_logs (false);
6064

6165
uri = mongoc_uri_new (good_uri);
6266
ASSERT (uri);
63-
appname = mongoc_uri_get_appname (uri);
64-
ASSERT (appname);
65-
ASSERT (!strcmp (appname, appname));
67+
value = mongoc_uri_get_appname (uri);
68+
ASSERT (value);
69+
ASSERT_CMPSTR (appname, value);
6670
mongoc_uri_destroy (uri);
6771

6872
uri = mongoc_uri_new (NULL);
6973
ASSERT (uri);
7074
ASSERT (!mongoc_uri_set_appname (uri, long_string));
7175
ASSERT (mongoc_uri_set_appname (uri, appname));
72-
appname = mongoc_uri_get_appname (uri);
73-
ASSERT (appname);
74-
ASSERT (!strcmp (appname, appname));
76+
value = mongoc_uri_get_appname (uri);
77+
ASSERT (value);
78+
ASSERT_CMPSTR (appname, value);
7579
mongoc_uri_destroy (uri);
7680

7781
bson_free (uri_str);
@@ -86,7 +90,12 @@ test_mongoc_metadata_appname_frozen_single (void)
8690
client = mongoc_client_new (good_uri);
8791

8892
/* Shouldn't be able to set appname again */
93+
capture_logs (true);
8994
ASSERT (!mongoc_client_set_appname (client, "a"));
95+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
96+
MONGOC_LOG_LEVEL_ERROR,
97+
"Cannot set appname more than once");
98+
capture_logs (false);
9099

91100
mongoc_client_destroy (client);
92101
}
@@ -101,7 +110,12 @@ test_mongoc_metadata_appname_frozen_pooled (void)
101110
uri = mongoc_uri_new (good_uri);
102111

103112
pool = mongoc_client_pool_new (uri);
113+
capture_logs (true);
104114
ASSERT (!mongoc_client_pool_set_appname (pool, "test"));
115+
ASSERT_CAPTURED_LOG ("_mongoc_topology_scanner_set_appname",
116+
MONGOC_LOG_LEVEL_ERROR,
117+
"Cannot set appname more than once");
118+
capture_logs (false);
105119

106120
mongoc_client_pool_destroy (pool);
107121
mongoc_uri_destroy (uri);
@@ -160,7 +174,7 @@ test_mongoc_handshake_data_append_success (void)
160174
ASSERT (bson_iter_find (&inner_iter, "name"));
161175
val = bson_iter_utf8 (&inner_iter, NULL);
162176
ASSERT (val);
163-
ASSERT (!strcmp (val, "testapp"));
177+
ASSERT_CMPSTR (val, "testapp");
164178

165179
/* Make sure driver.name and driver.version and platform are all right */
166180
ASSERT (bson_iter_find (&md_iter, "driver"));
@@ -228,7 +242,12 @@ test_mongoc_handshake_data_append_after_cmd (void)
228242

229243
client = mongoc_client_pool_pop (pool);
230244

245+
capture_logs (true);
231246
ASSERT (!mongoc_handshake_data_append ("a", "a", "a"));
247+
ASSERT_CAPTURED_LOG ("mongoc_metadata_append",
248+
MONGOC_LOG_LEVEL_ERROR,
249+
"Cannot set metadata more than once");
250+
capture_logs (false);
232251

233252
mongoc_client_pool_push (pool, client);
234253

0 commit comments

Comments
 (0)