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