|
| 1 | +//! Program entrypoint definitions |
| 2 | +
|
| 3 | +#![cfg(feature = "program")] |
| 4 | +#![cfg(not(feature = "no-entrypoint"))] |
| 5 | + |
| 6 | +use solana_sdk::{ |
| 7 | + account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, program_error::ProgramError, |
| 8 | + pubkey::Pubkey, |
| 9 | +}; |
| 10 | +use std::str::from_utf8; |
| 11 | + |
| 12 | +entrypoint!(process_instruction); |
| 13 | +fn process_instruction<'a>( |
| 14 | + _program_id: &Pubkey, |
| 15 | + _accounts: &'a [AccountInfo<'a>], |
| 16 | + instruction_data: &[u8], |
| 17 | +) -> ProgramResult { |
| 18 | + from_utf8(instruction_data).map_err(|_| ProgramError::InvalidInstructionData)?; |
| 19 | + Ok(()) |
| 20 | +} |
| 21 | + |
| 22 | +// Pull in syscall stubs when building for non-BPF targets |
| 23 | +#[cfg(not(target_arch = "bpf"))] |
| 24 | +solana_sdk::program_stubs!(); |
| 25 | + |
| 26 | +#[cfg(test)] |
| 27 | +mod tests { |
| 28 | + use super::*; |
| 29 | + use solana_sdk::{program_error::ProgramError, pubkey::Pubkey}; |
| 30 | + |
| 31 | + #[test] |
| 32 | + fn test_utf8_memo() { |
| 33 | + let program_id = Pubkey::new(&[0; 32]); |
| 34 | + |
| 35 | + let string = b"letters and such"; |
| 36 | + assert_eq!(Ok(()), process_instruction(&program_id, &[], string)); |
| 37 | + |
| 38 | + let emoji = "🐆".as_bytes(); |
| 39 | + let bytes = [0xF0, 0x9F, 0x90, 0x86]; |
| 40 | + assert_eq!(emoji, bytes); |
| 41 | + assert_eq!(Ok(()), process_instruction(&program_id, &[], &emoji)); |
| 42 | + |
| 43 | + let mut bad_utf8 = bytes; |
| 44 | + bad_utf8[3] = 0xFF; // Invalid UTF-8 byte |
| 45 | + assert_eq!( |
| 46 | + Err(ProgramError::InvalidInstructionData), |
| 47 | + process_instruction(&program_id, &[], &bad_utf8) |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
0 commit comments