@@ -235,45 +235,33 @@ public async Task CancelTokenOn_ServerWaitForConnectionAsync_Throws_OperationCan
235
235
}
236
236
}
237
237
238
- [ DllImport ( "api-ms-win-core-io-l1-1-0.dll" , SetLastError = true ) ]
239
- private static unsafe extern bool CancelIoEx ( SafeHandle handle , NativeOverlapped * lpOverlapped ) ;
240
-
241
238
[ Fact ]
242
239
[ PlatformSpecific ( PlatformID . Windows ) ]
243
- // [ActiveIssue(2640)]
244
240
public async Task CancelTokenOff_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_OperationCanceledException ( )
245
241
{
246
242
using ( NamedPipePair pair = CreateNamedPipePair ( ) )
247
243
{
248
244
NamedPipeServerStream server = pair . serverStream ;
249
245
Task waitForConnectionTask = server . WaitForConnectionAsync ( CancellationToken . None ) ;
250
- unsafe
251
- {
252
- Assert . True ( CancelIoEx ( server . SafePipeHandle , null ) , "Outer cancellation failed" ) ;
253
- }
254
246
247
+ Assert . True ( Interop . CancelIoEx ( server . SafePipeHandle ) , "Outer cancellation failed" ) ;
255
248
await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => waitForConnectionTask ) ;
256
249
Assert . True ( waitForConnectionTask . IsCanceled ) ;
257
250
}
258
251
}
259
252
260
253
[ Fact ]
261
254
[ PlatformSpecific ( PlatformID . Windows ) ]
262
- // [ActiveIssue(2640)]
263
255
public async Task CancelTokenOn_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_IOException ( )
264
256
{
265
257
using ( NamedPipePair pair = CreateNamedPipePair ( ) )
266
258
{
267
259
var cts = new CancellationTokenSource ( ) ;
268
260
NamedPipeServerStream server = pair . serverStream ;
269
261
Task waitForConnectionTask = server . WaitForConnectionAsync ( cts . Token ) ;
270
- unsafe
271
- {
272
- Assert . True ( CancelIoEx ( server . SafePipeHandle , null ) , "Outer cancellation failed" ) ;
273
- }
274
262
263
+ Assert . True ( Interop . CancelIoEx ( server . SafePipeHandle ) , "Outer cancellation failed" ) ;
275
264
await Assert . ThrowsAsync < IOException > ( ( ) => waitForConnectionTask ) ;
276
- Assert . False ( waitForConnectionTask . IsCanceled ) ;
277
265
}
278
266
}
279
267
@@ -310,6 +298,7 @@ public async Task OperationsOnDisconnectedServer()
310
298
311
299
Assert . Throws < InvalidOperationException > ( ( ) => server . Flush ( ) ) ;
312
300
Assert . Throws < InvalidOperationException > ( ( ) => server . IsMessageComplete ) ;
301
+ Assert . Throws < InvalidOperationException > ( ( ) => server . GetImpersonationUserName ( ) ) ;
313
302
}
314
303
}
315
304
@@ -337,12 +326,18 @@ public virtual async Task OperationsOnDisconnectedClient()
337
326
Assert . Throws < IOException > ( ( ) => client . WriteByte ( 5 ) ) ;
338
327
Assert . Throws < IOException > ( ( ) => { client . WriteAsync ( buffer , 0 , buffer . Length ) ; } ) ;
339
328
Assert . Throws < IOException > ( ( ) => client . Flush ( ) ) ;
329
+ Assert . Throws < IOException > ( ( ) => client . NumberOfServerInstances ) ;
340
330
}
341
331
else
342
332
{
343
333
// Nothing for the client to read, but no exception throwing
344
334
Assert . Equal ( 0 , client . Read ( buffer , 0 , buffer . Length ) ) ;
345
335
Assert . Equal ( - 1 , client . ReadByte ( ) ) ;
336
+
337
+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
338
+ {
339
+ Assert . Throws < PlatformNotSupportedException > ( ( ) => client . NumberOfServerInstances ) ;
340
+ }
346
341
}
347
342
348
343
Assert . Throws < InvalidOperationException > ( ( ) => client . IsMessageComplete ) ;
@@ -361,6 +356,7 @@ public async Task Windows_OperationsOnNamedServerWithDisposedClient()
361
356
362
357
Assert . Throws < IOException > ( ( ) => server . WaitForConnection ( ) ) ;
363
358
await Assert . ThrowsAsync < IOException > ( ( ) => server . WaitForConnectionAsync ( ) ) ;
359
+ Assert . Throws < IOException > ( ( ) => server . GetImpersonationUserName ( ) ) ;
364
360
}
365
361
}
366
362
@@ -377,6 +373,7 @@ public async Task Unix_OperationsOnNamedServerWithDisposedClient()
377
373
// On Unix, the server still thinks that it is connected after client Disposal.
378
374
Assert . Throws < InvalidOperationException > ( ( ) => server . WaitForConnection ( ) ) ;
379
375
await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => server . WaitForConnectionAsync ( ) ) ;
376
+ Assert . Throws < PlatformNotSupportedException > ( ( ) => server . GetImpersonationUserName ( ) ) ;
380
377
}
381
378
}
382
379
@@ -463,6 +460,7 @@ public async Task DisposedServerPipe_Throws_ObjectDisposedException()
463
460
byte [ ] buffer = new byte [ ] { 0 , 0 , 0 , 0 } ;
464
461
465
462
Assert . Throws < ObjectDisposedException > ( ( ) => pipe . Disconnect ( ) ) ;
463
+ Assert . Throws < ObjectDisposedException > ( ( ) => pipe . GetImpersonationUserName ( ) ) ;
466
464
Assert . Throws < ObjectDisposedException > ( ( ) => pipe . WaitForConnection ( ) ) ;
467
465
await Assert . ThrowsAsync < ObjectDisposedException > ( ( ) => pipe . WaitForConnectionAsync ( ) ) ;
468
466
}
@@ -473,12 +471,14 @@ public async Task DisposedClientPipe_Throws_ObjectDisposedException()
473
471
{
474
472
using ( NamedPipePair pair = CreateNamedPipePair ( ) )
475
473
{
474
+ pair . Connect ( ) ;
476
475
NamedPipeClientStream pipe = pair . clientStream ;
477
476
pipe . Dispose ( ) ;
478
477
byte [ ] buffer = new byte [ ] { 0 , 0 , 0 , 0 } ;
479
478
480
479
Assert . Throws < ObjectDisposedException > ( ( ) => pipe . Connect ( ) ) ;
481
480
await Assert . ThrowsAsync < ObjectDisposedException > ( ( ) => pipe . ConnectAsync ( ) ) ;
481
+ Assert . Throws < ObjectDisposedException > ( ( ) => pipe . NumberOfServerInstances ) ;
482
482
}
483
483
}
484
484
@@ -512,6 +512,66 @@ public async Task Server_ReadWriteCancelledToken_Throws_OperationCanceledExcepti
512
512
}
513
513
}
514
514
515
+ [ Fact ]
516
+ [ PlatformSpecific ( PlatformID . Windows ) ]
517
+ public async Task CancelTokenOff_Server_ReadWriteCancelledToken_Throws_OperationCanceledException ( )
518
+ {
519
+ using ( NamedPipePair pair = CreateNamedPipePair ( ) )
520
+ {
521
+ NamedPipeServerStream server = pair . serverStream ;
522
+ byte [ ] buffer = new byte [ ] { 0 , 0 , 0 , 0 } ;
523
+
524
+ pair . Connect ( ) ;
525
+
526
+ if ( server . CanRead )
527
+ {
528
+ Task serverReadToken = server . ReadAsync ( buffer , 0 , buffer . Length , CancellationToken . None ) ;
529
+
530
+ Assert . True ( Interop . CancelIoEx ( server . SafePipeHandle ) , "Outer cancellation failed" ) ;
531
+ await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => serverReadToken ) ;
532
+ Assert . True ( serverReadToken . IsCanceled ) ;
533
+ }
534
+ if ( server . CanWrite )
535
+ {
536
+ Task serverWriteToken = server . WriteAsync ( buffer , 0 , buffer . Length , CancellationToken . None ) ;
537
+
538
+ Assert . True ( Interop . CancelIoEx ( server . SafePipeHandle ) , "Outer cancellation failed" ) ;
539
+ await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => serverWriteToken ) ;
540
+ Assert . True ( serverWriteToken . IsCanceled ) ;
541
+ }
542
+ }
543
+ }
544
+
545
+ [ Fact ]
546
+ [ PlatformSpecific ( PlatformID . Windows ) ]
547
+ public async Task CancelTokenOn_Server_ReadWriteCancelledToken_Throws_IOException ( )
548
+ {
549
+ using ( NamedPipePair pair = CreateNamedPipePair ( ) )
550
+ {
551
+ NamedPipeServerStream server = pair . serverStream ;
552
+ byte [ ] buffer = new byte [ ] { 0 , 0 , 0 , 0 } ;
553
+
554
+ pair . Connect ( ) ;
555
+
556
+ if ( server . CanRead )
557
+ {
558
+ var cts = new CancellationTokenSource ( ) ;
559
+ Task serverReadToken = server . ReadAsync ( buffer , 0 , buffer . Length , cts . Token ) ;
560
+
561
+ Assert . True ( Interop . CancelIoEx ( server . SafePipeHandle ) , "Outer cancellation failed" ) ;
562
+ await Assert . ThrowsAsync < IOException > ( ( ) => serverReadToken ) ;
563
+ }
564
+ if ( server . CanWrite )
565
+ {
566
+ var cts = new CancellationTokenSource ( ) ;
567
+ Task serverWriteToken = server . WriteAsync ( buffer , 0 , buffer . Length , cts . Token ) ;
568
+
569
+ Assert . True ( Interop . CancelIoEx ( server . SafePipeHandle ) , "Outer cancellation failed" ) ;
570
+ await Assert . ThrowsAsync < IOException > ( ( ) => serverWriteToken ) ;
571
+ }
572
+ }
573
+ }
574
+
515
575
[ Fact ]
516
576
[ ActiveIssue ( 812 , PlatformID . AnyUnix ) ] // the cancellation token is ignored after the operation is initiated, due to base Stream's implementation
517
577
public async Task Client_ReadWriteCancelledToken_Throws_OperationCanceledException ( )
@@ -540,6 +600,64 @@ public async Task Client_ReadWriteCancelledToken_Throws_OperationCanceledExcepti
540
600
}
541
601
}
542
602
}
603
+
604
+ [ Fact ]
605
+ [ PlatformSpecific ( PlatformID . Windows ) ]
606
+ public async Task CancelTokenOff_Client_ReadWriteCancelledToken_Throws_OperationCanceledException ( )
607
+ {
608
+ using ( NamedPipePair pair = CreateNamedPipePair ( ) )
609
+ {
610
+ NamedPipeClientStream client = pair . clientStream ;
611
+ byte [ ] buffer = new byte [ ] { 0 , 0 , 0 , 0 } ;
612
+
613
+ pair . Connect ( ) ;
614
+ if ( client . CanRead )
615
+ {
616
+ Task clientReadToken = client . ReadAsync ( buffer , 0 , buffer . Length , CancellationToken . None ) ;
617
+
618
+ Assert . True ( Interop . CancelIoEx ( client . SafePipeHandle ) , "Outer cancellation failed" ) ;
619
+ await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => clientReadToken ) ;
620
+ Assert . True ( clientReadToken . IsCanceled ) ;
621
+ }
622
+ if ( client . CanWrite )
623
+ {
624
+ Task clientWriteToken = client . WriteAsync ( buffer , 0 , buffer . Length , CancellationToken . None ) ;
625
+
626
+ Assert . True ( Interop . CancelIoEx ( client . SafePipeHandle ) , "Outer cancellation failed" ) ;
627
+ await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => clientWriteToken ) ;
628
+ Assert . True ( clientWriteToken . IsCanceled ) ;
629
+ }
630
+ }
631
+ }
632
+
633
+ [ Fact ]
634
+ [ PlatformSpecific ( PlatformID . Windows ) ]
635
+ public async Task CancelTokenOn_Client_ReadWriteCancelledToken_Throws_IOException ( )
636
+ {
637
+ using ( NamedPipePair pair = CreateNamedPipePair ( ) )
638
+ {
639
+ NamedPipeClientStream client = pair . clientStream ;
640
+ byte [ ] buffer = new byte [ ] { 0 , 0 , 0 , 0 } ;
641
+
642
+ pair . Connect ( ) ;
643
+ if ( client . CanRead )
644
+ {
645
+ var cts = new CancellationTokenSource ( ) ;
646
+ Task clientReadToken = client . ReadAsync ( buffer , 0 , buffer . Length , cts . Token ) ;
647
+
648
+ Assert . True ( Interop . CancelIoEx ( client . SafePipeHandle ) , "Outer cancellation failed" ) ;
649
+ await Assert . ThrowsAsync < IOException > ( ( ) => clientReadToken ) ;
650
+ }
651
+ if ( client . CanWrite )
652
+ {
653
+ var cts = new CancellationTokenSource ( ) ;
654
+ Task clientWriteToken = client . WriteAsync ( buffer , 0 , buffer . Length , cts . Token ) ;
655
+
656
+ Assert . True ( Interop . CancelIoEx ( client . SafePipeHandle ) , "Outer cancellation failed" ) ;
657
+ await Assert . ThrowsAsync < IOException > ( ( ) => clientWriteToken ) ;
658
+ }
659
+ }
660
+ }
543
661
}
544
662
545
663
public class NamedPipeTest_Simple_ServerInOutRead_ClientInOutWrite : NamedPipeTest_Simple
@@ -581,6 +699,7 @@ public override async Task OperationsOnDisconnectedClient()
581
699
await client . WriteAsync ( buffer , 0 , buffer . Length ) ;
582
700
client . Flush ( ) ;
583
701
Assert . Throws < InvalidOperationException > ( ( ) => client . IsMessageComplete ) ;
702
+ Assert . Throws < PlatformNotSupportedException > ( ( ) => client . NumberOfServerInstances ) ;
584
703
}
585
704
}
586
705
}
0 commit comments