11cb0ef41Sopenharmony_ci// Test for "overlapped" stdio option. This test uses the "overlapped-checker" 21cb0ef41Sopenharmony_ci// helper program which basically a specialized echo program. 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci// The test has two goals: 51cb0ef41Sopenharmony_ci// 61cb0ef41Sopenharmony_ci// - Verify that overlapped I/O works on windows. The test program will deadlock 71cb0ef41Sopenharmony_ci// if stdin doesn't have the FILE_FLAG_OVERLAPPED flag set on startup (see 81cb0ef41Sopenharmony_ci// test/overlapped-checker/main_win.c for more details). 91cb0ef41Sopenharmony_ci// - Verify that "overlapped" stdio option works transparently as a pipe (on 101cb0ef41Sopenharmony_ci// unix/windows) 111cb0ef41Sopenharmony_ci// 121cb0ef41Sopenharmony_ci// This is how the test works: 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// - This script assumes only numeric strings are written to the test program 151cb0ef41Sopenharmony_ci// stdout. 161cb0ef41Sopenharmony_ci// - The test program will be spawned with "overlapped" set on stdin and "pipe" 171cb0ef41Sopenharmony_ci// set on stdout/stderr and at startup writes a number to its stdout 181cb0ef41Sopenharmony_ci// - When this script receives some data, it will parse the number, add 50 and 191cb0ef41Sopenharmony_ci// write to the test program's stdin. 201cb0ef41Sopenharmony_ci// - The test program will then echo the number back to us which will repeat the 211cb0ef41Sopenharmony_ci// cycle until the number reaches 200, at which point we send the "exit" 221cb0ef41Sopenharmony_ci// string, which causes the test program to exit. 231cb0ef41Sopenharmony_ci// - Extra assertion: Every time the test program writes a string to its stdout, 241cb0ef41Sopenharmony_ci// it will write the number of bytes written to stderr. 251cb0ef41Sopenharmony_ci// - If overlapped I/O is not setup correctly, this test is going to hang. 261cb0ef41Sopenharmony_ci'use strict'; 271cb0ef41Sopenharmony_ciconst common = require('../common'); 281cb0ef41Sopenharmony_ciconst assert = require('assert'); 291cb0ef41Sopenharmony_ciconst path = require('path'); 301cb0ef41Sopenharmony_ciconst child_process = require('child_process'); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst exeExtension = process.platform === 'win32' ? '.exe' : ''; 331cb0ef41Sopenharmony_ciconst exe = 'overlapped-checker' + exeExtension; 341cb0ef41Sopenharmony_ciconst exePath = path.join(path.dirname(process.execPath), exe); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciif (!require('fs').existsSync(exePath)) { 371cb0ef41Sopenharmony_ci common.skip(exe + ' binary is not available'); 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciconst child = child_process.spawn(exePath, [], { 411cb0ef41Sopenharmony_ci stdio: ['overlapped', 'pipe', 'pipe'] 421cb0ef41Sopenharmony_ci}); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_cichild.stdin.setEncoding('utf8'); 451cb0ef41Sopenharmony_cichild.stdout.setEncoding('utf8'); 461cb0ef41Sopenharmony_cichild.stderr.setEncoding('utf8'); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_cifunction writeNext(n) { 491cb0ef41Sopenharmony_ci child.stdin.write((n + 50).toString()); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cichild.stdout.on('data', (s) => { 531cb0ef41Sopenharmony_ci const n = Number(s); 541cb0ef41Sopenharmony_ci if (n >= 200) { 551cb0ef41Sopenharmony_ci child.stdin.write('exit'); 561cb0ef41Sopenharmony_ci return; 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci writeNext(n); 591cb0ef41Sopenharmony_ci}); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_cilet stderr = ''; 621cb0ef41Sopenharmony_cichild.stderr.on('data', (s) => { 631cb0ef41Sopenharmony_ci stderr += s; 641cb0ef41Sopenharmony_ci}); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_cichild.stderr.on('end', common.mustCall(() => { 671cb0ef41Sopenharmony_ci // This is the sequence of numbers sent to us: 681cb0ef41Sopenharmony_ci // - 0 (1 byte written) 691cb0ef41Sopenharmony_ci // - 50 (2 bytes written) 701cb0ef41Sopenharmony_ci // - 100 (3 bytes written) 711cb0ef41Sopenharmony_ci // - 150 (3 bytes written) 721cb0ef41Sopenharmony_ci // - 200 (3 bytes written) 731cb0ef41Sopenharmony_ci assert.strictEqual(stderr, '12333'); 741cb0ef41Sopenharmony_ci})); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_cichild.on('exit', common.mustCall((status) => { 771cb0ef41Sopenharmony_ci // The test program will return the number of writes as status code. 781cb0ef41Sopenharmony_ci assert.strictEqual(status, 0); 791cb0ef41Sopenharmony_ci})); 80