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 BASE_TEST_FUZZED_DATA_PROVIDER_H_ | 5 #ifndef BASE_TEST_FUZZED_DATA_PROVIDER_H_ |
6 #define BASE_TEST_FUZZED_DATA_PROVIDER_H_ | 6 #define BASE_TEST_FUZZED_DATA_PROVIDER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <string> |
| 11 |
10 #include "base/base_export.h" | 12 #include "base/base_export.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 | 17 |
16 // Utility class to break up fuzzer input for multiple consumers. Whenever run | 18 // Utility class to break up fuzzer input for multiple consumers. Whenever run |
17 // on the same input, provides the same output, as long as its methods are | 19 // on the same input, provides the same output, as long as its methods are |
18 // called in the same order, with the same arguments. | 20 // called in the same order, with the same arguments. |
19 class FuzzedDataProvider { | 21 class FuzzedDataProvider { |
20 public: | 22 public: |
21 // |data| is an array of length |size| that the FuzzedDataProvider wraps to | 23 // |data| is an array of length |size| that the FuzzedDataProvider wraps to |
22 // provide more granular access. |data| must outlive the FuzzedDataProvider. | 24 // provide more granular access. |data| must outlive the FuzzedDataProvider. |
23 FuzzedDataProvider(const uint8_t* data, size_t size); | 25 FuzzedDataProvider(const uint8_t* data, size_t size); |
24 ~FuzzedDataProvider(); | 26 ~FuzzedDataProvider(); |
25 | 27 |
26 // Returns a StringPiece containing |num_bytes| of input data. If fewer than | 28 // Returns a std::string containing |num_bytes| of input data. If fewer than |
27 // |num_bytes| of data remain, returns a shorter StringPiece containing all | 29 // |num_bytes| of data remain, returns a shorter std::string containing all |
28 // of the data that's left. The data pointed at by the returned StringPiece | 30 // of the data that's left. |
29 // must not be used after the FuzzedDataProvider is destroyed. | 31 std::string ConsumeBytes(size_t num_bytes); |
30 StringPiece ConsumeBytes(size_t num_bytes); | |
31 | 32 |
32 // Returns a StringPiece containing all remaining bytes of the input data. | 33 // Returns a std::string containing all remaining bytes of the input data. |
33 // The data pointed at by the returned StringPiece must not be used after the | 34 std::string ConsumeRemainingBytes(); |
34 // FuzzedDataProvider is destroyed. | |
35 StringPiece ConsumeRemainingBytes(); | |
36 | 35 |
37 // Returns a number in the range [min, max] by consuming bytes from the input | 36 // Returns a number in the range [min, max] by consuming bytes from the input |
38 // data. The value might not be uniformly distributed in the given range. If | 37 // data. The value might not be uniformly distributed in the given range. If |
39 // there's no input data left, always returns |min|. |min| must be less than | 38 // there's no input data left, always returns |min|. |min| must be less than |
40 // or equal to |max|. | 39 // or equal to |max|. |
41 uint32_t ConsumeUint32InRange(uint32_t min, uint32_t max); | 40 uint32_t ConsumeUint32InRange(uint32_t min, uint32_t max); |
42 int ConsumeInt32InRange(int min, int max); | 41 int ConsumeInt32InRange(int min, int max); |
43 | 42 |
44 // Returns a bool, or false when no data remains. | 43 // Returns a bool, or false when no data remains. |
45 bool ConsumeBool(); | 44 bool ConsumeBool(); |
(...skipping 20 matching lines...) Expand all Loading... |
66 | 65 |
67 private: | 66 private: |
68 StringPiece remaining_data_; | 67 StringPiece remaining_data_; |
69 | 68 |
70 DISALLOW_COPY_AND_ASSIGN(FuzzedDataProvider); | 69 DISALLOW_COPY_AND_ASSIGN(FuzzedDataProvider); |
71 }; | 70 }; |
72 | 71 |
73 } // namespace base | 72 } // namespace base |
74 | 73 |
75 #endif // BASE_TEST_FUZZED_DATA_PROVIDER_H_ | 74 #endif // BASE_TEST_FUZZED_DATA_PROVIDER_H_ |
OLD | NEW |