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

Side by Side Diff: net/http/http_stream_parser_unittest.cc

Issue 1535363003: Switch to standard integer types in net/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: stddef Created 5 years 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 | « net/http/http_stream_parser.cc ('k') | net/http/http_util.h » ('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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "net/http/http_stream_parser.h" 5 #include "net/http/http_stream_parser.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 TEST(HttpStreamParser, ReceivedBytesNormal) { 1028 TEST(HttpStreamParser, ReceivedBytesNormal) {
1029 std::string headers = "HTTP/1.1 200 OK\r\n" 1029 std::string headers = "HTTP/1.1 200 OK\r\n"
1030 "Content-Length: 7\r\n\r\n"; 1030 "Content-Length: 7\r\n\r\n";
1031 std::string body = "content"; 1031 std::string body = "content";
1032 std::string response = headers + body; 1032 std::string response = headers + body;
1033 1033
1034 SimpleGetRunner get_runner; 1034 SimpleGetRunner get_runner;
1035 get_runner.AddRead(response); 1035 get_runner.AddRead(response);
1036 get_runner.SetupParserAndSendRequest(); 1036 get_runner.SetupParserAndSendRequest();
1037 get_runner.ReadHeaders(); 1037 get_runner.ReadHeaders();
1038 int64 headers_size = headers.size(); 1038 int64_t headers_size = headers.size();
1039 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); 1039 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1040 int body_size = body.size(); 1040 int body_size = body.size();
1041 int read_lengths[] = {body_size, 0}; 1041 int read_lengths[] = {body_size, 0};
1042 get_runner.ReadBody(body_size, read_lengths); 1042 get_runner.ReadBody(body_size, read_lengths);
1043 int64 response_size = response.size(); 1043 int64_t response_size = response.size();
1044 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); 1044 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1045 } 1045 }
1046 1046
1047 // Test that bytes that represent "next" response are not counted 1047 // Test that bytes that represent "next" response are not counted
1048 // as current response "received_bytes". 1048 // as current response "received_bytes".
1049 TEST(HttpStreamParser, ReceivedBytesExcludesNextResponse) { 1049 TEST(HttpStreamParser, ReceivedBytesExcludesNextResponse) {
1050 std::string headers = "HTTP/1.1 200 OK\r\n" 1050 std::string headers = "HTTP/1.1 200 OK\r\n"
1051 "Content-Length: 8\r\n\r\n"; 1051 "Content-Length: 8\r\n\r\n";
1052 std::string body = "content8"; 1052 std::string body = "content8";
1053 std::string response = headers + body; 1053 std::string response = headers + body;
1054 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO"; 1054 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
1055 std::string data = response + next_response; 1055 std::string data = response + next_response;
1056 1056
1057 SimpleGetRunner get_runner; 1057 SimpleGetRunner get_runner;
1058 get_runner.AddRead(data); 1058 get_runner.AddRead(data);
1059 get_runner.SetupParserAndSendRequest(); 1059 get_runner.SetupParserAndSendRequest();
1060 get_runner.ReadHeaders(); 1060 get_runner.ReadHeaders();
1061 EXPECT_EQ(39, get_runner.parser()->received_bytes()); 1061 EXPECT_EQ(39, get_runner.parser()->received_bytes());
1062 int64 headers_size = headers.size(); 1062 int64_t headers_size = headers.size();
1063 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); 1063 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1064 int body_size = body.size(); 1064 int body_size = body.size();
1065 int read_lengths[] = {body_size, 0}; 1065 int read_lengths[] = {body_size, 0};
1066 get_runner.ReadBody(body_size, read_lengths); 1066 get_runner.ReadBody(body_size, read_lengths);
1067 int64 response_size = response.size(); 1067 int64_t response_size = response.size();
1068 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); 1068 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1069 int64 next_response_size = next_response.size(); 1069 int64_t next_response_size = next_response.size();
1070 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset()); 1070 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1071 } 1071 }
1072 1072
1073 // Test that "received_bytes" calculation works fine when last read 1073 // Test that "received_bytes" calculation works fine when last read
1074 // contains more data than requested by user. 1074 // contains more data than requested by user.
1075 // We send data in two reads: 1075 // We send data in two reads:
1076 // 1) Headers + beginning of response 1076 // 1) Headers + beginning of response
1077 // 2) remaining part of response + next response start 1077 // 2) remaining part of response + next response start
1078 // We setup user read buffer so it fully accepts the beginnig of response 1078 // We setup user read buffer so it fully accepts the beginnig of response
1079 // body, but it is larger that remaining part of body. 1079 // body, but it is larger that remaining part of body.
1080 TEST(HttpStreamParser, ReceivedBytesMultiReadExcludesNextResponse) { 1080 TEST(HttpStreamParser, ReceivedBytesMultiReadExcludesNextResponse) {
1081 std::string headers = "HTTP/1.1 200 OK\r\n" 1081 std::string headers = "HTTP/1.1 200 OK\r\n"
1082 "Content-Length: 36\r\n\r\n"; 1082 "Content-Length: 36\r\n\r\n";
1083 int64 user_buf_len = 32; 1083 int64_t user_buf_len = 32;
1084 std::string body_start = std::string(user_buf_len, '#'); 1084 std::string body_start = std::string(user_buf_len, '#');
1085 int body_start_size = body_start.size(); 1085 int body_start_size = body_start.size();
1086 EXPECT_EQ(user_buf_len, body_start_size); 1086 EXPECT_EQ(user_buf_len, body_start_size);
1087 std::string response_start = headers + body_start; 1087 std::string response_start = headers + body_start;
1088 std::string body_end = "abcd"; 1088 std::string body_end = "abcd";
1089 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO"; 1089 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
1090 std::string response_end = body_end + next_response; 1090 std::string response_end = body_end + next_response;
1091 1091
1092 SimpleGetRunner get_runner; 1092 SimpleGetRunner get_runner;
1093 get_runner.AddRead(response_start); 1093 get_runner.AddRead(response_start);
1094 get_runner.AddRead(response_end); 1094 get_runner.AddRead(response_end);
1095 get_runner.SetupParserAndSendRequest(); 1095 get_runner.SetupParserAndSendRequest();
1096 get_runner.ReadHeaders(); 1096 get_runner.ReadHeaders();
1097 int64 headers_size = headers.size(); 1097 int64_t headers_size = headers.size();
1098 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); 1098 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1099 int body_end_size = body_end.size(); 1099 int body_end_size = body_end.size();
1100 int read_lengths[] = {body_start_size, body_end_size, 0}; 1100 int read_lengths[] = {body_start_size, body_end_size, 0};
1101 get_runner.ReadBody(body_start_size, read_lengths); 1101 get_runner.ReadBody(body_start_size, read_lengths);
1102 int64 response_size = response_start.size() + body_end_size; 1102 int64_t response_size = response_start.size() + body_end_size;
1103 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); 1103 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1104 int64 next_response_size = next_response.size(); 1104 int64_t next_response_size = next_response.size();
1105 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset()); 1105 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1106 } 1106 }
1107 1107
1108 // Test that "received_bytes" calculation works fine when there is no 1108 // Test that "received_bytes" calculation works fine when there is no
1109 // network activity at all; that is when all data is read from read buffer. 1109 // network activity at all; that is when all data is read from read buffer.
1110 // In this case read buffer contains two responses. We expect that only 1110 // In this case read buffer contains two responses. We expect that only
1111 // bytes that correspond to the first one are taken into account. 1111 // bytes that correspond to the first one are taken into account.
1112 TEST(HttpStreamParser, ReceivedBytesFromReadBufExcludesNextResponse) { 1112 TEST(HttpStreamParser, ReceivedBytesFromReadBufExcludesNextResponse) {
1113 std::string headers = "HTTP/1.1 200 OK\r\n" 1113 std::string headers = "HTTP/1.1 200 OK\r\n"
1114 "Content-Length: 7\r\n\r\n"; 1114 "Content-Length: 7\r\n\r\n";
1115 std::string body = "content"; 1115 std::string body = "content";
1116 std::string response = headers + body; 1116 std::string response = headers + body;
1117 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO"; 1117 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
1118 std::string data = response + next_response; 1118 std::string data = response + next_response;
1119 1119
1120 SimpleGetRunner get_runner; 1120 SimpleGetRunner get_runner;
1121 get_runner.AddInitialData(data); 1121 get_runner.AddInitialData(data);
1122 get_runner.SetupParserAndSendRequest(); 1122 get_runner.SetupParserAndSendRequest();
1123 get_runner.ReadHeaders(); 1123 get_runner.ReadHeaders();
1124 int64 headers_size = headers.size(); 1124 int64_t headers_size = headers.size();
1125 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); 1125 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1126 int body_size = body.size(); 1126 int body_size = body.size();
1127 int read_lengths[] = {body_size, 0}; 1127 int read_lengths[] = {body_size, 0};
1128 get_runner.ReadBody(body_size, read_lengths); 1128 get_runner.ReadBody(body_size, read_lengths);
1129 int64 response_size = response.size(); 1129 int64_t response_size = response.size();
1130 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); 1130 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1131 int64 next_response_size = next_response.size(); 1131 int64_t next_response_size = next_response.size();
1132 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset()); 1132 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1133 } 1133 }
1134 1134
1135 // Test calculating "received_bytes" when part of request has been already 1135 // Test calculating "received_bytes" when part of request has been already
1136 // loaded and placed to read buffer by previous stream parser. 1136 // loaded and placed to read buffer by previous stream parser.
1137 TEST(HttpStreamParser, ReceivedBytesUseReadBuf) { 1137 TEST(HttpStreamParser, ReceivedBytesUseReadBuf) {
1138 std::string buffer = "HTTP/1.1 200 OK\r\n"; 1138 std::string buffer = "HTTP/1.1 200 OK\r\n";
1139 std::string remaining_headers = "Content-Length: 7\r\n\r\n"; 1139 std::string remaining_headers = "Content-Length: 7\r\n\r\n";
1140 int64 headers_size = buffer.size() + remaining_headers.size(); 1140 int64_t headers_size = buffer.size() + remaining_headers.size();
1141 std::string body = "content"; 1141 std::string body = "content";
1142 std::string response = remaining_headers + body; 1142 std::string response = remaining_headers + body;
1143 1143
1144 SimpleGetRunner get_runner; 1144 SimpleGetRunner get_runner;
1145 get_runner.AddInitialData(buffer); 1145 get_runner.AddInitialData(buffer);
1146 get_runner.AddRead(response); 1146 get_runner.AddRead(response);
1147 get_runner.SetupParserAndSendRequest(); 1147 get_runner.SetupParserAndSendRequest();
1148 get_runner.ReadHeaders(); 1148 get_runner.ReadHeaders();
1149 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); 1149 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1150 int body_size = body.size(); 1150 int body_size = body.size();
(...skipping 14 matching lines...) Expand all
1165 "0\r\n\r\n"; 1165 "0\r\n\r\n";
1166 std::string next_response = "foo bar\r\n"; 1166 std::string next_response = "foo bar\r\n";
1167 std::string data = response + next_response; 1167 std::string data = response + next_response;
1168 1168
1169 SimpleGetRunner get_runner; 1169 SimpleGetRunner get_runner;
1170 get_runner.AddInitialData(data); 1170 get_runner.AddInitialData(data);
1171 get_runner.SetupParserAndSendRequest(); 1171 get_runner.SetupParserAndSendRequest();
1172 get_runner.ReadHeaders(); 1172 get_runner.ReadHeaders();
1173 int read_lengths[] = {4, 3, 6, 2, 6, 0}; 1173 int read_lengths[] = {4, 3, 6, 2, 6, 0};
1174 get_runner.ReadBody(7, read_lengths); 1174 get_runner.ReadBody(7, read_lengths);
1175 int64 response_size = response.size(); 1175 int64_t response_size = response.size();
1176 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); 1176 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1177 int64 next_response_size = next_response.size(); 1177 int64_t next_response_size = next_response.size();
1178 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset()); 1178 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1179 } 1179 }
1180 1180
1181 // Test that data transfered in multiple reads is correctly processed. 1181 // Test that data transfered in multiple reads is correctly processed.
1182 // We feed data into 4-bytes reads. Also we set length of read 1182 // We feed data into 4-bytes reads. Also we set length of read
1183 // buffer to 5-bytes to test all possible buffer misaligments. 1183 // buffer to 5-bytes to test all possible buffer misaligments.
1184 TEST(HttpStreamParser, ReceivedBytesMultipleReads) { 1184 TEST(HttpStreamParser, ReceivedBytesMultipleReads) {
1185 std::string headers = "HTTP/1.1 200 OK\r\n" 1185 std::string headers = "HTTP/1.1 200 OK\r\n"
1186 "Content-Length: 33\r\n\r\n"; 1186 "Content-Length: 33\r\n\r\n";
1187 std::string body = "foo bar baz\r\n" 1187 std::string body = "foo bar baz\r\n"
1188 "sputnik mir babushka"; 1188 "sputnik mir babushka";
1189 std::string response = headers + body; 1189 std::string response = headers + body;
1190 1190
1191 size_t receive_length = 4; 1191 size_t receive_length = 4;
1192 std::vector<std::string> blocks; 1192 std::vector<std::string> blocks;
1193 for (size_t i = 0; i < response.size(); i += receive_length) { 1193 for (size_t i = 0; i < response.size(); i += receive_length) {
1194 size_t length = std::min(receive_length, response.size() - i); 1194 size_t length = std::min(receive_length, response.size() - i);
1195 blocks.push_back(response.substr(i, length)); 1195 blocks.push_back(response.substr(i, length));
1196 } 1196 }
1197 1197
1198 SimpleGetRunner get_runner; 1198 SimpleGetRunner get_runner;
1199 for (std::vector<std::string>::size_type i = 0; i < blocks.size(); ++i) 1199 for (std::vector<std::string>::size_type i = 0; i < blocks.size(); ++i)
1200 get_runner.AddRead(blocks[i]); 1200 get_runner.AddRead(blocks[i]);
1201 get_runner.SetupParserAndSendRequest(); 1201 get_runner.SetupParserAndSendRequest();
1202 get_runner.ReadHeaders(); 1202 get_runner.ReadHeaders();
1203 int64 headers_size = headers.size(); 1203 int64_t headers_size = headers.size();
1204 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); 1204 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1205 int read_lengths[] = {1, 4, 4, 4, 4, 4, 4, 4, 4, 0}; 1205 int read_lengths[] = {1, 4, 4, 4, 4, 4, 4, 4, 4, 0};
1206 get_runner.ReadBody(receive_length + 1, read_lengths); 1206 get_runner.ReadBody(receive_length + 1, read_lengths);
1207 int64 response_size = response.size(); 1207 int64_t response_size = response.size();
1208 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); 1208 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1209 } 1209 }
1210 1210
1211 // Test that "continue" HTTP header is counted as "received_bytes". 1211 // Test that "continue" HTTP header is counted as "received_bytes".
1212 TEST(HttpStreamParser, ReceivedBytesIncludesContinueHeader) { 1212 TEST(HttpStreamParser, ReceivedBytesIncludesContinueHeader) {
1213 std::string status100 = "HTTP/1.1 100 OK\r\n\r\n"; 1213 std::string status100 = "HTTP/1.1 100 OK\r\n\r\n";
1214 std::string headers = "HTTP/1.1 200 OK\r\n" 1214 std::string headers = "HTTP/1.1 200 OK\r\n"
1215 "Content-Length: 7\r\n\r\n"; 1215 "Content-Length: 7\r\n\r\n";
1216 int64 headers_size = status100.size() + headers.size(); 1216 int64_t headers_size = status100.size() + headers.size();
1217 std::string body = "content"; 1217 std::string body = "content";
1218 std::string response = headers + body; 1218 std::string response = headers + body;
1219 1219
1220 SimpleGetRunner get_runner; 1220 SimpleGetRunner get_runner;
1221 get_runner.AddRead(status100); 1221 get_runner.AddRead(status100);
1222 get_runner.AddRead(response); 1222 get_runner.AddRead(response);
1223 get_runner.SetupParserAndSendRequest(); 1223 get_runner.SetupParserAndSendRequest();
1224 get_runner.ReadHeaders(); 1224 get_runner.ReadHeaders();
1225 EXPECT_EQ(100, get_runner.response_info()->headers->response_code()); 1225 EXPECT_EQ(100, get_runner.response_info()->headers->response_code());
1226 int64 status100_size = status100.size(); 1226 int64_t status100_size = status100.size();
1227 EXPECT_EQ(status100_size, get_runner.parser()->received_bytes()); 1227 EXPECT_EQ(status100_size, get_runner.parser()->received_bytes());
1228 get_runner.ReadHeaders(); 1228 get_runner.ReadHeaders();
1229 EXPECT_EQ(200, get_runner.response_info()->headers->response_code()); 1229 EXPECT_EQ(200, get_runner.response_info()->headers->response_code());
1230 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); 1230 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1231 int64 response_size = headers_size + body.size(); 1231 int64_t response_size = headers_size + body.size();
1232 int body_size = body.size(); 1232 int body_size = body.size();
1233 int read_lengths[] = {body_size, 0}; 1233 int read_lengths[] = {body_size, 0};
1234 get_runner.ReadBody(body_size, read_lengths); 1234 get_runner.ReadBody(body_size, read_lengths);
1235 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); 1235 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1236 } 1236 }
1237 1237
1238 // Test that an HttpStreamParser can be read from after it's received headers 1238 // Test that an HttpStreamParser can be read from after it's received headers
1239 // and data structures owned by its owner have been deleted. This happens 1239 // and data structures owned by its owner have been deleted. This happens
1240 // when a ResponseBodyDrainer is used. 1240 // when a ResponseBodyDrainer is used.
1241 TEST(HttpStreamParser, ReadAfterUnownedObjectsDestroyed) { 1241 TEST(HttpStreamParser, ReadAfterUnownedObjectsDestroyed) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1282 ASSERT_EQ(kBodySize, parser.ReadResponseBody( 1282 ASSERT_EQ(kBodySize, parser.ReadResponseBody(
1283 body_buffer.get(), kBodySize, callback.callback())); 1283 body_buffer.get(), kBodySize, callback.callback()));
1284 1284
1285 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 1285 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
1286 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); 1286 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
1287 } 1287 }
1288 1288
1289 } // namespace 1289 } // namespace
1290 1290
1291 } // namespace net 1291 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_stream_parser.cc ('k') | net/http/http_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698