Skip to content

Commit d1c74ab

Browse files
committed
CDRIVER-1779 fam+collation should error if max_wire_version<5
1 parent 3e34af3 commit d1c74ab

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,14 +2503,15 @@ mongoc_collection_find_and_modify_with_opts (mongoc_collection_t
25032503
}
25042504
}
25052505

2506-
if (!bson_concat (&command, &opts->extra)) {
2507-
bson_set_error (error,
2508-
MONGOC_ERROR_COMMAND,
2509-
MONGOC_ERROR_COMMAND_INVALID_ARG,
2510-
"mongoc_find_and_modify_opts_t.extra is corrupt.");
2511-
bson_destroy (&command);
2512-
mongoc_server_stream_cleanup (server_stream);
2513-
RETURN (false);
2506+
if (bson_iter_init (&iter, &opts->extra)) {
2507+
bool ok = _mongoc_client_command_append_iterator_opts_to_command (
2508+
&iter, server_stream->sd->max_wire_version, &command, error
2509+
);
2510+
if (!ok) {
2511+
bson_destroy (&command);
2512+
mongoc_server_stream_cleanup (server_stream);
2513+
RETURN (false);
2514+
}
25142515
}
25152516

25162517
ret = mongoc_cluster_run_command_monitored (cluster, server_stream,

tests/test-mongoc-find-and-modify.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,76 @@ test_find_and_modify_opts (void)
380380
}
381381

382382

383+
static void
384+
test_find_and_modify_collation (int wire)
385+
{
386+
mock_server_t *server;
387+
mongoc_client_t *client;
388+
mongoc_collection_t *collection;
389+
bson_error_t error;
390+
mongoc_find_and_modify_opts_t *opts;
391+
future_t *future;
392+
request_t *request;
393+
bson_t *collation;
394+
395+
server = mock_server_with_autoismaster (wire);
396+
mock_server_run (server);
397+
398+
client = mongoc_client_new_from_uri (mock_server_get_uri (server));
399+
collection = mongoc_client_get_collection (client, "db", "collection");
400+
401+
402+
collation = BCON_NEW ("collation", "{",
403+
"locale", BCON_UTF8 ("en_US"),
404+
"caseFirst", BCON_UTF8 ("lower"),
405+
"}");
406+
opts = mongoc_find_and_modify_opts_new ();
407+
mongoc_find_and_modify_opts_append (opts, collation);
408+
409+
if (wire >= WIRE_VERSION_COLLATION) {
410+
future = future_collection_find_and_modify_with_opts (
411+
collection, tmp_bson ("{}"), opts, NULL, &error);
412+
413+
request = mock_server_receives_command (
414+
server, "db", MONGOC_QUERY_NONE,
415+
"{'findAndModify': 'collection',"
416+
" 'collation': { 'locale': 'en_US', 'caseFirst': 'lower'}"
417+
"}");
418+
mock_server_replies_ok_and_destroys (request);
419+
ASSERT_OR_PRINT (future_get_bool (future), error);
420+
future_destroy (future);
421+
} else {
422+
bool ok = mongoc_collection_find_and_modify_with_opts (
423+
collection, tmp_bson ("{}"), opts, NULL, &error);
424+
425+
ASSERT_ERROR_CONTAINS (error,
426+
MONGOC_ERROR_COMMAND,
427+
MONGOC_ERROR_COMMAND_INVALID_ARG,
428+
"The selected server does not support collation");
429+
ASSERT (!ok);
430+
}
431+
432+
bson_destroy (collation);
433+
mongoc_find_and_modify_opts_destroy (opts);
434+
mongoc_collection_destroy (collection);
435+
mongoc_client_destroy (client);
436+
437+
mock_server_destroy (server);
438+
}
439+
440+
static void
441+
test_find_and_modify_collation_ok (void)
442+
{
443+
test_find_and_modify_collation (WIRE_VERSION_COLLATION);
444+
}
445+
446+
447+
static void
448+
test_find_and_modify_collation_fail (void)
449+
{
450+
test_find_and_modify_collation (WIRE_VERSION_COLLATION-1);
451+
}
452+
383453
void
384454
test_find_and_modify_install (TestSuite *suite)
385455
{
@@ -396,4 +466,6 @@ test_find_and_modify_install (TestSuite *suite)
396466
test_find_and_modify_write_concern_wire_32_failure, NULL, NULL,
397467
should_run_fam_wc);
398468
TestSuite_Add (suite, "/find_and_modify/opts", test_find_and_modify_opts);
469+
TestSuite_Add (suite, "/find_and_modify/collation/ok", test_find_and_modify_collation_ok);
470+
TestSuite_Add (suite, "/find_and_modify/collation/fail", test_find_and_modify_collation_fail);
399471
}

0 commit comments

Comments
 (0)