Skip to content

Commit 9237aed

Browse files
committed
Update mock servers to forbid max_wire_version < 6
1 parent 04dd838 commit 9237aed

File tree

2 files changed

+97
-99
lines changed

2 files changed

+97
-99
lines changed

src/libmongoc/tests/mock_server/mock-rs.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
#include "mock-rs.h"
2424
#include "sync-queue.h"
25-
#include "../test-libmongoc.h"
25+
#include "test-libmongoc.h"
26+
#include "TestSuite.h"
2627

2728

2829
struct _mock_rs_t {
@@ -114,31 +115,17 @@ hello_json (mock_rs_t *rs,
114115
const bson_t *tags)
115116
{
116117
char *server_type;
117-
char *mongos_36_fields = "";
118118
char *tags_json;
119119
char *hosts_str;
120120
char *json;
121121

122122
if (type == MONGOC_SERVER_RS_PRIMARY) {
123-
server_type = "'isWritablePrimary': true, 'secondary': false, ";
123+
server_type = "'isWritablePrimary': true, 'secondary': false,";
124124
} else if (type == MONGOC_SERVER_RS_SECONDARY) {
125-
server_type = "'isWritablePrimary': false, 'secondary': true, ";
125+
server_type = "'isWritablePrimary': false, 'secondary': true,";
126126
} else {
127127
BSON_ASSERT (type == MONGOC_SERVER_RS_ARBITER);
128-
server_type = "'isWritablePrimary': false, 'arbiterOnly': true, ";
129-
}
130-
131-
if (rs->max_wire_version >= WIRE_VERSION_OP_MSG) {
132-
mongos_36_fields =
133-
"'$clusterTime': {"
134-
" 'clusterTime': {'$timestamp': {'t': 1, 'i': 1}},"
135-
" 'signature': {"
136-
" 'hash': {'$binary': {'subType': '0', 'base64': ''}},"
137-
" 'keyId': {'$numberLong': '6446735049323708417'}"
138-
" },"
139-
" 'operationTime': {'$timestamp': {'t': 1, 'i': 1}}"
140-
"}, "
141-
"'logicalSessionTimeoutMinutes': 30, ";
128+
server_type = "'isWritablePrimary': false, 'arbiterOnly': true,";
142129
}
143130

144131
if (bson_empty0 (tags)) {
@@ -149,14 +136,28 @@ hello_json (mock_rs_t *rs,
149136

150137
hosts_str = hosts (&rs->servers);
151138

152-
json = bson_strdup_printf ("{'ok': 1, %s %s 'tags': %s,"
153-
" 'maxWireVersion': %d, "
154-
"'setName': 'rs', 'hosts': [%s]}",
155-
server_type,
156-
mongos_36_fields,
157-
tags_json,
158-
rs->max_wire_version,
159-
hosts_str);
139+
json = bson_strdup_printf (
140+
"{'ok': 1,"
141+
" %s"
142+
" '$clusterTime': {"
143+
" 'clusterTime': {'$timestamp': {'t': 1, 'i': 1}},"
144+
" 'signature': {"
145+
" 'hash': {'$binary': {'subType': '0', 'base64': ''}},"
146+
" 'keyId': {'$numberLong': '6446735049323708417'}"
147+
" },"
148+
" 'operationTime': {'$timestamp': {'t': 1, 'i': 1}}"
149+
" },"
150+
"'logicalSessionTimeoutMinutes': 30,"
151+
" 'tags': %s,"
152+
" 'minWireVersion': %d,"
153+
" 'maxWireVersion': %d,"
154+
" 'setName': 'rs',"
155+
" 'hosts': [%s]}",
156+
server_type,
157+
tags_json,
158+
WIRE_VERSION_MIN,
159+
rs->max_wire_version,
160+
hosts_str);
160161

161162
bson_free (tags_json);
162163
bson_free (hosts_str);
@@ -212,6 +213,12 @@ mock_rs_with_auto_hello (int32_t max_wire_version,
212213
int i;
213214
mock_rs_t *rs = (mock_rs_t *) bson_malloc0 (sizeof (mock_rs_t));
214215

216+
ASSERT_WITH_MSG (max_wire_version >= WIRE_VERSION_MIN,
217+
"max_wire_version %" PRId32
218+
" must be greater than or equal to minimum wire version %d",
219+
max_wire_version,
220+
WIRE_VERSION_MIN);
221+
215222
rs->max_wire_version = max_wire_version;
216223
rs->has_primary = has_primary;
217224
rs->n_secondaries = n_secondaries;
@@ -394,9 +401,7 @@ mock_rs_run (mock_rs_t *rs)
394401

395402
bson_free (hello);
396403

397-
if (rs->max_wire_version >= WIRE_VERSION_OP_MSG) {
398-
mock_rs_auto_endsessions (rs);
399-
}
404+
mock_rs_auto_endsessions (rs);
400405
}
401406

402407

src/libmongoc/tests/mock_server/mock-server.c

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,19 @@ mock_server_with_auto_hello (int32_t max_wire_version)
169169
{
170170
mock_server_t *server = mock_server_new ();
171171

172-
char *hello = bson_strdup_printf ("{'ok': 1.0,"
173-
" 'isWritablePrimary': true,"
174-
" 'minWireVersion': 0,"
175-
" 'maxWireVersion': %d}",
176-
max_wire_version);
177-
178-
BSON_ASSERT (max_wire_version > 0);
179-
mock_server_auto_hello (server, hello);
180-
181-
bson_free (hello);
172+
ASSERT_WITH_MSG (max_wire_version >= WIRE_VERSION_MIN,
173+
"max_wire_version %" PRId32
174+
" must be greater than or equal to minimum wire version %d",
175+
max_wire_version,
176+
WIRE_VERSION_MIN);
177+
178+
mock_server_auto_hello (server,
179+
"{'ok': 1.0,"
180+
" 'isWritablePrimary': true,"
181+
" 'minWireVersion': %d,"
182+
" 'maxWireVersion': %d}",
183+
WIRE_VERSION_MIN,
184+
max_wire_version);
182185

183186
return server;
184187
}
@@ -205,36 +208,30 @@ mock_server_t *
205208
mock_mongos_new (int32_t max_wire_version)
206209
{
207210
mock_server_t *server = mock_server_new ();
208-
char *mongos_36_fields = "";
209-
char *hello;
210-
211-
if (max_wire_version >= WIRE_VERSION_OP_MSG) {
212-
mongos_36_fields =
213-
","
214-
"'$clusterTime': {"
215-
" 'clusterTime': {'$timestamp': {'t': 1, 'i': 1}},"
216-
" 'signature': {"
217-
" 'hash': {'$binary': {'subType': '0', 'base64': ''}},"
218-
" 'keyId': {'$numberLong': '6446735049323708417'}"
219-
" },"
220-
" 'operationTime': {'$timestamp': {'t': 1, 'i': 1}}"
221-
"},"
222-
"'logicalSessionTimeoutMinutes': 30";
223-
}
224-
225-
hello = bson_strdup_printf ("{'ok': 1.0,"
226-
" 'isWritablePrimary': true,"
227-
" 'msg': 'isdbgrid',"
228-
" 'minWireVersion': 2,"
229-
" 'maxWireVersion': %d"
230-
" %s}",
231-
max_wire_version,
232-
mongos_36_fields);
233-
234-
BSON_ASSERT (max_wire_version > 0);
235-
mock_server_auto_hello (server, hello);
236-
237-
bson_free (hello);
211+
212+
ASSERT_WITH_MSG (max_wire_version >= WIRE_VERSION_MIN,
213+
"max_wire_version %" PRId32
214+
" must be greater than or equal to minimum wire version %d",
215+
max_wire_version,
216+
WIRE_VERSION_MIN);
217+
218+
mock_server_auto_hello (
219+
server,
220+
"{'ok': 1.0,"
221+
" 'isWritablePrimary': true,"
222+
" 'msg': 'isdbgrid',"
223+
" 'minWireVersion': %d,"
224+
" 'maxWireVersion': %d,"
225+
" '$clusterTime': {"
226+
" 'clusterTime': {'$timestamp': {'t': 1, 'i': 1}},"
227+
" 'signature': {"
228+
" 'hash': {'$binary': {'subType': '0', 'base64': ''}},"
229+
" 'keyId': {'$numberLong': '6446735049323708417'}"
230+
" },"
231+
" 'operationTime': {'$timestamp': {'t': 1, 'i': 1}}},"
232+
" 'logicalSessionTimeoutMinutes': 30}",
233+
WIRE_VERSION_MIN,
234+
max_wire_version);
238235

239236
return server;
240237
}
@@ -553,7 +550,7 @@ auto_hello (request_t *request, void *data)
553550
BSON_APPEND_INT32 (&response, "minWireVersion", WIRE_VERSION_MIN);
554551
}
555552
if (!bson_iter_init_find (&iter, &response, "maxWireVersion")) {
556-
BSON_APPEND_INT32 (&response, "maxWireVersion", WIRE_VERSION_OP_MSG - 1);
553+
BSON_APPEND_INT32 (&response, "maxWireVersion", WIRE_VERSION_MAX);
557554
}
558555

559556
response_json = bson_as_json (&response, 0);
@@ -2125,8 +2122,12 @@ rs_response_to_hello (
21252122
bson_string_t *hosts;
21262123
bool first;
21272124
mock_server_t *host;
2128-
const char *session_timeout;
2129-
char *hello;
2125+
2126+
ASSERT_WITH_MSG (max_wire_version >= WIRE_VERSION_MIN,
2127+
"max_wire_version %" PRId32
2128+
" must be greater than or equal to minimum wire version %d",
2129+
max_wire_version,
2130+
WIRE_VERSION_MIN);
21302131

21312132
hosts = bson_string_new ("");
21322133

@@ -2146,32 +2147,24 @@ rs_response_to_hello (
21462147

21472148
va_end (ap);
21482149

2149-
if (max_wire_version >= 6) {
2150-
session_timeout = ", 'logicalSessionTimeoutMinutes': 30";
2151-
mock_server_auto_endsessions (server);
2152-
} else {
2153-
session_timeout = "";
2154-
}
2155-
2156-
hello = bson_strdup_printf ("{'ok': 1, "
2157-
" 'setName': 'rs',"
2158-
" 'isWritablePrimary': %s,"
2159-
" 'secondary': %s,"
2160-
" 'tags': {%s},"
2161-
" 'minWireVersion': 3,"
2162-
" 'maxWireVersion': %d,"
2163-
" 'hosts': [%s]"
2164-
" %s"
2165-
"}",
2166-
primary ? "true" : "false",
2167-
primary ? "false" : "true",
2168-
has_tags ? "'key': 'value'" : "",
2169-
max_wire_version,
2170-
hosts->str,
2171-
session_timeout);
2172-
2173-
mock_server_auto_hello (server, "%s", hello);
2174-
2175-
bson_free (hello);
2150+
mock_server_auto_endsessions (server);
2151+
2152+
mock_server_auto_hello (server,
2153+
"{'ok': 1, "
2154+
" 'setName': 'rs',"
2155+
" 'isWritablePrimary': %s,"
2156+
" 'secondary': %s,"
2157+
" 'tags': {%s},"
2158+
" 'minWireVersion': %d,"
2159+
" 'maxWireVersion': %d,"
2160+
" 'hosts': [%s],"
2161+
" 'logicalSessionTimeoutMinutes': 30}",
2162+
primary ? "true" : "false",
2163+
primary ? "false" : "true",
2164+
has_tags ? "'key': 'value'" : "",
2165+
WIRE_VERSION_MIN,
2166+
max_wire_version,
2167+
hosts->str);
2168+
21762169
bson_string_free (hosts, true);
21772170
}

0 commit comments

Comments
 (0)