|
1 | | -#![cfg(feature = "cli")] |
| 1 | +#![cfg(feature = "serde-decoded")] |
2 | 2 |
|
3 | 3 | use stellar_strkey::{ed25519, *}; |
4 | 4 |
|
@@ -123,6 +123,52 @@ fn test_ed25519_signed_payload() { |
123 | 123 | ); |
124 | 124 | } |
125 | 125 |
|
| 126 | +// Regression tests for deserializing `Decoded<Strkey>` from deserializers that |
| 127 | +// yield owned (rather than borrowed) string keys. `serde_json::from_str` |
| 128 | +// borrows the variant key directly from the input buffer, but `from_value` and |
| 129 | +// `from_reader` cannot, so the map visitor must accept an owned key. The |
| 130 | +// `encode` CLI is one consumer that hits this, via `from_value`. See issue |
| 131 | +// #124. |
| 132 | + |
| 133 | +#[test] |
| 134 | +fn test_decode_strkey_from_value() { |
| 135 | + let value = serde_json::json!({ |
| 136 | + "public_key_ed25519": "3330317ec241d79943bb9aa7c8ea5f8b89f9c6ea351fe03f8ec3d1127137d484", |
| 137 | + }); |
| 138 | + let Decoded(strkey): Decoded<Strkey> = serde_json::from_value(value).unwrap(); |
| 139 | + assert!(matches!(strkey, Strkey::PublicKeyEd25519(_))); |
| 140 | +} |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn test_decode_strkey_from_reader() { |
| 144 | + // `from_reader` consumes a stream and cannot borrow keys from the input. |
| 145 | + let json = r#"{"public_key_ed25519":"3330317ec241d79943bb9aa7c8ea5f8b89f9c6ea351fe03f8ec3d1127137d484"}"#; |
| 146 | + let Decoded(strkey): Decoded<Strkey> = serde_json::from_reader(json.as_bytes()).unwrap(); |
| 147 | + assert!(matches!(strkey, Strkey::PublicKeyEd25519(_))); |
| 148 | +} |
| 149 | + |
| 150 | +#[test] |
| 151 | +fn test_decode_strkey_from_value_nested_variant() { |
| 152 | + // A compound variant (nested object) exercised through the owned-key path. |
| 153 | + let value = serde_json::json!({ |
| 154 | + "signed_payload_ed25519": { |
| 155 | + "ed25519": "0000000000000000000000000000000000000000000000000000000000000000", |
| 156 | + "payload": "01020304", |
| 157 | + } |
| 158 | + }); |
| 159 | + let Decoded(strkey): Decoded<Strkey> = serde_json::from_value(value).unwrap(); |
| 160 | + assert!(matches!(strkey, Strkey::SignedPayloadEd25519(_))); |
| 161 | +} |
| 162 | + |
| 163 | +#[test] |
| 164 | +fn test_roundtrip_public_key_via_value() { |
| 165 | + // Serialize to a `serde_json::Value`, then deserialize back via `from_value`. |
| 166 | + let original = Strkey::PublicKeyEd25519(ed25519::PublicKey([7u8; 32])); |
| 167 | + let value = serde_json::to_value(Decoded(&original)).unwrap(); |
| 168 | + let Decoded(deserialized): Decoded<Strkey> = serde_json::from_value(value).unwrap(); |
| 169 | + assert_eq!(original, deserialized); |
| 170 | +} |
| 171 | + |
126 | 172 | #[test] |
127 | 173 | fn test_roundtrip_muxed_account() { |
128 | 174 | let original = Strkey::MuxedAccountEd25519(ed25519::MuxedAccount { |
|
0 commit comments