11import assert from 'node:assert'
2- import { describe , it } from 'node:test'
3- import type { TLSPacket } from '../types/index.ts'
2+ import { beforeEach , describe , it } from 'node:test'
3+ import type { PacketProcessor , TLSPacket , TLSPacketWithType } from '../types/index.ts'
44import { logger } from '../utils/index.ts'
55import { makeMessageProcessor } from '../utils/index.ts'
66import { expectBuffsEq } from './utils.ts'
77
88describe ( 'TLS Message Processor' , ( ) => {
99
10- it ( 'should process a complete message' , async ( ) => {
11- const processor = makeTestMsgProcessor ( )
12- const pkts = await processor . onData (
13- Buffer . from ( '15030300050101010101' , 'hex' )
10+ let processor : PacketProcessor
11+ beforeEach ( ( ) => {
12+ processor = makeMessageProcessor ( logger )
13+ } )
14+
15+ it ( 'should process a complete message' , ( ) => {
16+ const pkts = Array . from (
17+ processor . onData ( Buffer . from ( '15030300050101010101' , 'hex' ) )
1418 )
1519 assert . equal ( pkts . length , 1 )
16- expectBuffsEq ( pkts [ 0 ] . header , Buffer . from ( '1503030005' , 'hex' ) )
17- expectBuffsEq ( pkts [ 0 ] . content , Buffer . from ( '0101010101' , 'hex' ) )
20+ expectBuffsEq ( pkts [ 0 ] . packet . header , Buffer . from ( '1503030005' , 'hex' ) )
21+ expectBuffsEq ( pkts [ 0 ] . packet . content , Buffer . from ( '0101010101' , 'hex' ) )
1822 } )
1923
20- it ( 'should process a message byte-by-byte' , async ( ) => {
21- const processor = makeTestMsgProcessor ( )
24+ it ( 'should process a message byte-by-byte' , ( ) => {
2225 const buffer = Buffer . from ( '15030300050101010101' , 'hex' )
2326 for ( let i = 0 ; i < buffer . length ; i ++ ) {
24- const pkts = await processor . onData ( buffer . subarray ( i , i + 1 ) )
27+ const pkts = Array . from ( processor . onData ( buffer . subarray ( i , i + 1 ) ) )
2528 if ( i < buffer . length - 1 ) {
2629 assert . equal ( pkts . length , 0 )
2730 } else {
2831 assert . equal ( pkts . length , 1 )
29- expectBuffsEq ( pkts [ 0 ] . content , Buffer . from ( '0101010101' , 'hex' ) )
32+ expectBuffsEq ( pkts [ 0 ] . packet . content , Buffer . from ( '0101010101' , 'hex' ) )
3033 }
3134 }
3235 } )
3336
3437 it ( 'should process multiple messages' , async ( ) => {
35- const processor = makeTestMsgProcessor ( )
3638 const buffers = [
3739 Buffer . from ( '15030300050101010101' , 'hex' ) ,
3840 Buffer . from ( '1503030006010101010101' , 'hex' )
3941 ]
40- const pkts = await processor . onData ( Buffer . concat ( buffers ) )
42+ const pkts = Array . from ( processor . onData ( Buffer . concat ( buffers ) ) )
4143 assert . equal ( pkts . length , 2 )
42- expectBuffsEq ( pkts [ 0 ] . content , Buffer . from ( '0101010101' , 'hex' ) )
43- expectBuffsEq ( pkts [ 1 ] . content , Buffer . from ( '010101010101' , 'hex' ) )
44+ expectBuffsEq ( pkts [ 0 ] . packet . content , Buffer . from ( '0101010101' , 'hex' ) )
45+ expectBuffsEq ( pkts [ 1 ] . packet . content , Buffer . from ( '010101010101' , 'hex' ) )
4446 } )
4547
4648 it ( 'should process a message and a half' , async ( ) => {
47- const processor = makeTestMsgProcessor ( )
4849 const msgAndHalfBuffer = Buffer . concat (
4950 [
5051 Buffer . from ( '15030300050101010101' , 'hex' ) ,
5152 Buffer . from ( '1503030006' , 'hex' )
5253 ]
5354 )
5455 const finalBuffer = Buffer . from ( '010101010101' , 'hex' )
55- const pkts = await processor . onData ( msgAndHalfBuffer )
56+ const pkts = Array . from ( processor . onData ( msgAndHalfBuffer ) )
5657 assert . equal ( pkts . length , 1 )
57- const pkts2 = await processor . onData ( finalBuffer )
58+ const pkts2 = Array . from ( processor . onData ( finalBuffer ) )
5859 assert . equal ( pkts2 . length , 1 )
5960 } )
60-
61- // eslint-disable-next-line unicorn/consistent-function-scoping
62- function makeTestMsgProcessor ( ) {
63- const processor = makeMessageProcessor ( logger )
64-
65- return {
66- ...processor ,
67- async onData ( packet : Buffer ) {
68- const packets : TLSPacket [ ] = [ ]
69- await processor . onData (
70- packet ,
71- ( _ , pkt ) => {
72- packets . push ( pkt )
73- }
74- )
75-
76- return packets
77- }
78- }
79- }
8061} )
0 commit comments