/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/// We'd like to just assert_eq!(original_payload, our_payload), but our JSON /// serialization format is different (we don't have spaces after commas or /// colons), so we need to do this instead. fn assert_payloads_equivalent(our_payload: &[u8], original_payload: &[u8]) { // Per the above we expect our payload to always be smaller than the // original.
assert!(our_payload.len() <= original_payload.len());
let our_value: serde_json::Value = serde_json::from_slice(our_payload).unwrap(); let original_value: serde_json::Value = serde_json::from_slice(original_payload).unwrap(); if our_value == original_value { return;
}
fn test_roundtrip(payload: &[u8], token: &Token, base64: &[u8]) { let binary = base64::decode(base64).unwrap(); let raw_token = RawToken::from_buffer(&binary).unwrap(); let from_binary_token = Token::from_raw_token(&raw_token, mock_verify).unwrap();
assert_eq!(&from_binary_token, token);
// LMAO, payload in the documentation and the examples have members out of // order so this doesn't hold. // assert_eq!(std::str::from_utf8(raw_token.payload()).unwrap(), std::str::from_utf8(payload).unwrap());
let our_payload = from_binary_token.to_payload();
assert_payloads_equivalent(&our_payload, payload);
assert_payloads_equivalent(&our_payload, raw_token.payload());
let signed = from_binary_token
.to_signed_token_with_payload(|_data| raw_token.signature.clone(), raw_token.payload());
assert_eq!(binary, signed);
let new_base64 = base64::encode(signed);
assert_eq!(new_base64, std::str::from_utf8(base64).unwrap());
}
#[test] fn basic() { // The one from the example. let payload =
r#"{"origin": "https://example.com:443", "feature": "Frobulate", "expiry": 1609459199}"#; let token = Token::from_payload(LATEST_VERSION, payload.as_bytes()).unwrap();
assert_eq!(token.origin, "https://example.com:443");
assert_eq!(token.feature, "Frobulate");
assert_eq!(token.expiry, 1609459199);
assert_eq!(token.is_subdomain, false);
assert_eq!(token.is_third_party, false);
assert!(token.usage.is_none());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.