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