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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
// LNP/BP client-side-validation foundation libraries implementing LNPBP
// specifications & standards (LNPBP-4, 7, 8, 9, 42, 81)
//
// Written in 2019-2022 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the Apache 2.0 License along with this
// software. If not, see <https://opensource.org/licenses/Apache-2.0>.
// TODO: #35 Remove from here upon v 2.0 release (the code is already a part
// of separate crate `strict_encoding_test`
#![allow(deprecated)]
//! Deprecated: use strict_encoding_test crate instead
use std::fmt::Debug;
use crate::{Error, StrictDecode, StrictEncode};
/// Failures happening during strict encoding tests of enum encodings.
///
/// NB: These errors are specific for testing configuration and should not be
/// used in non-test environment.
#[derive(Clone, PartialEq, Debug, Display, Error)]
pub enum EnumEncodingTestFailure<T>
where
T: Clone + PartialEq + Debug,
{
/// Failure during encoding enum variant
#[display("Failure during encoding enum variant `{0:?}`: {1:?}")]
EncoderFailure(T, Error),
/// Failure during decoding binary representation of enum variant
#[display(
"Failure during decoding binary representation of enum variant \
`{0:?}`: `{1:?}`
\tByte representation: {2:?}"
)]
DecoderFailure(T, Error, Vec<u8>),
/// Test case failure representing mismatch between enum variant produced
/// by decoding from the originally encoded enum variant
#[display(
"Roundtrip encoding of enum variant `{original:?}` results in \
different variant `{decoded:?}`"
)]
DecodedDiffersFromOriginal {
/// Original value, which was encoded
original: T,
/// The value resulting from decoding encoded `original` value
decoded: T,
},
/// Test case failure representing mismatch between expected enum variant
/// primitive value and the actual primitive value assigned to the enum
/// variant by the rust compiler
#[display(
"Expected value `{expected}` for enum variant \
`{enum_name}::{variant_name}` does not match the actual value \
`{actual}`"
)]
ValueMismatch {
/// Name of the enum being tested
enum_name: &'static str,
/// Name of the enum variant being tested
variant_name: &'static str,
/// Expected primitive value for the tested enum variant
expected: usize,
/// Actual primitive value of the tested enum variant
actual: usize,
},
/// Test case failure representing failed byte string representation of the
/// encoded enum variant
#[display(
"Enum variant `{enum_name}:{variant_name}` has incorrect encoding:
\tExpected: {expected:?}
\tActual: {actual:?}
"
)]
EncodedValueMismatch {
/// Name of the enum being tested
enum_name: &'static str,
/// Name of the enum variant being tested
variant_name: &'static str,
/// Expected encoded byte string for the tested enum variant
expected: Vec<u8>,
/// Actual encoded byte string of the tested enum variant
actual: Vec<u8>,
},
/// Test case failure representing incorrect decoder error during
/// processing out-of-enum range value
#[display(
"Decoding of out-of-enum-range value `{0}` results in incorrect \
decoder error `{1:?}`"
)]
DecoderWrongErrorOnUnknownValue(
/// Value which was decoded into an enum variant
u8,
/// Error which was produced during decoding that value
Error,
),
/// Test case failure representing a out-of-enum range primitive value
/// still being interpreted as one of enum variants
#[display(
"Out-of-enum-range value `{0}` is interpreted as `{1:?}` enum variant \
by rust compiler"
)]
UnknownDecodesToVariant(
/// Value which was decoded into an enum variant
u8,
/// Enum variant resulting from decoding wrong value
T,
),
/// Test case failure due to wrong `PartialEq` or `Eq` implementation:
/// enum variant is not equal to itself
#[display("Enum variant `{0:?}` is not equal to itself")]
FailedEq(#[doc = "Enum variant which is not equal to itself"] T),
/// Test case failure due to wrong `PartialEq` or `Eq` implementation:
/// two distinct enum variants are still equal
#[display("Two distinct enum variants `{0:?}` and `{1:?}` are equal")]
FailedNe(
/// First of two enum variants which are treated as equal
T,
/// Second of two enum variants which are treated as equal
T,
),
/// Test case failure due to wrong `PartialOrd` or `Ord` implementation
/// happening when enum variants ordering is broken
#[display(
"Comparing enum variants `{0:?}` and `{1:?}` results in wrong ordering"
)]
FailedOrd(
/// First of two enum variants which are disordered. This variant
/// should smaller than the second one, but `Ord` operation
/// treats it as a larger one
T,
/// Second of two enum variants which are disordered. This variant
/// should larger than the second one, but `Ord` operation
/// treats it as a smaller one
T,
),
}
/// Macro testing encodings of all enum variants.
///
/// NB: If the enum has primitive assigned values,
/// [`test_encoding_enum_by_values`] should be used instead if this macro. If
/// primitive values are `u8`-based, please use
/// [`test_encoding_enum_u8_exhaustive`].
///
/// Macro expands into an expression of `Result<(),`
/// [`EnumEncodingTestFailure`]`>` type.
///
/// # Covered test case
///
/// - Strict encoding must match little-endian encoding of the value
/// - Roundtrip encoding-decoding of the enum variant must result in the
/// original value
#[macro_export]
#[deprecated(since = "1.6.1", note = "use strict_encoding_test crate instead")]
macro_rules! test_encoding_enum {
($enum:path as $ty:ty; $( $item:path => $val:expr ),+) => {
test_encoding_enum!(strict_encoding => $enum as $ty; $( $item => $val ),+)
};
($se:ident => $enum:path as $ty:ty; $( $item:path => $val:expr ),+) => {
Ok(())
$(
.and_then(|_| {
use $se::test_helpers::EnumEncodingTestFailure;
match $se::strict_serialize(&$item) {
Ok(bytes) if bytes == &$val.to_le_bytes() => {
let deser = $se::strict_deserialize(bytes.clone())
.map_err(|e| EnumEncodingTestFailure::DecoderFailure(
$item, e, bytes
))?;
if deser != $item {
Err(EnumEncodingTestFailure::DecodedDiffersFromOriginal {
original: $item,
decoded: deser,
})
} else {
Ok(())
}
},
Ok(wrong) => Err(EnumEncodingTestFailure::EncodedValueMismatch {
enum_name: stringify!($enum),
variant_name: stringify!($item),
expected: $val.to_le_bytes().to_vec(),
actual: wrong,
}),
Err(err) => Err(
EnumEncodingTestFailure::EncoderFailure($item, err)
),
}
})
)+
}
}
/// Macro testing encodings of all enum variants for enums with assigned
/// primitive integer values.
///
/// Macro expands into an expression of `Result<(),`
/// [`EnumEncodingTestFailure`]`>` type.
///
/// Macro extends functionality of [`test_encoding_enum`] and should be used
/// whenever enum has assigned primitive integer values. If the primitive values
/// are of `u8` type, [`test_encoding_enum_u8_exhaustive`] should be used
/// instead of this macro.
///
/// # Covered test cases
///
/// - Each enum variant must have a primitive value
/// - Primitive value representing enum variant must be equal to strict encoding
/// of the same variant. If a primitive enum value occupies of several bytes
/// (`u16`, `u32` and other large integer types), strict encoding must match
/// little-endian encoding of the value
/// - Roundtrip encoding-decoding of the enum variant must result in the
/// original value
/// - Each enum variant must be equal to itself
/// - Each enum variant must not be equal to any other enum variant
/// - Enum variants must be ordered according to their primitive values
#[macro_export]
#[deprecated(since = "1.6.1", note = "use strict_encoding_test crate instead")]
macro_rules! test_encoding_enum_by_values {
($enum:path as $ty:ty; $( $item:path => $val:expr ),+) => {
test_encoding_enum_by_values!(strict_encoding => $enum as $ty; $( $item => $val ),+)
};
($se:ident => $enum:path as $ty:ty; $( $item:path => $val:expr ),+) => {
test_encoding_enum!($se => $enum as $ty; $( $item => $val ),+)
$(
.and_then(|_| {
use $se::test_helpers::EnumEncodingTestFailure;
if $item as $ty != ($val) {
return Err(EnumEncodingTestFailure::ValueMismatch {
enum_name: stringify!($enum),
variant_name: stringify!($item),
expected: ($val) as usize,
actual: $item as usize,
})
}
Ok(())
})
)+
.and_then(|_| {
use $se::test_helpers::EnumEncodingTestFailure;
let mut all = ::std::collections::BTreeSet::new();
$( all.insert($item); )+
for (idx, a) in all.iter().enumerate() {
if a != a {
return Err(EnumEncodingTestFailure::FailedEq(*a));
}
for b in all.iter().skip(idx + 1) {
if a == b || (*a as usize) == (*b as usize) {
return Err(EnumEncodingTestFailure::FailedNe(*a, *b))
}
if (a >= b && (*a as usize) < (*b as usize)) ||
(a <= b && (*a as usize) > (*b as usize)) {
return Err(EnumEncodingTestFailure::FailedOrd(*a, *b))
}
}
}
Ok(())
})
}
}
/// Macro testing encoding of all possible enum values, covering full range of
/// `u8` values, including enum out-of-range values.
///
/// Macro expands into an expression of `Result<(),`
/// [`EnumEncodingTestFailure`]`>` type.
///
/// Macro extends functionality of [`test_encoding_enum_by_values`] and should
/// be used whenever enum with assigned primitive values is represented by `u8`
/// integers.
///
/// # Covered test cases
///
/// - Each enum variant must have a primitive value
/// - Primitive value representing enum variant must be equal to strict encoding
/// of the same variant. If a primitive enum value occupies of several bytes
/// (`u16`, `u32` and other large integer types), strict encoding must match
/// little-endian encoding of the value
/// - Roundtrip encoding-decoding of the enum variant must result in the
/// original value
/// - Each enum variant must be equal to itself
/// - Each enum variant must not be equal to any other enum variant
/// - Enum variants must be ordered according to their primitive values
/// - All 8-bit integers which do not match any of enum variants must not be
/// decoded with strict decoder into a valid enum and their decoding must
/// result in [`Error::EnumValueNotKnown`] error.
#[macro_export]
#[deprecated(since = "1.6.1", note = "use strict_encoding_test crate instead")]
macro_rules! test_encoding_enum_u8_exhaustive {
($enum:path; $( $item:path => $val:expr ),+) => {
test_encoding_enum_u8_exhaustive!(strict_encoding => $enum as u8; $( $item => $val ),+)
};
($enum:path as $ty:ty; $( $item:path => $val:expr ),+) => {
test_encoding_enum_u8_exhaustive!(strict_encoding => $enum as $ty; $( $item => $val ),+)
};
($se:ident => $enum:path; $( $item:path => $val:expr ),+) => {
test_encoding_enum_u8_exhaustive!($se => $enum as u8; $( $item => $val ),+)
};
($se:ident => $enum:path as $ty:ty; $( $item:path => $val:expr ),+) => {
test_encoding_enum_by_values!($se => $enum as $ty; $( $item => $val ),+).and_then(|_| {
use $se::test_helpers::EnumEncodingTestFailure;
let mut set = ::std::collections::HashSet::new();
$( set.insert($val); )+
for x in 0..=u8::MAX {
if !set.contains(&x) {
match $se::strict_deserialize(&[x]) {
Err($se::Error::EnumValueNotKnown(stringify!($enum), a)) if a == x as usize => {},
Err(err) => return Err(
EnumEncodingTestFailure::DecoderWrongErrorOnUnknownValue(x, err)
),
Ok(variant) => return Err(
EnumEncodingTestFailure::UnknownDecodesToVariant(x, variant)
),
}
}
}
Ok(())
})
}
}
/// Failures happening during strict encoding tests of struct and union
/// encodings.
///
/// NB: These errors are specific for testing configuration and should not be
/// used in non-test environment.
#[derive(Clone, PartialEq, Debug, Display, Error)]
pub enum DataEncodingTestFailure<T>
where
T: StrictEncode + StrictDecode + PartialEq + Debug + Clone,
{
/// Failure during encoding enum variant
#[display("Failure during encoding: {0:?}")]
EncoderFailure(#[doc = "Encoder error"] Error),
/// Failure during decoding binary representation of enum variant
#[display(
"Failure during decoding: `{0:?}`
\tByte representation: {1:#x?}"
)]
DecoderFailure(
#[doc = "Decoder error"] Error,
#[doc = "Byte string which failed to decode"] Vec<u8>,
),
/// Failure of the strict encode implementation: encoder reports incorrect
/// length of the serialized data
#[display(
"Encoder reported incorrect length of the serialized data: \
`{returned}` instead of `{actual}`"
)]
EncoderReturnedWrongLength {
/// Actual length of the serialized data
actual: usize,
/// Incorrect length returned by the encoder
returned: usize,
},
/// Test case failure representing mismatch between object produced
/// by decoding from the originally encoded object
#[display(
"Roundtrip encoding of `{original:?}` produced different object \
`{transcoded:?}`"
)]
TranscodedObjectDiffersFromOriginal {
/// Original value, which was encoded
original: T,
/// The value resulting from decoding encoded `original` value
transcoded: T,
},
/// Test case failure representing mismatch between original test vector
/// and serialization of the object decoded from that test vector
#[display(
"Serialization of the object `{object:?}` decoded from a test vector \
results in a different byte string:
\tOriginal: {original:#x?}
\tSerialization: {transcoded:#x?}
"
)]
TranscodedVecDiffersFromOriginal {
/// Original test vector, which was decoded
original: Vec<u8>,
/// Byte string produced by encoding object, decoded from the test
/// vector
transcoded: Vec<u8>,
/// Object decoded from the test vector
object: T,
},
}
/// Test helper performing encode-decode roundtrip for a provided object. Object
/// type must be `PartialEq + Clone + Debug`.
///
/// # Returns
///
/// If suceeds, encoded byte string representing the object. Otheriwse,
/// [`DataEncodingTestFailure`] (see description below)
///
/// # Error
///
/// Errors on:
/// - encoding or decoding failures;
/// - if the original object is not equivalent to its decoded version;
/// - if encoder returns number of bytes that does not match the length of the
/// encoded data.
///
/// # Panics
///
/// Function does not panics and instead returns [`DataEncodingTestFailure`] for
/// each type of test failures.
#[inline]
#[deprecated(since = "1.6.1", note = "use strict_encoding_test crate instead")]
pub fn test_object_encoding_roundtrip<T>(
object: &T,
) -> Result<Vec<u8>, DataEncodingTestFailure<T>>
where
T: StrictEncode + StrictDecode + PartialEq + Clone + Debug,
{
let mut encoded_object: Vec<u8> = vec![];
let written = object
.strict_encode(&mut encoded_object)
.map_err(DataEncodingTestFailure::EncoderFailure)?;
let len = encoded_object.len();
if written != len {
return Err(DataEncodingTestFailure::EncoderReturnedWrongLength {
actual: len,
returned: written,
});
}
let decoded_object =
T::strict_decode(&encoded_object[..]).map_err(|e| {
DataEncodingTestFailure::DecoderFailure(e, encoded_object.clone())
})?;
if &decoded_object != object {
return Err(
DataEncodingTestFailure::TranscodedObjectDiffersFromOriginal {
original: object.clone(),
transcoded: decoded_object,
},
);
}
Ok(encoded_object)
}
/// Test helper performing decode-eecode roundtrip for a provided test vector
/// byte string.
///
/// # Returns
///
/// If suceeds, decoded object, which must have `PartialEq + Clone + Debug`
/// type. Otheriwse, [`DataEncodingTestFailure`] (see description below)
///
/// # Error
///
/// Errors on:
/// - encoding or decoding failures;
/// - if the original test vector is not equivalent to its transcoded version;
/// - if encoder returns number of bytes that does not match the length of the
/// test vector.
///
/// # Panics
///
/// Function does not panics and instead returns [`DataEncodingTestFailure`] for
/// each type of test failures.
#[deprecated(since = "1.6.1", note = "use strict_encoding_test crate instead")]
pub fn test_vec_decoding_roundtrip<T>(
test_vec: impl AsRef<[u8]>,
) -> Result<T, DataEncodingTestFailure<T>>
where
T: StrictEncode + StrictDecode + PartialEq + Clone + Debug,
{
let test_vec = test_vec.as_ref();
let decoded_object = T::strict_decode(test_vec).map_err(|e| {
DataEncodingTestFailure::DecoderFailure(e, test_vec.to_vec())
})?;
let encoded_object = test_object_encoding_roundtrip(&decoded_object)?;
if test_vec != encoded_object {
return Err(
DataEncodingTestFailure::TranscodedVecDiffersFromOriginal {
original: test_vec.to_vec(),
transcoded: encoded_object,
object: decoded_object,
},
);
}
Ok(decoded_object)
}
/// Test helper performing double encode-decode roundtrip for an object
/// and a matching binary encoding test vector. Object type must be
/// `PartialEq + Clone + Debug`.
///
/// # Error
///
/// Errors on:
/// - encoding or decoding failures;
/// - if the original object is not equivalent to its decoded version;
/// - if the original test vector is not equivalent to its transcoded version;
/// - if encoder returns number of bytes that does not match the length of the
/// test vector.
///
/// # Panics
///
/// Function does not panics and instead returns [`DataEncodingTestFailure`] for
/// each type of test failures.
#[deprecated(since = "1.6.1", note = "use strict_encoding_test crate instead")]
pub fn test_encoding_roundtrip<T>(
object: &T,
test_vec: impl AsRef<[u8]>,
) -> Result<(), DataEncodingTestFailure<T>>
where
T: StrictEncode + StrictDecode + PartialEq + Clone + Debug,
{
let decoded_object = test_vec_decoding_roundtrip(test_vec)?;
if object != &decoded_object {
return Err(
DataEncodingTestFailure::TranscodedObjectDiffersFromOriginal {
original: object.clone(),
transcoded: decoded_object,
},
);
}
Ok(())
}