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

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

Issue 2928653002: [Cronet-iOS] Public-Key-Pinning Tests (Closed)
Patch Set: Fixed DEPS Created 3 years, 6 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 | « components/cronet/ios/test/DEPS ('k') | components/cronet/ios/test/cronet_pkp_test.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"
11 #include "base/mac/scoped_nsobject.h" 11 #include "base/mac/scoped_nsobject.h"
12 #include "base/strings/sys_string_conversions.h" 12 #include "base/strings/sys_string_conversions.h"
13 #include "components/cronet/ios/test/cronet_test_base.h"
13 #include "components/cronet/ios/test/start_cronet.h" 14 #include "components/cronet/ios/test/start_cronet.h"
14 #include "components/cronet/ios/test/test_server.h" 15 #include "components/cronet/ios/test/test_server.h"
15 #include "components/grpc_support/test/quic_test_server.h" 16 #include "components/grpc_support/test/quic_test_server.h"
16 #include "net/base/mac/url_conversions.h" 17 #include "net/base/mac/url_conversions.h"
17 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
18 #include "net/cert/mock_cert_verifier.h" 19 #include "net/cert/mock_cert_verifier.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/gtest_mac.h" 21 #include "testing/gtest_mac.h"
21 22
22 #include "url/gurl.h" 23 #include "url/gurl.h"
23 24
24 @interface Cronet (ExposedForTesting)
25 + (void)shutdownForTesting;
26 @end
27
28 @interface TestDelegate : NSObject<NSURLSessionDataDelegate,
29 NSURLSessionDelegate,
30 NSURLSessionTaskDelegate>
31
32 // Completion semaphore for this TestDelegate. When the request this delegate is
33 // attached to finishes (either successfully or with an error), this delegate
34 // signals this semaphore.
35 @property(assign, atomic) dispatch_semaphore_t semaphore;
36
37 // Error the request this delegate is attached to failed with, if any.
38 @property(retain, atomic) NSError* error;
39
40 // Contains total amount of received data.
41 @property(readonly) long totalBytesReceived;
42
43 @end
44
45 @implementation TestDelegate
46
47 @synthesize semaphore = _semaphore;
48 @synthesize error = _error;
49 @synthesize totalBytesReceived = _totalBytesReceived;
50
51 NSMutableArray<NSData*>* _responseData;
52
53 - (id)init {
54 if (self = [super init]) {
55 _semaphore = dispatch_semaphore_create(0);
56 }
57 return self;
58 }
59
60 - (void)dealloc {
61 dispatch_release(_semaphore);
62 [_error release];
63 _error = nil;
64 [super dealloc];
65 }
66
67 - (void)reset {
68 [_responseData dealloc];
69 _responseData = nil;
70 _error = nil;
71 _totalBytesReceived = 0;
72 }
73
74 - (NSString*)responseBody {
75 if (_responseData == nil) {
76 return nil;
77 }
78 NSMutableString* body = [NSMutableString string];
79 for (NSData* data in _responseData) {
80 [body appendString:[[NSString alloc] initWithData:data
81 encoding:NSUTF8StringEncoding]];
82 }
83 VLOG(3) << "responseBody size:" << [body length]
84 << " chunks:" << [_responseData count];
85 return body;
86 }
87
88 - (void)URLSession:(NSURLSession*)session
89 didBecomeInvalidWithError:(NSError*)error {
90 }
91
92 - (void)URLSession:(NSURLSession*)session
93 task:(NSURLSessionTask*)task
94 didCompleteWithError:(NSError*)error {
95 [self setError:error];
96 dispatch_semaphore_signal(_semaphore);
97 }
98
99 - (void)URLSession:(NSURLSession*)session
100 task:(NSURLSessionTask*)task
101 didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge
102 completionHandler:
103 (void (^)(NSURLSessionAuthChallengeDisposition disp,
104 NSURLCredential* credential))completionHandler {
105 completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
106 }
107
108 - (void)URLSession:(NSURLSession*)session
109 dataTask:(NSURLSessionDataTask*)dataTask
110 didReceiveResponse:(NSURLResponse*)response
111 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))
112 completionHandler {
113 completionHandler(NSURLSessionResponseAllow);
114 }
115
116 - (void)URLSession:(NSURLSession*)session
117 dataTask:(NSURLSessionDataTask*)dataTask
118 didReceiveData:(NSData*)data {
119 _totalBytesReceived += [data length];
120 if (_responseData == nil) {
121 _responseData = [[NSMutableArray alloc] init];
122 }
123 [_responseData addObject:data];
124 }
125
126 - (void)URLSession:(NSURLSession*)session
127 dataTask:(NSURLSessionDataTask*)dataTask
128 willCacheResponse:(NSCachedURLResponse*)proposedResponse
129 completionHandler:
130 (void (^)(NSCachedURLResponse* cachedResponse))completionHandler {
131 completionHandler(proposedResponse);
132 }
133
134 @end
135
136 namespace cronet { 25 namespace cronet {
137 // base::TimeDelta would normally be ideal for this but it does not support
138 // nanosecond resolution.
139 static const int64_t ns_in_second = 1000000000LL;
140 const char kUserAgent[] = "CronetTest/1.0.0.0"; 26 const char kUserAgent[] = "CronetTest/1.0.0.0";
141 27
142 class HttpTest : public ::testing::Test { 28 class HttpTest : public CronetTestBase {
143 protected: 29 protected:
144 HttpTest() {} 30 HttpTest() {}
145 ~HttpTest() override {} 31 ~HttpTest() override {}
146 32
147 void SetUp() override { 33 void SetUp() override {
148 grpc_support::StartQuicTestServer(); 34 CronetTestBase::SetUp();
149 TestServer::Start(); 35 TestServer::Start();
150 36
151 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { 37 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
152 return YES; 38 return YES;
153 }]; 39 }];
154 StartCronet(grpc_support::GetQuicTestServerPort()); 40 StartCronet(grpc_support::GetQuicTestServerPort());
155 [Cronet registerHttpProtocolHandler]; 41 [Cronet registerHttpProtocolHandler];
156 NSURLSessionConfiguration* config = 42 NSURLSessionConfiguration* config =
157 [NSURLSessionConfiguration ephemeralSessionConfiguration]; 43 [NSURLSessionConfiguration ephemeralSessionConfiguration];
158 [Cronet installIntoSessionConfiguration:config]; 44 [Cronet installIntoSessionConfiguration:config];
159 delegate_.reset([[TestDelegate alloc] init]); 45 session_ = [NSURLSession sessionWithConfiguration:config
160 NSURLSession* session = [NSURLSession sessionWithConfiguration:config 46 delegate:delegate_
161 delegate:delegate_ 47 delegateQueue:nil];
162 delegateQueue:nil];
163 // Take a reference to the session and store it so it doesn't get
164 // deallocated until this object does.
165 session_.reset([session retain]);
166 } 48 }
167 49
168 void TearDown() override { 50 void TearDown() override {
169 grpc_support::ShutdownQuicTestServer();
170 TestServer::Shutdown(); 51 TestServer::Shutdown();
171 52
172 [Cronet stopNetLog]; 53 [Cronet stopNetLog];
173 [Cronet shutdownForTesting]; 54 [Cronet shutdownForTesting];
55 CronetTestBase::TearDown();
174 } 56 }
175 57
176 // Launches the supplied |task| and blocks until it completes, with a timeout 58 NSURLSession* session_;
177 // of 1 second.
178 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) {
179 [delegate_ reset];
180 [task resume];
181 int64_t deadline_ns = 20 * ns_in_second;
182 ASSERT_EQ(0, dispatch_semaphore_wait(
183 [delegate_ semaphore],
184 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)));
185 }
186
187 base::scoped_nsobject<NSURLSession> session_;
188 base::scoped_nsobject<TestDelegate> delegate_;
189 }; 59 };
190 60
191 TEST_F(HttpTest, CreateSslKeyLogFile) { 61 TEST_F(HttpTest, CreateSslKeyLogFile) {
192 // Shutdown Cronet so that it can be restarted with specific configuration 62 // Shutdown Cronet so that it can be restarted with specific configuration
193 // (SSL key log file specified in experimental options) for this one test. 63 // (SSL key log file specified in experimental options) for this one test.
194 // This is necessary because SslKeyLogFile can only be set once, before any 64 // This is necessary because SslKeyLogFile can only be set once, before any
195 // SSL Client Sockets are created. 65 // SSL Client Sockets are created.
196 66
197 [Cronet shutdownForTesting]; 67 [Cronet shutdownForTesting];
198 68
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { 366 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
497 EXPECT_TRUE(false) << "Block should not be called for unsupported requests"; 367 EXPECT_TRUE(false) << "Block should not be called for unsupported requests";
498 return YES; 368 return YES;
499 }]; 369 }];
500 StartDataTaskAndWaitForCompletion(task); 370 StartDataTaskAndWaitForCompletion(task);
501 EXPECT_EQ(nil, [delegate_ error]); 371 EXPECT_EQ(nil, [delegate_ error]);
502 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]); 372 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]);
503 } 373 }
504 374
505 } // namespace cronet 375 } // namespace cronet
OLDNEW
« no previous file with comments | « components/cronet/ios/test/DEPS ('k') | components/cronet/ios/test/cronet_pkp_test.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698