blob: a1565c22326e1c35d2314cafa9b77aa4d362f51b [file] [log] [blame]
Vitaly Bukab5c12da2016-10-19 13:12:41 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef LIBPROTOBUG_MUTATOR_PROTOBUG_MUTATOR_H
16#define LIBPROTOBUG_MUTATOR_PROTOBUG_MUTATOR_H
17
Vitaly Buka0e17fd72016-11-18 10:02:46 -080018#include <random>
Vitaly Buka00b61072016-10-19 16:22:51 -070019
Vitaly Buka0e17fd72016-11-18 10:02:46 -080020namespace google {
21namespace protobuf {
Vitaly Bukab5c12da2016-10-19 13:12:41 -070022class Message;
Vitaly Buka00b61072016-10-19 16:22:51 -070023class FieldDescriptor;
24class EnumValueDescriptor;
Vitaly Buka0e17fd72016-11-18 10:02:46 -080025}
26}
Vitaly Bukab5c12da2016-10-19 13:12:41 -070027
28class ProtobufMutator {
29 public:
Vitaly Buka0e17fd72016-11-18 10:02:46 -080030 ProtobufMutator(uint32_t seed, bool always_initialized);
Vitaly Buka00b61072016-10-19 16:22:51 -070031 virtual ~ProtobufMutator();
32
Vitaly Buka2cfe02b2016-11-18 16:34:09 -080033 bool Mutate(google::protobuf::Message* message, size_t current_size,
34 size_t max_size);
Vitaly Buka0e17fd72016-11-18 10:02:46 -080035 bool CrossOver(const google::protobuf::Message& with,
36 google::protobuf::Message* message);
Vitaly Bukab5c12da2016-10-19 13:12:41 -070037
Vitaly Buka0e17fd72016-11-18 10:02:46 -080038 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
39 int32_t* value);
40 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
41 int64_t* value);
42 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
43 uint32_t* value);
44 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
45 uint64_t* value);
46 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
47 double* value);
48 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
49 float* value);
50 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
51 bool* value);
52 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
53 const google::protobuf::EnumValueDescriptor** value);
54 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
55 google::protobuf::Message* value);
Vitaly Buka00b61072016-10-19 16:22:51 -070056
Vitaly Buka13245af2016-11-18 13:20:12 -080057 void InitializeMessage(google::protobuf::Message* message);
58
Vitaly Buka2cfe02b2016-11-18 16:34:09 -080059 virtual size_t Mutate(void* data, size_t size, size_t max_size);
60
Vitaly Bukab5c12da2016-10-19 13:12:41 -070061 private:
Vitaly Buka0e17fd72016-11-18 10:02:46 -080062 size_t GetRandomIndex(size_t count);
63
64 bool always_initialized_ = true;
65 std::mt19937_64 rng_;
Vitaly Bukab5c12da2016-10-19 13:12:41 -070066};
67
68#endif // LIBPROTOBUG_MUTATOR_PROTOBUG_MUTATOR_H