forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLEncodingTests.m
More file actions
193 lines (151 loc) · 6.75 KB
/
Copy pathHTMLEncodingTests.m
File metadata and controls
193 lines (151 loc) · 6.75 KB
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
// HTMLEncodingTests.m
//
// Public domain. https://github.com/nolanw/HTMLReader
#import "HTMLTestUtilities.h"
#import "HTMLEncoding.h"
#import "HTMLParser.h"
@interface DataScanner : NSObject
- (instancetype)initWithData:(NSData *)data;
@property (readonly, copy, nonatomic) NSData *data;
@property (assign, nonatomic) NSUInteger scanLocation;
- (NSData *)scanUpToString:(const char *)string;
- (NSData *)scanString:(const char *)string;
@end
@implementation DataScanner
- (instancetype)initWithData:(NSData *)data
{
if ((self = [super init])) {
_data = [data copy];
}
return self;
}
- (NSData *)scanUpToString:(const char *)string
{
NSRange rangeOfString = [self rangeOfString:string anchored:NO];
NSRange rangeOfSubdata;
if (rangeOfString.location == NSNotFound) {
rangeOfSubdata = NSMakeRange(self.scanLocation, self.data.length - self.scanLocation);
self.scanLocation = self.data.length;
} else {
rangeOfSubdata = NSMakeRange(self.scanLocation, rangeOfString.location - self.scanLocation);
self.scanLocation = rangeOfString.location;
}
return [self.data subdataWithRange:rangeOfSubdata];
}
- (NSData *)scanString:(const char *)string
{
NSRange rangeOfString = [self rangeOfString:string anchored:YES];
if (rangeOfString.location == NSNotFound) {
return nil;
}
self.scanLocation = NSMaxRange(rangeOfString);
return [self.data subdataWithRange:rangeOfString];
}
- (NSRange)rangeOfString:(const char *)string anchored:(BOOL)anchored
{
NSData *dataToFind = [NSData dataWithBytes:string length:strlen(string)];
NSRange workingRange = NSMakeRange(self.scanLocation, self.data.length - self.scanLocation);
NSUInteger options = anchored ? NSDataSearchAnchored : 0;
return [self.data rangeOfData:dataToFind options:options range:workingRange];
}
@end
@interface HTMLEncodingTest : NSObject
@property (readonly, copy, nonatomic) NSData *testData;
@property (readonly, copy, nonatomic) NSString *correctEncodingLabel;
@property (readonly, nonatomic) NSString *testString;
@property (readonly, nonatomic) NSStringEncoding correctEncoding;
@end
@implementation HTMLEncodingTest
+ (instancetype)testFromScanner:(DataScanner *)scanner
{
[scanner scanUpToString:"#data"];
if (![scanner scanString:"#data\n"]) {
return nil;
}
NSData *testData = [scanner scanUpToString:"\n#encoding"];
NSAssert(testData, @"malformed test: #data with no subsequent #encoding");
[scanner scanString:"\n#encoding\n"];
NSData *rawEncodingLabel = [scanner scanUpToString:"\n"];
NSAssert(rawEncodingLabel, @"malformed test: couldn't read encoding label");
NSString *encodingLabel = [[NSString alloc] initWithData:rawEncodingLabel encoding:NSASCIIStringEncoding];
NSAssert(encodingLabel, @"could not decode encoding label");
HTMLEncodingTest *test = [self new];
test->_testData = [testData copy];
test->_correctEncodingLabel = [encodingLabel copy];
return test;
}
- (NSStringEncoding)correctEncoding
{
return StringEncodingForLabel(self.correctEncodingLabel);
}
- (NSString *)testString
{
return [[NSString alloc] initWithData:self.testData encoding:NSISOLatin1StringEncoding];
}
@end
@interface HTMLEncodingTests : XCTestCase
@end
@implementation HTMLEncodingTests
- (void)testEncodingDetection
{
for (NSURL *fileURL in TestFileURLs()) {
NSString *testName = [fileURL.lastPathComponent stringByDeletingPathExtension];
[TestsInFileAtURL(fileURL) enumerateObjectsUsingBlock:^(HTMLEncodingTest *test, NSUInteger i, BOOL *stop) {
// tests1.dat has four tests that require implementations HTMLReader doesn't have, so we'll skip those.
if ([fileURL.lastPathComponent isEqualToString:@"tests1.dat"]) {
// These three tests require scripting support, which HTMLReader does not.
if (i == 54 || i == 55 || i == 56) {
return;
}
// This test passes successfully if one prescans the byte stream, which HTMLReader does not.
if (i == 57) {
return;
}
}
HTMLParser *parser = ParserWithDataAndContentType(test.testData, nil);
CFStringEncoding cfcorrectEncoding = CFStringConvertNSStringEncodingToEncoding(test.correctEncoding);
CFStringEncoding cfparserEncoding = CFStringConvertNSStringEncodingToEncoding(parser.encoding.encoding);
NSString *description = [NSString stringWithFormat:@"%@ test%tu expected %@ (CFStringEncoding 0x%X) but got CFStringEncoding 0x%X; fixture:\n%@", testName, i, test.correctEncodingLabel, (unsigned int)cfcorrectEncoding, (unsigned int)cfparserEncoding, test.testString];
XCTAssertEqual(parser.encoding.encoding, test.correctEncoding, @"%@", description);
}];
}
}
static NSArray * TestFileURLs(void)
{
NSURL *directory = [[NSURL URLWithString:html5libTestPath()] URLByAppendingPathComponent:@"encoding"];
NSError *error;
NSArray *candidates = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:directory
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
error:&error];
if (candidates == nil) {
NSLog(@"Cannot find the HTML5 tests, do you have the submodule (%@) checked out?", html5libTestPath());
}
// Skipping test-yahoo-jp.dat because I can't be bothered to figure out how it's encoded.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension = 'dat' && lastPathComponent != 'test-yahoo-jp.dat'"];
return [candidates filteredArrayUsingPredicate:predicate];
}
static NSArray * TestsInFileAtURL(NSURL *URL)
{
NSData *allTests = [NSData dataWithContentsOfURL:URL];
NSCAssert(allTests, @"possible error loading test file at %@", URL);
NSMutableArray *tests = [NSMutableArray new];
DataScanner *scanner = [[DataScanner alloc] initWithData:allTests];
for (;;) {
HTMLEncodingTest *test = [HTMLEncodingTest testFromScanner:scanner];
if (!test) {
break;
}
[tests addObject:test];
}
return tests;
}
- (void)testIncorrectContentTypeHeader
{
const char neitherUTF8NorWin1252[] = "\x90";
NSData *data = [NSData dataWithBytes:neitherUTF8NorWin1252 length:sizeof(neitherUTF8NorWin1252)];
HTMLParser *parser = ParserWithDataAndContentType(data, @"charset=utf-8");
XCTAssertNotNil(parser);
XCTAssertTrue(parser.encoding.encoding == NSISOLatin1StringEncoding);
}
@end