diff --git a/command/assets/protocol_mismatch.json b/command/assets/protocol_mismatch.json index ec83fd11d..224d3bc81 100644 --- a/command/assets/protocol_mismatch.json +++ b/command/assets/protocol_mismatch.json @@ -1,6 +1,6 @@ { "id": "ID_TEST", - "version": 0, + "version": 1, "type": "PROXY", "data": { "type": "ADD_INSTANCE", diff --git a/command/src/data.rs b/command/src/data.rs index 8f909c38a..13f8055e7 100644 --- a/command/src/data.rs +++ b/command/src/data.rs @@ -173,7 +173,14 @@ impl<'de> serde::de::Visitor<'de> for ConfigMessageVisitor { None => return Err(serde::de::Error::missing_field("id")), }; let _version = match version { - Some(version) => version, + Some(version) => { + if version > PROTOCOL_VERSION { + let msg = format!("configuration protocol version mismatch: Sōzu handles up to version {}, the message uses version {}", PROTOCOL_VERSION, version); + return Err(serde::de::Error::custom(msg)); + } else { + version + } + }, None => return Err(serde::de::Error::missing_field("version")), }; @@ -305,6 +312,7 @@ impl serde::Serialize for ConfigMessage { mod tests { use super::*; use serde_json; + use std::error::Error; use hex::FromHex; use certificate::split_certificate_chain; use messages::{Application,CertificateAndKey,CertFingerprint,Order,HttpFront,HttpsFront,Instance}; @@ -322,6 +330,16 @@ mod tests { }))); } + #[test] + fn protocol_version_mismatch_test() { + let data = include_str!("../assets/protocol_mismatch.json"); + let msg = format!("configuration protocol version mismatch: Sōzu handles up to version {}, the message uses version {} at line 14 column 1", PROTOCOL_VERSION, 1); + let res: Result = serde_json::from_str(data); + + let err = format!("{}", res.unwrap_err()); + assert_eq!(err, msg); + } + macro_rules! test_message ( ($name: ident, $filename: expr, $expected_message: expr) => (