use flow_abi_testing::catalog as common;
use common::*;
use flow_abi::{
Catalog, DecodeErrorKind, Limits, Mode, Scalar, Step, decode_bytes, encode_bytes, write_scalar,
};
use flow_types::RecordShape;
use flow_types::{Body, Decl, DeclId, Type};
use flow_value::Value;
#[test]
fn a_scalar_written_from_a_value_of_another_type_is_refused_naming_the_scalar() {
for (scalar, name) in [
(Scalar::Unit, "Unit"),
(Scalar::Bool, "Bool"),
(Scalar::Int, "Int"),
(Scalar::Decimal, "Decimal"),
(Scalar::Duration, "Duration"),
(Scalar::Instant, "Instant"),
] {
let mut out = Vec::new();
let error = write_scalar(&mut out, scalar, &text("a")).unwrap_err();
assert_eq!(error.problem, format!("is not a value of {name}"));
assert!(out.is_empty(), "nothing is written for a refused {name}");
}
}
#[test]
fn a_value_whose_shape_is_not_its_types_is_not_encoded() {
let catalog = catalog();
let error = encode_bytes(&catalog, &Type::list(Type::Int), &int("1")).unwrap_err();
assert_eq!(error.path, "");
assert_eq!(error.problem, "is not a value of List<Int>");
}
#[test]
fn a_dict_entry_not_of_its_type_is_refused_at_its_place() {
let catalog = catalog();
let ty = Type::dict(Type::Text, Type::Int);
let error = encode_bytes(&catalog, &ty, &dict(vec![(text("a"), text("b"))])).unwrap_err();
assert_eq!(error.path, "[0][1]");
assert_eq!(error.problem, "is not a value of Int");
}
#[test]
fn a_constructors_payload_not_of_its_type_is_refused_at_its_place() {
let catalog = catalog();
let failed = ctor(&catalog, STATUS, 1, vec![int("1")]);
let error = encode_bytes(&catalog, &named(STATUS), &failed).unwrap_err();
assert_eq!(error.path, "Failed[0]");
assert_eq!(error.problem, "is not a value of Text");
}
#[test]
fn rebuilding_a_type_with_no_encoding_holds_no_value() {
let function = Type::Function {
params: vec![],
result: Box::new(Type::Unit),
};
let error =
decode_bytes(&catalog(), &function, &[], Mode::Rebuild, &Limits::LANGUAGE).unwrap_err();
assert_eq!(
error.problem,
"has a type with no encoding, so it holds no value"
);
}
#[test]
fn a_set_of_host_tokens_cannot_be_ordered_and_is_refused() {
let ty = Type::set(named(PAGE));
let error = decode_bytes(
&catalog(),
&ty,
&[2, 1, b'a', 1, b'b'],
Mode::Reply,
&Limits::LANGUAGE,
)
.unwrap_err();
assert_eq!(error.kind, DecodeErrorKind::Mismatch);
assert_eq!(error.problem, "has elements that are not data");
}
#[test]
fn a_refusal_in_a_wide_record_names_the_field_it_is_in() {
let names: Vec<String> = (0..4096).map(|at| format!("f{at}")).collect();
let decl = Decl {
lexical_ordinal: 1,
identity: local("Wide"),
doc: None,
members: Vec::new(),
params: Vec::new(),
opaque: false,
body: Body::Record(
RecordShape::new("Wide", names.iter().map(String::as_str)),
vec![Type::Bool; names.len()],
),
};
let catalog =
Catalog::new(vec![decl], option_shape(), Some(json_shape())).expect("std's shapes");
let wide = Type::Named(DeclId(0), vec![]);
let mut bytes = vec![1; names.len()];
assert!(decode_bytes(&catalog, &wide, &bytes, Mode::Reply, &Limits::LANGUAGE).is_ok());
*bytes.last_mut().expect("a field") = 2;
let error = decode_bytes(&catalog, &wide, &bytes, Mode::Reply, &Limits::LANGUAGE)
.expect_err("2 is no Bool");
assert_eq!(error.path.steps(), [Step::Field("f4095".into())]);
assert_eq!(error.kind, DecodeErrorKind::Syntax { at: 4095 });
}
fn looping() -> (Catalog, Type) {
let id = DeclId(0);
let decl = Decl {
lexical_ordinal: 1,
identity: local("Loop"),
doc: None,
members: Vec::new(),
params: Vec::new(),
opaque: false,
body: Body::Record(
RecordShape::new("Loop", ["next"]),
vec![Type::Named(id, vec![])],
),
};
let catalog =
Catalog::new(vec![decl], option_shape(), Some(json_shape())).expect("std's shapes");
(catalog, Type::Named(id, vec![]))
}
#[test]
fn a_type_that_holds_itself_through_parts_that_take_no_bytes_counts_as_one_that_takes_bytes() {
let (catalog, looping) = looping();
let error = decode_bytes(
&catalog,
&Type::list(looping),
&[2],
Mode::Reply,
&Limits::LANGUAGE,
)
.unwrap_err();
assert_eq!(error.kind, DecodeErrorKind::Mismatch);
assert!(
error.problem.contains("more than the 0 bytes left can"),
"{}",
error.problem
);
let pairs = Type::Tuple(vec![Type::Unit, Type::Unit]);
assert!(matches!(
decode_bytes(
&catalog,
&Type::list(pairs),
&[2],
Mode::Reply,
&Limits::LANGUAGE
),
Ok(Value::List(_))
));
}
#[test]
fn elements_written_in_no_bytes_are_bounded_across_the_whole_value_not_per_collection() {
let catalog = catalog();
let limits = Limits {
text_bytes: 10,
..Limits::LANGUAGE
};
let nested = Type::list(Type::list(Type::Unit));
let error = decode_bytes(&catalog, &nested, &[2, 6, 6], Mode::Reply, &limits).unwrap_err();
assert_eq!(error.kind, DecodeErrorKind::TooLarge);
assert_eq!(error.path, "[1]");
assert_eq!(
error.problem,
"holds more than 10 elements written in no bytes"
);
assert!(
matches!(
decode_bytes(&catalog, &nested, &[2, 4, 6], Mode::Reply, &limits),
Ok(Value::List(_))
),
"ten elements written in no bytes are within a limit of ten"
);
}
#[test]
fn a_count_of_elements_written_in_no_bytes_past_the_limit_is_refused_before_any_is_made() {
let catalog = catalog();
let mut bytes = Vec::new();
flow_abi::primitives::write_uint(&mut bytes, u64::MAX >> 1);
let error = decode_bytes(
&catalog,
&Type::list(Type::Unit),
&bytes,
Mode::Reply,
&Limits::LANGUAGE,
)
.unwrap_err();
assert_eq!(error.kind, DecodeErrorKind::TooLarge, "{}", error.problem);
}