Skip to content

Commit

Permalink
Make sure there aren't version checks that panic with minimum support…
Browse files Browse the repository at this point in the history
…ed version (#25)

* Make sure there aren't version checks that panic with minimum supported version

* Check for maximum versions as well

---------

Co-authored-by: slinkydeveloper <[email protected]>
  • Loading branch information
jackkleeman and slinkydeveloper authored Dec 13, 2024
1 parent 777676f commit 3c342c4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/service_protocol/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ pub struct Encoder {}

impl Encoder {
pub fn new(service_protocol_version: Version) -> Self {
assert_eq!(
service_protocol_version,
Version::maximum_supported_version(),
"Encoder only supports service protocol version {:?}",
assert!(
service_protocol_version >= Version::minimum_supported_version(),
"Encoder only supports service protocol version {:?} <= x <= {:?}",
Version::minimum_supported_version(),
Version::maximum_supported_version()
);
Self {}
Expand Down Expand Up @@ -105,10 +105,10 @@ pub struct Decoder {

impl Decoder {
pub fn new(service_protocol_version: Version) -> Self {
assert_eq!(
service_protocol_version,
Version::maximum_supported_version(),
"Decoder only supports service protocol version {:?}",
assert!(
service_protocol_version >= Version::minimum_supported_version(),
"Decoder only supports service protocol version {:?} <= x <= {:?}",
Version::minimum_supported_version(),
Version::maximum_supported_version()
);
Self {
Expand Down
5 changes: 5 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,8 @@ fn take_output_on_newly_initialized_vm() {
eq(TakeOutputResult::Buffer(Bytes::default()))
);
}

#[test]
fn instantiate_core_vm_minimum_supported_version() {
CoreVM::mock_init(Version::minimum_supported_version());
}
11 changes: 9 additions & 2 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ impl super::VM for CoreVM {
.ok_or(errors::MISSING_CONTENT_TYPE)?
.parse::<Version>()?;

if version != Version::maximum_supported_version() {
if version < Version::minimum_supported_version()
|| version > Version::maximum_supported_version()
{
return Err(Error::new(
errors::codes::UNSUPPORTED_MEDIA_TYPE,
format!("Unsupported protocol version {:?}", version),
format!(
"Unsupported protocol version {:?}. Supported versions: {:?} to {:?}",
version,
Version::minimum_supported_version(),
Version::maximum_supported_version()
),
));
}

Expand Down

0 comments on commit 3c342c4

Please sign in to comment.