Skip to content

Commit

Permalink
check for configuration protocol version incompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Dec 21, 2017
1 parent 1228b3c commit 2278726
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion command/assets/protocol_mismatch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "ID_TEST",
"version": 0,
"version": 1,
"type": "PROXY",
"data": {
"type": "ADD_INSTANCE",
Expand Down
20 changes: 19 additions & 1 deletion command/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
};

Expand Down Expand Up @@ -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};
Expand All @@ -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<ConfigMessage, serde_json::Error> = 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) => (

Expand Down

0 comments on commit 2278726

Please sign in to comment.