blob: fa42518c431f0d780b376e85f20a140b49c68989 [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 Buka0e17fd72016-11-18 10:02:46 -080033 bool Mutate(google::protobuf::Message* proto);
34 bool CrossOver(const google::protobuf::Message& with,
35 google::protobuf::Message* message);
Vitaly Bukab5c12da2016-10-19 13:12:41 -070036
Vitaly Buka0e17fd72016-11-18 10:02:46 -080037 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
38 int32_t* value);
39 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
40 int64_t* value);
41 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
42 uint32_t* value);
43 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
44 uint64_t* value);
45 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
46 double* value);
47 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
48 float* value);
49 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
50 bool* value);
51 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
52 const google::protobuf::EnumValueDescriptor** value);
53 virtual bool MutateField(const google::protobuf::FieldDescriptor& field,
54 google::protobuf::Message* value);
Vitaly Buka00b61072016-10-19 16:22:51 -070055
Vitaly Bukab5c12da2016-10-19 13:12:41 -070056 private:
Vitaly Buka0e17fd72016-11-18 10:02:46 -080057 // Returns true with probability n/m;
58 bool GetRandom(size_t n, size_t m);
59 size_t GetRandomIndex(size_t count);
60
61 bool always_initialized_ = true;
62 std::mt19937_64 rng_;
Vitaly Bukab5c12da2016-10-19 13:12:41 -070063};
64
65#endif // LIBPROTOBUG_MUTATOR_PROTOBUG_MUTATOR_H