ASIDataCompressorTests.m
7.08 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
//
// ASIDataCompressorTests.m
// Mac
//
// Created by Ben Copsey on 17/08/2010.
// Copyright 2010 All-Seeing Interactive. All rights reserved.
//
// Sadly these tests only work on Mac because of the dependency on NSTask, but I'm fairly sure this class should behave in the same way on iOS
#import "ASIDataCompressorTests.h"
#import "ASIDataCompressor.h"
#import "ASIDataDecompressor.h"
#import "ASIHTTPRequest.h"
@implementation ASIDataCompressorTests
- (void)setUp
{
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
// Download a 1.7MB text file
NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"];
if (![fileManager fileExistsAtPath:filePath] || [[[fileManager attributesOfItemAtPath:filePath error:NULL] objectForKey:NSFileSize] unsignedLongLongValue] < 1693961) {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_hound_of_the_baskervilles.text"]];
[request setDownloadDestinationPath:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"]];
[request startSynchronous];
}
}
- (void)testInflateData
{
NSString *originalString = [NSString stringWithContentsOfFile:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"] encoding:NSUTF8StringEncoding error:NULL];
// Test in-memory inflate using uncompressData:error:
NSError *error = nil;
NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt"];
NSString *gzippedFilePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt.gz"];
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error];
if (error) {
GHFail(@"Failed to remove old file, cannot proceed with test");
}
[originalString writeToFile:filePath atomically:NO encoding:NSUTF8StringEncoding error:&error];
if (error) {
GHFail(@"Failed to write string, cannot proceed with test");
}
NSTask *task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/gzip"];
[task setArguments:[NSArray arrayWithObject:filePath]];
[task launch];
[task waitUntilExit];
NSData *deflatedData = [NSData dataWithContentsOfFile:gzippedFilePath];
NSData *inflatedData = [ASIDataDecompressor uncompressData:deflatedData error:&error];
if (error) {
GHFail(@"Inflate failed because %@",error);
}
NSString *inflatedString = [[[NSString alloc] initWithBytes:[inflatedData bytes] length:[inflatedData length] encoding:NSUTF8StringEncoding] autorelease];
BOOL success = [inflatedString isEqualToString:originalString];
GHAssertTrue(success,@"inflated data is not the same as original");
// Test file to file inflate
NSString *inflatedFilePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"inflated_file.txt"];
[ASIHTTPRequest removeFileAtPath:inflatedFilePath error:&error];
if (error) {
GHFail(@"Failed to remove old file, cannot proceed with test");
}
if (![ASIDataDecompressor uncompressDataFromFile:gzippedFilePath toFile:inflatedFilePath error:&error]) {
GHFail(@"Inflate failed because %@",error);
}
originalString = [NSString stringWithContentsOfFile:inflatedFilePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
GHFail(@"Failed to read the inflated data, cannot proceed with test");
}
success = [inflatedString isEqualToString:originalString];
GHAssertTrue(success,@"inflated data is not the same as original");
}
- (void)testDeflateData
{
NSString *originalString = [NSString stringWithContentsOfFile:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"] encoding:NSUTF8StringEncoding error:NULL];
// Test in-memory deflate using compressData:error:
NSError *error = nil;
NSData *deflatedData = [ASIDataCompressor compressData:[originalString dataUsingEncoding:NSUTF8StringEncoding] error:&error];
if (error) {
GHFail(@"Failed to deflate the data");
}
NSString *gzippedFilePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt.gz"];
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error];
if (error) {
GHFail(@"Failed to remove old file, cannot proceed with test");
}
[deflatedData writeToFile:gzippedFilePath options:0 error:&error];
if (error) {
GHFail(@"Failed to write data, cannot proceed with test");
}
NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt"];
[ASIHTTPRequest removeFileAtPath:filePath error:&error];
if (error) {
GHFail(@"Failed to remove old file, cannot proceed with test");
}
NSTask *task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/gzip"];
[task setArguments:[NSArray arrayWithObjects:@"-d",gzippedFilePath,nil]];
[task launch];
[task waitUntilExit];
NSString *inflatedString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
GHFail(@"Failed to read the inflated data, cannot proceed with test");
}
BOOL success = [inflatedString isEqualToString:originalString];
GHAssertTrue(success,@"inflated data is not the same as original");
// Test file to file deflate
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error];
if (![ASIDataCompressor compressDataFromFile:filePath toFile:gzippedFilePath error:&error]) {
GHFail(@"Deflate failed because %@",error);
}
[ASIHTTPRequest removeFileAtPath:filePath error:&error];
task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/gzip"];
[task setArguments:[NSArray arrayWithObjects:@"-d",gzippedFilePath,nil]];
[task launch];
[task waitUntilExit];
inflatedString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
success = ([inflatedString isEqualToString:originalString]);
GHAssertTrue(success,@"deflate data is not the same as that generated by gzip");
// Test for bug https://github.com/pokeb/asi-http-request/issues/147
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error];
[ASIHTTPRequest removeFileAtPath:filePath error:&error];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://spaceharvest.com/i/screen6.png"]];
[request setDownloadDestinationPath:filePath];
[request startSynchronous];
if (![ASIDataCompressor compressDataFromFile:filePath toFile:gzippedFilePath error:&error]) {
GHFail(@"Deflate failed because %@",error);
}
unsigned long long originalFileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] fileSize];
[ASIHTTPRequest removeFileAtPath:filePath error:&error];
task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/gzip"];
[task setArguments:[NSArray arrayWithObjects:@"-d",gzippedFilePath,nil]];
[task launch];
[task waitUntilExit];
unsigned long long inflatedFileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] fileSize];
success = (originalFileSize == inflatedFileSize);
GHAssertTrue(success,@"inflated data is not the same size as the original");
}
@end