Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(846)

Side by Side Diff: components/cronet/ios/test/cronet_http_test.mm

Issue 2857733002: iOS Cronet: Put cap on the buffer size passed to onReadCompleted delegate (Closed)
Patch Set: DCHECK Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ios/net/crn_http_protocol_handler.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cronet/Cronet.h> 5 #import <Cronet/Cronet.h>
6 #import <Foundation/Foundation.h> 6 #import <Foundation/Foundation.h>
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 15 matching lines...) Expand all
26 NSURLSessionTaskDelegate> 26 NSURLSessionTaskDelegate>
27 27
28 // Completion semaphore for this TestDelegate. When the request this delegate is 28 // Completion semaphore for this TestDelegate. When the request this delegate is
29 // attached to finishes (either successfully or with an error), this delegate 29 // attached to finishes (either successfully or with an error), this delegate
30 // signals this semaphore. 30 // signals this semaphore.
31 @property(assign, atomic) dispatch_semaphore_t semaphore; 31 @property(assign, atomic) dispatch_semaphore_t semaphore;
32 32
33 // Error the request this delegate is attached to failed with, if any. 33 // Error the request this delegate is attached to failed with, if any.
34 @property(retain, atomic) NSError* error; 34 @property(retain, atomic) NSError* error;
35 35
36 // Contains total amount of received data.
37 @property(readonly) long totalBytesReceived;
38
36 @end 39 @end
37 40
38 @implementation TestDelegate 41 @implementation TestDelegate
42
39 @synthesize semaphore = _semaphore; 43 @synthesize semaphore = _semaphore;
40 @synthesize error = _error; 44 @synthesize error = _error;
45 @synthesize totalBytesReceived = _totalBytesReceived;
41 46
42 NSMutableArray<NSData*>* _responseData; 47 NSMutableArray<NSData*>* _responseData;
43 48
44 - (id)init { 49 - (id)init {
45 if (self = [super init]) { 50 if (self = [super init]) {
46 _semaphore = dispatch_semaphore_create(0); 51 _semaphore = dispatch_semaphore_create(0);
47 } 52 }
48 return self; 53 return self;
49 } 54 }
50 55
51 - (void)dealloc { 56 - (void)dealloc {
52 dispatch_release(_semaphore); 57 dispatch_release(_semaphore);
53 [_error release]; 58 [_error release];
54 _error = nil; 59 _error = nil;
55 [super dealloc]; 60 [super dealloc];
56 } 61 }
57 62
58 - (void)reset { 63 - (void)reset {
59 [_responseData dealloc]; 64 [_responseData dealloc];
60 _responseData = nil; 65 _responseData = nil;
61 _error = nil; 66 _error = nil;
67 _totalBytesReceived = 0;
62 } 68 }
63 69
64 - (NSString*)responseBody { 70 - (NSString*)responseBody {
65 if (_responseData == nil) { 71 if (_responseData == nil) {
66 return nil; 72 return nil;
67 } 73 }
68 NSMutableString* body = [NSMutableString string]; 74 NSMutableString* body = [NSMutableString string];
69 for (NSData* data in _responseData) { 75 for (NSData* data in _responseData) {
70 [body appendString:[[NSString alloc] initWithData:data 76 [body appendString:[[NSString alloc] initWithData:data
71 encoding:NSUTF8StringEncoding]]; 77 encoding:NSUTF8StringEncoding]];
(...skipping 27 matching lines...) Expand all
99 dataTask:(NSURLSessionDataTask*)dataTask 105 dataTask:(NSURLSessionDataTask*)dataTask
100 didReceiveResponse:(NSURLResponse*)response 106 didReceiveResponse:(NSURLResponse*)response
101 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition)) 107 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))
102 completionHandler { 108 completionHandler {
103 completionHandler(NSURLSessionResponseAllow); 109 completionHandler(NSURLSessionResponseAllow);
104 } 110 }
105 111
106 - (void)URLSession:(NSURLSession*)session 112 - (void)URLSession:(NSURLSession*)session
107 dataTask:(NSURLSessionDataTask*)dataTask 113 dataTask:(NSURLSessionDataTask*)dataTask
108 didReceiveData:(NSData*)data { 114 didReceiveData:(NSData*)data {
115 _totalBytesReceived += [data length];
109 if (_responseData == nil) { 116 if (_responseData == nil) {
110 _responseData = [[NSMutableArray alloc] init]; 117 _responseData = [[NSMutableArray alloc] init];
111 } 118 }
112 [_responseData addObject:data]; 119 [_responseData addObject:data];
113 } 120 }
114 121
115 - (void)URLSession:(NSURLSession*)session 122 - (void)URLSession:(NSURLSession*)session
116 dataTask:(NSURLSessionDataTask*)dataTask 123 dataTask:(NSURLSessionDataTask*)dataTask
117 willCacheResponse:(NSCachedURLResponse*)proposedResponse 124 willCacheResponse:(NSCachedURLResponse*)proposedResponse
118 completionHandler: 125 completionHandler:
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 return YES; 225 return YES;
219 }]; 226 }];
220 NSDate* start = [NSDate date]; 227 NSDate* start = [NSDate date];
221 StartDataTaskAndWaitForCompletion(task); 228 StartDataTaskAndWaitForCompletion(task);
222 NSTimeInterval elapsed = -[start timeIntervalSinceNow]; 229 NSTimeInterval elapsed = -[start timeIntervalSinceNow];
223 elapsed_avg += elapsed; 230 elapsed_avg += elapsed;
224 if (elapsed > elapsed_max) 231 if (elapsed > elapsed_max)
225 elapsed_max = elapsed; 232 elapsed_max = elapsed;
226 EXPECT_TRUE(block_used); 233 EXPECT_TRUE(block_used);
227 EXPECT_EQ(nil, [delegate_ error]); 234 EXPECT_EQ(nil, [delegate_ error]);
235 EXPECT_EQ(size, [delegate_ totalBytesReceived]);
228 } 236 }
229 // Release the response buffer. 237 // Release the response buffer.
230 TestServer::ReleaseBigDataURL(); 238 TestServer::ReleaseBigDataURL();
231 LOG(INFO) << "Elapsed Average:" << elapsed_avg * 1000 / iterations 239 LOG(INFO) << "Elapsed Average:" << elapsed_avg * 1000 / iterations
232 << "ms Max:" << elapsed_max * 1000 << "ms"; 240 << "ms Max:" << elapsed_max * 1000 << "ms";
233 } 241 }
234 242
235 TEST_F(HttpTest, GetGlobalMetricsDeltas) { 243 TEST_F(HttpTest, GetGlobalMetricsDeltas) {
236 NSData* delta1 = [Cronet getGlobalMetricsDeltas]; 244 NSData* delta1 = [Cronet getGlobalMetricsDeltas];
237 245
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { 470 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
463 EXPECT_TRUE(false) << "Block should not be called for unsupported requests"; 471 EXPECT_TRUE(false) << "Block should not be called for unsupported requests";
464 return YES; 472 return YES;
465 }]; 473 }];
466 StartDataTaskAndWaitForCompletion(task); 474 StartDataTaskAndWaitForCompletion(task);
467 EXPECT_EQ(nil, [delegate_ error]); 475 EXPECT_EQ(nil, [delegate_ error]);
468 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]); 476 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]);
469 } 477 }
470 478
471 } // namespace cronet 479 } // namespace cronet
OLDNEW
« no previous file with comments | « no previous file | ios/net/crn_http_protocol_handler.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698