@@ -1275,3 +1275,58 @@ uintptr_t beep(void) {
1275
1275
t .Fatal ("LoadLibraryEx unexpectedly found beep.dll" )
1276
1276
}
1277
1277
}
1278
+
1279
+ func TestReadWriteFileOverlapped (t * testing.T ) {
1280
+ name := filepath .Join (t .TempDir (), "test.txt" )
1281
+ fd , err := windows .CreateFile (windows .StringToUTF16Ptr (name ), windows .GENERIC_READ | windows .GENERIC_WRITE , 0 , nil , windows .CREATE_NEW , windows .FILE_FLAG_OVERLAPPED , 0 )
1282
+ if err != nil {
1283
+ t .Fatal (err )
1284
+ }
1285
+ defer windows .CloseHandle (fd )
1286
+
1287
+ content := []byte ("hello" )
1288
+ // Test that we can write to a file using overlapped I/O.
1289
+ var ow windows.Overlapped
1290
+ ow .HEvent , err = windows .CreateEvent (nil , 0 , 0 , nil )
1291
+ if err != nil {
1292
+ t .Fatal (err )
1293
+ }
1294
+ defer windows .CloseHandle (ow .HEvent )
1295
+ if err := windows .WriteFile (fd , content , nil , & ow ); err != nil && err != windows .ERROR_IO_PENDING {
1296
+ t .Fatal (err )
1297
+ }
1298
+ if _ , err := windows .WaitForSingleObject (ow .HEvent , windows .INFINITE ); err != nil {
1299
+ t .Fatal (err )
1300
+ }
1301
+ var n uint32
1302
+ if err := windows .GetOverlappedResult (fd , & ow , & n , true ); err != nil {
1303
+ t .Fatal (err )
1304
+ }
1305
+ if n != uint32 (len (content )) {
1306
+ t .Fatalf ("got %d bytes written; want %d" , n , len (content ))
1307
+ }
1308
+
1309
+ // Test that we can read from a file using overlapped I/O.
1310
+ var or windows.Overlapped
1311
+ or .HEvent , err = windows .CreateEvent (nil , 0 , 0 , nil )
1312
+ if err != nil {
1313
+ t .Fatal (err )
1314
+ }
1315
+ defer windows .CloseHandle (ow .HEvent )
1316
+ buf := make ([]byte , len (content ))
1317
+ if err := windows .ReadFile (fd , buf , nil , & or ); err != nil && err != windows .ERROR_IO_PENDING {
1318
+ t .Fatal (err )
1319
+ }
1320
+ if err != nil {
1321
+ t .Fatal (err )
1322
+ }
1323
+ if _ , err := windows .WaitForSingleObject (or .HEvent , windows .INFINITE ); err != nil {
1324
+ t .Fatal (err )
1325
+ }
1326
+ if err := windows .GetOverlappedResult (fd , & or , & n , true ); err != nil {
1327
+ t .Fatal (err )
1328
+ }
1329
+ if string (buf ) != string (content ) {
1330
+ t .Fatalf ("got %q; want %q" , buf , content )
1331
+ }
1332
+ }
0 commit comments