diff options
author | ttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-04 09:26:24 +0000 |
---|---|---|
committer | ttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-04 09:26:24 +0000 |
commit | 5a62b8be1dae0bb706104ccffc57466a8d411923 (patch) | |
tree | 6811a37af075c8619311a99c55e62293f3ca5b63 /net/spdy | |
parent | 5bfe3bf04f473256770bd8c5148dc3f4a43d4240 (diff) | |
download | chromium_src-5a62b8be1dae0bb706104ccffc57466a8d411923.zip chromium_src-5a62b8be1dae0bb706104ccffc57466a8d411923.tar.gz chromium_src-5a62b8be1dae0bb706104ccffc57466a8d411923.tar.bz2 |
Move version-agnostic functions from spdy_test_util_spdy[23] to _common.
There are some functions in net/spdy/spdy_test_util_spdy[23] that are
identical across the two files. Move them into net/spdy/spdy_test_util_common
and include spdy_test_util_common.h in _spdy[23].h so tests can see them.
BUG=226192
Review URL: https://chromiumcodereview.appspot.com/13582002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192262 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/spdy')
-rw-r--r-- | net/spdy/spdy_test_util_common.cc | 167 | ||||
-rw-r--r-- | net/spdy/spdy_test_util_common.h | 89 | ||||
-rw-r--r-- | net/spdy/spdy_test_util_spdy2.cc | 166 | ||||
-rw-r--r-- | net/spdy/spdy_test_util_spdy2.h | 89 | ||||
-rw-r--r-- | net/spdy/spdy_test_util_spdy3.cc | 166 | ||||
-rw-r--r-- | net/spdy/spdy_test_util_spdy3.h | 89 |
6 files changed, 258 insertions, 508 deletions
diff --git a/net/spdy/spdy_test_util_common.cc b/net/spdy/spdy_test_util_common.cc index b2cb945..061b4b1 100644 --- a/net/spdy/spdy_test_util_common.cc +++ b/net/spdy/spdy_test_util_common.cc @@ -7,12 +7,179 @@ #include <cstddef> #include "base/compiler_specific.h" +#include "net/socket/socket_test_util.h" #include "net/spdy/buffered_spdy_framer.h" #include "net/spdy/spdy_session.h" #include "net/spdy/spdy_stream.h" namespace net { +// Chop a frame into an array of MockWrites. +// |data| is the frame to chop. +// |length| is the length of the frame to chop. +// |num_chunks| is the number of chunks to create. +MockWrite* ChopWriteFrame(const char* data, int length, int num_chunks) { + MockWrite* chunks = new MockWrite[num_chunks]; + int chunk_size = length / num_chunks; + for (int index = 0; index < num_chunks; index++) { + const char* ptr = data + (index * chunk_size); + if (index == num_chunks - 1) + chunk_size += length % chunk_size; // The last chunk takes the remainder. + chunks[index] = MockWrite(ASYNC, ptr, chunk_size); + } + return chunks; +} + +// Chop a SpdyFrame into an array of MockWrites. +// |frame| is the frame to chop. +// |num_chunks| is the number of chunks to create. +MockWrite* ChopWriteFrame(const SpdyFrame& frame, int num_chunks) { + return ChopWriteFrame(frame.data(), frame.size(), num_chunks); +} + +// Chop a frame into an array of MockReads. +// |data| is the frame to chop. +// |length| is the length of the frame to chop. +// |num_chunks| is the number of chunks to create. +MockRead* ChopReadFrame(const char* data, int length, int num_chunks) { + MockRead* chunks = new MockRead[num_chunks]; + int chunk_size = length / num_chunks; + for (int index = 0; index < num_chunks; index++) { + const char* ptr = data + (index * chunk_size); + if (index == num_chunks - 1) + chunk_size += length % chunk_size; // The last chunk takes the remainder. + chunks[index] = MockRead(ASYNC, ptr, chunk_size); + } + return chunks; +} + +// Chop a SpdyFrame into an array of MockReads. +// |frame| is the frame to chop. +// |num_chunks| is the number of chunks to create. +MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks) { + return ChopReadFrame(frame.data(), frame.size(), num_chunks); +} + +// Adds headers and values to a map. +// |extra_headers| is an array of { name, value } pairs, arranged as strings +// where the even entries are the header names, and the odd entries are the +// header values. +// |headers| gets filled in from |extra_headers|. +void AppendToHeaderBlock(const char* const extra_headers[], + int extra_header_count, + SpdyHeaderBlock* headers) { + std::string this_header; + std::string this_value; + + if (!extra_header_count) + return; + + // Sanity check: Non-NULL header list. + DCHECK(NULL != extra_headers) << "NULL header value pair list"; + // Sanity check: Non-NULL header map. + DCHECK(NULL != headers) << "NULL header map"; + // Copy in the headers. + for (int i = 0; i < extra_header_count; i++) { + // Sanity check: Non-empty header. + DCHECK_NE('\0', *extra_headers[i * 2]) << "Empty header value pair"; + this_header = extra_headers[i * 2]; + std::string::size_type header_len = this_header.length(); + if (!header_len) + continue; + this_value = extra_headers[1 + (i * 2)]; + std::string new_value; + if (headers->find(this_header) != headers->end()) { + // More than one entry in the header. + // Don't add the header again, just the append to the value, + // separated by a NULL character. + + // Adjust the value. + new_value = (*headers)[this_header]; + // Put in a NULL separator. + new_value.append(1, '\0'); + // Append the new value. + new_value += this_value; + } else { + // Not a duplicate, just write the value. + new_value = this_value; + } + (*headers)[this_header] = new_value; + } +} + +// Writes |val| to a location of size |len|, in big-endian format. +// in the buffer pointed to by |buffer_handle|. +// Updates the |*buffer_handle| pointer by |len| +// Returns the number of bytes written +int AppendToBuffer(int val, + int len, + unsigned char** buffer_handle, + int* buffer_len_remaining) { + if (len <= 0) + return 0; + DCHECK((size_t) len <= sizeof(len)) << "Data length too long for data type"; + DCHECK(NULL != buffer_handle) << "NULL buffer handle"; + DCHECK(NULL != *buffer_handle) << "NULL pointer"; + DCHECK(NULL != buffer_len_remaining) + << "NULL buffer remainder length pointer"; + DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; + for (int i = 0; i < len; i++) { + int shift = (8 * (len - (i + 1))); + unsigned char val_chunk = (val >> shift) & 0x0FF; + *(*buffer_handle)++ = val_chunk; + *buffer_len_remaining += 1; + } + return len; +} + +// Create a MockWrite from the given SpdyFrame. +MockWrite CreateMockWrite(const SpdyFrame& req) { + return MockWrite(ASYNC, req.data(), req.size()); +} + +// Create a MockWrite from the given SpdyFrame and sequence number. +MockWrite CreateMockWrite(const SpdyFrame& req, int seq) { + return CreateMockWrite(req, seq, ASYNC); +} + +// Create a MockWrite from the given SpdyFrame and sequence number. +MockWrite CreateMockWrite(const SpdyFrame& req, int seq, IoMode mode) { + return MockWrite(mode, req.data(), req.size(), seq); +} + +// Create a MockRead from the given SpdyFrame. +MockRead CreateMockRead(const SpdyFrame& resp) { + return MockRead(ASYNC, resp.data(), resp.size()); +} + +// Create a MockRead from the given SpdyFrame and sequence number. +MockRead CreateMockRead(const SpdyFrame& resp, int seq) { + return CreateMockRead(resp, seq, ASYNC); +} + +// Create a MockRead from the given SpdyFrame and sequence number. +MockRead CreateMockRead(const SpdyFrame& resp, int seq, IoMode mode) { + return MockRead(mode, resp.data(), resp.size(), seq); +} + +// Combines the given SpdyFrames into the given char array and returns +// the total length. +int CombineFrames(const SpdyFrame** frames, int num_frames, + char* buff, int buff_len) { + int total_len = 0; + for (int i = 0; i < num_frames; ++i) { + total_len += frames[i]->size(); + } + DCHECK_LE(total_len, buff_len); + char* ptr = buff; + for (int i = 0; i < num_frames; ++i) { + int len = frames[i]->size(); + memcpy(ptr, frames[i]->data(), len); + ptr += len; + } + return total_len; +} + namespace { class PriorityGetter : public BufferedSpdyFramerVisitorInterface { diff --git a/net/spdy/spdy_test_util_common.h b/net/spdy/spdy_test_util_common.h index ff857ca..b5be964 100644 --- a/net/spdy/spdy_test_util_common.h +++ b/net/spdy/spdy_test_util_common.h @@ -9,6 +9,7 @@ #include "net/base/completion_callback.h" #include "net/base/request_priority.h" #include "net/base/test_completion_callback.h" +#include "net/socket/socket_test_util.h" #include "net/spdy/spdy_protocol.h" class GURL; @@ -20,6 +21,94 @@ class SpdySession; class SpdyStream; class SpdyStreamRequest; +// Default upload data used by both, mock objects and framer when creating +// data frames. +const char kDefaultURL[] = "http://www.google.com"; +const char kUploadData[] = "hello!"; +const int kUploadDataSize = arraysize(kUploadData)-1; + +// Chop a frame into an array of MockWrites. +// |data| is the frame to chop. +// |length| is the length of the frame to chop. +// |num_chunks| is the number of chunks to create. +MockWrite* ChopWriteFrame(const char* data, int length, int num_chunks); + +// Chop a SpdyFrame into an array of MockWrites. +// |frame| is the frame to chop. +// |num_chunks| is the number of chunks to create. +MockWrite* ChopWriteFrame(const SpdyFrame& frame, int num_chunks); + +// Chop a frame into an array of MockReads. +// |data| is the frame to chop. +// |length| is the length of the frame to chop. +// |num_chunks| is the number of chunks to create. +MockRead* ChopReadFrame(const char* data, int length, int num_chunks); + +// Chop a SpdyFrame into an array of MockReads. +// |frame| is the frame to chop. +// |num_chunks| is the number of chunks to create. +MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks); + +// Adds headers and values to a map. +// |extra_headers| is an array of { name, value } pairs, arranged as strings +// where the even entries are the header names, and the odd entries are the +// header values. +// |headers| gets filled in from |extra_headers|. +void AppendToHeaderBlock(const char* const extra_headers[], + int extra_header_count, + SpdyHeaderBlock* headers); + +// Writes |str| of the given |len| to the buffer pointed to by |buffer_handle|. +// Uses a template so buffer_handle can be a char* or an unsigned char*. +// Updates the |*buffer_handle| pointer by |len| +// Returns the number of bytes written into *|buffer_handle| +template<class T> +int AppendToBuffer(const char* str, + int len, + T** buffer_handle, + int* buffer_len_remaining) { + DCHECK_GT(len, 0); + DCHECK(NULL != buffer_handle) << "NULL buffer handle"; + DCHECK(NULL != *buffer_handle) << "NULL pointer"; + DCHECK(NULL != buffer_len_remaining) + << "NULL buffer remainder length pointer"; + DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; + memcpy(*buffer_handle, str, len); + *buffer_handle += len; + *buffer_len_remaining -= len; + return len; +} + +// Writes |val| to a location of size |len|, in big-endian format. +// in the buffer pointed to by |buffer_handle|. +// Updates the |*buffer_handle| pointer by |len| +// Returns the number of bytes written +int AppendToBuffer(int val, + int len, + unsigned char** buffer_handle, + int* buffer_len_remaining); + +// Create an async MockWrite from the given SpdyFrame. +MockWrite CreateMockWrite(const SpdyFrame& req); + +// Create an async MockWrite from the given SpdyFrame and sequence number. +MockWrite CreateMockWrite(const SpdyFrame& req, int seq); + +MockWrite CreateMockWrite(const SpdyFrame& req, int seq, IoMode mode); + +// Create a MockRead from the given SpdyFrame. +MockRead CreateMockRead(const SpdyFrame& resp); + +// Create a MockRead from the given SpdyFrame and sequence number. +MockRead CreateMockRead(const SpdyFrame& resp, int seq); + +MockRead CreateMockRead(const SpdyFrame& resp, int seq, IoMode mode); + +// Combines the given SpdyFrames into the given char array and returns +// the total length. +int CombineFrames(const SpdyFrame** frames, int num_frames, + char* buff, int buff_len); + // Returns the SpdyPriority embedded in the given frame. Returns true // and fills in |priority| on success. bool GetSpdyPriority(int version, diff --git a/net/spdy/spdy_test_util_spdy2.cc b/net/spdy/spdy_test_util_spdy2.cc index 703371c..c990451 100644 --- a/net/spdy/spdy_test_util_spdy2.cc +++ b/net/spdy/spdy_test_util_spdy2.cc @@ -39,99 +39,6 @@ void ParseUrl(const char* const url, std::string* scheme, std::string* host, } // namespace -// Chop a frame into an array of MockWrites. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const char* data, int length, int num_chunks) { - MockWrite* chunks = new MockWrite[num_chunks]; - int chunk_size = length / num_chunks; - for (int index = 0; index < num_chunks; index++) { - const char* ptr = data + (index * chunk_size); - if (index == num_chunks - 1) - chunk_size += length % chunk_size; // The last chunk takes the remainder. - chunks[index] = MockWrite(ASYNC, ptr, chunk_size); - } - return chunks; -} - -// Chop a SpdyFrame into an array of MockWrites. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const SpdyFrame& frame, int num_chunks) { - return ChopWriteFrame(frame.data(), frame.size(), num_chunks); -} - -// Chop a frame into an array of MockReads. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const char* data, int length, int num_chunks) { - MockRead* chunks = new MockRead[num_chunks]; - int chunk_size = length / num_chunks; - for (int index = 0; index < num_chunks; index++) { - const char* ptr = data + (index * chunk_size); - if (index == num_chunks - 1) - chunk_size += length % chunk_size; // The last chunk takes the remainder. - chunks[index] = MockRead(ASYNC, ptr, chunk_size); - } - return chunks; -} - -// Chop a SpdyFrame into an array of MockReads. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks) { - return ChopReadFrame(frame.data(), frame.size(), num_chunks); -} - -// Adds headers and values to a map. -// |extra_headers| is an array of { name, value } pairs, arranged as strings -// where the even entries are the header names, and the odd entries are the -// header values. -// |headers| gets filled in from |extra_headers|. -void AppendToHeaderBlock(const char* const extra_headers[], - int extra_header_count, - SpdyHeaderBlock* headers) { - std::string this_header; - std::string this_value; - - if (!extra_header_count) - return; - - // Sanity check: Non-NULL header list. - DCHECK(NULL != extra_headers) << "NULL header value pair list"; - // Sanity check: Non-NULL header map. - DCHECK(NULL != headers) << "NULL header map"; - // Copy in the headers. - for (int i = 0; i < extra_header_count; i++) { - // Sanity check: Non-empty header. - DCHECK_NE('\0', *extra_headers[i * 2]) << "Empty header value pair"; - this_header = extra_headers[i * 2]; - std::string::size_type header_len = this_header.length(); - if (!header_len) - continue; - this_value = extra_headers[1 + (i * 2)]; - std::string new_value; - if (headers->find(this_header) != headers->end()) { - // More than one entry in the header. - // Don't add the header again, just the append to the value, - // separated by a NULL character. - - // Adjust the value. - new_value = (*headers)[this_header]; - // Put in a NULL separator. - new_value.append(1, '\0'); - // Append the new value. - new_value += this_value; - } else { - // Not a duplicate, just write the value. - new_value = this_value; - } - (*headers)[this_header] = new_value; - } -} - scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlock(base::StringPiece url) { std::string scheme, host, path; ParseUrl(url.data(), &scheme, &host, &path); @@ -165,31 +72,6 @@ scoped_ptr<SpdyHeaderBlock> ConstructPostHeaderBlock(base::StringPiece url, return header_block.Pass(); } -// Writes |val| to a location of size |len|, in big-endian format. -// in the buffer pointed to by |buffer_handle|. -// Updates the |*buffer_handle| pointer by |len| -// Returns the number of bytes written -int AppendToBuffer(int val, - int len, - unsigned char** buffer_handle, - int* buffer_len_remaining) { - if (len <= 0) - return 0; - DCHECK((size_t) len <= sizeof(len)) << "Data length too long for data type"; - DCHECK(NULL != buffer_handle) << "NULL buffer handle"; - DCHECK(NULL != *buffer_handle) << "NULL pointer"; - DCHECK(NULL != buffer_len_remaining) - << "NULL buffer remainder length pointer"; - DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; - for (int i = 0; i < len; i++) { - int shift = (8 * (len - (i + 1))); - unsigned char val_chunk = (val >> shift) & 0x0FF; - *(*buffer_handle)++ = val_chunk; - *buffer_len_remaining += 1; - } - return len; -} - SpdyFrame* ConstructSpdyFrame(const SpdyHeaderInfo& header_info, scoped_ptr<SpdyHeaderBlock> headers) { BufferedSpdyFramer framer(kSpdyVersion2, header_info.compressed); @@ -804,54 +686,6 @@ int ConstructSpdyReplyString(const char* const extra_headers[], return frame_size; } -// Create a MockWrite from the given SpdyFrame. -MockWrite CreateMockWrite(const SpdyFrame& req) { - return MockWrite(ASYNC, req.data(), req.size()); -} - -// Create a MockWrite from the given SpdyFrame and sequence number. -MockWrite CreateMockWrite(const SpdyFrame& req, int seq) { - return CreateMockWrite(req, seq, ASYNC); -} - -// Create a MockWrite from the given SpdyFrame and sequence number. -MockWrite CreateMockWrite(const SpdyFrame& req, int seq, IoMode mode) { - return MockWrite(mode, req.data(), req.size(), seq); -} - -// Create a MockRead from the given SpdyFrame. -MockRead CreateMockRead(const SpdyFrame& resp) { - return MockRead(ASYNC, resp.data(), resp.size()); -} - -// Create a MockRead from the given SpdyFrame and sequence number. -MockRead CreateMockRead(const SpdyFrame& resp, int seq) { - return CreateMockRead(resp, seq, ASYNC); -} - -// Create a MockRead from the given SpdyFrame and sequence number. -MockRead CreateMockRead(const SpdyFrame& resp, int seq, IoMode mode) { - return MockRead(mode, resp.data(), resp.size(), seq); -} - -// Combines the given SpdyFrames into the given char array and returns -// the total length. -int CombineFrames(const SpdyFrame** frames, int num_frames, - char* buff, int buff_len) { - int total_len = 0; - for (int i = 0; i < num_frames; ++i) { - total_len += frames[i]->size(); - } - DCHECK_LE(total_len, buff_len); - char* ptr = buff; - for (int i = 0; i < num_frames; ++i) { - int len = frames[i]->size(); - memcpy(ptr, frames[i]->data(), len); - ptr += len; - } - return total_len; -} - SpdySessionDependencies::SpdySessionDependencies() : host_resolver(new MockCachingHostResolver), cert_verifier(new MockCertVerifier), diff --git a/net/spdy/spdy_test_util_spdy2.h b/net/spdy/spdy_test_util_spdy2.h index 4023689..d65df39 100644 --- a/net/spdy/spdy_test_util_spdy2.h +++ b/net/spdy/spdy_test_util_spdy2.h @@ -21,6 +21,7 @@ #include "net/proxy/proxy_service.h" #include "net/socket/socket_test_util.h" #include "net/spdy/spdy_session.h" +#include "net/spdy/spdy_test_util_common.h" #include "net/ssl/ssl_config_service_defaults.h" #include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context_storage.h" @@ -29,12 +30,6 @@ namespace net { namespace test_spdy2 { -// Default upload data used by both, mock objects and framer when creating -// data frames. -const char kDefaultURL[] = "http://www.google.com"; -const char kUploadData[] = "hello!"; -const int kUploadDataSize = arraysize(kUploadData)-1; - // NOTE: In GCC, on a Mac, this can't be in an anonymous namespace! // This struct holds information used to construct spdy control and data frames. struct SpdyHeaderInfo { @@ -50,37 +45,6 @@ struct SpdyHeaderInfo { SpdyDataFlags data_flags; }; -// Chop a frame into an array of MockWrites. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const char* data, int length, int num_chunks); - -// Chop a SpdyFrame into an array of MockWrites. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const SpdyFrame& frame, int num_chunks); - -// Chop a frame into an array of MockReads. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const char* data, int length, int num_chunks); - -// Chop a SpdyFrame into an array of MockReads. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks); - -// Adds headers and values to a map. -// |extra_headers| is an array of { name, value } pairs, arranged as strings -// where the even entries are the header names, and the odd entries are the -// header values. -// |headers| gets filled in from |extra_headers|. -void AppendToHeaderBlock(const char* const extra_headers[], - int extra_header_count, - SpdyHeaderBlock* headers); - // Constructs a HeaderBlock for a GET request for the given URL. scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlock(base::StringPiece url); @@ -88,36 +52,6 @@ scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlock(base::StringPiece url); scoped_ptr<SpdyHeaderBlock> ConstructPostHeaderBlock(base::StringPiece url, int64 content_length); -// Writes |str| of the given |len| to the buffer pointed to by |buffer_handle|. -// Uses a template so buffer_handle can be a char* or an unsigned char*. -// Updates the |*buffer_handle| pointer by |len| -// Returns the number of bytes written into *|buffer_handle| -template<class T> -int AppendToBuffer(const char* str, - int len, - T** buffer_handle, - int* buffer_len_remaining) { - DCHECK_GT(len, 0); - DCHECK(NULL != buffer_handle) << "NULL buffer handle"; - DCHECK(NULL != *buffer_handle) << "NULL pointer"; - DCHECK(NULL != buffer_len_remaining) - << "NULL buffer remainder length pointer"; - DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; - memcpy(*buffer_handle, str, len); - *buffer_handle += len; - *buffer_len_remaining -= len; - return len; -} - -// Writes |val| to a location of size |len|, in big-endian format. -// in the buffer pointed to by |buffer_handle|. -// Updates the |*buffer_handle| pointer by |len| -// Returns the number of bytes written -int AppendToBuffer(int val, - int len, - unsigned char** buffer_handle, - int* buffer_len_remaining); - // Construct a SPDY frame. SpdyFrame* ConstructSpdyFrame(const SpdyHeaderInfo& header_info, scoped_ptr<SpdyHeaderBlock> headers); @@ -328,27 +262,6 @@ SpdyFrame* ConstructSpdyBodyFrame(int stream_id, const char* data, SpdyFrame* ConstructWrappedSpdyFrame(const scoped_ptr<SpdyFrame>& frame, int stream_id); -// Create an async MockWrite from the given SpdyFrame. -MockWrite CreateMockWrite(const SpdyFrame& req); - -// Create an async MockWrite from the given SpdyFrame and sequence number. -MockWrite CreateMockWrite(const SpdyFrame& req, int seq); - -MockWrite CreateMockWrite(const SpdyFrame& req, int seq, IoMode mode); - -// Create a MockRead from the given SpdyFrame. -MockRead CreateMockRead(const SpdyFrame& resp); - -// Create a MockRead from the given SpdyFrame and sequence number. -MockRead CreateMockRead(const SpdyFrame& resp, int seq); - -MockRead CreateMockRead(const SpdyFrame& resp, int seq, IoMode mode); - -// Combines the given SpdyFrames into the given char array and returns -// the total length. -int CombineFrames(const SpdyFrame** frames, int num_frames, - char* buff, int buff_len); - // Helper to manage the lifetimes of the dependencies for a // HttpNetworkTransaction. struct SpdySessionDependencies { diff --git a/net/spdy/spdy_test_util_spdy3.cc b/net/spdy/spdy_test_util_spdy3.cc index 37827a9..1bfd4df 100644 --- a/net/spdy/spdy_test_util_spdy3.cc +++ b/net/spdy/spdy_test_util_spdy3.cc @@ -83,99 +83,6 @@ crypto::ECSignatureCreator* MockECSignatureCreatorFactory::Create( return new MockECSignatureCreator(key); } -// Chop a frame into an array of MockWrites. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const char* data, int length, int num_chunks) { - MockWrite* chunks = new MockWrite[num_chunks]; - int chunk_size = length / num_chunks; - for (int index = 0; index < num_chunks; index++) { - const char* ptr = data + (index * chunk_size); - if (index == num_chunks - 1) - chunk_size += length % chunk_size; // The last chunk takes the remainder. - chunks[index] = MockWrite(ASYNC, ptr, chunk_size); - } - return chunks; -} - -// Chop a SpdyFrame into an array of MockWrites. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const SpdyFrame& frame, int num_chunks) { - return ChopWriteFrame(frame.data(), frame.size(), num_chunks); -} - -// Chop a frame into an array of MockReads. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const char* data, int length, int num_chunks) { - MockRead* chunks = new MockRead[num_chunks]; - int chunk_size = length / num_chunks; - for (int index = 0; index < num_chunks; index++) { - const char* ptr = data + (index * chunk_size); - if (index == num_chunks - 1) - chunk_size += length % chunk_size; // The last chunk takes the remainder. - chunks[index] = MockRead(ASYNC, ptr, chunk_size); - } - return chunks; -} - -// Chop a SpdyFrame into an array of MockReads. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks) { - return ChopReadFrame(frame.data(), frame.size(), num_chunks); -} - -// Adds headers and values to a map. -// |extra_headers| is an array of { name, value } pairs, arranged as strings -// where the even entries are the header names, and the odd entries are the -// header values. -// |headers| gets filled in from |extra_headers|. -void AppendToHeaderBlock(const char* const extra_headers[], - int extra_header_count, - SpdyHeaderBlock* headers) { - std::string this_header; - std::string this_value; - - if (!extra_header_count) - return; - - // Sanity check: Non-NULL header list. - DCHECK(NULL != extra_headers) << "NULL header value pair list"; - // Sanity check: Non-NULL header map. - DCHECK(NULL != headers) << "NULL header map"; - // Copy in the headers. - for (int i = 0; i < extra_header_count; i++) { - // Sanity check: Non-empty header. - DCHECK_NE('\0', *extra_headers[i * 2]) << "Empty header value pair"; - this_header = extra_headers[i * 2]; - std::string::size_type header_len = this_header.length(); - if (!header_len) - continue; - this_value = extra_headers[1 + (i * 2)]; - std::string new_value; - if (headers->find(this_header) != headers->end()) { - // More than one entry in the header. - // Don't add the header again, just the append to the value, - // separated by a NULL character. - - // Adjust the value. - new_value = (*headers)[this_header]; - // Put in a NULL separator. - new_value.append(1, '\0'); - // Append the new value. - new_value += this_value; - } else { - // Not a duplicate, just write the value. - new_value = this_value; - } - (*headers)[this_header] = new_value; - } -} - scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlock(base::StringPiece url) { std::string scheme, host, path; ParseUrl(url.data(), &scheme, &host, &path); @@ -209,31 +116,6 @@ scoped_ptr<SpdyHeaderBlock> ConstructPostHeaderBlock(base::StringPiece url, return header_block.Pass(); } -// Writes |val| to a location of size |len|, in big-endian format. -// in the buffer pointed to by |buffer_handle|. -// Updates the |*buffer_handle| pointer by |len| -// Returns the number of bytes written -int AppendToBuffer(int val, - int len, - unsigned char** buffer_handle, - int* buffer_len_remaining) { - if (len <= 0) - return 0; - DCHECK((size_t) len <= sizeof(len)) << "Data length too long for data type"; - DCHECK(NULL != buffer_handle) << "NULL buffer handle"; - DCHECK(NULL != *buffer_handle) << "NULL pointer"; - DCHECK(NULL != buffer_len_remaining) - << "NULL buffer remainder length pointer"; - DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; - for (int i = 0; i < len; i++) { - int shift = (8 * (len - (i + 1))); - unsigned char val_chunk = (val >> shift) & 0x0FF; - *(*buffer_handle)++ = val_chunk; - *buffer_len_remaining += 1; - } - return len; -} - SpdyFrame* ConstructSpdyFrame(const SpdyHeaderInfo& header_info, scoped_ptr<SpdyHeaderBlock> headers) { BufferedSpdyFramer framer(kSpdyVersion3, header_info.compressed); @@ -850,54 +732,6 @@ int ConstructSpdyReplyString(const char* const extra_headers[], return frame_size; } -// Create a MockWrite from the given SpdyFrame. -MockWrite CreateMockWrite(const SpdyFrame& req) { - return MockWrite(ASYNC, req.data(), req.size()); -} - -// Create a MockWrite from the given SpdyFrame and sequence number. -MockWrite CreateMockWrite(const SpdyFrame& req, int seq) { - return CreateMockWrite(req, seq, ASYNC); -} - -// Create a MockWrite from the given SpdyFrame and sequence number. -MockWrite CreateMockWrite(const SpdyFrame& req, int seq, IoMode mode) { - return MockWrite(mode, req.data(), req.size(), seq); -} - -// Create a MockRead from the given SpdyFrame. -MockRead CreateMockRead(const SpdyFrame& resp) { - return MockRead(ASYNC, resp.data(), resp.size()); -} - -// Create a MockRead from the given SpdyFrame and sequence number. -MockRead CreateMockRead(const SpdyFrame& resp, int seq) { - return CreateMockRead(resp, seq, ASYNC); -} - -// Create a MockRead from the given SpdyFrame and sequence number. -MockRead CreateMockRead(const SpdyFrame& resp, int seq, IoMode mode) { - return MockRead(mode, resp.data(), resp.size(), seq); -} - -// Combines the given SpdyFrames into the given char array and returns -// the total length. -int CombineFrames(const SpdyFrame** frames, int num_frames, - char* buff, int buff_len) { - int total_len = 0; - for (int i = 0; i < num_frames; ++i) { - total_len += frames[i]->size(); - } - DCHECK_LE(total_len, buff_len); - char* ptr = buff; - for (int i = 0; i < num_frames; ++i) { - int len = frames[i]->size(); - memcpy(ptr, frames[i]->data(), len); - ptr += len; - } - return total_len; -} - SpdySessionDependencies::SpdySessionDependencies() : host_resolver(new MockCachingHostResolver), cert_verifier(new MockCertVerifier), diff --git a/net/spdy/spdy_test_util_spdy3.h b/net/spdy/spdy_test_util_spdy3.h index 0a8b24c..fcc1c0d 100644 --- a/net/spdy/spdy_test_util_spdy3.h +++ b/net/spdy/spdy_test_util_spdy3.h @@ -23,6 +23,7 @@ #include "net/proxy/proxy_service.h" #include "net/socket/socket_test_util.h" #include "net/spdy/spdy_session.h" +#include "net/spdy/spdy_test_util_common.h" #include "net/ssl/ssl_config_service_defaults.h" #include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context_storage.h" @@ -35,12 +36,6 @@ namespace net { namespace test_spdy3 { -// Default upload data used by both, mock objects and framer when creating -// data frames. -const char kDefaultURL[] = "http://www.google.com"; -const char kUploadData[] = "hello!"; -const int kUploadDataSize = arraysize(kUploadData)-1; - // NOTE: In GCC, on a Mac, this can't be in an anonymous namespace! // This struct holds information used to construct spdy control and data frames. struct SpdyHeaderInfo { @@ -89,37 +84,6 @@ class MockECSignatureCreatorFactory : public crypto::ECSignatureCreatorFactory { DISALLOW_COPY_AND_ASSIGN(MockECSignatureCreatorFactory); }; -// Chop a frame into an array of MockWrites. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const char* data, int length, int num_chunks); - -// Chop a SpdyFrame into an array of MockWrites. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockWrite* ChopWriteFrame(const SpdyFrame& frame, int num_chunks); - -// Chop a frame into an array of MockReads. -// |data| is the frame to chop. -// |length| is the length of the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const char* data, int length, int num_chunks); - -// Chop a SpdyFrame into an array of MockReads. -// |frame| is the frame to chop. -// |num_chunks| is the number of chunks to create. -MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks); - -// Adds headers and values to a map. -// |extra_headers| is an array of { name, value } pairs, arranged as strings -// where the even entries are the header names, and the odd entries are the -// header values. -// |headers| gets filled in from |extra_headers|. -void AppendHeadersToBlock(const char* const extra_headers[], - int extra_header_count, - SpdyHeaderBlock* headers); - // Constructs a HeaderBlock for the given URL. scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlock(base::StringPiece url); @@ -127,36 +91,6 @@ scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlock(base::StringPiece url); scoped_ptr<SpdyHeaderBlock> ConstructPostHeaderBlock(base::StringPiece url, int64 content_length); -// Writes |str| of the given |len| to the buffer pointed to by |buffer_handle|. -// Uses a template so buffer_handle can be a char* or an unsigned char*. -// Updates the |*buffer_handle| pointer by |len| -// Returns the number of bytes written into *|buffer_handle| -template<class T> -int AppendToBuffer(const char* str, - int len, - T** buffer_handle, - int* buffer_len_remaining) { - DCHECK_GT(len, 0); - DCHECK(NULL != buffer_handle) << "NULL buffer handle"; - DCHECK(NULL != *buffer_handle) << "NULL pointer"; - DCHECK(NULL != buffer_len_remaining) - << "NULL buffer remainder length pointer"; - DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; - memcpy(*buffer_handle, str, len); - *buffer_handle += len; - *buffer_len_remaining -= len; - return len; -} - -// Writes |val| to a location of size |len|, in big-endian format. -// in the buffer pointed to by |buffer_handle|. -// Updates the |*buffer_handle| pointer by |len| -// Returns the number of bytes written -int AppendToBuffer(int val, - int len, - unsigned char** buffer_handle, - int* buffer_len_remaining); - // Construct a SPDY frame. SpdyFrame* ConstructSpdyFrame(const SpdyHeaderInfo& header_info, scoped_ptr<SpdyHeaderBlock> headers); @@ -369,27 +303,6 @@ SpdyFrame* ConstructSpdyBodyFrame(int stream_id, const char* data, SpdyFrame* ConstructWrappedSpdyFrame(const scoped_ptr<SpdyFrame>& frame, int stream_id); -// Create an async MockWrite from the given SpdyFrame. -MockWrite CreateMockWrite(const SpdyFrame& req); - -// Create an async MockWrite from the given SpdyFrame and sequence number. -MockWrite CreateMockWrite(const SpdyFrame& req, int seq); - -MockWrite CreateMockWrite(const SpdyFrame& req, int seq, IoMode mode); - -// Create a MockRead from the given SpdyFrame. -MockRead CreateMockRead(const SpdyFrame& resp); - -// Create a MockRead from the given SpdyFrame and sequence number. -MockRead CreateMockRead(const SpdyFrame& resp, int seq); - -MockRead CreateMockRead(const SpdyFrame& resp, int seq, IoMode mode); - -// Combines the given SpdyFrames into the given char array and returns -// the total length. -int CombineFrames(const SpdyFrame** frames, int num_frames, - char* buff, int buff_len); - // Helper to manage the lifetimes of the dependencies for a // HttpNetworkTransaction. struct SpdySessionDependencies { |