@@ -255,12 +255,150 @@ impl Read for FilesystemReader {
255255#[ cfg( test) ]
256256mod tests {
257257 use super :: * ;
258- use crate :: test_utils:: do_read_write_remove_list_persist;
258+ use crate :: test_utils:: { do_read_write_remove_list_persist, do_test_store} ;
259+
260+ use bitcoin:: hashes:: hex:: FromHex ;
261+ use bitcoin:: Txid ;
262+
263+ use lightning:: chain:: ChannelMonitorUpdateStatus ;
264+ use lightning:: chain:: chainmonitor:: Persist ;
265+ use lightning:: chain:: transaction:: OutPoint ;
266+ use lightning:: check_closed_event;
267+ use lightning:: events:: { ClosureReason , MessageSendEventsProvider } ;
268+ use lightning:: ln:: functional_test_utils:: * ;
269+ use lightning:: util:: test_utils;
270+ use lightning:: util:: persist:: read_channel_monitors;
271+ use std:: fs;
272+ #[ cfg( target_os = "windows" ) ]
273+ use {
274+ lightning:: get_event_msg,
275+ lightning:: ln:: msgs:: ChannelMessageHandler ,
276+ } ;
277+
278+ impl Drop for FilesystemStore {
279+ fn drop ( & mut self ) {
280+ // We test for invalid directory names, so it's OK if directory removal
281+ // fails.
282+ match fs:: remove_dir_all ( & self . data_dir ) {
283+ Err ( e) => println ! ( "Failed to remove test persister directory: {}" , e) ,
284+ _ => { }
285+ }
286+ }
287+ }
259288
260289 #[ test]
261290 fn read_write_remove_list_persist ( ) {
262291 let temp_path = std:: env:: temp_dir ( ) ;
263292 let fs_store = FilesystemStore :: new ( temp_path) ;
264293 do_read_write_remove_list_persist ( & fs_store) ;
265294 }
295+
296+ #[ test]
297+ fn test_if_monitors_is_not_dir ( ) {
298+ let store = FilesystemStore :: new ( "test_monitors_is_not_dir" . into ( ) ) ;
299+
300+ fs:: create_dir_all ( & store. get_data_dir ( ) ) . unwrap ( ) ;
301+ let mut path = std:: path:: PathBuf :: from ( & store. get_data_dir ( ) ) ;
302+ path. push ( "monitors" ) ;
303+ fs:: File :: create ( path) . unwrap ( ) ;
304+
305+ let chanmon_cfgs = create_chanmon_cfgs ( 1 ) ;
306+ let mut node_cfgs = create_node_cfgs ( 1 , & chanmon_cfgs) ;
307+ 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 ) ;
308+ node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
309+ let node_chanmgrs = create_node_chanmgrs ( 1 , & node_cfgs, & [ None ] ) ;
310+ let nodes = create_network ( 1 , & node_cfgs, & node_chanmgrs) ;
311+
312+ // Check that read_channel_monitors() returns error if monitors/ is not a
313+ // directory.
314+ assert ! ( read_channel_monitors( & store, nodes[ 0 ] . keys_manager, nodes[ 0 ] . keys_manager) . is_err( ) ) ;
315+ }
316+
317+ #[ test]
318+ fn test_filesystem_store ( ) {
319+ // Create the nodes, giving them FilesystemStores for data stores.
320+ let store_0 = FilesystemStore :: new ( "test_filesystem_store_0" . into ( ) ) ;
321+ let store_1 = FilesystemStore :: new ( "test_filesystem_store_1" . into ( ) ) ;
322+ do_test_store ( & store_0, & store_1)
323+ }
324+
325+ // Test that if the store's path to channel data is read-only, writing a
326+ // monitor to it results in the store returning a PermanentFailure.
327+ // Windows ignores the read-only flag for folders, so this test is Unix-only.
328+ #[ cfg( not( target_os = "windows" ) ) ]
329+ #[ test]
330+ fn test_readonly_dir_perm_failure ( ) {
331+ let store = FilesystemStore :: new ( "test_readonly_dir_perm_failure" . into ( ) ) ;
332+ fs:: create_dir_all ( & store. get_data_dir ( ) ) . unwrap ( ) ;
333+
334+ // Set up a dummy channel and force close. This will produce a monitor
335+ // that we can then use to test persistence.
336+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
337+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
338+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
339+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
340+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
341+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
342+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
343+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
344+ let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
345+ let update_id = update_map. get ( & added_monitors[ 0 ] . 0 . to_channel_id ( ) ) . unwrap ( ) ;
346+
347+ // Set the store's directory to read-only, which should result in
348+ // returning a permanent failure when we then attempt to persist a
349+ // channel update.
350+ let path = & store. get_data_dir ( ) ;
351+ let mut perms = fs:: metadata ( path) . unwrap ( ) . permissions ( ) ;
352+ perms. set_readonly ( true ) ;
353+ fs:: set_permissions ( path, perms) . unwrap ( ) ;
354+
355+ let test_txo = OutPoint {
356+ txid : Txid :: from_hex ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
357+ index : 0
358+ } ;
359+ match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 , update_id. 2 ) {
360+ ChannelMonitorUpdateStatus :: PermanentFailure => { } ,
361+ _ => panic ! ( "unexpected result from persisting new channel" )
362+ }
363+
364+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
365+ added_monitors. clear ( ) ;
366+ }
367+
368+ // Test that if a store's directory name is invalid, monitor persistence
369+ // will fail.
370+ #[ cfg( target_os = "windows" ) ]
371+ #[ test]
372+ fn test_fail_on_open ( ) {
373+ // Set up a dummy channel and force close. This will produce a monitor
374+ // that we can then use to test persistence.
375+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
376+ let mut node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
377+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
378+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
379+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
380+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
381+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
382+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
383+ let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
384+ let update_id = update_map. get ( & added_monitors[ 0 ] . 0 . to_channel_id ( ) ) . unwrap ( ) ;
385+
386+ // Create the store with an invalid directory name and test that the
387+ // channel fails to open because the directories fail to be created. There
388+ // don't seem to be invalid filename characters on Unix that Rust doesn't
389+ // handle, hence why the test is Windows-only.
390+ let store = FilesystemStore :: new ( ":<>/" . into ( ) ) ;
391+
392+ let test_txo = OutPoint {
393+ txid : Txid :: from_hex ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
394+ index : 0
395+ } ;
396+ match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 , update_id. 2 ) {
397+ ChannelMonitorUpdateStatus :: PermanentFailure => { } ,
398+ _ => panic ! ( "unexpected result from persisting new channel" )
399+ }
400+
401+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
402+ added_monitors. clear ( ) ;
403+ }
266404}
0 commit comments