@@ -52,6 +52,18 @@ const SIGNER_B_BYTES = new Uint8Array([
5252 0x94 , 0x8f , 0xd8 , 0xbb , 0x2c , 0x10 , 0xa1 , 0x01 , 0x02 , 0xce , 0x98 , 0xb3 , 0xa6 ,
5353] ) ;
5454
55+ const PREAMBLE_LENGTH_WITH_TWO_SIGNERS_BYTES =
56+ OFFCHAIN_MESSAGE_SIGNING_DOMAIN_BYTES . length +
57+ 1 + // Version
58+ APPLICATION_DOMAIN_BYTES . length +
59+ 1 + // Message format
60+ 1 + // Signer count
61+ SIGNER_A_BYTES . length +
62+ SIGNER_B_BYTES . length +
63+ 2 ; // Message length
64+ const MAX_BODY_LENGTH_WITH_TWO_SIGNERS_BYTES = 1232 - PREAMBLE_LENGTH_WITH_TWO_SIGNERS_BYTES ;
65+ const BODY_LENGTH_EXCEEDING_TWO_SIGNER_PREAMBLE_BYTES = MAX_BODY_LENGTH_WITH_TWO_SIGNERS_BYTES + 1 ;
66+
5567describe ( 'getOffchainMessageV0Decoder()' , ( ) => {
5668 let decoder : VariableSizeDecoder < OffchainMessageV0 > ;
5769 beforeEach ( ( ) => {
@@ -423,6 +435,39 @@ describe('getOffchainMessageV0Decoder()', () => {
423435 } ) ,
424436 ) ;
425437 } ) ;
438+ it . each ( [
439+ [ OffchainMessageContentFormat . RESTRICTED_ASCII_1232_BYTES_MAX , 'ASCII' ] ,
440+ [ OffchainMessageContentFormat . UTF8_1232_BYTES_MAX , '1232-byte-max UTF-8' ] ,
441+ ] ) ( 'throws when decoding a %s message whose preamble plus body is too long' , messageFormat => {
442+ const text = '!' . repeat ( BODY_LENGTH_EXCEEDING_TWO_SIGNER_PREAMBLE_BYTES ) ;
443+ const encodedMessage =
444+ // prettier-ignore
445+ new Uint8Array ( [
446+ // Signing domain
447+ ...OFFCHAIN_MESSAGE_SIGNING_DOMAIN_BYTES ,
448+ // Version
449+ 0x00 ,
450+ // Application domain
451+ ...APPLICATION_DOMAIN_BYTES ,
452+ // Message format
453+ messageFormat ,
454+ // Signer count
455+ 0x02 ,
456+ // Signer addresses
457+ ...SIGNER_A_BYTES ,
458+ ...SIGNER_B_BYTES ,
459+ // Message length
460+ text . length & 0xff , text . length >> 8 ,
461+ // Message
462+ ...Array . from ( text , character => character . charCodeAt ( 0 ) ) ,
463+ ] ) ;
464+ expect ( ( ) => decoder . decode ( encodedMessage ) ) . toThrow (
465+ new SolanaError ( SOLANA_ERROR__OFFCHAIN_MESSAGE__MAXIMUM_LENGTH_EXCEEDED , {
466+ actualBytes : BODY_LENGTH_EXCEEDING_TWO_SIGNER_PREAMBLE_BYTES ,
467+ maxBytes : MAX_BODY_LENGTH_WITH_TWO_SIGNERS_BYTES ,
468+ } ) ,
469+ ) ;
470+ } ) ;
426471 it ( 'throws when decoding a 1232-byte-max UTF-8 message whose message content does not match the length specified in the preamble' , ( ) => {
427472 const encodedMessage =
428473 // prettier-ignore
@@ -792,6 +837,26 @@ describe('getOffchainMessageEncoder()', () => {
792837 } ) ,
793838 ) ;
794839 } ) ;
840+ it . each ( [
841+ [ OffchainMessageContentFormat . RESTRICTED_ASCII_1232_BYTES_MAX , 'ASCII' ] ,
842+ [ OffchainMessageContentFormat . UTF8_1232_BYTES_MAX , '1232-byte-max UTF-8' ] ,
843+ ] ) ( 'throws when encoding a %s message whose preamble plus body is too long' , format => {
844+ const offchainMessage = {
845+ applicationDomain : APPLICATION_DOMAIN ,
846+ content : {
847+ format,
848+ text : '!' . repeat ( BODY_LENGTH_EXCEEDING_TWO_SIGNER_PREAMBLE_BYTES ) ,
849+ } ,
850+ requiredSignatories : [ { address : SIGNER_A } , { address : SIGNER_B } ] ,
851+ version : 0 ,
852+ } as unknown as OffchainMessageV0 ;
853+ expect ( ( ) => encoder . encode ( offchainMessage ) ) . toThrow (
854+ new SolanaError ( SOLANA_ERROR__OFFCHAIN_MESSAGE__MAXIMUM_LENGTH_EXCEEDED , {
855+ actualBytes : BODY_LENGTH_EXCEEDING_TWO_SIGNER_PREAMBLE_BYTES ,
856+ maxBytes : MAX_BODY_LENGTH_WITH_TWO_SIGNERS_BYTES ,
857+ } ) ,
858+ ) ;
859+ } ) ;
795860 it ( 'encodes a well-formed 65535-byte-max UTF-8 message according to spec' , ( ) => {
796861 const offchainMessage : OffchainMessageV0 = {
797862 applicationDomain : APPLICATION_DOMAIN ,
0 commit comments