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