1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/platform/basic.h"
#include <array>
#include <fmt/format.h>
#include "mongo/bson/bsonelement.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/uuid.h"
namespace mongo {
namespace {
using namespace fmt::literals;
TEST(BSONElement, BinDataToString) {
BSONObjBuilder builder;
unsigned char bintype0[] = {0xDE, 0xEA, 0xBE, 0xEF, 0x01}; // Random BinData shorter than UUID
const UUID validUUID = UUID::gen();
unsigned char zeroUUID[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char overlongUUID[] = {0xBF,
0xF7,
0x1F,
0x75,
0x04,
0x67,
0x45,
0xA4,
0x9A,
0x06,
0xE9,
0xBB,
0x02,
0x72,
0x81,
0x64,
0xff}; // Valid RFC4122v4 UUID, but with extra byte added.
unsigned char zeroLength[1] = {0}; // Not truly zero because Windows doesn't support that.
StringData unknownType = "binary data\000with an unknown type"_sd; // No terminating zero
const BinDataType unknownBinDataType = BinDataType(42);
builder.appendBinData("bintype0", sizeof(bintype0), BinDataGeneral, bintype0);
validUUID.appendToBuilder(&builder, "validUUID");
builder.appendBinData("zeroUUID", sizeof(zeroUUID), newUUID, zeroUUID);
builder.appendBinData("overlongUUID", sizeof(overlongUUID), newUUID, overlongUUID);
builder.appendBinData("zeroLength", 0, BinDataGeneral, zeroLength);
builder.appendBinData(
"unknownType", unknownType.size(), unknownBinDataType, unknownType.rawData());
BSONObj obj = builder.obj();
ASSERT_EQ(obj["bintype0"].toString(), "bintype0: BinData(0, DEEABEEF01)");
ASSERT_EQ(obj["validUUID"].toString(), "validUUID: UUID(\"" + validUUID.toString() + "\")");
ASSERT_EQ(obj["zeroUUID"].toString(),
"zeroUUID: UUID(\"00000000-0000-0000-0000-000000000000\")");
ASSERT_EQ(obj["overlongUUID"].toString(),
"overlongUUID: BinData(4, BFF71F75046745A49A06E9BB02728164FF)");
ASSERT_EQ(obj["zeroLength"].toString(), "zeroLength: BinData(0, )");
ASSERT_EQ(obj["unknownType"].toString(),
"unknownType: BinData(42, "
"62696E6172792064617461007769746820616E20756E6B6E6F776E2074797065)");
}
std::string vecStr(std::vector<uint8_t> v) {
std::string r = "[";
StringData sep;
for (const uint8_t& b : v) {
r += "{}{:02x}"_format(sep, (unsigned)b);
sep = ","_sd;
}
r += "]";
return r;
}
TEST(BSONElement, BinDataVectorWithByteArrayDeprecated) {
// ByteArrayDeprecated has a nonsense 4-byte redundant length field
// that _binDataVector should ignore.
std::vector<uint8_t> payload{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
std::vector<uint8_t> input = payload;
// Insert a 4-byte prefix for the ignored ByteArrayDeprecated "length"
std::array<uint8_t, 4> ignoredPrefix{0xcc, 0xdd, 0xee, 0xff};
input.insert(input.begin(), ignoredPrefix.begin(), ignoredPrefix.end());
ASSERT_EQ(vecStr(BSONObjBuilder{}
.appendBinData("f", input.size(), ByteArrayDeprecated, input.data())
.obj()["f"]
._binDataVector()),
vecStr(payload));
}
TEST(BSONElement, BinDataVectorWithBinDataGeneral) {
std::vector<uint8_t> payload{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
std::vector<uint8_t> input = payload;
ASSERT_EQ(vecStr(BSONObjBuilder{}
.appendBinData("f", input.size(), BinDataGeneral, input.data())
.obj()["f"]
._binDataVector()),
vecStr(payload));
}
TEST(BSONElement, TimestampToString) {
// Testing default BSONObj Timestamp method, which constructs an empty Timestamp
const BSONElement b;
auto ts = b.timestamp();
ASSERT_EQ(ts.toString(), "Timestamp(0, 0)");
BSONObjBuilder builder;
builder.append("ts0", Timestamp(Seconds(100), 1U));
builder.append("ts1", Timestamp(Seconds(50000), 25));
builder.append("ts2", Timestamp(Seconds(100000), 1U));
// Testing max allowable integer values
builder.append("ts3", Timestamp::max());
// Testing for correct format when printing BSONObj Timestamps
// using .toString(includeFieldName = false, full = false)
BSONObj obj = builder.obj();
ASSERT_EQ(obj["ts0"].toString(false, false), "Timestamp(100, 1)");
ASSERT_EQ(obj["ts1"].toString(false, false), "Timestamp(50000, 25)");
ASSERT_EQ(obj["ts2"].toString(false, false), "Timestamp(100000, 1)");
ASSERT_EQ(obj["ts3"].toString(false, false), "Timestamp(4294967295, 4294967295)");
}
TEST(BSONElement, ExtractLargeSubObject) {
std::int32_t size = 17 * 1024 * 1024;
std::vector<char> buffer(size);
DataRange bufferRange(&buffer.front(), &buffer.back());
ASSERT_OK(bufferRange.writeNoThrow(LittleEndian<int32_t>(size)));
BSONObj obj(buffer.data(), BSONObj::LargeSizeTrait{});
BSONObjBuilder bigObjectBuilder;
bigObjectBuilder.append("a", obj);
BSONObj bigObj = bigObjectBuilder.obj<BSONObj::LargeSizeTrait>();
BSONElement element = bigObj["a"];
ASSERT_EQ(BSONType::Object, element.type());
BSONObj subObj = element.Obj();
}
TEST(BSONElement, SafeNumberLongPositiveBound) {
BSONObj obj =
BSON("kLongLongMaxPlusOneAsDouble"
<< BSONElement::kLongLongMaxPlusOneAsDouble << "towardsZero"
<< std::nextafter(BSONElement::kLongLongMaxPlusOneAsDouble, 0.0) << "towardsInfinity"
<< std::nextafter(BSONElement::kLongLongMaxPlusOneAsDouble,
std::numeric_limits<double>::max())
<< "positiveInfinity" << std::numeric_limits<double>::infinity());
// kLongLongMaxPlusOneAsDouble is the least double value that will overflow a 64-bit signed
// two's-complement integer. Historically, converting this value with safeNumberLong() would
// return the result of casting to double with a C-style cast. That operation is undefined
// because of the overflow, but on most platforms we support, it returned the min 64-bit value
// (-2^63). The safeNumberLongForHash() function should preserve that behavior indefinitely for
// compatibility with on-disk data.
ASSERT_EQ(obj["kLongLongMaxPlusOneAsDouble"].safeNumberLongForHash(),
std::numeric_limits<long long>::lowest());
// The safeNumberLong() function clamps kLongLongMaxPlusOneAsDouble to the max 64-bit value
// (2^63 - 1).
ASSERT_EQ(obj["kLongLongMaxPlusOneAsDouble"].safeNumberLong(),
std::numeric_limits<long long>::max());
// One quantum below kLongLongMaxPlusOneAsDouble is the largest double that safely converts to a
// 64-bit signed two-s complement integer. Both safeNumberLong() and safeNumberLongForHash()
// convert this using a C or C-style cast, an operation with defined behavior. This conversion
// is exact.
ASSERT_EQ(obj["towardsZero"].safeNumberLongForHash(), 0x7ffffffffffffc00ll);
ASSERT_EQ(obj["towardsZero"].safeNumberLong(), 0x7ffffffffffffc00ll);
// One quantum above kLongLongMaxPlusOneAsDouble is another number that that is too large to
// convert. The safeNumberLong() function has always clamped this value to the max 64-bit value
// (2^63 - 1), and that should continue to be the behavior for both safeNumberLong() and
// safeNumberLongForHash().
ASSERT_EQ(obj["towardsInfinity"].safeNumberLongForHash(),
std::numeric_limits<long long>::max());
ASSERT_EQ(obj["towardsInfinity"].safeNumberLong(), std::numeric_limits<long long>::max());
// Both safeNumberLong() and safeNumberLongForHash() also clamp positive infinity to the max
// 64-bit value (2^63 - 1).
ASSERT_EQ(obj["positiveInfinity"].safeNumberLongForHash(),
std::numeric_limits<long long>::max());
ASSERT_EQ(obj["positiveInfinity"].safeNumberLong(), std::numeric_limits<long long>::max());
}
TEST(BSONElement, SafeNumberLongNegativeBound) {
// Unlike the max long long value, the least long long value (-2^63) converts exactly to a
// double value and can safely be used as a bound to check which double values are in the range
// of long long.
const double lowestLongLongAsDouble =
static_cast<double>(std::numeric_limits<long long>::lowest());
BSONObj obj =
BSON("lowestLongLongAsDouble" // This comment forces clang-format to break here.
<< lowestLongLongAsDouble << "towardsZero"
<< std::nextafter(lowestLongLongAsDouble, 0.0) << "towardsNegativeInfinity"
<< std::nextafter(lowestLongLongAsDouble, std::numeric_limits<double>::lowest())
<< "negativeInfinity" << -std::numeric_limits<double>::infinity());
ASSERT_EQ(obj["lowestLongLongAsDouble"].safeNumberLongForHash(),
std::numeric_limits<long long>::lowest());
ASSERT_EQ(obj["lowestLongLongAsDouble"].safeNumberLong(),
std::numeric_limits<long long>::lowest());
ASSERT_EQ(obj["towardsZero"].safeNumberLongForHash(), -0x7ffffffffffffc00);
ASSERT_EQ(obj["towardsZero"].safeNumberLong(), -0x7ffffffffffffc00);
ASSERT_EQ(obj["towardsNegativeInfinity"].safeNumberLongForHash(),
std::numeric_limits<long long>::lowest());
ASSERT_EQ(obj["towardsNegativeInfinity"].safeNumberLong(),
std::numeric_limits<long long>::lowest());
ASSERT_EQ(obj["negativeInfinity"].safeNumberLongForHash(),
std::numeric_limits<long long>::lowest());
ASSERT_EQ(obj["negativeInfinity"].safeNumberLong(), std::numeric_limits<long long>::lowest());
}
TEST(BSONElement, SafeNumberDoublePositiveBound) {
BSONObj obj = BSON("kLargestSafeLongLongAsDouble"
<< BSONElement::kLargestSafeLongLongAsDouble << "towardsZero"
<< BSONElement::kLargestSafeLongLongAsDouble - 1 << "towardsInfinity"
<< BSONElement::kLargestSafeLongLongAsDouble + 1 << "positiveInfinity"
<< std::numeric_limits<long long>::max());
ASSERT_EQ(obj["kLargestSafeLongLongAsDouble"].safeNumberDouble(),
(double)BSONElement::kLargestSafeLongLongAsDouble);
ASSERT_EQ(obj["towardsZero"].safeNumberDouble(),
(double)(BSONElement::kLargestSafeLongLongAsDouble - 1));
ASSERT_EQ(obj["towardsInfinity"].safeNumberDouble(),
(double)BSONElement::kLargestSafeLongLongAsDouble);
ASSERT_EQ(obj["positiveInfinity"].safeNumberDouble(),
(double)BSONElement::kLargestSafeLongLongAsDouble);
}
TEST(BSONElement, SafeNumberDoubleNegativeBound) {
BSONObj obj =
BSON("kSmallestSafeLongLongAsDouble"
<< BSONElement::kSmallestSafeLongLongAsDouble << "towardsZero"
<< BSONElement::kSmallestSafeLongLongAsDouble + 1 << "towardsNegativeInfinity"
<< BSONElement::kSmallestSafeLongLongAsDouble - 1 << "negativeInfinity"
<< std::numeric_limits<long long>::min());
ASSERT_EQ(obj["kSmallestSafeLongLongAsDouble"].safeNumberDouble(),
(double)BSONElement::kSmallestSafeLongLongAsDouble);
ASSERT_EQ(obj["towardsZero"].safeNumberDouble(),
(double)(BSONElement::kSmallestSafeLongLongAsDouble + 1));
ASSERT_EQ(obj["towardsNegativeInfinity"].safeNumberDouble(),
(double)BSONElement::kSmallestSafeLongLongAsDouble);
ASSERT_EQ(obj["negativeInfinity"].safeNumberDouble(),
(double)BSONElement::kSmallestSafeLongLongAsDouble);
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongRejectsNegative) {
BSONObj query = BSON("" << -2LL);
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToNonNegativeLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongAcceptsNegative) {
BSONObj query = BSON("" << -2LL);
auto result = query.firstElement().parseIntegerElementToLong();
ASSERT_OK(result.getStatus());
ASSERT_EQ(-2LL, result.getValue());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongRejectsTooLargeDouble) {
BSONObj query = BSON("" << BSONElement::kLongLongMaxPlusOneAsDouble);
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToNonNegativeLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongRejectsTooLargeDouble) {
BSONObj query = BSON("" << BSONElement::kLongLongMaxPlusOneAsDouble);
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongRejectsTooLargeNegativeDouble) {
BSONObj query = BSON("" << std::numeric_limits<double>::min());
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongRejectsString) {
BSONObj query = BSON(""
<< "1");
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToNonNegativeLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongRejectsString) {
BSONObj query = BSON(""
<< "1");
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongRejectsNonIntegralDouble) {
BSONObj query = BSON("" << 2.5);
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToNonNegativeLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongRejectsNonIntegralDouble) {
BSONObj query = BSON("" << 2.5);
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongRejectsNonIntegralDecimal) {
BSONObj query = BSON("" << Decimal128("2.5"));
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToNonNegativeLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongRejectsNonIntegralDecimal) {
BSONObj query = BSON("" << Decimal128("2.5"));
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongRejectsLargestDecimal) {
BSONObj query = BSON("" << Decimal128(Decimal128::kLargestPositive));
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToNonNegativeLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongRejectsLargestDecimal) {
BSONObj query = BSON("" << Decimal128(Decimal128::kLargestPositive));
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToLong());
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongAcceptsZero) {
BSONObj query = BSON("" << 0);
auto result = query.firstElement().parseIntegerElementToNonNegativeLong();
ASSERT_OK(result.getStatus());
ASSERT_EQ(result.getValue(), 0LL);
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongAcceptsZero) {
BSONObj query = BSON("" << 0);
auto result = query.firstElement().parseIntegerElementToLong();
ASSERT_OK(result.getStatus());
ASSERT_EQ(result.getValue(), 0LL);
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongAcceptsThree) {
BSONObj query = BSON("" << 3.0);
auto result = query.firstElement().parseIntegerElementToNonNegativeLong();
ASSERT_OK(result.getStatus());
ASSERT_EQ(result.getValue(), 3LL);
}
TEST(BSONElementIntegerParseTest, ParseIntegerElementToLongAcceptsThree) {
BSONObj query = BSON("" << 3.0);
auto result = query.firstElement().parseIntegerElementToLong();
ASSERT_OK(result.getStatus());
ASSERT_EQ(result.getValue(), 3LL);
}
} // namespace
} // namespace mongo
|