@@ -309,7 +309,61 @@ describe('BlockStatementControlFlowTransformer', function () {
309309 } ) ;
310310 } ) ;
311311
312- describe ( 'Variant #8: block statement contain while statement with break statement' , ( ) => {
312+ describe ( 'Variant #8: block statement contain break statement #3' , ( ) => {
313+ const statementRegExp : RegExp = / ^ \( f u n c t i o n * \( * \) * \{ * w h i l e * \( ! ! \[ \] \) * \{ * i f * \( ! ! \[ \] \) * b r e a k ; * c o n s o l e \[ ' l o g ' \] \( 0 x 1 \) ; / ;
314+
315+ let obfuscatedCode : string ;
316+
317+ before ( ( ) => {
318+ const code : string = readFileAsString ( __dirname + '/fixtures/break-statement-3.js' ) ;
319+
320+ obfuscatedCode = JavaScriptObfuscator . obfuscate (
321+ code ,
322+ {
323+ ...NO_ADDITIONAL_NODES_PRESET ,
324+ controlFlowFlattening : true ,
325+ controlFlowFlatteningThreshold : 1
326+ }
327+ ) . getObfuscatedCode ( ) ;
328+ } ) ;
329+
330+ it ( 'shouldn\'t transform block statement' , ( ) => {
331+ assert . match ( obfuscatedCode , statementRegExp ) ;
332+ } ) ;
333+ } ) ;
334+
335+ describe ( 'Variant #9: block statement contain while statement with break statement' , ( ) => {
336+ const switchCaseRegExp : RegExp = / s w i t c h * \( _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \[ _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \+ \+ \] \) * \{ / ;
337+ const switchCaseLengthRegExp : RegExp = / c a s e * ' [ 0 - 5 ] ' : * c o n s o l e \[ ' l o g ' \] \( 0 x [ 0 - 6 ] \) ; / g;
338+ const expectedSwitchCaseLength : number = 5 ;
339+
340+ let obfuscatedCode : string ,
341+ switchCaseLength : number ;
342+
343+ before ( ( ) => {
344+ const code : string = readFileAsString ( __dirname + '/fixtures/break-statement-inside-while-statement-1.js' ) ;
345+
346+ obfuscatedCode = JavaScriptObfuscator . obfuscate (
347+ code ,
348+ {
349+ ...NO_ADDITIONAL_NODES_PRESET ,
350+ controlFlowFlattening : true ,
351+ controlFlowFlatteningThreshold : 1
352+ }
353+ ) . getObfuscatedCode ( ) ;
354+ switchCaseLength = obfuscatedCode . match ( switchCaseLengthRegExp ) ! . length ;
355+ } ) ;
356+
357+ it ( 'should wrap block statement statements in switch-case structure' , ( ) => {
358+ assert . match ( obfuscatedCode , switchCaseRegExp ) ;
359+ } ) ;
360+
361+ it ( 'each statement should be wrapped by switch-case structure' , ( ) => {
362+ assert . equal ( switchCaseLength , expectedSwitchCaseLength ) ;
363+ } ) ;
364+ } ) ;
365+
366+ describe ( 'Variant #10: block statement contain while statement with break statement' , ( ) => {
313367 const switchCaseRegExp : RegExp = / s w i t c h * \( _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \[ _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \+ \+ \] \) * \{ / ;
314368 const switchCaseLengthRegExp : RegExp = / c a s e * ' [ 0 - 5 ] ' : * c o n s o l e \[ ' l o g ' \] \( 0 x [ 0 - 6 ] \) ; / g;
315369 const expectedSwitchCaseLength : number = 5 ;
@@ -318,7 +372,7 @@ describe('BlockStatementControlFlowTransformer', function () {
318372 switchCaseLength : number ;
319373
320374 before ( ( ) => {
321- const code : string = readFileAsString ( __dirname + '/fixtures/break-statement-inside-while-statement.js' ) ;
375+ const code : string = readFileAsString ( __dirname + '/fixtures/break-statement-inside-while-statement-2 .js' ) ;
322376
323377 obfuscatedCode = JavaScriptObfuscator . obfuscate (
324378 code ,
@@ -340,7 +394,7 @@ describe('BlockStatementControlFlowTransformer', function () {
340394 } ) ;
341395 } ) ;
342396
343- describe ( 'Variant #9 : block statement contain continue statement #1' , ( ) => {
397+ describe ( 'Variant #11 : block statement contain continue statement #1' , ( ) => {
344398 const statementRegExp : RegExp = / ^ \( f u n c t i o n * \( * \) * \{ * w h i l e * \( ! ! \[ \] \) * \{ * c o n t i n u e ; * c o n s o l e \[ ' l o g ' \] \( 0 x 1 \) ; / ;
345399
346400 let obfuscatedCode : string ;
@@ -363,7 +417,7 @@ describe('BlockStatementControlFlowTransformer', function () {
363417 } ) ;
364418 } ) ;
365419
366- describe ( 'Variant #10 : block statement contain continue statement #2' , ( ) => {
420+ describe ( 'Variant #12 : block statement contain continue statement #2' , ( ) => {
367421 const statementRegExp : RegExp = / ^ \( f u n c t i o n * \( * \) * \{ * w h i l e * \( ! ! \[ \] \) * \{ * i f * \( ! ! \[ \] \) * \{ * c o n t i n u e ; * \} * c o n s o l e \[ ' l o g ' \] \( 0 x 1 \) ; / ;
368422
369423 let obfuscatedCode : string ;
@@ -386,7 +440,61 @@ describe('BlockStatementControlFlowTransformer', function () {
386440 } ) ;
387441 } ) ;
388442
389- describe ( 'Variant #11: block statement contain while statement with continue statement' , ( ) => {
443+ describe ( 'Variant #13: block statement contain continue statement #3' , ( ) => {
444+ const statementRegExp : RegExp = / ^ \( f u n c t i o n * \( * \) * \{ * w h i l e * \( ! ! \[ \] \) * \{ * i f * \( ! ! \[ \] \) * c o n t i n u e ; * c o n s o l e \[ ' l o g ' \] \( 0 x 1 \) ; / ;
445+
446+ let obfuscatedCode : string ;
447+
448+ before ( ( ) => {
449+ const code : string = readFileAsString ( __dirname + '/fixtures/continue-statement-3.js' ) ;
450+
451+ obfuscatedCode = JavaScriptObfuscator . obfuscate (
452+ code ,
453+ {
454+ ...NO_ADDITIONAL_NODES_PRESET ,
455+ controlFlowFlattening : true ,
456+ controlFlowFlatteningThreshold : 1
457+ }
458+ ) . getObfuscatedCode ( ) ;
459+ } ) ;
460+
461+ it ( 'shouldn\'t transform block statement' , ( ) => {
462+ assert . match ( obfuscatedCode , statementRegExp ) ;
463+ } ) ;
464+ } ) ;
465+
466+ describe ( 'Variant #14: block statement contain while statement with continue statement' , ( ) => {
467+ const switchCaseRegExp : RegExp = / s w i t c h * \( _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \[ _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \+ \+ \] \) * \{ / ;
468+ const switchCaseLengthRegExp : RegExp = / c a s e * ' [ 0 - 5 ] ' : * c o n s o l e \[ ' l o g ' \] \( 0 x [ 0 - 6 ] \) ; / g;
469+ const expectedSwitchCaseLength : number = 5 ;
470+
471+ let obfuscatedCode : string ,
472+ switchCaseLength : number ;
473+
474+ before ( ( ) => {
475+ const code : string = readFileAsString ( __dirname + '/fixtures/continue-statement-inside-while-statement-1.js' ) ;
476+
477+ obfuscatedCode = JavaScriptObfuscator . obfuscate (
478+ code ,
479+ {
480+ ...NO_ADDITIONAL_NODES_PRESET ,
481+ controlFlowFlattening : true ,
482+ controlFlowFlatteningThreshold : 1
483+ }
484+ ) . getObfuscatedCode ( ) ;
485+ switchCaseLength = obfuscatedCode . match ( switchCaseLengthRegExp ) ! . length ;
486+ } ) ;
487+
488+ it ( 'should wrap block statement statements in switch-case structure' , ( ) => {
489+ assert . match ( obfuscatedCode , switchCaseRegExp ) ;
490+ } ) ;
491+
492+ it ( 'each statement should be wrapped by switch-case structure' , ( ) => {
493+ assert . equal ( switchCaseLength , expectedSwitchCaseLength ) ;
494+ } ) ;
495+ } ) ;
496+
497+ describe ( 'Variant #15: block statement contain continue statement #4' , ( ) => {
390498 const switchCaseRegExp : RegExp = / s w i t c h * \( _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \[ _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \+ \+ \] \) * \{ / ;
391499 const switchCaseLengthRegExp : RegExp = / c a s e * ' [ 0 - 5 ] ' : * c o n s o l e \[ ' l o g ' \] \( 0 x [ 0 - 6 ] \) ; / g;
392500 const expectedSwitchCaseLength : number = 5 ;
@@ -395,7 +503,7 @@ describe('BlockStatementControlFlowTransformer', function () {
395503 switchCaseLength : number ;
396504
397505 before ( ( ) => {
398- const code : string = readFileAsString ( __dirname + '/fixtures/continue-statement-inside-while-statement.js' ) ;
506+ const code : string = readFileAsString ( __dirname + '/fixtures/continue-statement-inside-while-statement-2 .js' ) ;
399507
400508 obfuscatedCode = JavaScriptObfuscator . obfuscate (
401509 code ,
@@ -417,7 +525,7 @@ describe('BlockStatementControlFlowTransformer', function () {
417525 } ) ;
418526 } ) ;
419527
420- describe ( 'Variant #12 : block statement contain function declaration' , ( ) => {
528+ describe ( 'Variant #16 : block statement contain function declaration' , ( ) => {
421529 const statementRegExp : RegExp = / ^ \( f u n c t i o n * \( * \) * \{ * f u n c t i o n * _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } * \( * \) * \{ * \} * c o n s o l e \[ ' l o g ' \] \( 0 x 1 \) ; /
422530
423531 let obfuscatedCode : string ;
@@ -440,7 +548,7 @@ describe('BlockStatementControlFlowTransformer', function () {
440548 } ) ;
441549 } ) ;
442550
443- describe ( 'Variant #13 : block statement contain class declaration' , ( ) => {
551+ describe ( 'Variant #17 : block statement contain class declaration' , ( ) => {
444552 const statementRegExp : RegExp = / ^ \( f u n c t i o n * \( * \) * { * * c l a s s * _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } * { .* ?} * } .* c l a s s * _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } * { .* ?} * } .* c l a s s * _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } * { .* ?} * } / ;
445553
446554 let obfuscatedCode : string ;
@@ -463,7 +571,7 @@ describe('BlockStatementControlFlowTransformer', function () {
463571 } ) ;
464572 } ) ;
465573
466- describe ( 'Variant #14 : `controlFlowFlatteningThreshold` chance' , ( ) => {
574+ describe ( 'Variant #18 : `controlFlowFlatteningThreshold` chance' , ( ) => {
467575 const samples : number = 1000 ;
468576 const delta : number = 0.1 ;
469577
@@ -507,7 +615,7 @@ describe('BlockStatementControlFlowTransformer', function () {
507615 } ) ;
508616 } ) ;
509617
510- describe ( 'Variant #15 : No `unreachable code after return statement` warning' , ( ) => {
618+ describe ( 'Variant #19 : No `unreachable code after return statement` warning' , ( ) => {
511619 const switchCaseRegExp : RegExp = / s w i t c h * \( _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \[ _ 0 x ( [ a - f 0 - 9 ] ) { 4 , 6 } \+ \+ \] \) * \{ / ;
512620 const switchCaseLengthRegExp : RegExp = / c a s e * ' [ 0 - 5 ] ' : * c o n s o l e \[ ' l o g ' \] \( 0 x [ 0 - 6 ] \) ; / g;
513621 const returnStatementRegExp : RegExp = / c a s e * ' [ 0 - 5 ] ' : * r e t u r n ; * ( c a s e | } ) / ;
0 commit comments