| •1 |
use std::{ |
| 2 |
convert::{TryFrom, TryInto}, |
| 3 |
iter::FromIterator, |
| 4 |
marker::PhantomData, |
| 5 |
str::FromStr, |
| 6 |
}; |
| 7 |
|
| 8 |
use crate::{ |
| 9 |
raw::{RawBsonRef, RawDocument}, |
| 10 |
tests::LOCK, |
| 11 |
Bson, |
| 12 |
Document, |
| 13 |
RawBson, |
| 14 |
RawDocumentBuf, |
| 15 |
}; |
| 16 |
use pretty_assertions::assert_eq; |
| 17 |
use serde::{Deserialize, Deserializer}; |
| 18 |
|
| 19 |
use super::run_spec_test; |
| 20 |
|
| 21 |
#[derive(Debug, Deserialize)] |
| 22 |
#[serde(deny_unknown_fields)] |
| 23 |
struct TestFile { |
| 24 |
description: String, |
| 25 |
bson_type: String, |
| 26 |
test_key: Option<String>, |
| 27 |
|
| 28 |
#[serde(default)] |
| 29 |
valid: Vec<Valid>, |
| 30 |
|
| 31 |
#[serde(rename = "decodeErrors")] |
| 32 |
#[serde(default)] |
| 33 |
decode_errors: Vec<DecodeError>, |
| 34 |
|
| 35 |
#[serde(rename = "parseErrors")] |
| 36 |
#[serde(default)] |
| 37 |
parse_errors: Vec<ParseError>, |
| 38 |
|
| 39 |
#[allow(dead_code)] |
| 40 |
deprecated: Option<bool>, |
| 41 |
} |
| 42 |
|
| 43 |
#[derive(Debug, Deserialize)] |
| 44 |
#[serde(deny_unknown_fields)] |
| 45 |
struct Valid { |
| 46 |
description: String, |
| 47 |
canonical_bson: String, |
| 48 |
canonical_extjson: String, |
| 49 |
relaxed_extjson: Option<String>, |
| 50 |
degenerate_bson: Option<String>, |
| 51 |
degenerate_extjson: Option<String>, |
| 52 |
#[allow(dead_code)] |
| 53 |
converted_bson: Option<String>, |
| 54 |
#[allow(dead_code)] |
| 55 |
converted_extjson: Option<String>, |
| 56 |
lossy: Option<bool>, |
| 57 |
} |
| 58 |
|
| 59 |
#[derive(Debug, Deserialize)] |
| 60 |
struct DecodeError { |
| 61 |
description: String, |
| 62 |
bson: String, |
| 63 |
} |
| 64 |
|
| 65 |
#[derive(Debug, Deserialize)] |
| 66 |
struct ParseError { |
| 67 |
description: String, |
| 68 |
string: String, |
| 69 |
} |
| 70 |
|
| 71 |
struct FieldVisitor<'a, T>(&'a str, PhantomData<T>); |
| 72 |
|
| 73 |
impl<'de, 'a, T> serde::de::Visitor<'de> for FieldVisitor<'a, T> |
| 74 |
where |
| 75 |
T: Deserialize<'de>, |
| 76 |
{ |
| 77 |
type Value = T; |
| 78 |
|
| 79 |
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 80 |
write!(formatter, "expecting RawBson at field {}", self.0) |
| 81 |
} |
| 82 |
|
| 83 |
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 84 |
where |
| 85 |
A: serde::de::MapAccess<'de>, |
| 86 |
{ |
| 87 |
while let Some((k, v)) = map.next_entry::<String, T>()? { |
| 88 |
if k.as_str() == self.0 { |
| 89 |
return Ok(v); |
| 90 |
} |
| 91 |
} |
| 92 |
Err(serde::de::Error::custom(format!( |
| 93 |
"missing field: {}", |
| 94 |
self.0 |
| 95 |
))) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
fn run_test(test: TestFile) { |
| 100 |
let _guard = LOCK.run_concurrently(); |
| 101 |
for valid in test.valid { |
| 102 |
let description = format!("{}: {}", test.description, valid.description); |
| 103 |
|
| 104 |
let canonical_bson = hex::decode(&valid.canonical_bson).expect(&description); |
| 105 |
|
| 106 |
// these four cover the four ways to create a [`Document`] from the provided BSON. |
| 107 |
let documentfromreader_cb = |
| 108 |
Document::from_reader(canonical_bson.as_slice()).expect(&description); |
| 109 |
|
| 110 |
let fromreader_cb: Document = |
| 111 |
crate::from_reader(canonical_bson.as_slice()).expect(&description); |
| 112 |
|
| 113 |
let fromdocument_documentfromreader_cb: Document = |
| 114 |
crate::from_document(documentfromreader_cb.clone()).expect(&description); |
| 115 |
|
| 116 |
let todocument_documentfromreader_cb: Document = |
| 117 |
crate::to_document(&documentfromreader_cb).expect(&description); |
| 118 |
|
| 119 |
let canonical_raw_document = |
| 120 |
RawDocument::from_bytes(canonical_bson.as_slice()).expect(&description); |
| 121 |
let document_from_raw_document: Document = |
| 122 |
canonical_raw_document.try_into().expect(&description); |
| 123 |
|
| 124 |
let canonical_raw_bson_from_slice = |
| 125 |
crate::from_slice::<RawBsonRef>(canonical_bson.as_slice()) |
| 126 |
.expect(&description) |
| 127 |
.as_document() |
| 128 |
.expect(&description); |
| 129 |
|
| 130 |
let canonical_owned_raw_bson_from_slice = |
| 131 |
crate::from_slice::<RawBson>(canonical_bson.as_slice()).expect(&description); |
| 132 |
|
| 133 |
let canonical_raw_document_from_slice = |
| 134 |
crate::from_slice::<&RawDocument>(canonical_bson.as_slice()).expect(&description); |
| 135 |
|
| 136 |
// These cover the ways to serialize those [`Documents`] back to BSON. |
| 137 |
let mut documenttowriter_documentfromreader_cb = Vec::new(); |
| 138 |
documentfromreader_cb |
| 139 |
.to_writer(&mut documenttowriter_documentfromreader_cb) |
| 140 |
.expect(&description); |
| 141 |
|
| 142 |
let mut documenttowriter_fromreader_cb = Vec::new(); |
| 143 |
fromreader_cb |
| 144 |
.to_writer(&mut documenttowriter_fromreader_cb) |
| 145 |
.expect(&description); |
| 146 |
|
| 147 |
let mut documenttowriter_fromdocument_documentfromreader_cb = Vec::new(); |
| 148 |
fromdocument_documentfromreader_cb |
| 149 |
.to_writer(&mut documenttowriter_fromdocument_documentfromreader_cb) |
| 150 |
.expect(&description); |
| 151 |
|
| 152 |
let mut documenttowriter_todocument_documentfromreader_cb = Vec::new(); |
| 153 |
todocument_documentfromreader_cb |
| 154 |
.to_writer(&mut documenttowriter_todocument_documentfromreader_cb) |
| 155 |
.expect(&description); |
| 156 |
|
| 157 |
let tovec_documentfromreader_cb = |
| 158 |
crate::to_vec(&documentfromreader_cb).expect(&description); |
| 159 |
|
| 160 |
let mut documenttowriter_document_from_raw_document = Vec::new(); |
| 161 |
document_from_raw_document |
| 162 |
.to_writer(&mut documenttowriter_document_from_raw_document) |
| 163 |
.expect(&description); |
| 164 |
|
| 165 |
// Serialize the raw versions "back" to BSON also. |
| 166 |
let tovec_rawdocument = crate::to_vec(&canonical_raw_document).expect(&description); |
| 167 |
let tovec_rawdocument_from_slice = |
| 168 |
crate::to_vec(&canonical_raw_document_from_slice).expect(&description); |
| 169 |
let tovec_rawbson = crate::to_vec(&canonical_raw_bson_from_slice).expect(&description); |
| 170 |
let tovec_ownedrawbson = |
| 171 |
crate::to_vec(&canonical_owned_raw_bson_from_slice).expect(&description); |
| 172 |
|
| 173 |
// test Bson / RawBson field deserialization |
| 174 |
if let Some(ref test_key) = test.test_key { |
| 175 |
// skip regex tests that don't have the value at the test key |
| 176 |
if !description.contains("$regex query operator") { |
| 177 |
// deserialize the field from raw Bytes into a RawBson |
| 178 |
let mut deserializer_raw = |
| 179 |
crate::de::RawDeserializer::new(canonical_bson.as_slice(), false); |
| 180 |
let raw_bson_field = deserializer_raw |
| 181 |
.deserialize_any(FieldVisitor(test_key.as_str(), PhantomData::<RawBsonRef>)) |
| 182 |
.expect(&description); |
| 183 |
// convert to an owned Bson and put into a Document |
| 184 |
let bson: Bson = raw_bson_field.try_into().expect(&description); |
| 185 |
let from_raw_doc = doc! { |
| 186 |
test_key: bson |
| 187 |
}; |
| 188 |
|
| 189 |
// deserialize the field from raw Bytes into an OwnedRawBson |
| 190 |
let mut deserializer_raw = |
| 191 |
crate::de::RawDeserializer::new(canonical_bson.as_slice(), false); |
| 192 |
let owned_raw_bson_field = deserializer_raw |
| 193 |
.deserialize_any(FieldVisitor(test_key.as_str(), PhantomData::<RawBson>)) |
| 194 |
.expect(&description); |
| 195 |
let from_slice_owned_vec = |
| 196 |
RawDocumentBuf::from_iter([(test_key, owned_raw_bson_field)]).into_bytes(); |
| 197 |
|
| 198 |
// deserialize the field from raw Bytes into a Bson |
| 199 |
let mut deserializer_value = |
| 200 |
crate::de::RawDeserializer::new(canonical_bson.as_slice(), false); |
| 201 |
let bson_field = deserializer_value |
| 202 |
.deserialize_any(FieldVisitor(test_key.as_str(), PhantomData::<Bson>)) |
| 203 |
.expect(&description); |
| 204 |
// put into a Document |
| 205 |
let from_slice_value_doc = doc! { |
| 206 |
test_key: bson_field, |
| 207 |
}; |
| 208 |
|
| 209 |
// deserialize the field from a Bson into a Bson |
| 210 |
let deserializer_value_value = |
| 211 |
crate::Deserializer::new(Bson::Document(documentfromreader_cb.clone())); |
| 212 |
let bson_field = deserializer_value_value |
| 213 |
.deserialize_any(FieldVisitor(test_key.as_str(), PhantomData::<Bson>)) |
| 214 |
.expect(&description); |
| 215 |
// put into a Document |
| 216 |
let from_value_value_doc = doc! { |
| 217 |
test_key: bson_field, |
| 218 |
}; |
| 219 |
|
| 220 |
// deserialize the field from a Bson into a RawBson |
| 221 |
let deserializer_value_raw = |
| 222 |
crate::Deserializer::new(Bson::Document(documentfromreader_cb.clone())); |
| 223 |
let raw_bson_field = deserializer_value_raw |
| 224 |
.deserialize_any(FieldVisitor(test_key.as_str(), PhantomData::<RawBson>)) |
| 225 |
.expect(&description); |
| 226 |
let from_value_raw_doc = doc! { |
| 227 |
test_key: Bson::try_from(raw_bson_field).expect(&description), |
| 228 |
}; |
| 229 |
|
| 230 |
// convert back into raw BSON for comparison with canonical BSON |
| 231 |
let from_raw_vec = crate::to_vec(&from_raw_doc).expect(&description); |
| 232 |
let from_slice_value_vec = |
| 233 |
crate::to_vec(&from_slice_value_doc).expect(&description); |
| 234 |
let from_bson_value_vec = crate::to_vec(&from_value_value_doc).expect(&description); |
| 235 |
let from_value_raw_vec = crate::to_vec(&from_value_raw_doc).expect(&description); |
| 236 |
|
| 237 |
assert_eq!(from_raw_vec, canonical_bson, "{}", description); |
| 238 |
assert_eq!(from_slice_value_vec, canonical_bson, "{}", description); |
| 239 |
assert_eq!(from_bson_value_vec, canonical_bson, "{}", description); |
| 240 |
assert_eq!(from_slice_owned_vec, canonical_bson, "{}", description); |
| 241 |
assert_eq!(from_value_raw_vec, canonical_bson, "{}", description); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// native_to_bson( bson_to_native(cB) ) = cB |
| 246 |
|
| 247 |
// now we ensure the hex for all 5 are equivalent to the canonical BSON provided by the |
| 248 |
// test. |
| 249 |
assert_eq!( |
| 250 |
hex::encode(documenttowriter_documentfromreader_cb).to_lowercase(), |
| 251 |
valid.canonical_bson.to_lowercase(), |
| 252 |
"{}", |
| 253 |
description, |
| 254 |
); |
| 255 |
|
| 256 |
assert_eq!( |
| 257 |
hex::encode(documenttowriter_fromreader_cb).to_lowercase(), |
| 258 |
valid.canonical_bson.to_lowercase(), |
| 259 |
"{}", |
| 260 |
description, |
| 261 |
); |
| 262 |
|
| 263 |
assert_eq!( |
| 264 |
hex::encode(documenttowriter_fromdocument_documentfromreader_cb).to_lowercase(), |
| 265 |
valid.canonical_bson.to_lowercase(), |
| 266 |
"{}", |
| 267 |
description, |
| 268 |
); |
| 269 |
|
| 270 |
assert_eq!( |
| 271 |
hex::encode(documenttowriter_todocument_documentfromreader_cb).to_lowercase(), |
| 272 |
valid.canonical_bson.to_lowercase(), |
| 273 |
"{}", |
| 274 |
description, |
| 275 |
); |
| 276 |
|
| 277 |
assert_eq!( |
| 278 |
hex::encode(tovec_documentfromreader_cb).to_lowercase(), |
| 279 |
valid.canonical_bson.to_lowercase(), |
| 280 |
"{}", |
| 281 |
description, |
| 282 |
); |
| 283 |
|
| 284 |
assert_eq!( |
| 285 |
hex::encode(documenttowriter_document_from_raw_document).to_lowercase(), |
| 286 |
valid.canonical_bson.to_lowercase(), |
| 287 |
"{}", |
| 288 |
description, |
| 289 |
); |
| 290 |
|
| 291 |
assert_eq!(tovec_rawdocument, tovec_rawbson, "{}", description); |
| 292 |
assert_eq!( |
| 293 |
tovec_rawdocument, tovec_rawdocument_from_slice, |
| 294 |
"{}", |
| 295 |
description |
| 296 |
); |
| 297 |
assert_eq!(tovec_rawdocument, tovec_ownedrawbson, "{}", description); |
| 298 |
|
| 299 |
assert_eq!( |
| 300 |
hex::encode(tovec_rawdocument).to_lowercase(), |
| 301 |
valid.canonical_bson.to_lowercase(), |
| 302 |
"{}", |
| 303 |
description, |
| 304 |
); |
| 305 |
|
| 306 |
// NaN == NaN is false, so we skip document comparisons that contain NaN |
| 307 |
if !description.to_ascii_lowercase().contains("nan") && !description.contains("decq541") { |
| 308 |
assert_eq!(documentfromreader_cb, fromreader_cb, "{}", description); |
| 309 |
|
| 310 |
assert_eq!( |
| 311 |
documentfromreader_cb, fromdocument_documentfromreader_cb, |
| 312 |
"{}", |
| 313 |
description |
| 314 |
); |
| 315 |
|
| 316 |
assert_eq!( |
| 317 |
documentfromreader_cb, todocument_documentfromreader_cb, |
| 318 |
"{}", |
| 319 |
description |
| 320 |
); |
| 321 |
|
| 322 |
assert_eq!( |
| 323 |
document_from_raw_document, documentfromreader_cb, |
| 324 |
"{}", |
| 325 |
description |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
// native_to_bson( bson_to_native(dB) ) = cB |
| 330 |
|
| 331 |
if let Some(db) = valid.degenerate_bson { |
| 332 |
let db = hex::decode(&db).expect(&description); |
| 333 |
|
| 334 |
let bson_to_native_db = Document::from_reader(db.as_slice()).expect(&description); |
| 335 |
let mut native_to_bson_bson_to_native_db = Vec::new(); |
| 336 |
bson_to_native_db |
| 337 |
.to_writer(&mut native_to_bson_bson_to_native_db) |
| 338 |
.unwrap(); |
| 339 |
assert_eq!( |
| 340 |
hex::encode(native_to_bson_bson_to_native_db).to_lowercase(), |
| 341 |
valid.canonical_bson.to_lowercase(), |
| 342 |
"{}", |
| 343 |
description, |
| 344 |
); |
| 345 |
|
| 346 |
let bson_to_native_db_serde: Document = |
| 347 |
crate::from_reader(db.as_slice()).expect(&description); |
| 348 |
let mut native_to_bson_bson_to_native_db_serde = Vec::new(); |
| 349 |
bson_to_native_db_serde |
| 350 |
.to_writer(&mut native_to_bson_bson_to_native_db_serde) |
| 351 |
.unwrap(); |
| 352 |
assert_eq!( |
| 353 |
hex::encode(native_to_bson_bson_to_native_db_serde).to_lowercase(), |
| 354 |
valid.canonical_bson.to_lowercase(), |
| 355 |
"{}", |
| 356 |
description, |
| 357 |
); |
| 358 |
|
| 359 |
let document_from_raw_document: Document = RawDocument::from_bytes(db.as_slice()) |
| 360 |
.expect(&description) |
| 361 |
.try_into() |
| 362 |
.expect(&description); |
| 363 |
let mut documenttowriter_document_from_raw_document = Vec::new(); |
| 364 |
document_from_raw_document |
| 365 |
.to_writer(&mut documenttowriter_document_from_raw_document) |
| 366 |
.expect(&description); |
| 367 |
assert_eq!( |
| 368 |
hex::encode(documenttowriter_document_from_raw_document).to_lowercase(), |
| 369 |
valid.canonical_bson.to_lowercase(), |
| 370 |
"{}", |
| 371 |
description, |
| 372 |
); |
| 373 |
|
| 374 |
// NaN == NaN is false, so we skip document comparisons that contain NaN |
| 375 |
if !description.contains("NaN") { |
| 376 |
assert_eq!( |
| 377 |
bson_to_native_db_serde, documentfromreader_cb, |
| 378 |
"{}", |
| 379 |
description |
| 380 |
); |
| 381 |
|
| 382 |
assert_eq!( |
| 383 |
document_from_raw_document, documentfromreader_cb, |
| 384 |
"{}", |
| 385 |
description |
| 386 |
); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
let cej: serde_json::Value = |
| 391 |
serde_json::from_str(&valid.canonical_extjson).expect(&description); |
| 392 |
|
| 393 |
// native_to_canonical_extended_json( bson_to_native(cB) ) = cEJ |
| 394 |
|
| 395 |
let mut cej_updated_float = cej.clone(); |
| 396 |
|
| 397 |
// Rust doesn't format f64 with exponential notation by default, and the spec doesn't give |
| 398 |
// guidance on when to use it, so we manually parse any $numberDouble fields with |
| 399 |
// exponential notation and replace them with non-exponential notation. |
| 400 |
if let Some(ref key) = test.test_key { |
| 401 |
if let Some(serde_json::Value::Object(subdoc)) = cej_updated_float.get_mut(key) { |
| 402 |
if let Some(&mut serde_json::Value::String(ref mut s)) = |
| 403 |
subdoc.get_mut("$numberDouble") |
| 404 |
{ |
| 405 |
if s.to_lowercase().contains('e') { |
| 406 |
let d = f64::from_str(s).unwrap(); |
| 407 |
let mut fixed_string = format!("{}", d); |
| 408 |
|
| 409 |
if d.fract() == 0.0 { |
| 410 |
fixed_string.push_str(".0"); |
| 411 |
} |
| 412 |
|
| 413 |
*s = fixed_string; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
assert_eq!( |
| 420 |
Bson::Document(documentfromreader_cb.clone()).into_canonical_extjson(), |
| 421 |
cej_updated_float, |
| 422 |
"{}", |
| 423 |
description |
| 424 |
); |
| 425 |
|
| 426 |
// native_to_relaxed_extended_json( bson_to_native(cB) ) = cEJ |
| 427 |
|
| 428 |
if let Some(ref relaxed_extjson) = valid.relaxed_extjson { |
| 429 |
let rej: serde_json::Value = serde_json::from_str(relaxed_extjson).expect(&description); |
| 430 |
|
| 431 |
assert_eq!( |
| 432 |
Bson::Document(documentfromreader_cb.clone()).into_relaxed_extjson(), |
| 433 |
rej, |
| 434 |
"{}", |
| 435 |
description |
| 436 |
); |
| 437 |
} |
| 438 |
|
| 439 |
// native_to_canonical_extended_json( json_to_native(cEJ) ) = cEJ |
| 440 |
|
| 441 |
let json_to_native_cej: Bson = cej.clone().try_into().expect("cej into bson should work"); |
| 442 |
|
| 443 |
let native_to_canonical_extended_json_bson_to_native_cej = |
| 444 |
json_to_native_cej.clone().into_canonical_extjson(); |
| 445 |
|
| 446 |
assert_eq!( |
| 447 |
native_to_canonical_extended_json_bson_to_native_cej, cej_updated_float, |
| 448 |
"{}", |
| 449 |
description, |
| 450 |
); |
| 451 |
|
| 452 |
// native_to_bson( json_to_native(cEJ) ) = cB (unless lossy) |
| 453 |
|
| 454 |
if valid.lossy != Some(true) { |
| 455 |
let mut native_to_bson_json_to_native_cej = Vec::new(); |
| 456 |
json_to_native_cej |
| 457 |
.as_document() |
| 458 |
.unwrap() |
| 459 |
.to_writer(&mut native_to_bson_json_to_native_cej) |
| 460 |
.unwrap(); |
| 461 |
|
| 462 |
assert_eq!( |
| 463 |
hex::encode(native_to_bson_json_to_native_cej).to_lowercase(), |
| 464 |
valid.canonical_bson.to_lowercase(), |
| 465 |
"{}", |
| 466 |
description, |
| 467 |
); |
| 468 |
} |
| 469 |
|
| 470 |
if let Some(ref degenerate_extjson) = valid.degenerate_extjson { |
| 471 |
let dej: serde_json::Value = |
| 472 |
serde_json::from_str(degenerate_extjson).expect(&description); |
| 473 |
|
| 474 |
let json_to_native_dej: Bson = dej.clone().try_into().unwrap(); |
| 475 |
|
| 476 |
// native_to_canonical_extended_json( json_to_native(dEJ) ) = cEJ |
| 477 |
|
| 478 |
let native_to_canonical_extended_json_json_to_native_dej = |
| 479 |
json_to_native_dej.clone().into_canonical_extjson(); |
| 480 |
|
| 481 |
assert_eq!( |
| 482 |
native_to_canonical_extended_json_json_to_native_dej, cej, |
| 483 |
"{}", |
| 484 |
description, |
| 485 |
); |
| 486 |
|
| 487 |
// native_to_bson( json_to_native(dEJ) ) = cB (unless lossy) |
| 488 |
|
| 489 |
if valid.lossy != Some(true) { |
| 490 |
let mut native_to_bson_json_to_native_dej = Vec::new(); |
| 491 |
json_to_native_dej |
| 492 |
.as_document() |
| 493 |
.unwrap() |
| 494 |
.to_writer(&mut native_to_bson_json_to_native_dej) |
| 495 |
.unwrap(); |
| 496 |
|
| 497 |
assert_eq!( |
| 498 |
hex::encode(native_to_bson_json_to_native_dej).to_lowercase(), |
| 499 |
valid.canonical_bson.to_lowercase(), |
| 500 |
"{}", |
| 501 |
description, |
| 502 |
); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
// native_to_relaxed_extended_json( json_to_native(rEJ) ) = rEJ |
| 507 |
|
| 508 |
if let Some(ref rej) = valid.relaxed_extjson { |
| 509 |
let rej: serde_json::Value = serde_json::from_str(rej).unwrap(); |
| 510 |
|
| 511 |
let json_to_native_rej: Bson = rej.clone().try_into().unwrap(); |
| 512 |
|
| 513 |
let native_to_relaxed_extended_json_bson_to_native_rej = |
| 514 |
json_to_native_rej.clone().into_relaxed_extjson(); |
| 515 |
|
| 516 |
assert_eq!( |
| 517 |
native_to_relaxed_extended_json_bson_to_native_rej, rej, |
| 518 |
"{}", |
| 519 |
description, |
| 520 |
); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
for decode_error in test.decode_errors.iter() { |
| 525 |
let description = format!( |
| 526 |
"{} decode error: {}", |
| 527 |
test.bson_type, decode_error.description |
| 528 |
); |
| 529 |
let bson = hex::decode(&decode_error.bson).expect("should decode from hex"); |
| 530 |
|
| 531 |
if let Ok(doc) = RawDocument::from_bytes(bson.as_slice()) { |
| 532 |
Document::try_from(doc).expect_err(description.as_str()); |
| 533 |
} |
| 534 |
|
| 535 |
// No meaningful definition of "byte count" for an arbitrary reader. |
| 536 |
if decode_error.description |
| 537 |
== "Stated length less than byte count, with garbage after envelope" |
| 538 |
{ |
| 539 |
continue; |
| 540 |
} |
| 541 |
|
| 542 |
Document::from_reader(bson.as_slice()).expect_err(&description); |
| 543 |
crate::from_reader::<_, Document>(bson.as_slice()).expect_err(description.as_str()); |
| 544 |
|
| 545 |
if decode_error.description.contains("invalid UTF-8") { |
| 546 |
crate::from_reader_utf8_lossy::<_, Document>(bson.as_slice()).unwrap_or_else(|err| { |
| 547 |
panic!( |
| 548 |
"{}: utf8_lossy should not fail (failed with {:?})", |
| 549 |
description, err |
| 550 |
) |
| 551 |
}); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
for parse_error in test.parse_errors { |
| 556 |
// no special support for dbref convention |
| 557 |
if parse_error.description.contains("DBRef") { |
| 558 |
continue; |
| 559 |
} |
| 560 |
|
| 561 |
let json: serde_json::Value = serde_json::Value::String(parse_error.string); |
| 562 |
|
| 563 |
if let Ok(bson) = Bson::try_from(json.clone()) { |
| 564 |
// if converting to bson succeeds, assert that translating that bson to bytes fails |
| 565 |
assert!(crate::to_vec(&bson).is_err()); |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
#[test] |
| 571 |
fn run() { |
| 572 |
run_spec_test(&["bson-corpus"], run_test); |
| 573 |
} |