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