[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/http/http_stream_parser.h" |
| 6 | |
| 7 | #include "base/string_piece.h" |
| 8 | #include "base/stringprintf.h" |
| 9 | #include "net/base/net_errors.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace net { |
| 13 | |
| 14 | const size_t kOutputSize = 1024; // Just large enough for this test. |
| 15 | // The number of bytes that can fit in a buffer of kOutputSize. |
| 16 | const size_t kMaxPayloadSize = |
| 17 | kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; |
| 18 | |
| 19 | // The empty payload is how the last chunk is encoded. |
| 20 | TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { |
| 21 | char output[kOutputSize]; |
| 22 | |
| 23 | const base::StringPiece kPayload = ""; |
| 24 | const base::StringPiece kExpected = "0\r\n\r\n"; |
| 25 | const int num_bytes_written = |
| 26 | HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 27 | ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| 28 | EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| 29 | } |
| 30 | |
| 31 | TEST(HttpStreamParser, EncodeChunk_ShortPayload) { |
| 32 | char output[kOutputSize]; |
| 33 | |
| 34 | const std::string kPayload("foo\x00\x11\x22", 6); |
| 35 | // 11 = payload size + sizeof("6") + CRLF x 2. |
| 36 | const std::string kExpected("6\r\nfoo\x00\x11\x22\r\n", 11); |
| 37 | const int num_bytes_written = |
| 38 | HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 39 | ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| 40 | EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| 41 | } |
| 42 | |
| 43 | TEST(HttpStreamParser, EncodeChunk_LargePayload) { |
| 44 | char output[kOutputSize]; |
| 45 | |
| 46 | const std::string kPayload(1000, '\xff'); // '\xff' x 1000. |
| 47 | // 3E8 = 1000 in hex. |
| 48 | const std::string kExpected = "3E8\r\n" + kPayload + "\r\n"; |
| 49 | const int num_bytes_written = |
| 50 | HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 51 | ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| 52 | EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| 53 | } |
| 54 | |
| 55 | TEST(HttpStreamParser, EncodeChunk_FullPayload) { |
| 56 | char output[kOutputSize]; |
| 57 | |
| 58 | const std::string kPayload(kMaxPayloadSize, '\xff'); |
| 59 | // 3F4 = 1012 in hex. |
| 60 | const std::string kExpected = "3F4\r\n" + kPayload + "\r\n"; |
| 61 | const int num_bytes_written = |
| 62 | HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 63 | ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| 64 | EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| 65 | } |
| 66 | |
| 67 | TEST(HttpStreamParser, EncodeChunk_TooLargePayload) { |
| 68 | char output[kOutputSize]; |
| 69 | |
| 70 | // The payload is one byte larger the output buffer size. |
| 71 | const std::string kPayload(kMaxPayloadSize + 1, '\xff'); |
| 72 | const int num_bytes_written = |
| 73 | HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 74 | ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written); |
| 75 | } |
| 76 | |
| 77 | } // namespace net |