OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef NET_HTTP2_TOOLS_RANDOM_DECODER_TEST_H_ | 5 #ifndef NET_HTTP2_TOOLS_RANDOM_DECODER_TEST_H_ |
6 #define NET_HTTP2_TOOLS_RANDOM_DECODER_TEST_H_ | 6 #define NET_HTTP2_TOOLS_RANDOM_DECODER_TEST_H_ |
7 | 7 |
8 // RandomDecoderTest is a base class for tests of decoding various kinds | 8 // RandomDecoderTest is a base class for tests of decoding various kinds |
9 // of HTTP/2 and HPACK encodings. | 9 // of HTTP/2 and HPACK encodings. |
10 | 10 |
11 // TODO(jamessynge): Move more methods into .cc file. | 11 // TODO(jamessynge): Move more methods into .cc file. |
12 | 12 |
13 #include <stddef.h> | 13 #include <stddef.h> |
14 | 14 |
15 #include <functional> | 15 #include <functional> |
16 #include <memory> | 16 #include <memory> |
17 #include <string> | 17 #include <string> |
18 #include <type_traits> | 18 #include <type_traits> |
19 | 19 |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/strings/string_piece.h" | 21 #include "base/strings/string_piece.h" |
22 #include "base/template_util.h" | |
23 #include "net/http2/decoder/decode_buffer.h" | 22 #include "net/http2/decoder/decode_buffer.h" |
24 #include "net/http2/decoder/decode_status.h" | 23 #include "net/http2/decoder/decode_status.h" |
25 #include "net/http2/tools/failure.h" | 24 #include "net/http2/tools/failure.h" |
26 #include "net/http2/tools/http2_random.h" | 25 #include "net/http2/tools/http2_random.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
28 | 27 |
29 namespace net { | 28 namespace net { |
30 namespace test { | 29 namespace test { |
31 | 30 |
32 // Some helpers. | 31 // Some helpers. |
33 | 32 |
34 template <typename T, size_t N> | 33 template <typename T, size_t N> |
35 base::StringPiece ToStringPiece(T (&data)[N]) { | 34 base::StringPiece ToStringPiece(T (&data)[N]) { |
36 return base::StringPiece(reinterpret_cast<const char*>(data), N * sizeof(T)); | 35 return base::StringPiece(reinterpret_cast<const char*>(data), N * sizeof(T)); |
37 } | 36 } |
38 | 37 |
39 // strings/hex_ascii_dump.h doesn't support StringPiece args for this case. | 38 // strings/hex_ascii_dump.h doesn't support StringPiece args for this case. |
40 std::string HexEncode(base::StringPiece s); | 39 std::string HexEncode(base::StringPiece s); |
41 | 40 |
42 // Overwrite the enum with some random value, probably not a valid value for | 41 // Overwrite the enum with some random value, probably not a valid value for |
43 // the enum type, but which fits into its storage. | 42 // the enum type, but which fits into its storage. |
44 template <typename T, | 43 template <typename T, |
45 typename E = typename std::enable_if<std::is_enum<T>::value>::type> | 44 typename E = typename std::enable_if<std::is_enum<T>::value>::type> |
46 void CorruptEnum(T* out, RandomBase* rng) { | 45 void CorruptEnum(T* out, RandomBase* rng) { |
47 // Per cppreference.com, if the destination type of a static_cast is | 46 // Per cppreference.com, if the destination type of a static_cast is |
48 // smaller than the source type (i.e. type of r and uint32 below), the | 47 // smaller than the source type (i.e. type of r and uint32 below), the |
49 // resulting value is the smallest unsigned value equal to the source value | 48 // resulting value is the smallest unsigned value equal to the source value |
50 // modulo 2^n, where n is the number of bits used to represent the | 49 // modulo 2^n, where n is the number of bits used to represent the |
51 // destination type unsigned U. | 50 // destination type unsigned U. |
52 typedef typename base::underlying_type<T>::type underlying_type_T; | 51 using underlying_type_T = typename std::underlying_type<T>::type; |
53 typedef typename std::make_unsigned<underlying_type_T>::type | 52 using unsigned_underlying_type_T = |
54 unsigned_underlying_type_T; | 53 typename std::make_unsigned<underlying_type_T>::type; |
55 auto r = static_cast<unsigned_underlying_type_T>(rng->Rand32()); | 54 auto r = static_cast<unsigned_underlying_type_T>(rng->Rand32()); |
56 *out = static_cast<T>(r); | 55 *out = static_cast<T>(r); |
57 } | 56 } |
58 | 57 |
59 // Base class for tests of the ability to decode a sequence of bytes with | 58 // Base class for tests of the ability to decode a sequence of bytes with |
60 // various boundaries between the DecodeBuffers provided to the decoder. | 59 // various boundaries between the DecodeBuffers provided to the decoder. |
61 class RandomDecoderTest : public ::testing::Test { | 60 class RandomDecoderTest : public ::testing::Test { |
62 public: | 61 public: |
63 // SelectSize returns the size of the next DecodeBuffer to be passed to the | 62 // SelectSize returns the size of the next DecodeBuffer to be passed to the |
64 // decoder. Note that RandomDecoderTest allows that size to be zero, though | 63 // decoder. Note that RandomDecoderTest allows that size to be zero, though |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 bool stop_decode_on_done_ = true; | 250 bool stop_decode_on_done_ = true; |
252 | 251 |
253 private: | 252 private: |
254 Http2Random random_; | 253 Http2Random random_; |
255 }; | 254 }; |
256 | 255 |
257 } // namespace test | 256 } // namespace test |
258 } // namespace net | 257 } // namespace net |
259 | 258 |
260 #endif // NET_HTTP2_TOOLS_RANDOM_DECODER_TEST_H_ | 259 #endif // NET_HTTP2_TOOLS_RANDOM_DECODER_TEST_H_ |
OLD | NEW |