Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ fn test_valid_private_keys_via_ed25519_private_key() {

#[test]
fn test_invalid_private_keys() {
// Too long strkey input. Strkey does not validate the private-key
// payload — any `S…` version byte returns `PrivateKey` to route the
// caller to `ed25519::PrivateKey`.
let r: Result<Strkey, _> = "SA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJV764SE".parse();
assert_eq!(r, Err(DecodeError::PrivateKey));
// Too short, only 31 bytes but must be 32 bytes.
let mut r: Result<ed25519::PrivateKey, _> =
"SA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UALAI".parse();
assert_eq!(r, Err(DecodeError::InvalidPayloadLength));

// Too long, 33 bytes but must be 32 bytes.
r = "SA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJV764SE".parse();
assert_eq!(r, Err(DecodeError::TooLong));
Comment thread
leighmcculloch marked this conversation as resolved.
}

#[test]
Expand Down Expand Up @@ -620,6 +623,28 @@ fn test_signed_payload_from_payload_inner_length_boundary() {
assert!(result.is_ok(), "inner payload length 64 should succeed");
}

/// A zero inner-payload length is invalid, but a payload declaring it can
/// still be exactly MIN_LENGTH (40 bytes) by carrying four trailing zero
/// bytes, check that case is prevented.
#[test]
fn test_signed_payload_from_payload_zero_inner_length_at_min_length() {
let payload: &[u8] = &[
// ed25519 public key (32 bytes)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, // length prefix (4 bytes, big-endian) = 64 (valid)
0x00, 0x00, 0x00, 0x00, // inner payload (4 bytes)
0x00, 0x00, 0x00, 0x00,
];

let result = stellar_strkey::ed25519::SignedPayload::from_payload(&payload);
assert_eq!(
result,
Err(DecodeError::InvalidPayloadLength),
"40 bytes declaring a 0-byte inner payload should fail"
);
}

#[test]
fn test_valid_liquidity_pool() {
assert_convert_roundtrip(
Expand Down
25 changes: 25 additions & 0 deletions tests/tools/strkey-encode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,31 @@ func main() {
0xff, // one byte too long
}),
},
{
"Valid Private Key",
encode(18<<3|0, []byte{
0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99, 0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7,
0x05, 0x51, 0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2, 0xfb, 0x0d, 0x7a, 0x03,
0xfc, 0x7f, 0xe8, 0x9a,
}),
},
{
"Invalid too short for Private Key",
encode(18<<3|0, []byte{
0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99, 0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7,
0x05, 0x51, 0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2, 0xfb, 0x0d, 0x7a, 0x03,
0xfc, 0x7f, 0xe8, // 0x9a, one byte short
}),
},
{
"Invalid too long for Private Key",
encode(18<<3|0, []byte{
0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99, 0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7,
0x05, 0x51, 0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2, 0xfb, 0x0d, 0x7a, 0x03,
0xfc, 0x7f, 0xe8, 0x9a,
0xff, // one byte too long
}),
},
}
csvw := csv.NewWriter(os.Stdout)
err := csvw.WriteAll(recs)
Expand Down
Loading