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