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