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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
#![allow(clippy::zero_sized_map_values)]
use indoc::indoc;
use serde::de::Deserialize;
#[cfg(not(miri))]
use serde::de::{SeqAccess, Visitor};
use serde_derive::{Deserialize, Serialize};
use serde_yaml::value::{Tag, TaggedValue};
use serde_yaml::{Deserializer, Value};
#[cfg(not(miri))]
use std::collections::BTreeMap;
#[cfg(not(miri))]
use std::fmt;
use std::fmt::Debug;
fn test_error<'de, T>(yaml: &'de str, expected: &str)
where
T: Deserialize<'de> + Debug,
{
let result = serde_yaml::from_str::<T>(yaml);
assert_eq!(expected, result.unwrap_err().to_string());
let mut deserializer = Deserializer::from_str(yaml);
if let Some(first_document) = deserializer.next() {
if deserializer.next().is_none() {
let result = T::deserialize(first_document);
assert_eq!(expected, result.unwrap_err().to_string());
}
}
}
#[test]
fn test_scan_error() {
let yaml = ">\n@";
let expected = "found character that cannot start any token at line 2 column 1, while scanning for the next token";
test_error::<Value>(yaml, expected);
}
#[test]
fn test_incorrect_type() {
let yaml = indoc! {"
---
str
"};
let expected = "invalid type: string \"str\", expected i16 at line 2 column 1";
test_error::<i16>(yaml, expected);
}
#[test]
fn test_incorrect_nested_type() {
#[derive(Deserialize, Debug)]
pub struct A {
#[allow(dead_code)]
pub b: Vec<B>,
}
#[derive(Deserialize, Debug)]
pub enum B {
C(#[allow(dead_code)] C),
}
#[derive(Deserialize, Debug)]
pub struct C {
#[allow(dead_code)]
pub d: bool,
}
let yaml = indoc! {"
b:
- !C
d: fase
"};
let expected = "b[0].d: invalid type: string \"fase\", expected a boolean at line 3 column 8";
test_error::<A>(yaml, expected);
}
#[test]
fn test_empty() {
let expected = "EOF while parsing a value";
test_error::<String>("", expected);
}
#[test]
fn test_missing_field() {
#[derive(Deserialize, Debug)]
pub struct Basic {
#[allow(dead_code)]
pub v: bool,
#[allow(dead_code)]
pub w: bool,
}
let yaml = indoc! {"
---
v: true
"};
let expected = "missing field `w` at line 2 column 1";
test_error::<Basic>(yaml, expected);
}
#[test]
fn test_unknown_anchor() {
let yaml = indoc! {"
---
*some
"};
let expected = "unknown anchor at line 2 column 1";
test_error::<String>(yaml, expected);
}
#[test]
fn test_ignored_unknown_anchor() {
#[derive(Deserialize, Debug)]
pub struct Wrapper {
#[allow(dead_code)]
pub c: (),
}
let yaml = indoc! {"
b: [*a]
c: ~
"};
let expected = "unknown anchor at line 1 column 5";
test_error::<Wrapper>(yaml, expected);
}
#[test]
fn test_bytes() {
let expected = "serialization and deserialization of bytes in YAML is not implemented";
test_error::<&[u8]>("...", expected);
}
#[test]
fn test_two_documents() {
let yaml = indoc! {"
---
0
---
1
"};
let expected = "deserializing from YAML containing more than one document is not supported";
test_error::<usize>(yaml, expected);
}
#[test]
fn test_second_document_syntax_error() {
let yaml = indoc! {"
---
0
---
]
"};
let mut de = Deserializer::from_str(yaml);
let first_doc = de.next().unwrap();
let result = <usize as serde::Deserialize>::deserialize(first_doc);
assert_eq!(0, result.unwrap());
let second_doc = de.next().unwrap();
let result = <usize as serde::Deserialize>::deserialize(second_doc);
let expected =
"did not find expected node content at line 4 column 1, while parsing a block node";
assert_eq!(expected, result.unwrap_err().to_string());
}
#[test]
fn test_missing_enum_tag() {
#[derive(Deserialize, Debug)]
pub enum E {
V(#[allow(dead_code)] usize),
}
let yaml = indoc! {r#"
"V": 16
"other": 32
"#};
let expected = "invalid type: map, expected a YAML tag starting with '!'";
test_error::<E>(yaml, expected);
}
#[test]
fn test_serialize_nested_enum() {
#[derive(Serialize, Debug)]
pub enum Outer {
Inner(Inner),
}
#[derive(Serialize, Debug)]
pub enum Inner {
Newtype(usize),
Tuple(usize, usize),
Struct { x: usize },
}
let expected = "serializing nested enums in YAML is not supported yet";
let e = Outer::Inner(Inner::Newtype(0));
let error = serde_yaml::to_string(&e).unwrap_err();
assert_eq!(error.to_string(), expected);
let e = Outer::Inner(Inner::Tuple(0, 0));
let error = serde_yaml::to_string(&e).unwrap_err();
assert_eq!(error.to_string(), expected);
let e = Outer::Inner(Inner::Struct { x: 0 });
let error = serde_yaml::to_string(&e).unwrap_err();
assert_eq!(error.to_string(), expected);
let e = Value::Tagged(Box::new(TaggedValue {
tag: Tag::new("Outer"),
value: Value::Tagged(Box::new(TaggedValue {
tag: Tag::new("Inner"),
value: Value::Null,
})),
}));
let error = serde_yaml::to_string(&e).unwrap_err();
assert_eq!(error.to_string(), expected);
}
#[test]
fn test_deserialize_nested_enum() {
#[derive(Deserialize, Debug)]
pub enum Outer {
Inner(#[allow(dead_code)] Inner),
}
#[derive(Deserialize, Debug)]
pub enum Inner {
Variant(#[allow(dead_code)] Vec<usize>),
}
let yaml = indoc! {"
---
!Inner []
"};
let expected = "deserializing nested enum in Outer::Inner from YAML is not supported yet at line 2 column 1";
test_error::<Outer>(yaml, expected);
let yaml = indoc! {"
---
!Variant []
"};
let expected = "unknown variant `Variant`, expected `Inner`";
test_error::<Outer>(yaml, expected);
let yaml = indoc! {"
---
!Inner !Variant []
"};
let expected = "deserializing nested enum in Outer::Inner from YAML is not supported yet at line 2 column 1";
test_error::<Outer>(yaml, expected);
}
#[test]
fn test_variant_not_a_seq() {
#[derive(Deserialize, Debug)]
pub enum E {
V(#[allow(dead_code)] usize),
}
let yaml = indoc! {"
---
!V
value: 0
"};
let expected = "invalid type: map, expected usize at line 2 column 1";
test_error::<E>(yaml, expected);
}
#[test]
fn test_struct_from_sequence() {
#[derive(Deserialize, Debug)]
pub struct Struct {
#[allow(dead_code)]
pub x: usize,
#[allow(dead_code)]
pub y: usize,
}
let yaml = indoc! {"
[0, 0]
"};
let expected = "invalid type: sequence, expected struct Struct";
test_error::<Struct>(yaml, expected);
}
#[test]
fn test_bad_bool() {
let yaml = indoc! {"
---
!!bool str
"};
let expected = "invalid value: string \"str\", expected a boolean at line 2 column 1";
test_error::<bool>(yaml, expected);
}
#[test]
fn test_bad_int() {
let yaml = indoc! {"
---
!!int str
"};
let expected = "invalid value: string \"str\", expected an integer at line 2 column 1";
test_error::<i64>(yaml, expected);
}
#[test]
fn test_bad_float() {
let yaml = indoc! {"
---
!!float str
"};
let expected = "invalid value: string \"str\", expected a float at line 2 column 1";
test_error::<f64>(yaml, expected);
}
#[test]
fn test_bad_null() {
let yaml = indoc! {"
---
!!null str
"};
let expected = "invalid value: string \"str\", expected null at line 2 column 1";
test_error::<()>(yaml, expected);
}
#[test]
fn test_short_tuple() {
let yaml = indoc! {"
---
[0, 0]
"};
let expected = "invalid length 2, expected a tuple of size 3 at line 2 column 1";
test_error::<(u8, u8, u8)>(yaml, expected);
}
#[test]
fn test_long_tuple() {
let yaml = indoc! {"
---
[0, 0, 0]
"};
let expected = "invalid length 3, expected sequence of 2 elements at line 2 column 1";
test_error::<(u8, u8)>(yaml, expected);
}
#[test]
fn test_invalid_scalar_type() {
#[derive(Deserialize, Debug)]
pub struct S {
#[allow(dead_code)]
pub x: [i32; 1],
}
let yaml = "x: ''\n";
let expected = "x: invalid type: string \"\", expected an array of length 1 at line 1 column 4";
test_error::<S>(yaml, expected);
}
#[cfg(not(miri))]
#[test]
fn test_infinite_recursion_objects() {
#[derive(Deserialize, Debug)]
pub struct S {
#[allow(dead_code)]
pub x: Option<Box<S>>,
}
let yaml = "&a {'x': *a}";
let expected = "recursion limit exceeded";
test_error::<S>(yaml, expected);
}
#[cfg(not(miri))]
#[test]
fn test_infinite_recursion_arrays() {
#[derive(Deserialize, Debug)]
pub struct S(
#[allow(dead_code)] pub usize,
#[allow(dead_code)] pub Option<Box<S>>,
);
let yaml = "&a [0, *a]";
let expected = "recursion limit exceeded";
test_error::<S>(yaml, expected);
}
#[cfg(not(miri))]
#[test]
fn test_infinite_recursion_newtype() {
#[derive(Deserialize, Debug)]
pub struct S(#[allow(dead_code)] pub Option<Box<S>>);
let yaml = "&a [*a]";
let expected = "recursion limit exceeded";
test_error::<S>(yaml, expected);
}
#[cfg(not(miri))]
#[test]
fn test_finite_recursion_objects() {
#[derive(Deserialize, Debug)]
pub struct S {
#[allow(dead_code)]
pub x: Option<Box<S>>,
}
let yaml = "{'x':".repeat(1_000) + &"}".repeat(1_000);
let expected = "recursion limit exceeded at line 1 column 641";
test_error::<S>(&yaml, expected);
}
#[cfg(not(miri))]
#[test]
fn test_finite_recursion_arrays() {
#[derive(Deserialize, Debug)]
pub struct S(
#[allow(dead_code)] pub usize,
#[allow(dead_code)] pub Option<Box<S>>,
);
let yaml = "[0, ".repeat(1_000) + &"]".repeat(1_000);
let expected = "recursion limit exceeded at line 1 column 513";
test_error::<S>(&yaml, expected);
}
#[cfg(not(miri))]
#[test]
fn test_billion_laughs() {
#[derive(Debug)]
struct X;
impl<'de> Visitor<'de> for X {
type Value = X;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("exponential blowup")
}
fn visit_unit<E>(self) -> Result<X, E> {
Ok(X)
}
fn visit_seq<S>(self, mut seq: S) -> Result<X, S::Error>
where
S: SeqAccess<'de>,
{
while let Some(X) = seq.next_element()? {}
Ok(X)
}
}
impl<'de> Deserialize<'de> for X {
fn deserialize<D>(deserializer: D) -> Result<X, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_any(X)
}
}
let yaml = indoc! {"
a: &a ~
b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]
c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]
d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]
e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]
f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]
g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]
h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]
i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]
"};
let expected = "repetition limit exceeded";
test_error::<BTreeMap<String, X>>(yaml, expected);
}
#[test]
fn test_duplicate_keys() {
let yaml = indoc! {"
---
thing: true
thing: false
"};
let expected = "duplicate entry with key \"thing\" at line 2 column 1";
test_error::<Value>(yaml, expected);
let yaml = indoc! {"
---
null: true
~: false
"};
let expected = "duplicate entry with null key at line 2 column 1";
test_error::<Value>(yaml, expected);
let yaml = indoc! {"
---
99: true
99: false
"};
let expected = "duplicate entry with key 99 at line 2 column 1";
test_error::<Value>(yaml, expected);
let yaml = indoc! {"
---
{}: true
{}: false
"};
let expected = "duplicate entry in YAML map at line 2 column 1";
test_error::<Value>(yaml, expected);
}
|