@@ -603,6 +603,7 @@ pub fn start(config: Arc<Config>, query: Arc<Query>) -> Handle {
603603/// This enum is used to discern between a txid or a usize that is used
604604/// for pagination on the /api/address|scripthash/:address/txs/chain
605605/// endpoint.
606+ #[ cfg_attr( test, derive( Debug , PartialEq ) ) ]
606607pub enum AddressPaginator {
607608 Txid ( Txid ) ,
608609 Skip ( usize ) ,
@@ -1379,7 +1380,8 @@ impl From<address::AddressError> for HttpError {
13791380
13801381#[ cfg( test) ]
13811382mod tests {
1382- use crate :: rest:: HttpError ;
1383+ use crate :: rest:: { AddressPaginator , HttpError } ;
1384+ use bitcoin:: { Txid , hashes:: hex:: FromHex } ;
13831385 use serde_json:: Value ;
13841386 use std:: collections:: HashMap ;
13851387
@@ -1444,4 +1446,67 @@ mod tests {
14441446
14451447 assert ! ( err. is_err( ) ) ;
14461448 }
1449+
1450+ #[ test]
1451+ fn test_address_paginator ( ) {
1452+ // Each vector is (result, expected, assert_reason)
1453+ let vectors = [
1454+ (
1455+ "" . parse :: < AddressPaginator > ( ) ,
1456+ Err ( "Invalid AddressPaginator" . to_string ( ) ) ,
1457+ "fails both Txid from_hex and usize parse" ,
1458+ ) ,
1459+ (
1460+ "0" . parse :: < AddressPaginator > ( ) ,
1461+ Err ( "Invalid AddressPaginator" . to_string ( ) ) ,
1462+ "skipping 0 is pointless, so fails" ,
1463+ ) ,
1464+ (
1465+ "18446744073709551615" . parse :: < AddressPaginator > ( ) ,
1466+ Ok ( AddressPaginator :: Skip ( 18446744073709551615 ) ) ,
1467+ "valid usize (u64::MAX)" ,
1468+ ) ,
1469+ (
1470+ "1" . parse :: < AddressPaginator > ( ) ,
1471+ Ok ( AddressPaginator :: Skip ( 1 ) ) ,
1472+ "valid usize" ,
1473+ ) ,
1474+ (
1475+ "0000000000000000000000000000000000000000000000000000000000000001"
1476+ . parse :: < AddressPaginator > ( ) ,
1477+ Ok ( AddressPaginator :: Txid (
1478+ Txid :: from_hex ( "0000000000000000000000000000000000000000000000000000000000000001" )
1479+ . unwrap ( ) ,
1480+ ) ) ,
1481+ "64 length is always treated as Txid" ,
1482+ ) ,
1483+ (
1484+ "0000000000000000000000000000000000000000000018446744073709551615"
1485+ . parse :: < AddressPaginator > ( ) ,
1486+ Ok ( AddressPaginator :: Txid (
1487+ Txid :: from_hex ( "0000000000000000000000000000000000000000000018446744073709551615" )
1488+ . unwrap ( ) ,
1489+ ) ) ,
1490+ "64 length is always treated as Txid" ,
1491+ ) ,
1492+ (
1493+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
1494+ . parse :: < AddressPaginator > ( ) ,
1495+ Ok ( AddressPaginator :: Txid (
1496+ Txid :: from_hex ( "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" )
1497+ . unwrap ( ) ,
1498+ ) ) ,
1499+ "valid Txid" ,
1500+ ) ,
1501+ (
1502+ "ffffffxfffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
1503+ . parse :: < AddressPaginator > ( ) ,
1504+ Err ( "Invalid AddressPaginator" . to_string ( ) ) ,
1505+ "fails both Txid from_hex and usize parse" ,
1506+ ) ,
1507+ ] ;
1508+ for vector in vectors {
1509+ assert_eq ! ( vector. 0 , vector. 1 , "{}" , vector. 2 ) ;
1510+ }
1511+ }
14471512}
0 commit comments