Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

homoglyph.test.ts 5.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  1. import {
  2. addHomoglyphs,
  3. homoglyphMap,
  4. toHomoglyphs,
  5. } from '../../../src/redteam/strategies/homoglyph';
  6. import type { TestCase } from '../../../src/types';
  7. describe('homoglyph strategy', () => {
  8. const testCases: TestCase[] = [
  9. {
  10. vars: {
  11. prompt: 'Hello World! 123',
  12. expected: 'normal value',
  13. },
  14. assert: [
  15. {
  16. type: 'equals',
  17. value: 'expected value',
  18. metric: 'original-metric',
  19. },
  20. ],
  21. },
  22. ];
  23. afterEach(() => {
  24. jest.resetAllMocks();
  25. });
  26. describe('toHomoglyphs', () => {
  27. it('should convert lowercase letters', () => {
  28. expect(toHomoglyphs('abcdefghijklmnopqrstuvwxyz')).not.toBe('abcdefghijklmnopqrstuvwxyz');
  29. expect(toHomoglyphs('a')).toBe(homoglyphMap['a']);
  30. expect(toHomoglyphs('z')).toBe(homoglyphMap['z']);
  31. });
  32. it('should convert uppercase letters', () => {
  33. expect(toHomoglyphs('ABCDEFGHIJKLMNOPQRSTUVWXYZ')).not.toBe('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  34. expect(toHomoglyphs('A')).toBe(homoglyphMap['A']);
  35. expect(toHomoglyphs('Z')).toBe(homoglyphMap['Z']);
  36. });
  37. it('should convert numbers', () => {
  38. expect(toHomoglyphs('0123456789')).not.toBe('0123456789');
  39. expect(toHomoglyphs('0')).toBe(homoglyphMap['0']);
  40. expect(toHomoglyphs('9')).toBe(homoglyphMap['9']);
  41. });
  42. it('should handle empty strings', () => {
  43. expect(toHomoglyphs('')).toBe('');
  44. });
  45. it('should preserve unmapped characters', () => {
  46. expect(toHomoglyphs('!@#$%^&*()')).toBe('!@#$%^&*()');
  47. expect(toHomoglyphs(' ')).toBe(' ');
  48. });
  49. it('should handle mixed content', () => {
  50. const input = 'Hello123!@#';
  51. const output = toHomoglyphs(input);
  52. expect(output).not.toBe(input);
  53. // Note: Length check removed since homoglyphs may have different UTF-16 lengths
  54. expect(output).toBeTruthy();
  55. });
  56. it('should handle all mapped characters', () => {
  57. // Test each mapped character individually rather than all at once
  58. Object.keys(homoglyphMap).forEach((char) => {
  59. const result = toHomoglyphs(char);
  60. expect(result).toBe(homoglyphMap[char]);
  61. });
  62. });
  63. it('should handle non-ASCII characters not in the map', () => {
  64. const nonAsciiChars = '☺★♥♦♣♠€£¥©®™';
  65. expect(toHomoglyphs(nonAsciiChars)).toBe(nonAsciiChars);
  66. });
  67. });
  68. describe('addHomoglyphs', () => {
  69. it('should convert text to homoglyphs', () => {
  70. const injectVar = 'prompt';
  71. const result = addHomoglyphs(testCases, injectVar);
  72. expect(result).toEqual([
  73. {
  74. ...testCases[0],
  75. vars: {
  76. ...testCases[0].vars,
  77. prompt: expect.not.stringMatching(/^Hello World! 123$/),
  78. },
  79. metadata: {
  80. strategyId: 'homoglyph',
  81. originalText: 'Hello World! 123',
  82. },
  83. assert: [
  84. {
  85. type: 'equals',
  86. value: 'expected value',
  87. metric: 'original-metric/Homoglyph',
  88. },
  89. ],
  90. },
  91. ]);
  92. });
  93. it('should handle undefined vars', () => {
  94. const testCase: TestCase = { vars: {} };
  95. const result = addHomoglyphs([testCase], 'prompt');
  96. expect(result[0].vars!.prompt).toBe(toHomoglyphs('undefined'));
  97. });
  98. it('should handle missing inject var', () => {
  99. const testCase: TestCase = { vars: { other: 'value' } };
  100. const result = addHomoglyphs([testCase], 'prompt');
  101. expect(result[0].vars!.prompt).toBe(toHomoglyphs('undefined'));
  102. expect(result[0].vars!.other).toBe('value');
  103. });
  104. it('should handle very long strings', () => {
  105. const longString = 'a'.repeat(1000);
  106. const testCase: TestCase = { vars: { prompt: longString } };
  107. const result = addHomoglyphs([testCase], 'prompt');
  108. expect(result[0].vars!.prompt).not.toBe(longString);
  109. expect(result[0].vars!.prompt).toBe(homoglyphMap['a'].repeat(1000));
  110. });
  111. it('should handle special characters', () => {
  112. const specialChars = '!@#$%^&*()_+-=[]{}|;:,.<>?`~';
  113. const testCase: TestCase = { vars: { prompt: specialChars } };
  114. const result = addHomoglyphs([testCase], 'prompt');
  115. expect(result[0].vars!.prompt).toBe(specialChars);
  116. });
  117. it('should handle null input by converting to string', () => {
  118. const testCase: TestCase = { vars: { prompt: null as any } };
  119. const result = addHomoglyphs([testCase], 'prompt');
  120. expect(result[0].vars!.prompt).toBe(toHomoglyphs('null'));
  121. });
  122. it('should handle numeric input by converting to string', () => {
  123. const testCase: TestCase = { vars: { prompt: 12345 } };
  124. const result = addHomoglyphs([testCase], 'prompt');
  125. expect(result[0].vars!.prompt).toBe(toHomoglyphs('12345'));
  126. });
  127. it('should preserve assertion objects', () => {
  128. const testCase: TestCase = {
  129. vars: { prompt: 'test' },
  130. assert: [
  131. { type: 'equals', value: 'expected', metric: 'metric1' },
  132. { type: 'contains', value: 'partial', metric: 'metric2' },
  133. ],
  134. };
  135. const result = addHomoglyphs([testCase], 'prompt');
  136. expect(result[0].assert).toEqual([
  137. { type: 'equals', value: 'expected', metric: 'metric1/Homoglyph' },
  138. { type: 'contains', value: 'partial', metric: 'metric2/Homoglyph' },
  139. ]);
  140. });
  141. it('should handle test cases with no assertions', () => {
  142. const testCase: TestCase = { vars: { prompt: 'test' } };
  143. const result = addHomoglyphs([testCase], 'prompt');
  144. expect(result[0].assert).toBeUndefined();
  145. });
  146. });
  147. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...