Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

SDK: Add test for illegal Pubkey::create_with_seed owners (backport #18398) #18401

Merged
merged 1 commit into from
Jul 3, 2021
Merged
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
22 changes: 22 additions & 0 deletions sdk/program/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,26 @@ mod tests {
assert!(sysvar::id().is_native_program_id());
assert!(vote::program::id().is_native_program_id());
}

fn pubkey_from_seed_by_marker(marker: &[u8]) -> Result<Pubkey, PubkeyError> {
let key = Pubkey::new_unique();
let owner = Pubkey::default();

let mut to_fake = owner.to_bytes().to_vec();
to_fake.extend_from_slice(marker);

let seed = &String::from_utf8(to_fake[..to_fake.len() - 32].to_vec()).expect("not utf8");
let base = &Pubkey::try_from_slice(&to_fake[to_fake.len() - 32..]).unwrap();

Pubkey::create_with_seed(&key, seed, base)
}

#[test]
fn test_create_with_seed_rejects_illegal_owner() {
assert_eq!(
pubkey_from_seed_by_marker(PDA_MARKER),
Err(PubkeyError::IllegalOwner)
);
assert!(pubkey_from_seed_by_marker(&PDA_MARKER[1..]).is_ok());
}
}