From 00c7ccbbcbf04d9bc7e44da0c3201108d254417c Mon Sep 17 00:00:00 2001 From: Carlos Santos Date: Mon, 28 Sep 2026 16:00:17 -0300 Subject: [PATCH] Add test coverage for a claimed req- parameter Closes https://github.com/payjoin/bitcoin_uri/issues/7 `required_understood` tested that a non-`req-` unknown parameter is silently ignored, not that a `req-` parameter someone claims lets the URI parse. Rename it to say what it actually covers, and add the missing case: an Extras implementation that claims a `req-` key must not error, the same way payjoin's `pj`/`pjos` already don't. A third test confirms claiming one `req-` key doesn't excuse a different unclaimed one. Test-only change, no production code touched. --- src/lib.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7a37b7e..0a73c0e 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -420,8 +420,14 @@ mod tests { assert!(uri.is_err()); } + /// Renamed from `required_understood`. The old name claimed to cover + /// `req-` enforcement, but the params here don't carry the `req-` + /// prefix at all, so what it actually tests is that plain unknown + /// (non-`req-`) params are silently ignored rather than rejected. See + /// https://github.com/payjoin/bitcoin_uri/issues/7, which also asks for + /// the test added below. #[test] - fn required_understood() { + fn non_required_unknown_params_are_silently_ignored() { let input = "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?somethingyoudontunderstand=50&somethingelseyoudontget=999"; let uri = input.parse::>().unwrap().require_network(bitcoin::Network::Bitcoin).unwrap(); assert_eq!(uri.address.to_string(), "1andreas3batLhQa2FawWjeyjCqyBzypd"); @@ -432,6 +438,68 @@ mod tests { assert_eq!(uri.to_string(), "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd"); } + /// A minimal `Extras` that claims exactly one `req-` key, to exercise + /// the side `required_not_understood` above doesn't: a `req-` parameter + /// that some extension *does* understand must let the URI parse, not + /// just reject the ones nobody claims. + #[derive(Debug, Default, Clone)] + struct ReqFooExtras; + + #[derive(Debug, Default, Clone)] + struct ReqFooState; + + impl DeserializationError for ReqFooExtras { + type Error = core::convert::Infallible; + } + + impl DeserializeParams<'_> for ReqFooExtras { + type DeserializationState = ReqFooState; + } + + impl DeserializationState<'_> for ReqFooState { + type Value = ReqFooExtras; + + fn is_param_known(&self, key: &str) -> bool { + key == "req-foo" + } + + fn deserialize_temp(&mut self, key: &str, _value: Param<'_>) -> Result { + Ok(if key == "req-foo" { + de::ParamKind::Known + } else { + de::ParamKind::Unknown + }) + } + + fn finalize(self) -> Result { + Ok(ReqFooExtras) + } + } + + #[test] + fn a_claimed_req_parameter_parses_successfully() { + // https://github.com/payjoin/bitcoin_uri/issues/7: `required_not_understood` + // above only covers a `req-` key nobody claims. This is the other half: + // a `req-` key an `Extras` implementation does claim must not cause a + // parse error, the same way `pj`/`pjos` already don't for payjoin. + let input = "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?req-foo=50"; + let uri = input + .parse::>() + .unwrap() + .require_network(bitcoin::Network::Bitcoin) + .unwrap(); + assert_eq!(uri.address.to_string(), "1andreas3batLhQa2FawWjeyjCqyBzypd"); + } + + #[test] + fn an_unclaimed_and_a_claimed_req_parameter_together_still_fail() { + // Claiming one `req-` key does not excuse an unrelated unclaimed one: + // every `req-` present must be understood by someone, not just one. + let input = "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?req-foo=50&req-bar=1"; + let uri = input.parse::>(); + assert!(uri.is_err()); + } + #[test] fn label_with_rfc3986_param_separator() { let input = "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?label=foo%26bar%20%3D%20baz/blah?;:@";