2019-08-05 03:47:42 +03:00
import { expect } from 'chai' ;
2023-06-15 17:42:27 +03:00
import * as http from 'node:http' ;
import * as fs from 'node:fs' ;
import * as os from 'node:os' ;
import * as path from 'node:path' ;
import * as ChildProcess from 'node:child_process' ;
2020-04-07 03:04:09 +03:00
import { session , net } from 'electron/main' ;
2023-06-15 17:42:27 +03:00
import { Socket } from 'node:net' ;
2023-02-20 14:30:57 +03:00
import { ifit , listen } from './lib/spec-helpers' ;
2023-06-15 17:42:27 +03:00
import { once } from 'node:events' ;
2020-03-20 23:28:31 +03:00
2018-06-19 04:45:58 +03:00
const appPath = path . join ( __dirname , 'fixtures' , 'api' , 'net-log' ) ;
const dumpFile = path . join ( os . tmpdir ( ) , 'net_log.json' ) ;
const dumpFileDynamic = path . join ( os . tmpdir ( ) , 'net_log_dynamic.json' ) ;
2020-03-20 23:28:31 +03:00
2019-07-25 02:01:08 +03:00
const testNetLog = ( ) = > session . fromPartition ( 'net-log' ) . netLog ;
2018-06-19 04:45:58 +03:00
describe ( 'netLog module' , ( ) = > {
2019-08-05 03:47:42 +03:00
let server : http.Server ;
let serverUrl : string ;
const connections : Set < Socket > = new Set ( ) ;
2018-06-19 04:45:58 +03:00
2023-02-20 14:30:57 +03:00
before ( async ( ) = > {
2018-06-19 04:45:58 +03:00
server = http . createServer ( ) ;
server . on ( 'connection' , ( connection ) = > {
connections . add ( connection ) ;
connection . once ( 'close' , ( ) = > {
connections . delete ( connection ) ;
} ) ;
} ) ;
server . on ( 'request' , ( request , response ) = > {
response . end ( ) ;
} ) ;
2023-02-20 14:30:57 +03:00
serverUrl = ( await listen ( server ) ) . url ;
2018-06-19 04:45:58 +03:00
} ) ;
2018-06-29 01:40:30 +03:00
after ( done = > {
2018-06-19 04:45:58 +03:00
for ( const connection of connections ) {
connection . destroy ( ) ;
}
server . close ( ( ) = > {
2019-08-05 03:47:42 +03:00
server = null as any ;
2018-06-19 04:45:58 +03:00
done ( ) ;
} ) ;
} ) ;
2019-05-24 01:31:38 +03:00
beforeEach ( ( ) = > {
2019-08-05 03:47:42 +03:00
expect ( testNetLog ( ) . currentlyLogging ) . to . be . false ( 'currently logging' ) ;
2019-05-24 01:31:38 +03:00
} ) ;
2018-06-19 04:45:58 +03:00
afterEach ( ( ) = > {
try {
2018-10-04 21:08:56 +03:00
if ( fs . existsSync ( dumpFile ) ) {
fs . unlinkSync ( dumpFile ) ;
}
if ( fs . existsSync ( dumpFileDynamic ) ) {
fs . unlinkSync ( dumpFileDynamic ) ;
}
2023-07-27 17:53:45 +03:00
} catch {
2018-06-19 04:45:58 +03:00
// Ignore error
}
2019-08-05 03:47:42 +03:00
expect ( testNetLog ( ) . currentlyLogging ) . to . be . false ( 'currently logging' ) ;
2018-06-19 04:45:58 +03:00
} ) ;
2019-02-19 13:48:27 +03:00
it ( 'should begin and end logging to file when .startLogging() and .stopLogging() is called' , async ( ) = > {
2019-07-25 02:01:08 +03:00
await testNetLog ( ) . startLogging ( dumpFileDynamic ) ;
2019-02-19 13:48:27 +03:00
2019-08-05 03:47:42 +03:00
expect ( testNetLog ( ) . currentlyLogging ) . to . be . true ( 'currently logging' ) ;
2019-05-24 01:31:38 +03:00
2020-03-19 02:46:05 +03:00
await testNetLog ( ) . stopLogging ( ) ;
2019-02-19 13:48:27 +03:00
2019-08-05 03:47:42 +03:00
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( 'currently logging' ) ;
2019-02-19 13:48:27 +03:00
} ) ;
2019-05-24 01:31:38 +03:00
it ( 'should throw an error when .stopLogging() is called without calling .startLogging()' , async ( ) = > {
2019-07-25 02:01:08 +03:00
await expect ( testNetLog ( ) . stopLogging ( ) ) . to . be . rejectedWith ( 'No net log in progress' ) ;
2019-05-24 01:31:38 +03:00
} ) ;
2019-02-19 13:48:27 +03:00
2019-05-24 01:31:38 +03:00
it ( 'should throw an error when .startLogging() is called with an invalid argument' , ( ) = > {
2019-07-25 02:01:08 +03:00
expect ( ( ) = > testNetLog ( ) . startLogging ( '' ) ) . to . throw ( ) ;
2019-08-05 03:47:42 +03:00
expect ( ( ) = > testNetLog ( ) . startLogging ( null as any ) ) . to . throw ( ) ;
expect ( ( ) = > testNetLog ( ) . startLogging ( [ ] as any ) ) . to . throw ( ) ;
2019-11-01 23:37:02 +03:00
expect ( ( ) = > testNetLog ( ) . startLogging ( 'aoeu' , { captureMode : 'aoeu' as any } ) ) . to . throw ( ) ;
expect ( ( ) = > testNetLog ( ) . startLogging ( 'aoeu' , { maxFileSize : null as any } ) ) . to . throw ( ) ;
2019-07-26 02:06:39 +03:00
} ) ;
it ( 'should include cookies when requested' , async ( ) = > {
2019-11-01 23:37:02 +03:00
await testNetLog ( ) . startLogging ( dumpFileDynamic , { captureMode : 'includeSensitive' } ) ;
2019-07-26 02:06:39 +03:00
const unique = require ( 'uuid' ) . v4 ( ) ;
2021-01-22 22:25:47 +03:00
await new Promise < void > ( ( resolve ) = > {
2019-08-05 03:47:42 +03:00
const req = net . request ( serverUrl ) ;
2019-07-26 02:06:39 +03:00
req . setHeader ( 'Cookie' , ` foo= ${ unique } ` ) ;
req . on ( 'response' , ( response ) = > {
2019-11-01 23:37:02 +03:00
response . on ( 'data' , ( ) = > { } ) ; // https://github.com/electron/electron/issues/19214
2019-07-26 02:06:39 +03:00
response . on ( 'end' , ( ) = > resolve ( ) ) ;
} ) ;
req . end ( ) ;
} ) ;
await testNetLog ( ) . stopLogging ( ) ;
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( 'dump file exists' ) ;
const dump = fs . readFileSync ( dumpFileDynamic , 'utf8' ) ;
expect ( dump ) . to . contain ( ` foo= ${ unique } ` ) ;
} ) ;
it ( 'should include socket bytes when requested' , async ( ) = > {
2019-11-01 23:37:02 +03:00
await testNetLog ( ) . startLogging ( dumpFileDynamic , { captureMode : 'everything' } ) ;
2019-07-26 02:06:39 +03:00
const unique = require ( 'uuid' ) . v4 ( ) ;
2021-01-22 22:25:47 +03:00
await new Promise < void > ( ( resolve ) = > {
2019-11-01 23:37:02 +03:00
const req = net . request ( { method : 'POST' , url : serverUrl } ) ;
2019-07-26 02:06:39 +03:00
req . on ( 'response' , ( response ) = > {
2019-11-01 23:37:02 +03:00
response . on ( 'data' , ( ) = > { } ) ; // https://github.com/electron/electron/issues/19214
2019-07-26 02:06:39 +03:00
response . on ( 'end' , ( ) = > resolve ( ) ) ;
} ) ;
req . end ( Buffer . from ( unique ) ) ;
} ) ;
await testNetLog ( ) . stopLogging ( ) ;
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( 'dump file exists' ) ;
const dump = fs . readFileSync ( dumpFileDynamic , 'utf8' ) ;
2019-08-05 03:47:42 +03:00
expect ( JSON . parse ( dump ) . events . some ( ( x : any ) = > x . params && x . params . bytes && Buffer . from ( x . params . bytes , 'base64' ) . includes ( unique ) ) ) . to . be . true ( 'uuid present in dump' ) ;
2019-02-19 13:48:27 +03:00
} ) ;
2019-10-31 02:38:21 +03:00
ifit ( process . platform !== 'linux' ) ( 'should begin and end logging automatically when --log-net-log is passed' , async ( ) = > {
2019-05-24 01:31:38 +03:00
const appProcess = ChildProcess . spawn ( process . execPath ,
2018-10-04 21:08:56 +03:00
[ appPath ] , {
2018-06-19 04:45:58 +03:00
env : {
2019-08-05 03:47:42 +03:00
TEST_REQUEST_URL : serverUrl ,
2018-10-04 21:08:56 +03:00
TEST_DUMP_FILE : dumpFile
2018-06-19 04:45:58 +03:00
}
} ) ;
2023-02-24 02:53:53 +03:00
await once ( appProcess , 'exit' ) ;
2019-10-31 02:38:21 +03:00
expect ( fs . existsSync ( dumpFile ) ) . to . be . true ( 'dump file exists' ) ;
2018-06-19 04:45:58 +03:00
} ) ;
2022-06-16 10:46:11 +03:00
ifit ( process . platform !== 'linux' ) ( 'should begin and end logging automatically when --log-net-log is passed, and behave correctly when .startLogging() and .stopLogging() is called' , async ( ) = > {
2019-05-24 01:31:38 +03:00
const appProcess = ChildProcess . spawn ( process . execPath ,
2018-10-04 21:08:56 +03:00
[ appPath ] , {
2018-06-19 04:45:58 +03:00
env : {
2019-08-05 03:47:42 +03:00
TEST_REQUEST_URL : serverUrl ,
2018-10-04 21:08:56 +03:00
TEST_DUMP_FILE : dumpFile ,
TEST_DUMP_FILE_DYNAMIC : dumpFileDynamic ,
2019-08-05 03:47:42 +03:00
TEST_MANUAL_STOP : 'true'
2018-06-19 04:45:58 +03:00
}
} ) ;
2023-02-24 02:53:53 +03:00
await once ( appProcess , 'exit' ) ;
2019-10-31 02:38:21 +03:00
expect ( fs . existsSync ( dumpFile ) ) . to . be . true ( 'dump file exists' ) ;
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( 'dynamic dump file exists' ) ;
2018-06-19 04:45:58 +03:00
} ) ;
2019-10-31 02:38:21 +03:00
ifit ( process . platform !== 'linux' ) ( 'should end logging automatically when only .startLogging() is called' , async ( ) = > {
2019-05-24 01:31:38 +03:00
const appProcess = ChildProcess . spawn ( process . execPath ,
2018-06-19 04:45:58 +03:00
[ appPath ] , {
env : {
2019-08-05 03:47:42 +03:00
TEST_REQUEST_URL : serverUrl ,
2018-10-04 21:08:56 +03:00
TEST_DUMP_FILE_DYNAMIC : dumpFileDynamic
2018-06-19 04:45:58 +03:00
}
} ) ;
2023-02-24 02:53:53 +03:00
await once ( appProcess , 'exit' ) ;
2019-10-31 02:38:21 +03:00
expect ( fs . existsSync ( dumpFileDynamic ) ) . to . be . true ( 'dynamic dump file exists' ) ;
2018-06-19 04:45:58 +03:00
} ) ;
} ) ;