22
33// Flags: --expose-internals
44
5- require ( '../common' ) ;
5+ const common = require ( '../common' ) ;
66const assert = require ( 'assert' ) ;
7- const isLegalPort = require ( 'internal/net' ) . isLegalPort ;
7+ const { isLegalPort, makeSyncWrite } = require ( 'internal/net' ) ;
88
99for ( let n = 0 ; n <= 0xFFFF ; n ++ ) {
1010 assert ( isLegalPort ( n ) ) ;
@@ -18,3 +18,38 @@ const bad = [-1, 'a', {}, [], false, true, 0xFFFF + 1, Infinity,
1818 - Infinity , NaN , undefined , null , '' , ' ' , 1.1 , '0x' ,
1919 '-0x1' , '-0o1' , '-0b1' , '0o' , '0b' ] ;
2020bad . forEach ( ( i ) => assert ( ! isLegalPort ( i ) ) ) ;
21+
22+
23+ function writeToStdout ( ) {
24+ const ctx = {
25+ _handle : {
26+ bytesWritten : 0
27+ }
28+ } ;
29+ const syncWrite = makeSyncWrite ( process . stdout . fd ) ;
30+ syncWrite . call ( ctx , 'hello' , 'utf-8' , common . mustCall ( ( ex ) => {
31+ assert . strictEqual ( undefined , ex ) ;
32+ } ) ) ;
33+ syncWrite . call ( ctx , Buffer . from ( 'world' ) , 'buffer' , common . mustCall ( ( ex ) => {
34+ assert . strictEqual ( undefined , ex ) ;
35+ } ) ) ;
36+ assert . strictEqual ( ctx . _handle . bytesWritten , 10 ) ;
37+ }
38+ function writeToInvalidFD ( ) {
39+ const ctx = {
40+ _handle : {
41+ bytesWritten : 0
42+ }
43+ } ;
44+ const invalidFD = - 1 ;
45+ const syncWrite = makeSyncWrite ( invalidFD ) ;
46+ syncWrite . call ( ctx , Buffer . from ( 'world' ) , 'buffer' , common . mustCall ( ( ex ) => {
47+ assert . strictEqual ( ex . code , 'EBADF' ) ;
48+ } ) ) ;
49+ assert . strictEqual ( ctx . _handle . bytesWritten , 5 ) ;
50+ }
51+ if ( ! common . isWindows ) {
52+ // This test will crash on windows, so skip it
53+ writeToStdout ( ) ;
54+ writeToInvalidFD ( ) ;
55+ }
0 commit comments