Merge rust-bitcoin/rust-bitcoin#2563: Added more tests for PublicKey::from_str

0d64ae6eb4 Added tests for PublicKey::from_str (Sh0g0-1758)

Pull request description:

  Fixes: #2550

  Added some new tests and refactored some older tests.

ACKs for top commit:
  sanket1729:
    ACK 0d64ae6eb4
  apoelstra:
    ACK 0d64ae6eb4 thanks for bearing with me!
  tcharding:
    ACK 0d64ae6eb4

Tree-SHA512: b6792590c56ccac8e8cf6f182e74cb77c4652c537c0357456ff21a7814ebcc8cf48e0fad4c8d47e6e786a50e2cbb48134cb64406bcc900b4fcad9304d9cf4167
This commit is contained in:
Andrew Poelstra 2024-03-15 07:55:10 +00:00
commit 42b5a6a26e
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 28 additions and 0 deletions

View File

@ -1394,6 +1394,34 @@ mod tests {
let res = PublicKey::from_str(s); let res = PublicKey::from_str(s);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::InvalidHexLength(8)); assert_eq!(res.unwrap_err(), ParsePublicKeyError::InvalidHexLength(8));
}
#[test]
fn public_key_from_str_invalid_str() {
// Ensuring test cases fail when PublicKey::from_str is used on invalid keys
let s = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b142";
assert_eq!(s.len(), 130);
let res = PublicKey::from_str(s);
assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::Encoding(FromSliceError::Secp256k1(secp256k1::Error::InvalidPublicKey)));
let s = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd169";
assert_eq!(s.len(), 66);
let res = PublicKey::from_str(s);
assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::Encoding(FromSliceError::Secp256k1(secp256k1::Error::InvalidPublicKey)));
let s = "062e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133";
assert_eq!(s.len(), 130);
let res = PublicKey::from_str(s);
assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::Encoding(FromSliceError::InvalidKeyPrefix(6)));
let s = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b13g";
assert_eq!(s.len(), 130);
let res = PublicKey::from_str(s);
assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::InvalidChar(103));
let s = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1ag"; let s = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1ag";
assert_eq!(s.len(), 66); assert_eq!(s.len(), 66);