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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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 <map>
#include <string>
#include <vector>
#include "mongo/db/field_parser.h"
#include "mongo/db/jsobj.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/time_support.h"
namespace {
using mongo::BSONArray;
using mongo::BSONField;
using mongo::BSONObj;
using mongo::BSONObjBuilder;
using mongo::Date_t;
using mongo::FieldParser;
using mongo::OID;
using std::map;
using std::string;
using std::vector;
class ExtractionFixture : public mongo::unittest::Test {
protected:
BSONObj doc;
bool valBool;
BSONArray valArray;
BSONObj valObj;
Date_t valDate;
string valString;
OID valOID;
long long valLong;
static BSONField<bool> aBool;
static BSONField<BSONArray> anArray;
static BSONField<BSONObj> anObj;
static BSONField<Date_t> aDate;
static BSONField<string> aString;
static BSONField<OID> anOID;
static BSONField<long long> aLong;
void setUp() {
valBool = true;
valArray = BSON_ARRAY(1 << 2 << 3);
valObj = BSON("a" << 1);
valDate = Date_t::fromMillisSinceEpoch(1);
valString = "a string";
valOID = OID::gen();
valLong = 1LL;
doc = BSON(aBool(valBool) << anArray(valArray) << anObj(valObj) << aDate(valDate)
<< aString(valString)
<< anOID(valOID)
<< aLong(valLong));
}
void tearDown() {}
};
BSONField<bool> ExtractionFixture::aBool("aBool");
BSONField<BSONArray> ExtractionFixture::anArray("anArray");
BSONField<BSONObj> ExtractionFixture::anObj("anObj");
BSONField<Date_t> ExtractionFixture::aDate("aDate");
BSONField<string> ExtractionFixture::aString("aString");
BSONField<OID> ExtractionFixture::anOID("anOID");
BSONField<long long> ExtractionFixture::aLong("aLong");
TEST_F(ExtractionFixture, GetBool) {
BSONField<bool> notThere("otherBool", true);
BSONField<bool> wrongType(anObj.name());
bool val;
ASSERT_TRUE(FieldParser::extract(doc, aBool, &val));
ASSERT_EQUALS(val, valBool);
ASSERT_TRUE(FieldParser::extract(doc, notThere, &val));
ASSERT_EQUALS(val, true);
ASSERT_FALSE(FieldParser::extract(doc, wrongType, &val));
}
TEST_F(ExtractionFixture, GetBSONArray) {
BSONField<BSONArray> notThere("otherArray",
BSON_ARRAY("a"
<< "b"));
BSONField<BSONArray> wrongType(aString.name());
BSONArray val;
ASSERT_TRUE(FieldParser::extract(doc, anArray, &val));
ASSERT_BSONOBJ_EQ(val, valArray);
ASSERT_TRUE(FieldParser::extract(doc, notThere, &val));
ASSERT_BSONOBJ_EQ(val,
BSON_ARRAY("a"
<< "b"));
ASSERT_FALSE(FieldParser::extract(doc, wrongType, &val));
}
TEST_F(ExtractionFixture, GetBSONObj) {
BSONField<BSONObj> notThere("otherObj", BSON("b" << 1));
BSONField<BSONObj> wrongType(aString.name());
BSONObj val;
ASSERT_TRUE(FieldParser::extract(doc, anObj, &val));
ASSERT_BSONOBJ_EQ(val, valObj);
ASSERT_TRUE(FieldParser::extract(doc, notThere, &val));
ASSERT_BSONOBJ_EQ(val, BSON("b" << 1));
ASSERT_FALSE(FieldParser::extract(doc, wrongType, &val));
}
TEST_F(ExtractionFixture, GetDate) {
BSONField<Date_t> notThere("otherDate", Date_t::fromMillisSinceEpoch(99));
BSONField<Date_t> wrongType(aString.name());
Date_t val;
ASSERT_TRUE(FieldParser::extract(doc, aDate, &val));
ASSERT_EQUALS(val, valDate);
ASSERT_TRUE(FieldParser::extract(doc, notThere, &val));
ASSERT_EQUALS(val, Date_t::fromMillisSinceEpoch(99));
ASSERT_FALSE(FieldParser::extract(doc, wrongType, &val));
}
TEST_F(ExtractionFixture, GetString) {
BSONField<string> notThere("otherString", "abc");
BSONField<string> wrongType(aBool.name());
string val;
ASSERT_TRUE(FieldParser::extract(doc, aString, &val));
ASSERT_EQUALS(val, valString);
ASSERT_TRUE(FieldParser::extract(doc, notThere, &val));
ASSERT_EQUALS(val, "abc");
ASSERT_FALSE(FieldParser::extract(doc, wrongType, &val));
}
TEST_F(ExtractionFixture, GetOID) {
OID defOID = OID::gen();
BSONField<OID> notThere("otherOID", defOID);
BSONField<OID> wrongType(aString.name());
OID val;
ASSERT_TRUE(FieldParser::extract(doc, anOID, &val));
ASSERT_EQUALS(val, valOID);
ASSERT_TRUE(FieldParser::extract(doc, notThere, &val));
ASSERT_EQUALS(val, defOID);
ASSERT_FALSE(FieldParser::extract(doc, wrongType, &val));
}
TEST_F(ExtractionFixture, GetLong) {
BSONField<long long> notThere("otherLong", 0);
BSONField<long long> wrongType(aString.name());
long long val;
ASSERT_TRUE(FieldParser::extract(doc, aLong, &val));
ASSERT_EQUALS(val, valLong);
ASSERT_TRUE(FieldParser::extract(doc, notThere, &val));
ASSERT_EQUALS(val, 0);
ASSERT_FALSE(FieldParser::extract(doc, wrongType, &val));
}
TEST_F(ExtractionFixture, IsFound) {
bool bool_val;
BSONField<bool> aBoolMissing("aBoolMissing");
ASSERT_EQUALS(FieldParser::extract(doc, aBool, &bool_val, NULL), FieldParser::FIELD_SET);
ASSERT_EQUALS(FieldParser::extract(doc, aBoolMissing, &bool_val, NULL),
FieldParser::FIELD_NONE);
Date_t Date_t_val;
BSONField<Date_t> aDateMissing("aDateMissing");
ASSERT_EQUALS(FieldParser::extract(doc, aDate, &Date_t_val, NULL), FieldParser::FIELD_SET);
ASSERT_EQUALS(FieldParser::extract(doc, aDateMissing, &Date_t_val, NULL),
FieldParser::FIELD_NONE);
string string_val;
BSONField<string> aStringMissing("aStringMissing");
ASSERT_EQUALS(FieldParser::extract(doc, aString, &string_val, NULL), FieldParser::FIELD_SET);
ASSERT_EQUALS(FieldParser::extract(doc, aStringMissing, &string_val, NULL),
FieldParser::FIELD_NONE);
OID OID_val;
BSONField<OID> anOIDMissing("anOIDMissing");
ASSERT_EQUALS(FieldParser::extract(doc, anOID, &OID_val, NULL), FieldParser::FIELD_SET);
ASSERT_EQUALS(FieldParser::extract(doc, anOIDMissing, &OID_val, NULL), FieldParser::FIELD_NONE);
long long long_long_val;
BSONField<long long> aLongMissing("aLongMissing");
ASSERT_EQUALS(FieldParser::extract(doc, aLong, &long_long_val, NULL), FieldParser::FIELD_SET);
ASSERT_EQUALS(FieldParser::extract(doc, aLongMissing, &long_long_val, NULL),
FieldParser::FIELD_NONE);
}
TEST(ComplexExtraction, GetStringVector) {
// Test valid string vector extraction
BSONField<vector<string>> vectorField("testVector");
BSONObjBuilder bob;
bob << vectorField() << BSON_ARRAY("a"
<< "b"
<< "c");
BSONObj obj = bob.obj();
vector<string> parsedVector;
ASSERT(FieldParser::extract(obj, vectorField, &parsedVector));
ASSERT_EQUALS("a", parsedVector[0]);
ASSERT_EQUALS("b", parsedVector[1]);
ASSERT_EQUALS("c", parsedVector[2]);
ASSERT_EQUALS(parsedVector.size(), static_cast<size_t>(3));
}
TEST(ComplexExtraction, GetObjectVector) {
// Test valid BSONObj vector extraction
BSONField<vector<BSONObj>> vectorField("testVector");
BSONObjBuilder bob;
bob << vectorField() << BSON_ARRAY(BSON("a" << 1) << BSON("b" << 1) << BSON("c" << 1));
BSONObj obj = bob.obj();
vector<BSONObj> parsedVector;
ASSERT(FieldParser::extract(obj, vectorField, &parsedVector));
ASSERT_BSONOBJ_EQ(BSON("a" << 1), parsedVector[0]);
ASSERT_BSONOBJ_EQ(BSON("b" << 1), parsedVector[1]);
ASSERT_BSONOBJ_EQ(BSON("c" << 1), parsedVector[2]);
ASSERT_EQUALS(parsedVector.size(), static_cast<size_t>(3));
}
TEST(ComplexExtraction, GetBadVector) {
// Test invalid vector extraction
BSONField<vector<BSONObj>> vectorField("testVector");
BSONObjBuilder bob;
bob << vectorField() << BSON_ARRAY(BSON("a" << 1) << "XXX" << BSON("c" << 1));
BSONObj obj = bob.obj();
vector<BSONObj> parsedVector;
string errMsg;
ASSERT(!FieldParser::extract(obj, vectorField, &parsedVector, &errMsg));
ASSERT_NOT_EQUALS(errMsg, "");
}
TEST(ComplexExtraction, RoundTripVector) {
// Test vector extraction after re-writing to BSON
BSONField<vector<string>> vectorField("testVector");
BSONObj obj;
{
BSONObjBuilder bob;
bob << vectorField() << BSON_ARRAY("a"
<< "b"
<< "c");
obj = bob.obj();
}
vector<string> parsedVector;
ASSERT(FieldParser::extract(obj, vectorField, &parsedVector));
{
BSONObjBuilder bob;
bob.append(vectorField(), parsedVector);
obj = bob.obj();
}
parsedVector.clear();
ASSERT(FieldParser::extract(obj, vectorField, &parsedVector));
ASSERT_EQUALS("a", parsedVector[0]);
ASSERT_EQUALS("b", parsedVector[1]);
ASSERT_EQUALS("c", parsedVector[2]);
ASSERT_EQUALS(parsedVector.size(), static_cast<size_t>(3));
}
TEST(ComplexExtraction, GetStringMap) {
// Test valid string->string map extraction
BSONField<map<string, string>> mapField("testMap");
BSONObjBuilder bob;
bob << mapField() << BSON("a"
<< "a"
<< "b"
<< "b"
<< "c"
<< "c");
BSONObj obj = bob.obj();
map<string, string> parsedMap;
ASSERT(FieldParser::extract(obj, mapField, &parsedMap));
ASSERT_EQUALS("a", parsedMap["a"]);
ASSERT_EQUALS("b", parsedMap["b"]);
ASSERT_EQUALS("c", parsedMap["c"]);
ASSERT_EQUALS(parsedMap.size(), static_cast<size_t>(3));
}
TEST(ComplexExtraction, GetObjectMap) {
// Test valid string->BSONObj map extraction
BSONField<map<string, BSONObj>> mapField("testMap");
BSONObjBuilder bob;
bob << mapField() << BSON("a" << BSON("a"
<< "a")
<< "b"
<< BSON("b"
<< "b")
<< "c"
<< BSON("c"
<< "c"));
BSONObj obj = bob.obj();
map<string, BSONObj> parsedMap;
ASSERT(FieldParser::extract(obj, mapField, &parsedMap));
ASSERT_BSONOBJ_EQ(BSON("a"
<< "a"),
parsedMap["a"]);
ASSERT_BSONOBJ_EQ(BSON("b"
<< "b"),
parsedMap["b"]);
ASSERT_BSONOBJ_EQ(BSON("c"
<< "c"),
parsedMap["c"]);
ASSERT_EQUALS(parsedMap.size(), static_cast<size_t>(3));
}
TEST(ComplexExtraction, GetBadMap) {
// Test invalid map extraction
BSONField<map<string, string>> mapField("testMap");
BSONObjBuilder bob;
bob << mapField() << BSON("a"
<< "a"
<< "b"
<< 123
<< "c"
<< "c");
BSONObj obj = bob.obj();
map<string, string> parsedMap;
string errMsg;
ASSERT(!FieldParser::extract(obj, mapField, &parsedMap, &errMsg));
ASSERT_NOT_EQUALS(errMsg, "");
}
TEST(ComplexExtraction, RoundTripMap) {
// Test map extraction after re-writing to BSON
BSONField<map<string, string>> mapField("testMap");
BSONObj obj;
{
BSONObjBuilder bob;
bob << mapField() << BSON("a"
<< "a"
<< "b"
<< "b"
<< "c"
<< "c");
obj = bob.obj();
}
map<string, string> parsedMap;
ASSERT(FieldParser::extract(obj, mapField, &parsedMap));
{
BSONObjBuilder bob;
bob.append(mapField(), parsedMap);
obj = bob.obj();
}
parsedMap.clear();
ASSERT(FieldParser::extract(obj, mapField, &parsedMap));
ASSERT_EQUALS("a", parsedMap["a"]);
ASSERT_EQUALS("b", parsedMap["b"]);
ASSERT_EQUALS("c", parsedMap["c"]);
ASSERT_EQUALS(parsedMap.size(), static_cast<size_t>(3));
}
TEST(ComplexExtraction, GetNestedMap) {
// Test extraction of complex nested vector and map
BSONField<vector<map<string, string>>> nestedField("testNested");
BSONObj nestedMapObj = BSON("a"
<< "a"
<< "b"
<< "b"
<< "c"
<< "c");
BSONObjBuilder bob;
bob << nestedField() << BSON_ARRAY(nestedMapObj << nestedMapObj << nestedMapObj);
BSONObj obj = bob.obj();
vector<map<string, string>> parsed;
ASSERT(FieldParser::extract(obj, nestedField, &parsed));
ASSERT_EQUALS(parsed.size(), static_cast<size_t>(3));
for (int i = 0; i < 3; i++) {
map<string, string>& parsedMap = parsed[i];
ASSERT_EQUALS("a", parsedMap["a"]);
ASSERT_EQUALS("b", parsedMap["b"]);
ASSERT_EQUALS("c", parsedMap["c"]);
ASSERT_EQUALS(parsedMap.size(), static_cast<size_t>(3));
}
}
TEST(ComplexExtraction, GetBadNestedMap) {
// Test extraction of invalid complex nested vector and map
BSONField<vector<map<string, string>>> nestedField("testNested");
BSONObj nestedMapObj = BSON("a"
<< "a"
<< "b"
<< 123
<< "c"
<< "c");
BSONObjBuilder bob;
bob << nestedField() << BSON_ARRAY(nestedMapObj << nestedMapObj << nestedMapObj);
BSONObj obj = bob.obj();
vector<map<string, string>> parsed;
string errMsg;
ASSERT(!FieldParser::extract(obj, nestedField, &parsed, &errMsg));
ASSERT_NOT_EQUALS(errMsg, "");
}
TEST(EdgeCases, EmbeddedNullStrings) {
// Test extraction of string values with embedded nulls.
BSONField<string> field("testStr");
const char* str = "a\0c";
const size_t strSize = 4;
BSONObjBuilder doc;
doc.append(field(), str, strSize);
BSONObj obj(doc.obj());
string parsed;
string errMsg;
ASSERT(FieldParser::extract(obj, field, &parsed, &errMsg));
ASSERT_EQUALS(0, memcmp(parsed.data(), str, strSize));
ASSERT_EQUALS(errMsg, "");
}
} // unnamed namespace
|