1 // Copyright 2015 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.
7 #include "base/basictypes.h"
8 #include "base/strings/string_util.h"
9 #include "tools/ipc_fuzzer/fuzzer/generator.h"
10 #include "tools/ipc_fuzzer/fuzzer/rand_util.h"
12 namespace ipc_fuzzer
{
15 void GenerateIntegralType(T
* value
) {
16 switch (RandInRange(16)) {
18 *value
= static_cast<T
>(0);
21 *value
= static_cast<T
>(1);
24 *value
= static_cast<T
>(-1);
27 *value
= static_cast<T
>(2);
30 *value
= static_cast<T
>(RandU64());
36 void GenerateFloatingType(T
* value
) {
37 *value
= RandDouble();
41 void GenerateStringType(T
* value
) {
43 size_t length
= RandInRange(300);
44 for (size_t i
= 0; i
< length
; ++i
)
45 temp_string
+= RandInRange(256);
49 void Generator::FuzzBool(bool* value
) {
50 *value
= RandInRange(2) ? true: false;
53 void Generator::FuzzInt(int* value
) {
54 GenerateIntegralType
<int>(value
);
57 void Generator::FuzzLong(long* value
) {
58 GenerateIntegralType
<long>(value
);
61 void Generator::FuzzSize(size_t* value
) {
62 GenerateIntegralType
<size_t>(value
);
65 void Generator::FuzzUChar(unsigned char* value
) {
66 GenerateIntegralType
<unsigned char>(value
);
69 void Generator::FuzzWChar(wchar_t* value
) {
70 GenerateIntegralType
<wchar_t>(value
);
73 void Generator::FuzzUInt16(uint16
* value
) {
74 GenerateIntegralType
<uint16
>(value
);
77 void Generator::FuzzUInt32(uint32
* value
) {
78 GenerateIntegralType
<uint32
>(value
);
81 void Generator::FuzzInt64(int64
* value
) {
82 GenerateIntegralType
<int64
>(value
);
85 void Generator::FuzzUInt64(uint64
* value
) {
86 GenerateIntegralType
<uint64
>(value
);
89 void Generator::FuzzFloat(float* value
) {
90 GenerateFloatingType
<float>(value
);
93 void Generator::FuzzDouble(double* value
) {
94 GenerateFloatingType
<double>(value
);
97 void Generator::FuzzString(std::string
* value
) {
98 GenerateStringType
<std::string
>(value
);
101 void Generator::FuzzString16(base::string16
* value
) {
102 GenerateStringType
<base::string16
>(value
);
105 void Generator::FuzzData(char* data
, int length
) {
106 for (int i
= 0; i
< length
; ++i
) {
107 GenerateIntegralType
<char>(&data
[i
]);
111 void Generator::FuzzBytes(void* data
, int data_len
) {
112 FuzzData(static_cast<char*>(data
), data_len
);
115 bool Generator::ShouldGenerate() {
116 // The generator fuzzer should always generate new values.
120 } // namespace ipc_fuzzer