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