test.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. use rent_example_api::prelude::*;
  2. use solana_program::hash::Hash;
  3. use solana_program_test::{processor, BanksClient, ProgramTest};
  4. use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction};
  5. use steel::*;
  6. async fn setup() -> (BanksClient, Keypair, Hash) {
  7. let mut program_test = ProgramTest::new(
  8. "rent_example_program",
  9. rent_example_api::ID,
  10. processor!(rent_example_program::process_instruction),
  11. );
  12. program_test.prefer_bpf(true);
  13. program_test.start().await
  14. }
  15. #[tokio::test]
  16. async fn test_create_system_account() {
  17. // Setup test environment
  18. let (mut banks_client, payer, recent_blockhash) = setup().await;
  19. // Generate a new keypair for the account we'll create
  20. let new_account = Keypair::new();
  21. // Test data
  22. let name = "John Doe";
  23. let address = "123 Blockchain Street";
  24. // Create the instruction
  25. let ix = create_system_account(
  26. payer.pubkey(),
  27. new_account.pubkey(),
  28. name.to_string(),
  29. address.to_string(),
  30. );
  31. // Create and send transaction
  32. let mut transaction = Transaction::new_signed_with_payer(
  33. &[ix],
  34. Some(&payer.pubkey()),
  35. &[&payer, &new_account],
  36. recent_blockhash,
  37. );
  38. // Process transaction
  39. let result = banks_client.process_transaction(transaction).await;
  40. assert!(
  41. result.is_ok(),
  42. "Failed to process transaction: {:?}",
  43. result
  44. );
  45. // Fetch and verify the created account
  46. let account = banks_client
  47. .get_account(new_account.pubkey())
  48. .await
  49. .expect("Failed to get account")
  50. .expect("Account not found");
  51. // Verify account owner
  52. assert_eq!(
  53. account.owner,
  54. rent_example_api::ID,
  55. "Incorrect account owner"
  56. );
  57. // Deserialize and verify account data
  58. let address_account =
  59. Address::try_from_bytes(&account.data).expect("Failed to deserialize account data");
  60. // Convert stored bytes back to strings for comparison
  61. let stored_name = String::from_utf8(
  62. address_account
  63. .name
  64. .iter()
  65. .take_while(|&&b| b != 0)
  66. .cloned()
  67. .collect::<Vec<u8>>(),
  68. )
  69. .unwrap();
  70. let stored_address = String::from_utf8(
  71. address_account
  72. .address
  73. .iter()
  74. .take_while(|&&b| b != 0)
  75. .cloned()
  76. .collect::<Vec<u8>>(),
  77. )
  78. .unwrap();
  79. // Verify the stored data matches what we sent
  80. assert_eq!(stored_name, name, "Stored name doesn't match");
  81. assert_eq!(stored_address, address, "Stored address doesn't match");
  82. }