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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/socket/transport_client_socket_pool.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/platform_thread.h"
#include "net/base/capturing_net_log.h"
#include "net/base/ip_endpoint.h"
#include "net/base/load_timing_info.h"
#include "net/base/load_timing_info_test_util.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/test_completion_callback.h"
#include "net/dns/mock_host_resolver.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/client_socket_pool_histograms.h"
#include "net/socket/socket_test_util.h"
#include "net/socket/stream_socket.h"
#include "net/socket/transport_client_socket_pool_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
using internal::ClientSocketPoolBaseHelper;
namespace {
const int kMaxSockets = 32;
const int kMaxSocketsPerGroup = 6;
const RequestPriority kDefaultPriority = LOW;
class TransportClientSocketPoolTest : public testing::Test {
protected:
TransportClientSocketPoolTest()
: connect_backup_jobs_enabled_(
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)),
params_(
new TransportSocketParams(
HostPortPair("www.google.com", 80),
false,
false,
OnHostResolutionCallback(),
TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)),
histograms_(new ClientSocketPoolHistograms("TCPUnitTest")),
host_resolver_(new MockHostResolver),
client_socket_factory_(&net_log_),
pool_(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL) {
}
virtual ~TransportClientSocketPoolTest() {
internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
connect_backup_jobs_enabled_);
}
scoped_refptr<TransportSocketParams> CreateParamsForTCPFastOpen() {
return new TransportSocketParams(HostPortPair("www.google.com", 80),
false, false, OnHostResolutionCallback(),
TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED);
}
int StartRequest(const std::string& group_name, RequestPriority priority) {
scoped_refptr<TransportSocketParams> params(new TransportSocketParams(
HostPortPair("www.google.com", 80), false, false,
OnHostResolutionCallback(),
TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
return test_base_.StartRequestUsingPool(
&pool_, group_name, priority, params);
}
int GetOrderOfRequest(size_t index) {
return test_base_.GetOrderOfRequest(index);
}
bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) {
return test_base_.ReleaseOneConnection(keep_alive);
}
void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) {
test_base_.ReleaseAllConnections(keep_alive);
}
ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); }
size_t completion_count() const { return test_base_.completion_count(); }
bool connect_backup_jobs_enabled_;
CapturingNetLog net_log_;
scoped_refptr<TransportSocketParams> params_;
scoped_ptr<ClientSocketPoolHistograms> histograms_;
scoped_ptr<MockHostResolver> host_resolver_;
MockTransportClientSocketFactory client_socket_factory_;
TransportClientSocketPool pool_;
ClientSocketPoolTest test_base_;
private:
DISALLOW_COPY_AND_ASSIGN(TransportClientSocketPoolTest);
};
TEST(TransportConnectJobTest, MakeAddrListStartWithIPv4) {
IPAddressNumber ip_number;
ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.1", &ip_number));
IPEndPoint addrlist_v4_1(ip_number, 80);
ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.2", &ip_number));
IPEndPoint addrlist_v4_2(ip_number, 80);
ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::64", &ip_number));
IPEndPoint addrlist_v6_1(ip_number, 80);
ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::66", &ip_number));
IPEndPoint addrlist_v6_2(ip_number, 80);
AddressList addrlist;
// Test 1: IPv4 only. Expect no change.
addrlist.clear();
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v4_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(2u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
// Test 2: IPv6 only. Expect no change.
addrlist.clear();
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v6_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(2u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[1].GetFamily());
// Test 3: IPv4 then IPv6. Expect no change.
addrlist.clear();
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v4_2);
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v6_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(4u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
// Test 4: IPv6, IPv4, IPv6, IPv4. Expect first IPv6 moved to the end.
addrlist.clear();
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v6_2);
addrlist.push_back(addrlist_v4_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(4u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[1].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[2].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
// Test 5: IPv6, IPv6, IPv4, IPv4. Expect first two IPv6's moved to the end.
addrlist.clear();
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v6_2);
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v4_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(4u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
}
TEST_F(TransportClientSocketPoolTest, Basic) {
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
TestLoadTimingInfoConnectedNotReused(handle);
}
// Make sure that TransportConnectJob passes on its priority to its
// HostResolver request on Init.
TEST_F(TransportClientSocketPoolTest, SetResolvePriorityOnInit) {
for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
RequestPriority priority = static_cast<RequestPriority>(i);
TestCompletionCallback callback;
ClientSocketHandle handle;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, priority, callback.callback(), &pool_,
BoundNetLog()));
EXPECT_EQ(priority, host_resolver_->last_request_priority());
}
}
TEST_F(TransportClientSocketPoolTest, InitHostResolutionFailure) {
host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name");
TestCompletionCallback callback;
ClientSocketHandle handle;
HostPortPair host_port_pair("unresolvable.host.name", 80);
scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
host_port_pair, false, false, OnHostResolutionCallback(),
TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", dest, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult());
}
TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) {
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET);
TestCompletionCallback callback;
ClientSocketHandle handle;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
// Make the host resolutions complete synchronously this time.
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(ERR_CONNECTION_FAILED,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
}
TEST_F(TransportClientSocketPoolTest, PendingRequests) {
// First request finishes asynchronously.
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
// Make all subsequent host resolutions complete synchronously.
host_resolver_->set_synchronous_mode(true);
// Rest of them finish synchronously, until we reach the per-group limit.
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
// The rest are pending since we've used all active sockets.
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count());
// One initial asynchronous request and then 10 pending requests.
EXPECT_EQ(11U, completion_count());
// First part of requests, all with the same priority, finishes in FIFO order.
EXPECT_EQ(1, GetOrderOfRequest(1));
EXPECT_EQ(2, GetOrderOfRequest(2));
EXPECT_EQ(3, GetOrderOfRequest(3));
EXPECT_EQ(4, GetOrderOfRequest(4));
EXPECT_EQ(5, GetOrderOfRequest(5));
EXPECT_EQ(6, GetOrderOfRequest(6));
// Make sure that rest of the requests complete in the order of priority.
EXPECT_EQ(7, GetOrderOfRequest(7));
EXPECT_EQ(14, GetOrderOfRequest(8));
EXPECT_EQ(15, GetOrderOfRequest(9));
EXPECT_EQ(10, GetOrderOfRequest(10));
EXPECT_EQ(13, GetOrderOfRequest(11));
EXPECT_EQ(8, GetOrderOfRequest(12));
EXPECT_EQ(16, GetOrderOfRequest(13));
EXPECT_EQ(11, GetOrderOfRequest(14));
EXPECT_EQ(12, GetOrderOfRequest(15));
EXPECT_EQ(9, GetOrderOfRequest(16));
// Make sure we test order of all requests made.
EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17));
}
TEST_F(TransportClientSocketPoolTest, PendingRequests_NoKeepAlive) {
// First request finishes asynchronously.
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
// Make all subsequent host resolutions complete synchronously.
host_resolver_->set_synchronous_mode(true);
// Rest of them finish synchronously, until we reach the per-group limit.
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
// The rest are pending since we've used all active sockets.
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
// The pending requests should finish successfully.
EXPECT_EQ(OK, (*requests())[6]->WaitForResult());
EXPECT_EQ(OK, (*requests())[7]->WaitForResult());
EXPECT_EQ(OK, (*requests())[8]->WaitForResult());
EXPECT_EQ(OK, (*requests())[9]->WaitForResult());
EXPECT_EQ(OK, (*requests())[10]->WaitForResult());
EXPECT_EQ(static_cast<int>(requests()->size()),
client_socket_factory_.allocation_count());
// First asynchronous request, and then last 5 pending requests.
EXPECT_EQ(6U, completion_count());
}
// This test will start up a RequestSocket() and then immediately Cancel() it.
// The pending host resolution will eventually complete, and destroy the
// ClientSocketPool which will crash if the group was not cleared properly.
TEST_F(TransportClientSocketPoolTest, CancelRequestClearGroup) {
TestCompletionCallback callback;
ClientSocketHandle handle;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
handle.Reset();
}
TEST_F(TransportClientSocketPoolTest, TwoRequestsCancelOne) {
ClientSocketHandle handle;
TestCompletionCallback callback;
ClientSocketHandle handle2;
TestCompletionCallback callback2;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
EXPECT_EQ(ERR_IO_PENDING,
handle2.Init("a", params_, kDefaultPriority, callback2.callback(),
&pool_, BoundNetLog()));
handle.Reset();
EXPECT_EQ(OK, callback2.WaitForResult());
handle2.Reset();
}
TEST_F(TransportClientSocketPoolTest, ConnectCancelConnect) {
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
ClientSocketHandle handle;
TestCompletionCallback callback;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
handle.Reset();
TestCompletionCallback callback2;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback2.callback(),
&pool_, BoundNetLog()));
host_resolver_->set_synchronous_mode(true);
// At this point, handle has two ConnectingSockets out for it. Due to the
// setting the mock resolver into synchronous mode, the host resolution for
// both will return in the same loop of the MessageLoop. The client socket
// is a pending socket, so the Connect() will asynchronously complete on the
// next loop of the MessageLoop. That means that the first
// ConnectingSocket will enter OnIOComplete, and then the second one will.
// If the first one is not cancelled, it will advance the load state, and
// then the second one will crash.
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_FALSE(callback.have_result());
handle.Reset();
}
TEST_F(TransportClientSocketPoolTest, CancelRequest) {
// First request finishes asynchronously.
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
// Make all subsequent host resolutions complete synchronously.
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
// Reached per-group limit, queue up requests.
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
// Cancel a request.
size_t index_to_cancel = kMaxSocketsPerGroup + 2;
EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized());
(*requests())[index_to_cancel]->handle()->Reset();
ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
EXPECT_EQ(kMaxSocketsPerGroup,
client_socket_factory_.allocation_count());
EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
EXPECT_EQ(1, GetOrderOfRequest(1));
EXPECT_EQ(2, GetOrderOfRequest(2));
EXPECT_EQ(3, GetOrderOfRequest(3));
EXPECT_EQ(4, GetOrderOfRequest(4));
EXPECT_EQ(5, GetOrderOfRequest(5));
EXPECT_EQ(6, GetOrderOfRequest(6));
EXPECT_EQ(14, GetOrderOfRequest(7));
EXPECT_EQ(7, GetOrderOfRequest(8));
EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound,
GetOrderOfRequest(9)); // Canceled request.
EXPECT_EQ(9, GetOrderOfRequest(10));
EXPECT_EQ(10, GetOrderOfRequest(11));
EXPECT_EQ(11, GetOrderOfRequest(12));
EXPECT_EQ(8, GetOrderOfRequest(13));
EXPECT_EQ(12, GetOrderOfRequest(14));
EXPECT_EQ(13, GetOrderOfRequest(15));
EXPECT_EQ(15, GetOrderOfRequest(16));
// Make sure we test order of all requests made.
EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17));
}
class RequestSocketCallback : public TestCompletionCallbackBase {
public:
RequestSocketCallback(ClientSocketHandle* handle,
TransportClientSocketPool* pool)
: handle_(handle),
pool_(pool),
within_callback_(false),
callback_(base::Bind(&RequestSocketCallback::OnComplete,
base::Unretained(this))) {
}
virtual ~RequestSocketCallback() {}
const CompletionCallback& callback() const { return callback_; }
private:
void OnComplete(int result) {
SetResult(result);
ASSERT_EQ(OK, result);
if (!within_callback_) {
// Don't allow reuse of the socket. Disconnect it and then release it and
// run through the MessageLoop once to get it completely released.
handle_->socket()->Disconnect();
handle_->Reset();
{
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
base::MessageLoop::current()->RunUntilIdle();
}
within_callback_ = true;
scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
HostPortPair("www.google.com", 80), false, false,
OnHostResolutionCallback(),
TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
int rv = handle_->Init("a", dest, LOWEST, callback(), pool_,
BoundNetLog());
EXPECT_EQ(OK, rv);
}
}
ClientSocketHandle* const handle_;
TransportClientSocketPool* const pool_;
bool within_callback_;
CompletionCallback callback_;
DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback);
};
TEST_F(TransportClientSocketPoolTest, RequestTwice) {
ClientSocketHandle handle;
RequestSocketCallback callback(&handle, &pool_);
scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
HostPortPair("www.google.com", 80), false, false,
OnHostResolutionCallback(),
TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
int rv = handle.Init("a", dest, LOWEST, callback.callback(), &pool_,
BoundNetLog());
ASSERT_EQ(ERR_IO_PENDING, rv);
// The callback is going to request "www.google.com". We want it to complete
// synchronously this time.
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(OK, callback.WaitForResult());
handle.Reset();
}
// Make sure that pending requests get serviced after active requests get
// cancelled.
TEST_F(TransportClientSocketPoolTest, CancelActiveRequestWithPendingRequests) {
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
// Queue up all the requests
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
// Now, kMaxSocketsPerGroup requests should be active. Let's cancel them.
ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size()));
for (int i = 0; i < kMaxSocketsPerGroup; i++)
(*requests())[i]->handle()->Reset();
// Let's wait for the rest to complete now.
for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) {
EXPECT_EQ(OK, (*requests())[i]->WaitForResult());
(*requests())[i]->handle()->Reset();
}
EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
}
// Make sure that pending requests get serviced after active requests fail.
TEST_F(TransportClientSocketPoolTest, FailingActiveRequestWithPendingRequests) {
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET);
const int kNumRequests = 2 * kMaxSocketsPerGroup + 1;
ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang.
// Queue up all the requests
for (int i = 0; i < kNumRequests; i++)
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
for (int i = 0; i < kNumRequests; i++)
EXPECT_EQ(ERR_CONNECTION_FAILED, (*requests())[i]->WaitForResult());
}
TEST_F(TransportClientSocketPoolTest, IdleSocketLoadTiming) {
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
TestLoadTimingInfoConnectedNotReused(handle);
handle.Reset();
// Need to run all pending to release the socket back to the pool.
base::MessageLoop::current()->RunUntilIdle();
// Now we should have 1 idle socket.
EXPECT_EQ(1, pool_.IdleSocketCount());
rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ(0, pool_.IdleSocketCount());
TestLoadTimingInfoConnectedReused(handle);
}
TEST_F(TransportClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) {
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
handle.Reset();
// Need to run all pending to release the socket back to the pool.
base::MessageLoop::current()->RunUntilIdle();
// Now we should have 1 idle socket.
EXPECT_EQ(1, pool_.IdleSocketCount());
// After an IP address change, we should have 0 idle sockets.
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->RunUntilIdle(); // Notification happens async.
EXPECT_EQ(0, pool_.IdleSocketCount());
}
TEST_F(TransportClientSocketPoolTest, BackupSocketConnect) {
// Case 1 tests the first socket stalling, and the backup connecting.
MockTransportClientSocketFactory::ClientSocketType case1_types[] = {
// The first socket will not connect.
MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
// The second socket will connect more quickly.
MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET
};
// Case 2 tests the first socket being slow, so that we start the
// second connect, but the second connect stalls, and we still
// complete the first.
MockTransportClientSocketFactory::ClientSocketType case2_types[] = {
// The first socket will connect, although delayed.
MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
// The second socket will not connect.
MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
};
MockTransportClientSocketFactory::ClientSocketType* cases[2] = {
case1_types,
case2_types
};
for (size_t index = 0; index < arraysize(cases); ++index) {
client_socket_factory_.set_client_socket_types(cases[index], 2);
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
// Create the first socket, set the timer.
base::MessageLoop::current()->RunUntilIdle();
// Wait for the backup socket timer to fire.
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs + 50));
// Let the appropriate socket connect.
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
// One socket is stalled, the other is active.
EXPECT_EQ(0, pool_.IdleSocketCount());
handle.Reset();
// Close all pending connect jobs and existing sockets.
pool_.FlushWithError(ERR_NETWORK_CHANGED);
}
}
// Test the case where a socket took long enough to start the creation
// of the backup socket, but then we cancelled the request after that.
TEST_F(TransportClientSocketPoolTest, BackupSocketCancel) {
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET);
enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT };
for (int index = CANCEL_BEFORE_WAIT; index < CANCEL_AFTER_WAIT; ++index) {
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("c", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
// Create the first socket, set the timer.
base::MessageLoop::current()->RunUntilIdle();
if (index == CANCEL_AFTER_WAIT) {
// Wait for the backup socket timer to fire.
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs));
}
// Let the appropriate socket connect.
base::MessageLoop::current()->RunUntilIdle();
handle.Reset();
EXPECT_FALSE(callback.have_result());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
// One socket is stalled, the other is active.
EXPECT_EQ(0, pool_.IdleSocketCount());
}
}
// Test the case where a socket took long enough to start the creation
// of the backup socket and never completes, and then the backup
// connection fails.
TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterStall) {
MockTransportClientSocketFactory::ClientSocketType case_types[] = {
// The first socket will not connect.
MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
// The second socket will fail immediately.
MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
// Create the first socket, set the timer.
base::MessageLoop::current()->RunUntilIdle();
// Wait for the backup socket timer to fire.
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs));
// Let the second connect be synchronous. Otherwise, the emulated
// host resolution takes an extra trip through the message loop.
host_resolver_->set_synchronous_mode(true);
// Let the appropriate socket connect.
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(0, pool_.IdleSocketCount());
handle.Reset();
// Reset for the next case.
host_resolver_->set_synchronous_mode(false);
}
// Test the case where a socket took long enough to start the creation
// of the backup socket and eventually completes, but the backup socket
// fails.
TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterDelay) {
MockTransportClientSocketFactory::ClientSocketType case_types[] = {
// The first socket will connect, although delayed.
MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
// The second socket will not connect.
MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(5));
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
// Create the first socket, set the timer.
base::MessageLoop::current()->RunUntilIdle();
// Wait for the backup socket timer to fire.
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs));
// Let the second connect be synchronous. Otherwise, the emulated
// host resolution takes an extra trip through the message loop.
host_resolver_->set_synchronous_mode(true);
// Let the appropriate socket connect.
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
handle.Reset();
// Reset for the next case.
host_resolver_->set_synchronous_mode(false);
}
// Test the case of the IPv6 address stalling, and falling back to the IPv4
// socket which finishes first.
TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv4FinishesFirst) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
MockTransportClientSocketFactory::ClientSocketType case_types[] = {
// This is the IPv6 socket.
MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
// This is the IPv4 socket.
MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
// Resolve an AddressList with a IPv6 address first and then a IPv4 address.
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
EXPECT_EQ(2, client_socket_factory_.allocation_count());
}
// Test the case of the IPv6 address being slow, thus falling back to trying to
// connect to the IPv4 address, but having the connect to the IPv6 address
// finish first.
TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv6FinishesFirst) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
MockTransportClientSocketFactory::ClientSocketType case_types[] = {
// This is the IPv6 socket.
MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
// This is the IPv4 socket.
MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
client_socket_factory_.set_delay(base::TimeDelta::FromMilliseconds(
TransportConnectJobHelper::kIPv6FallbackTimerInMs + 50));
// Resolve an AddressList with a IPv6 address first and then a IPv4 address.
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
EXPECT_EQ(2, client_socket_factory_.allocation_count());
}
TEST_F(TransportClientSocketPoolTest, IPv6NoIPv4AddressesToFallbackTo) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
// Resolve an AddressList with only IPv6 addresses.
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
EXPECT_EQ(1, client_socket_factory_.allocation_count());
}
TEST_F(TransportClientSocketPoolTest, IPv4HasNoFallback) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
// Resolve an AddressList with only IPv4 addresses.
host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
EXPECT_EQ(1, client_socket_factory_.allocation_count());
}
// Test that if TCP FastOpen is enabled, it is set on the socket
// when we have only an IPv4 address.
TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv4WithNoFallback) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
// Resolve an AddressList with only IPv4 addresses.
host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
// Enable TCP FastOpen in TransportSocketParams.
scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen();
handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.socket()->UsingTCPFastOpen());
}
// Test that if TCP FastOpen is enabled, it is set on the socket
// when we have only IPv6 addresses.
TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv6WithNoFallback) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
client_socket_factory_.set_default_client_socket_type(
MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
// Resolve an AddressList with only IPv6 addresses.
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
// Enable TCP FastOpen in TransportSocketParams.
scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen();
handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.socket()->UsingTCPFastOpen());
}
// Test that if TCP FastOpen is enabled, it does not do anything when there
// is a IPv6 address with fallback to an IPv4 address. This test tests the case
// when the IPv6 connect fails and the IPv4 one succeeds.
TEST_F(TransportClientSocketPoolTest,
NoTCPFastOpenOnIPv6FailureWithIPv4Fallback) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
MockTransportClientSocketFactory::ClientSocketType case_types[] = {
// This is the IPv6 socket.
MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
// This is the IPv4 socket.
MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
// Resolve an AddressList with a IPv6 address first and then a IPv4 address.
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
// Enable TCP FastOpen in TransportSocketParams.
scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen();
handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog());
EXPECT_EQ(OK, callback.WaitForResult());
// Verify that the socket used is connected to the fallback IPv4 address.
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
EXPECT_EQ(2, client_socket_factory_.allocation_count());
// Verify that TCP FastOpen was not turned on for the socket.
EXPECT_FALSE(handle.socket()->UsingTCPFastOpen());
}
// Test that if TCP FastOpen is enabled, it does not do anything when there
// is a IPv6 address with fallback to an IPv4 address. This test tests the case
// when the IPv6 connect succeeds.
TEST_F(TransportClientSocketPoolTest,
NoTCPFastOpenOnIPv6SuccessWithIPv4Fallback) {
// Create a pool without backup jobs.
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
MockTransportClientSocketFactory::ClientSocketType case_types[] = {
// This is the IPv6 socket.
MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET,
// This is the IPv4 socket.
MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
// Resolve an AddressList with a IPv6 address first and then a IPv4 address.
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
// Enable TCP FastOpen in TransportSocketParams.
scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen();
handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog());
EXPECT_EQ(OK, callback.WaitForResult());
// Verify that the socket used is connected to the IPv6 address.
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
EXPECT_EQ(1, client_socket_factory_.allocation_count());
// Verify that TCP FastOpen was not turned on for the socket.
EXPECT_FALSE(handle.socket()->UsingTCPFastOpen());
}
} // namespace
} // namespace net
|