22
33use libfuzzer_sys:: { arbitrary:: Result , fuzz_target, Corpus } ;
44
5+ use stellar_strkey:: Strkey ;
6+
57const BASE32_ALPHABET : & str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=" ;
68
79// Take inputs and attempt to parse them as a strkey.
@@ -11,13 +13,54 @@ fuzz_target!(|s: &str| -> Corpus {
1113 }
1214
1315 // Parse the input as a strkey. Ignore invalid strkeys.
14- let Ok ( r) : Result <stellar_strkey :: Strkey , _> = s. parse( ) else {
16+ let Ok ( r) : Result <Strkey , _> = s. parse( ) else {
1517 return Corpus :: Keep ;
1618 } ;
1719
1820 // Check that the strkey roundtrips back to the identical string.
1921 let roundtrip_s = r. to_string( ) ;
2022 assert_eq!( roundtrip_s, s) ;
2123
24+ // Check that the first character matches the expected prefix for the type.
25+ let first_char = s. chars( ) . next( ) . unwrap( ) ;
26+ assert_eq!(
27+ first_char,
28+ match r {
29+ Strkey :: PublicKeyEd25519 ( _) => 'G' ,
30+ Strkey :: PrivateKeyEd25519 ( _) => 'S' ,
31+ Strkey :: MuxedAccountEd25519 ( _) => 'M' ,
32+ Strkey :: PreAuthTx ( _) => 'T' ,
33+ Strkey :: HashX ( _) => 'X' ,
34+ Strkey :: SignedPayloadEd25519 ( _) => 'P' ,
35+ Strkey :: Contract ( _) => 'C' ,
36+ Strkey :: LiquidityPool ( _) => 'L' ,
37+ Strkey :: ClaimableBalance ( _) => 'B' ,
38+ }
39+ ) ;
40+
41+ // Check that the length of the strkey is what would be expected.
42+ let len = s. len( ) ;
43+ match & r {
44+ Strkey :: PublicKeyEd25519 ( _) => assert_eq!( len, 56 ) ,
45+ Strkey :: PrivateKeyEd25519 ( _) => assert_eq!( len, 56 ) ,
46+ Strkey :: PreAuthTx ( _) => assert_eq!( len, 56 ) ,
47+ Strkey :: HashX ( _) => assert_eq!( len, 56 ) ,
48+ Strkey :: MuxedAccountEd25519 ( _) => assert_eq!( len, 69 ) ,
49+ Strkey :: Contract ( _) => assert_eq!( len, 56 ) ,
50+ Strkey :: LiquidityPool ( _) => assert_eq!( len, 56 ) ,
51+ Strkey :: ClaimableBalance ( _) => assert_eq!( len, 58 ) ,
52+ Strkey :: SignedPayloadEd25519 ( sp) => {
53+ let payload_len = sp. payload. len( ) ;
54+ let binary_len = 1 // version
55+ + 32 // ed25519
56+ + 4 // payload length
57+ + payload_len // payload
58+ + ( 4 - payload_len % 4 ) % 4 // payload padding
59+ + 2 ; // crc
60+ let str_len = ( binary_len * 8 + 4 ) / 5 ; // base32: 5 bits per char, ceil(bits / 5)
61+ assert_eq!( len, str_len) ;
62+ }
63+ }
64+
2265 Corpus :: Keep
2366} ) ;
0 commit comments