@@ -367,7 +367,36 @@ impl KVStore for FilesystemStore {
367367#[ cfg( test) ]
368368mod tests {
369369 use super :: * ;
370- use crate :: test_utils:: do_read_write_remove_list_persist;
370+ use crate :: test_utils:: { do_read_write_remove_list_persist, do_test_store} ;
371+
372+ use bitcoin:: hashes:: hex:: FromHex ;
373+ use bitcoin:: Txid ;
374+
375+ use lightning:: chain:: ChannelMonitorUpdateStatus ;
376+ use lightning:: chain:: chainmonitor:: Persist ;
377+ use lightning:: chain:: transaction:: OutPoint ;
378+ use lightning:: check_closed_event;
379+ use lightning:: events:: { ClosureReason , MessageSendEventsProvider } ;
380+ use lightning:: ln:: functional_test_utils:: * ;
381+ use lightning:: util:: test_utils;
382+ use lightning:: util:: persist:: read_channel_monitors;
383+ use std:: fs;
384+ #[ cfg( target_os = "windows" ) ]
385+ use {
386+ lightning:: get_event_msg,
387+ lightning:: ln:: msgs:: ChannelMessageHandler ,
388+ } ;
389+
390+ impl Drop for FilesystemStore {
391+ fn drop ( & mut self ) {
392+ // We test for invalid directory names, so it's OK if directory removal
393+ // fails.
394+ match fs:: remove_dir_all ( & self . data_dir ) {
395+ Err ( e) => println ! ( "Failed to remove test persister directory: {}" , e) ,
396+ _ => { }
397+ }
398+ }
399+ }
371400
372401 #[ test]
373402 fn read_write_remove_list_persist ( ) {
@@ -376,4 +405,113 @@ mod tests {
376405 let fs_store = FilesystemStore :: new ( temp_path) ;
377406 do_read_write_remove_list_persist ( & fs_store) ;
378407 }
408+
409+ #[ test]
410+ fn test_if_monitors_is_not_dir ( ) {
411+ let store = FilesystemStore :: new ( "test_monitors_is_not_dir" . into ( ) ) ;
412+
413+ fs:: create_dir_all ( & store. get_data_dir ( ) ) . unwrap ( ) ;
414+ let mut path = std:: path:: PathBuf :: from ( & store. get_data_dir ( ) ) ;
415+ path. push ( "monitors" ) ;
416+ fs:: File :: create ( path) . unwrap ( ) ;
417+
418+ let chanmon_cfgs = create_chanmon_cfgs ( 1 ) ;
419+ let mut node_cfgs = create_node_cfgs ( 1 , & chanmon_cfgs) ;
420+ 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 ) ;
421+ node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
422+ let node_chanmgrs = create_node_chanmgrs ( 1 , & node_cfgs, & [ None ] ) ;
423+ let nodes = create_network ( 1 , & node_cfgs, & node_chanmgrs) ;
424+
425+ // Check that read_channel_monitors() returns error if monitors/ is not a
426+ // directory.
427+ assert ! ( read_channel_monitors( & store, nodes[ 0 ] . keys_manager, nodes[ 0 ] . keys_manager) . is_err( ) ) ;
428+ }
429+
430+ #[ test]
431+ fn test_filesystem_store ( ) {
432+ // Create the nodes, giving them FilesystemStores for data stores.
433+ let store_0 = FilesystemStore :: new ( "test_filesystem_store_0" . into ( ) ) ;
434+ let store_1 = FilesystemStore :: new ( "test_filesystem_store_1" . into ( ) ) ;
435+ do_test_store ( & store_0, & store_1)
436+ }
437+
438+ // Test that if the store's path to channel data is read-only, writing a
439+ // monitor to it results in the store returning a PermanentFailure.
440+ // Windows ignores the read-only flag for folders, so this test is Unix-only.
441+ #[ cfg( not( target_os = "windows" ) ) ]
442+ #[ test]
443+ fn test_readonly_dir_perm_failure ( ) {
444+ let store = FilesystemStore :: new ( "test_readonly_dir_perm_failure" . into ( ) ) ;
445+ fs:: create_dir_all ( & store. get_data_dir ( ) ) . unwrap ( ) ;
446+
447+ // Set up a dummy channel and force close. This will produce a monitor
448+ // that we can then use to test persistence.
449+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
450+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
451+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
452+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
453+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
454+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
455+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
456+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
457+ let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
458+ let update_id = update_map. get ( & added_monitors[ 0 ] . 0 . to_channel_id ( ) ) . unwrap ( ) ;
459+
460+ // Set the store's directory to read-only, which should result in
461+ // returning a permanent failure when we then attempt to persist a
462+ // channel update.
463+ let path = & store. get_data_dir ( ) ;
464+ let mut perms = fs:: metadata ( path) . unwrap ( ) . permissions ( ) ;
465+ perms. set_readonly ( true ) ;
466+ fs:: set_permissions ( path, perms) . unwrap ( ) ;
467+
468+ let test_txo = OutPoint {
469+ txid : Txid :: from_hex ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
470+ index : 0
471+ } ;
472+ match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 , update_id. 2 ) {
473+ ChannelMonitorUpdateStatus :: PermanentFailure => { } ,
474+ _ => panic ! ( "unexpected result from persisting new channel" )
475+ }
476+
477+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
478+ added_monitors. clear ( ) ;
479+ }
480+
481+ // Test that if a store's directory name is invalid, monitor persistence
482+ // will fail.
483+ #[ cfg( target_os = "windows" ) ]
484+ #[ test]
485+ fn test_fail_on_open ( ) {
486+ // Set up a dummy channel and force close. This will produce a monitor
487+ // that we can then use to test persistence.
488+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
489+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
490+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
491+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
492+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
493+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
494+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
495+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
496+ let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
497+ let update_id = update_map. get ( & added_monitors[ 0 ] . 0 . to_channel_id ( ) ) . unwrap ( ) ;
498+
499+ // Create the store with an invalid directory name and test that the
500+ // channel fails to open because the directories fail to be created. There
501+ // don't seem to be invalid filename characters on Unix that Rust doesn't
502+ // handle, hence why the test is Windows-only.
503+ let store = FilesystemStore :: new ( ":<>/" . into ( ) ) ;
504+
505+ let test_txo = OutPoint {
506+ txid : Txid :: from_hex ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
507+ index : 0
508+ } ;
509+ match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 , update_id. 2 ) {
510+ ChannelMonitorUpdateStatus :: PermanentFailure => { } ,
511+ _ => panic ! ( "unexpected result from persisting new channel" )
512+ }
513+
514+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
515+ added_monitors. clear ( ) ;
516+ }
379517}
0 commit comments