forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVT100XtermParserTest.m
333 lines (270 loc) · 12.9 KB
/
VT100XtermParserTest.m
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//
// VT100XtermParserTest.m
// iTerm2
//
// Created by George Nachman on 12/30/14.
//
//
#import <XCTest/XCTest.h>
#import "CVector.h"
#import "iTermParser.h"
#import "VT100XtermParser.h"
#import "VT100Token.h"
@interface VT100XtermParserTest : XCTestCase
@end
@implementation VT100XtermParserTest {
NSMutableDictionary *_savedState;
iTermParserContext _context;
CVector _incidentals;
}
- (void)setUp {
_savedState = [NSMutableDictionary dictionary];
CVectorCreate(&_incidentals, 1);
}
- (void)tearDown {
CVectorDestroy(&_incidentals);
}
- (VT100Token *)tokenForDataWithFormat:(NSString *)formatString, ... {
va_list args;
va_start(args, formatString);
NSString *string = [[[NSString alloc] initWithFormat:formatString arguments:args] autorelease];
va_end(args);
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
VT100Token *token = [[[VT100Token alloc] init] autorelease];
_context = iTermParserContextMake((unsigned char *)data.bytes, data.length);
[VT100XtermParser decodeFromContext:&_context
incidentals:&_incidentals
token:token
encoding:NSUTF8StringEncoding
savedState:_savedState];
return token;
}
- (void)testNoModeYet {
VT100Token *token = [self tokenForDataWithFormat:@"%c]", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
// In case saved state gets used, verify it can continue from there.
token = [self tokenForDataWithFormat:@"%c]0;title%c", VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"title"]);
}
- (void)testWellFormedSetWindowTitleTerminatedByBell {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;title%c", VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"title"]);
}
- (void)testWellFormedSetWindowTitleTerminatedByST {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;title%c\\", VT100CC_ESC, VT100CC_ESC];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"title"]);
}
- (void)testIgnoreEmbeddedOSC {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;ti%c]tle%c", VT100CC_ESC, VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"title"]);
}
- (void)testIgnoreEmbeddedOSCTwoPart_OutOfDataAfterBracket {
// Running out of data just after an embedded ESC ] hits a special path.
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;ti%c]", VT100CC_ESC, VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
token = [self tokenForDataWithFormat:@"%c]0;ti%c]tle%c", VT100CC_ESC, VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"title"]);
}
- (void)testIgnoreEmbeddedOSCTwoPart_OutOfDataAfterEsc {
// Running out of data just after an embedded ESC hits a special path.
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;ti%c", VT100CC_ESC, VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
token = [self tokenForDataWithFormat:@"%c]0;ti%c]tle%c", VT100CC_ESC, VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"title"]);
}
- (void)testFailOnEmbeddedEscapePlusCharacter {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;ti%cc", VT100CC_ESC, VT100CC_ESC];
XCTAssert(token->type == VT100_NOTSUPPORT);
}
- (void)testNonstandardLinuxSetPalette {
VT100Token *token = [self tokenForDataWithFormat:@"%c]Pa123456", VT100CC_ESC];
XCTAssert(token->type == XTERMCC_SET_PALETTE);
XCTAssert([token.string isEqualToString:@"a123456"]);
}
- (void)testUnsupportedFirstParameterNoTerminator {
VT100Token *token = [self tokenForDataWithFormat:@"%c]x", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
}
- (void)testUnsupportedFirstParameter {
VT100Token *token = [self tokenForDataWithFormat:@"%c]x%c", VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == VT100_NOTSUPPORT);
}
- (void)testPartialNonstandardLinuxSetPalette {
VT100Token *token = [self tokenForDataWithFormat:@"%c]Pa12345", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
}
- (void)testCancelAbortsOSC {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0%c", VT100CC_ESC, VT100CC_CAN];
XCTAssert(token->type == VT100_NOTSUPPORT);
}
- (void)testSubstituteAbortsOSC {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0%c", VT100CC_ESC, VT100CC_SUB];
XCTAssert(token->type == VT100_NOTSUPPORT);
}
- (void)testUnfinishedMultitoken {
VT100Token *token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:abc", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 2);
VT100Token *header = CVectorGetObject(&_incidentals, 0);
XCTAssert(header->type = XTERMCC_MULTITOKEN_HEADER_SET_KVP);
XCTAssert([header.kvpKey isEqualToString:@"File"]);
XCTAssert([header.kvpValue isEqualToString:@"blah;foo=bar"]);
VT100Token *body = CVectorGetObject(&_incidentals, 1);
XCTAssert(body->type = XTERMCC_MULTITOKEN_BODY);
XCTAssert([body.string isEqualToString:@"abc"]);
}
- (void)testCompleteMultitoken {
VT100Token *token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:abc%c",
VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_MULTITOKEN_END);
XCTAssert(CVectorCount(&_incidentals) == 2);
VT100Token *header = CVectorGetObject(&_incidentals, 0);
XCTAssert(header->type = XTERMCC_MULTITOKEN_HEADER_SET_KVP);
XCTAssert([header.kvpKey isEqualToString:@"File"]);
XCTAssert([header.kvpValue isEqualToString:@"blah;foo=bar"]);
VT100Token *body = CVectorGetObject(&_incidentals, 1);
XCTAssert(body->type = XTERMCC_MULTITOKEN_BODY);
XCTAssert([body.string isEqualToString:@"abc"]);
}
- (void)testCompleteMultitokenInMultiplePasses {
VT100Token *token = [self tokenForDataWithFormat:@"%c]1337;File=blah;", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 0);
// Give it some more header
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 0);
// Give it the final colon so the header can be parsed
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 1);
VT100Token *header = CVectorGetObject(&_incidentals, 0);
XCTAssert(header->type = XTERMCC_MULTITOKEN_HEADER_SET_KVP);
XCTAssert([header.kvpKey isEqualToString:@"File"]);
XCTAssert([header.kvpValue isEqualToString:@"blah;foo=bar"]);
// Give it some body.
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:a", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 2);
VT100Token *body = CVectorGetObject(&_incidentals, 1);
XCTAssert(body->type = XTERMCC_MULTITOKEN_BODY);
XCTAssert([body.string isEqualToString:@"a"]);
// More body
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:abc", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 3);
body = CVectorGetObject(&_incidentals, 2);
XCTAssert(body->type = XTERMCC_MULTITOKEN_BODY);
XCTAssert([body.string isEqualToString:@"bc"]);
// Start finishing up
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:abc%c", VT100CC_ESC, VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 3);
// And, done.
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:abc%c\\", VT100CC_ESC, VT100CC_ESC];
XCTAssert(token->type == XTERMCC_MULTITOKEN_END);
XCTAssert(CVectorCount(&_incidentals) == 3);
}
- (void)testLateFailureMultitokenInMultiplePasses {
VT100Token *token = [self tokenForDataWithFormat:@"%c]1337;File=blah;", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 0);
// Give it some more header
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 0);
// Give it the final colon so the header can be parsed
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 1);
VT100Token *header = CVectorGetObject(&_incidentals, 0);
XCTAssert(header->type = XTERMCC_MULTITOKEN_HEADER_SET_KVP);
XCTAssert([header.kvpKey isEqualToString:@"File"]);
XCTAssert([header.kvpValue isEqualToString:@"blah;foo=bar"]);
// Give it some body.
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:a", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 2);
VT100Token *body = CVectorGetObject(&_incidentals, 1);
XCTAssert(body->type = XTERMCC_MULTITOKEN_BODY);
XCTAssert([body.string isEqualToString:@"a"]);
// More body
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:abc", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 3);
body = CVectorGetObject(&_incidentals, 2);
XCTAssert(body->type = XTERMCC_MULTITOKEN_BODY);
XCTAssert([body.string isEqualToString:@"bc"]);
// Now a bogus character.
token = [self tokenForDataWithFormat:@"%c]1337;File=blah;foo=bar:abc%c", VT100CC_ESC, VT100CC_SUB];
XCTAssert(token->type == VT100_NOTSUPPORT);
}
- (void)testUnfinishedMultitokenWithDeprecatedMode {
VT100Token *token = [self tokenForDataWithFormat:@"%c]50;File=blah;foo=bar:abc", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(CVectorCount(&_incidentals) == 2);
VT100Token *header = CVectorGetObject(&_incidentals, 0);
XCTAssert(header->type = XTERMCC_MULTITOKEN_HEADER_SET_KVP);
XCTAssert([header.kvpKey isEqualToString:@"File"]);
XCTAssert([header.kvpValue isEqualToString:@"blah;foo=bar"]);
VT100Token *body = CVectorGetObject(&_incidentals, 1);
XCTAssert(body->type = XTERMCC_MULTITOKEN_BODY);
XCTAssert([body.string isEqualToString:@"abc"]);
}
- (void)testUnterminatedOSCWaits {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;foo", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
}
- (void)testUnterminateOSCWaits_2 {
VT100Token *token = [self tokenForDataWithFormat:@"%c]0", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
}
- (void)testMultiPartOSC {
// Pass in a partial escape code. The already-parsed data should be saved in the saved-state
// dictionary.
VT100Token *token = [self tokenForDataWithFormat:@"%c]0;foo", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(_savedState.allKeys.count > 0);
// Give it a more-formed code. The first three characters have changed. Normally they would be
// the same, but it's done here to ensure that they are ignored.
token = [self tokenForDataWithFormat:@"%c]0;XXXbar", VT100CC_ESC];
XCTAssert(token->type == VT100_WAIT);
XCTAssert(_savedState.allKeys.count > 0);
// Now a fully-formed code. The entire string value must come from saved state.
token = [self tokenForDataWithFormat:@"%c]0;XXXXXX%c", VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"foobar"]);
}
- (void)testEmbeddedColon {
VT100Token *token = [self tokenForDataWithFormat:@"%c]1;foo:bar%c", VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_ICON_TITLE);
XCTAssert([token.string isEqualToString:@"foo:bar"]);
}
- (void)testUnsupportedMode {
VT100Token *token = [self tokenForDataWithFormat:@"%c]999;foo%c", VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == VT100_NOTSUPPORT);
}
- (void)testBelAfterEmbeddedOSC {
VT100Token *token = [self tokenForDataWithFormat:@"%c]1;%c]%c", VT100CC_ESC, VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_ICON_TITLE);
XCTAssert([token.string isEqualToString:@""]);
}
- (void)testIgnoreEmbeddedOSCWhenFailing {
VT100Token *token = [self tokenForDataWithFormat:@"%c]x%c]%c", VT100CC_ESC, VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == VT100_NOTSUPPORT);
XCTAssert(iTermParserNumberOfBytesConsumed(&_context) == 6);
}
#pragma mark - Regression tests
// Bug 3371
- (void)testDefaultModeForDtermCodes {
VT100Token *token = [self tokenForDataWithFormat:@"%c];Foo%c", VT100CC_ESC, VT100CC_BEL];
XCTAssert(token->type == XTERMCC_WINICON_TITLE);
XCTAssert([token.string isEqualToString:@"Foo"]);
}
@end