|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import{escapeControlCharacters}from'../escapeControlCharacters'; |
| 9 | + |
| 10 | +describe('escapeControlCharacters',()=>{ |
| 11 | +test('preserves regular printable characters',()=>{ |
| 12 | +constinput='Jest 123!@#$%^&*()'; |
| 13 | +expect(escapeControlCharacters(input)).toBe(input); |
| 14 | +}); |
| 15 | + |
| 16 | +test('preserves whitespace characters that are meaningful for formatting',()=>{ |
| 17 | +constinput='line1\nline2\tindented\rcarriage'; |
| 18 | +expect(escapeControlCharacters(input)).toBe(input); |
| 19 | +}); |
| 20 | + |
| 21 | +test('escapes NULL character',()=>{ |
| 22 | +constinput='before\u0000after'; |
| 23 | +expect(escapeControlCharacters(input)).toBe('before\\x00after'); |
| 24 | +}); |
| 25 | + |
| 26 | +test('escapes SOH (Start of Heading) character',()=>{ |
| 27 | +constinput='before\u0001after'; |
| 28 | +expect(escapeControlCharacters(input)).toBe('before\\x01after'); |
| 29 | +}); |
| 30 | + |
| 31 | +test('escapes backspace character to \\b',()=>{ |
| 32 | +constinput='before\u0008after'; |
| 33 | +expect(escapeControlCharacters(input)).toBe('before\\bafter'); |
| 34 | +}); |
| 35 | + |
| 36 | +test('escapes vertical tab character to \\v',()=>{ |
| 37 | +constinput='before\u000Bafter'; |
| 38 | +expect(escapeControlCharacters(input)).toBe('before\\vafter'); |
| 39 | +}); |
| 40 | + |
| 41 | +test('escapes form feed character to \\f',()=>{ |
| 42 | +constinput='before\u000Cafter'; |
| 43 | +expect(escapeControlCharacters(input)).toBe('before\\fafter'); |
| 44 | +}); |
| 45 | + |
| 46 | +test('escapes ESC (Escape) character',()=>{ |
| 47 | +constinput='before\u001Bafter'; |
| 48 | +expect(escapeControlCharacters(input)).toBe('before\\x1bafter'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('escapes DEL character',()=>{ |
| 52 | +constinput='before\u007Fafter'; |
| 53 | +expect(escapeControlCharacters(input)).toBe('before\\x7fafter'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('escapes C1 control characters',()=>{ |
| 57 | +constinput='before\u0080\u0081\u009Fafter'; |
| 58 | +expect(escapeControlCharacters(input)).toBe('before\\x80\\x81\\x9fafter'); |
| 59 | +}); |
| 60 | + |
| 61 | +test('handles mixed control characters and regular text',()=>{ |
| 62 | +constinput='FIX\u00014.4\u00019=68\u00135=A\u0001MSG_TYPE=D'; |
| 63 | +expect(escapeControlCharacters(input)).toBe( |
| 64 | +'FIX\\x014.4\\x019=68\\x135=A\\x01MSG_TYPE=D', |
| 65 | +); |
| 66 | +}); |
| 67 | + |
| 68 | +test('handles financial message protocol string with control characters',()=>{ |
| 69 | +constinput='8=FIXT.1.1\u00019=68\u00135=A\u00134=1\u00149=ISLD'; |
| 70 | +expect(escapeControlCharacters(input)).toBe( |
| 71 | +'8=FIXT.1.1\\x019=68\\x135=A\\x134=1\\x149=ISLD', |
| 72 | +); |
| 73 | +}); |
| 74 | + |
| 75 | +test('preserves empty string',()=>{ |
| 76 | +expect(escapeControlCharacters('')).toBe(''); |
| 77 | +}); |
| 78 | + |
| 79 | +test('handles string with only control characters',()=>{ |
| 80 | +constinput='\u0000\u0001\u0002\u0003'; |
| 81 | +expect(escapeControlCharacters(input)).toBe('\\x00\\x01\\x02\\x03'); |
| 82 | +}); |
| 83 | + |
| 84 | +test('preserves Unicode characters that are not control characters',()=>{ |
| 85 | +constinput='café 中文 🚀 αβγ'; |
| 86 | +expect(escapeControlCharacters(input)).toBe(input); |
| 87 | +}); |
| 88 | + |
| 89 | +test('handles BEL (Bell) character',()=>{ |
| 90 | +constinput='alert\u0007sound'; |
| 91 | +expect(escapeControlCharacters(input)).toBe('alert\\x07sound'); |
| 92 | +}); |
| 93 | + |
| 94 | +test('preserves newlines in multiline strings',()=>{ |
| 95 | +constinput='line 1\nline 2\nline 3'; |
| 96 | +expect(escapeControlCharacters(input)).toBe(input); |
| 97 | +}); |
| 98 | + |
| 99 | +test('preserves tabs for code formatting',()=>{ |
| 100 | +constinput='function() {\n\treturn true;\n}'; |
| 101 | +expect(escapeControlCharacters(input)).toBe(input); |
| 102 | +}); |
| 103 | + |
| 104 | +test('escapes multiple consecutive control characters',()=>{ |
| 105 | +constinput='data\u0001\u0002\u0003separator'; |
| 106 | +expect(escapeControlCharacters(input)).toBe('data\\x01\\x02\\x03separator'); |
| 107 | +}); |
| 108 | + |
| 109 | +test('handles control characters at string boundaries',()=>{ |
| 110 | +conststartControl='\u0001start'; |
| 111 | +constendControl='end\u0001'; |
| 112 | +expect(escapeControlCharacters(startControl)).toBe('\\x01start'); |
| 113 | +expect(escapeControlCharacters(endControl)).toBe('end\\x01'); |
| 114 | +}); |
| 115 | +}); |