Debian Codegraph @

third_party/rust/origin-trial-token/tests.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5 use super::*;
6
7 fn mock_verify(_signature: &[u8; 64], _data: &[u8]) -> bool {
8 true
9 }
10
11 /// We'd like to just assert_eq!(original_payload, our_payload), but our JSON
12 /// serialization format is different (we don't have spaces after commas or
13 /// colons), so we need to do this instead.
14 fn assert_payloads_equivalent(our_payload: &[u8], original_payload: &[u8]) {
15 // Per the above we expect our payload to always be smaller than the
16 // original.
17 assert!(our_payload.len() <= original_payload.len());
18
19 let our_value: serde_json::Value = serde_json::from_slice(our_payload).unwrap();
20 let original_value: serde_json::Value = serde_json::from_slice(original_payload).unwrap();
21 if our_value == original_value {
22 return;
23 }
24
25 assert_eq!(
26 std::str::from_utf8(our_payload).unwrap(),
27 std::str::from_utf8(original_payload).unwrap(),
28 "Mismatched payloads"
29 );
30 }
31
32 fn test_roundtrip(payload: &[u8], token: &Token, base64: &[u8]) {
33 let binary = base64::decode(base64).unwrap();
34 let raw_token = RawToken::from_buffer(&binary).unwrap();
35 let from_binary_token = Token::from_raw_token(&raw_token, mock_verify).unwrap();
36 assert_eq!(&from_binary_token, token);
37
38 // LMAO, payload in the documentation and the examples have members out of
39 // order so this doesn't hold.
40 // assert_eq!(std::str::from_utf8(raw_token.payload()).unwrap(), std::str::from_utf8(payload).unwrap());
41
42 let our_payload = from_binary_token.to_payload();
43 assert_payloads_equivalent(&our_payload, payload);
44 assert_payloads_equivalent(&our_payload, raw_token.payload());
45
46 let signed = from_binary_token
47 .to_signed_token_with_payload(|_data| raw_token.signature.clone(), raw_token.payload());
48 assert_eq!(binary, signed);
49
50 let new_base64 = base64::encode(signed);
51 assert_eq!(new_base64, std::str::from_utf8(base64).unwrap());
52 }
53
54 #[test]
55 fn basic() {
56 // The one from the example.
57 let payload =
58 r#"{"origin": "https://example.com:443", "feature": "Frobulate", "expiry": 1609459199}"#;
59 let token = Token::from_payload(LATEST_VERSION, payload.as_bytes()).unwrap();
60 assert_eq!(token.origin, "https://example.com:443");
61 assert_eq!(token.feature, "Frobulate");
62 assert_eq!(token.expiry, 1609459199);
63 assert_eq!(token.is_subdomain, false);
64 assert_eq!(token.is_third_party, false);
65 assert!(token.usage.is_none());
66
67 test_roundtrip(payload.as_bytes(), &token, b"A9YTk5WLM0uhXPj2OE/dEj8mEdWbcWOvCyWMNdRFiCZpBRuynxJMx1i/SO5pRT7UhoCSDTieoh9qOCMHsc2y5w4AAABTeyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5IjogMTYwOTQ1OTE5OX0=");
68 }
69
70 #[test]
71 fn subdomain() {
72 // The one from the example.
73 let payload = r#"{"origin": "https://example.com:443", "isSubdomain": true, "feature": "Frobulate", "expiry": 1609459199}"#;
74 let token = Token::from_payload(LATEST_VERSION, payload.as_bytes()).unwrap();
75 assert_eq!(token.origin, "https://example.com:443");
76 assert_eq!(token.feature, "Frobulate");
77 assert_eq!(token.expiry, 1609459199);
78 assert_eq!(token.is_subdomain, true);
79 assert_eq!(token.is_third_party, false);
80 assert!(token.usage.is_none());
81
82 test_roundtrip(payload.as_bytes(), &token, b"AzHieSb3NXHXhJ1zvxNcmUeR351wzlXwJK7pYM8MCFfNenvonZi30kS0GOKWUleIyats/2aTB1HoiCmLWIvG5AgAAABoeyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLmNvbTo0NDMiLCAiaXNTdWJkb21haW4iOiB0cnVlLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5IjogMTYwOTQ1OTE5OX0=");
83 }
84
85 #[test]
86 fn third_party() {
87 let payload = r#"{"origin": "https://thirdparty.com:443", "feature": "Frobulate", "expiry": 1609459199, "isThirdParty": true}"#;
88 let token = Token::from_payload(LATEST_VERSION, payload.as_bytes()).unwrap();
89 assert_eq!(token.origin, "https://thirdparty.com:443");
90 assert_eq!(token.feature, "Frobulate");
91 assert_eq!(token.expiry, 1609459199);
92 assert_eq!(token.is_subdomain, false);
93 assert_eq!(token.is_third_party, true);
94 assert!(token.usage.is_none());
95
96 test_roundtrip(payload.as_bytes(), &token, b"Ax8UsCU9EUBRj8PZG147cOO7VqR86BF13TSu6w2wRqixzJ+fEUULvOQimXwWl1ETYCfAZMlvvAqoFYB8HxrsZA4AAABseyJvcmlnaW4iOiAiaHR0cHM6Ly90aGlyZHBhcnR5LmNvbTo0NDMiLCAiaXNUaGlyZFBhcnR5IjogdHJ1ZSwgImZlYXR1cmUiOiAiRnJvYnVsYXRlIiwgImV4cGlyeSI6IDE2MDk0NTkxOTl9");
97 }
98
99 #[test]
100 fn third_party_usage_restriction() {
101 let payload = r#"{"origin": "https://thirdparty.com:443", "feature": "Frobulate", "expiry": 1609459199, "isThirdParty": true, "usage": "subset"}"#;
102 let token = Token::from_payload(LATEST_VERSION, payload.as_bytes()).unwrap();
103 assert_eq!(token.origin, "https://thirdparty.com:443");
104 assert_eq!(token.feature, "Frobulate");
105 assert_eq!(token.expiry, 1609459199);
106 assert_eq!(token.is_subdomain, false);
107 assert_eq!(token.is_third_party, true);
108 assert_eq!(token.usage, Usage::Subset);
109
110 test_roundtrip(payload.as_bytes(), &token, b"AzEs7XzQG5ktWF/puroSU5RzxPEdEUUhqwXtL2hItZoJU0bghKwbsTKVghkR95GHSfINTBnxwRBnFVfYGJLm8AUAAAB/eyJvcmlnaW4iOiAiaHR0cHM6Ly90aGlyZHBhcnR5LmNvbTo0NDMiLCAiaXNUaGlyZFBhcnR5IjogdHJ1ZSwgInVzYWdlIjogInN1YnNldCIsICJmZWF0dXJlIjogIkZyb2J1bGF0ZSIsICJleHBpcnkiOiAxNjA5NDU5MTk5fQ==");
111 }