@@ -452,7 +452,36 @@ impl KVStore for FilesystemStore {
452452#[ cfg( test) ]
453453mod tests {
454454 use super :: * ;
455- use crate :: test_utils:: do_read_write_remove_list_persist;
455+ use crate :: test_utils:: { do_read_write_remove_list_persist, do_test_store} ;
456+
457+ use bitcoin:: hashes:: hex:: FromHex ;
458+ use bitcoin:: Txid ;
459+
460+ use lightning:: chain:: ChannelMonitorUpdateStatus ;
461+ use lightning:: chain:: chainmonitor:: Persist ;
462+ use lightning:: chain:: transaction:: OutPoint ;
463+ use lightning:: check_closed_event;
464+ use lightning:: events:: { ClosureReason , MessageSendEventsProvider } ;
465+ use lightning:: ln:: functional_test_utils:: * ;
466+ use lightning:: util:: test_utils;
467+ use lightning:: util:: persist:: read_channel_monitors;
468+ use std:: fs;
469+ #[ cfg( target_os = "windows" ) ]
470+ use {
471+ lightning:: get_event_msg,
472+ lightning:: ln:: msgs:: ChannelMessageHandler ,
473+ } ;
474+
475+ impl Drop for FilesystemStore {
476+ fn drop ( & mut self ) {
477+ // We test for invalid directory names, so it's OK if directory removal
478+ // fails.
479+ match fs:: remove_dir_all ( & self . data_dir ) {
480+ Err ( e) => println ! ( "Failed to remove test persister directory: {}" , e) ,
481+ _ => { }
482+ }
483+ }
484+ }
456485
457486 #[ test]
458487 fn read_write_remove_list_persist ( ) {
@@ -461,4 +490,113 @@ mod tests {
461490 let fs_store = FilesystemStore :: new ( temp_path) ;
462491 do_read_write_remove_list_persist ( & fs_store) ;
463492 }
493+
494+ #[ test]
495+ fn test_if_monitors_is_not_dir ( ) {
496+ let store = FilesystemStore :: new ( "test_monitors_is_not_dir" . into ( ) ) ;
497+
498+ fs:: create_dir_all ( & store. get_data_dir ( ) ) . unwrap ( ) ;
499+ let mut path = std:: path:: PathBuf :: from ( & store. get_data_dir ( ) ) ;
500+ path. push ( "monitors" ) ;
501+ fs:: File :: create ( path) . unwrap ( ) ;
502+
503+ let chanmon_cfgs = create_chanmon_cfgs ( 1 ) ;
504+ let mut node_cfgs = create_node_cfgs ( 1 , & chanmon_cfgs) ;
505+ let chain_mon_0 = test_utils:: TestChainMonitor :: new ( Some ( & chanmon_cfgs[ 0 ] . chain_source ) , & chanmon_cfgs[ 0 ] . tx_broadcaster , & chanmon_cfgs[ 0 ] . logger , & chanmon_cfgs[ 0 ] . fee_estimator , & store, node_cfgs[ 0 ] . keys_manager ) ;
506+ node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
507+ let node_chanmgrs = create_node_chanmgrs ( 1 , & node_cfgs, & [ None ] ) ;
508+ let nodes = create_network ( 1 , & node_cfgs, & node_chanmgrs) ;
509+
510+ // Check that read_channel_monitors() returns error if monitors/ is not a
511+ // directory.
512+ assert ! ( read_channel_monitors( & store, nodes[ 0 ] . keys_manager, nodes[ 0 ] . keys_manager) . is_err( ) ) ;
513+ }
514+
515+ #[ test]
516+ fn test_filesystem_store ( ) {
517+ // Create the nodes, giving them FilesystemStores for data stores.
518+ let store_0 = FilesystemStore :: new ( "test_filesystem_store_0" . into ( ) ) ;
519+ let store_1 = FilesystemStore :: new ( "test_filesystem_store_1" . into ( ) ) ;
520+ do_test_store ( & store_0, & store_1)
521+ }
522+
523+ // Test that if the store's path to channel data is read-only, writing a
524+ // monitor to it results in the store returning a PermanentFailure.
525+ // Windows ignores the read-only flag for folders, so this test is Unix-only.
526+ #[ cfg( not( target_os = "windows" ) ) ]
527+ #[ test]
528+ fn test_readonly_dir_perm_failure ( ) {
529+ let store = FilesystemStore :: new ( "test_readonly_dir_perm_failure" . into ( ) ) ;
530+ fs:: create_dir_all ( & store. get_data_dir ( ) ) . unwrap ( ) ;
531+
532+ // Set up a dummy channel and force close. This will produce a monitor
533+ // that we can then use to test persistence.
534+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
535+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
536+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
537+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
538+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
539+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
540+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
541+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
542+ let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
543+ let update_id = update_map. get ( & added_monitors[ 0 ] . 0 . to_channel_id ( ) ) . unwrap ( ) ;
544+
545+ // Set the store's directory to read-only, which should result in
546+ // returning a permanent failure when we then attempt to persist a
547+ // channel update.
548+ let path = & store. get_data_dir ( ) ;
549+ let mut perms = fs:: metadata ( path) . unwrap ( ) . permissions ( ) ;
550+ perms. set_readonly ( true ) ;
551+ fs:: set_permissions ( path, perms) . unwrap ( ) ;
552+
553+ let test_txo = OutPoint {
554+ txid : Txid :: from_hex ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
555+ index : 0
556+ } ;
557+ match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 , update_id. 2 ) {
558+ ChannelMonitorUpdateStatus :: PermanentFailure => { } ,
559+ _ => panic ! ( "unexpected result from persisting new channel" )
560+ }
561+
562+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
563+ added_monitors. clear ( ) ;
564+ }
565+
566+ // Test that if a store's directory name is invalid, monitor persistence
567+ // will fail.
568+ #[ cfg( target_os = "windows" ) ]
569+ #[ test]
570+ fn test_fail_on_open ( ) {
571+ // Set up a dummy channel and force close. This will produce a monitor
572+ // that we can then use to test persistence.
573+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
574+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
575+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
576+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
577+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
578+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
579+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
580+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
581+ let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
582+ let update_id = update_map. get ( & added_monitors[ 0 ] . 0 . to_channel_id ( ) ) . unwrap ( ) ;
583+
584+ // Create the store with an invalid directory name and test that the
585+ // channel fails to open because the directories fail to be created. There
586+ // don't seem to be invalid filename characters on Unix that Rust doesn't
587+ // handle, hence why the test is Windows-only.
588+ let store = FilesystemStore :: new ( ":<>/" . into ( ) ) ;
589+
590+ let test_txo = OutPoint {
591+ txid : Txid :: from_hex ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
592+ index : 0
593+ } ;
594+ match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 , update_id. 2 ) {
595+ ChannelMonitorUpdateStatus :: PermanentFailure => { } ,
596+ _ => panic ! ( "unexpected result from persisting new channel" )
597+ }
598+
599+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
600+ added_monitors. clear ( ) ;
601+ }
464602}
0 commit comments