Vitaly Buka | 00b6107 | 2016-10-19 16:22:51 -0700 | [diff] [blame] | 1 | // 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 | |
Vitaly Buka | 9dd2f8e | 2017-01-13 00:48:31 -0800 | [diff] [blame] | 15 | #include "src/protobuf_mutator.h" |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 16 | |
Vitaly Buka | f047a00 | 2017-01-12 23:57:00 -0800 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <set> |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 19 | #include <string> |
Vitaly Buka | f047a00 | 2017-01-12 23:57:00 -0800 | [diff] [blame] | 20 | #include <utility> |
| 21 | #include <vector> |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 22 | |
Vitaly Buka | 35df2e4 | 2017-02-25 01:17:25 -0800 | [diff] [blame] | 23 | #include "port/gtest.h" |
Vitaly Buka | 5b1a003 | 2017-02-17 00:06:06 -0800 | [diff] [blame] | 24 | #include "src/protobuf_mutator.pb.h" |
Vitaly Buka | 1109c00 | 2016-10-20 17:25:19 -0700 | [diff] [blame] | 25 | |
Vitaly Buka | f86815c | 2017-02-27 14:19:19 -0800 | [diff] [blame] | 26 | namespace protobuf_mutator { |
| 27 | |
Vitaly Buka | 6c6dbbe | 2017-02-22 13:58:24 -0800 | [diff] [blame] | 28 | using protobuf::TextFormat; |
| 29 | using protobuf::util::MessageDifferencer; |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 30 | using testing::TestWithParam; |
| 31 | using testing::ValuesIn; |
| 32 | using testing::WithParamInterface; |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 33 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 34 | const char kMessages[] = R"( |
| 35 | required_msg {} |
| 36 | optional_msg {} |
| 37 | repeated_msg {} |
| 38 | repeated_msg {required_sint32: 56} |
| 39 | repeated_msg {} |
| 40 | repeated_msg { |
| 41 | required_msg {} |
| 42 | optional_msg {} |
| 43 | repeated_msg {} |
| 44 | repeated_msg { required_int32: 67 } |
| 45 | repeated_msg {} |
| 46 | } |
| 47 | )"; |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 48 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 49 | const char kRequiredFields[] = R"( |
| 50 | required_double: 1.26685288449177e-313 |
| 51 | required_float: 5.9808638e-39 |
| 52 | required_int32: 67 |
| 53 | required_int64: 5285068 |
| 54 | required_uint32: 14486213 |
| 55 | required_uint64: 520229415 |
| 56 | required_sint32: 56 |
| 57 | required_sint64: -6057486163525532641 |
| 58 | required_fixed32: 8812173 |
| 59 | required_fixed64: 273731277756 |
| 60 | required_sfixed32: 43142 |
| 61 | required_sfixed64: 132 |
| 62 | required_bool: false |
| 63 | required_string: "qwert" |
| 64 | required_bytes: "asdf" |
| 65 | )"; |
| 66 | |
| 67 | const char kOptionalFields[] = R"( |
| 68 | optional_double: 1.93177850152856e-314 |
| 69 | optional_float: 4.7397519e-41 |
| 70 | optional_int32: 40020 |
| 71 | optional_int64: 10 |
| 72 | optional_uint32: 40 |
| 73 | optional_uint64: 159 |
| 74 | optional_sint32: 44015 |
| 75 | optional_sint64: 17493625000076 |
| 76 | optional_fixed32: 193 |
| 77 | optional_fixed64: 8542688694448488723 |
| 78 | optional_sfixed32: 4926 |
| 79 | optional_sfixed64: 60 |
| 80 | optional_bool: false |
| 81 | optional_string: "QWERT" |
| 82 | optional_bytes: "ASDF" |
| 83 | optional_enum: ENUM_5 |
| 84 | )"; |
| 85 | |
| 86 | const char kRepeatedFields[] = R"( |
| 87 | repeated_double: 1.93177850152856e-314 |
| 88 | repeated_double: 1.26685288449177e-313 |
| 89 | repeated_float: 4.7397519e-41 |
| 90 | repeated_float: 5.9808638e-39 |
| 91 | repeated_int32: 40020 |
| 92 | repeated_int32: 67 |
| 93 | repeated_int64: 10 |
| 94 | repeated_int64: 5285068 |
| 95 | repeated_uint32: 40 |
| 96 | repeated_uint32: 14486213 |
| 97 | repeated_uint64: 159 |
| 98 | repeated_uint64: 520229415 |
| 99 | repeated_sint32: 44015 |
| 100 | repeated_sint32: 56 |
| 101 | repeated_sint64: 17493625000076 |
| 102 | repeated_sint64: -6057486163525532641 |
| 103 | repeated_fixed32: 193 |
| 104 | repeated_fixed32: 8812173 |
| 105 | repeated_fixed64: 8542688694448488723 |
| 106 | repeated_fixed64: 273731277756 |
| 107 | repeated_sfixed32: 4926 |
| 108 | repeated_sfixed32: 43142 |
| 109 | repeated_sfixed64: 60 |
| 110 | repeated_sfixed64: 132 |
| 111 | repeated_bool: false |
| 112 | repeated_bool: true |
| 113 | repeated_string: "QWERT" |
| 114 | repeated_string: "qwert" |
| 115 | repeated_bytes: "ASDF" |
| 116 | repeated_bytes: "asdf" |
| 117 | repeated_enum: ENUM_5 |
| 118 | repeated_enum: ENUM_4 |
| 119 | )"; |
| 120 | |
| 121 | const char kRequiredNestedFields[] = R"( |
| 122 | required_int32: 123 |
| 123 | optional_msg { |
| 124 | required_double: 1.26685288449177e-313 |
| 125 | required_float: 5.9808638e-39 |
| 126 | required_int32: 67 |
| 127 | required_int64: 5285068 |
| 128 | required_uint32: 14486213 |
| 129 | required_uint64: 520229415 |
| 130 | required_sint32: 56 |
| 131 | required_sint64: -6057486163525532641 |
| 132 | required_fixed32: 8812173 |
| 133 | required_fixed64: 273731277756 |
| 134 | required_sfixed32: 43142 |
| 135 | required_sfixed64: 132 |
| 136 | required_bool: false |
| 137 | required_string: "qwert" |
| 138 | required_bytes: "asdf" |
| 139 | } |
| 140 | )"; |
| 141 | |
| 142 | const char kOptionalNestedFields[] = R"( |
| 143 | required_int32: 123 |
| 144 | optional_msg { |
| 145 | optional_double: 1.93177850152856e-314 |
| 146 | optional_float: 4.7397519e-41 |
| 147 | optional_int32: 40020 |
| 148 | optional_int64: 10 |
| 149 | optional_uint32: 40 |
| 150 | optional_uint64: 159 |
| 151 | optional_sint32: 44015 |
| 152 | optional_sint64: 17493625000076 |
| 153 | optional_fixed32: 193 |
| 154 | optional_fixed64: 8542688694448488723 |
| 155 | optional_sfixed32: 4926 |
| 156 | optional_sfixed64: 60 |
| 157 | optional_bool: false |
| 158 | optional_string: "QWERT" |
| 159 | optional_bytes: "ASDF" |
| 160 | optional_enum: ENUM_5 |
| 161 | } |
| 162 | )"; |
| 163 | |
| 164 | const char kRepeatedNestedFields[] = R"( |
| 165 | required_int32: 123 |
| 166 | optional_msg { |
| 167 | repeated_double: 1.93177850152856e-314 |
| 168 | repeated_double: 1.26685288449177e-313 |
| 169 | repeated_float: 4.7397519e-41 |
| 170 | repeated_float: 5.9808638e-39 |
| 171 | repeated_int32: 40020 |
| 172 | repeated_int32: 67 |
| 173 | repeated_int64: 10 |
| 174 | repeated_int64: 5285068 |
| 175 | repeated_uint32: 40 |
| 176 | repeated_uint32: 14486213 |
| 177 | repeated_uint64: 159 |
| 178 | repeated_uint64: 520229415 |
| 179 | repeated_sint32: 44015 |
| 180 | repeated_sint32: 56 |
| 181 | repeated_sint64: 17493625000076 |
| 182 | repeated_sint64: -6057486163525532641 |
| 183 | repeated_fixed32: 193 |
| 184 | repeated_fixed32: 8812173 |
| 185 | repeated_fixed64: 8542688694448488723 |
| 186 | repeated_fixed64: 273731277756 |
| 187 | repeated_sfixed32: 4926 |
| 188 | repeated_sfixed32: 43142 |
| 189 | repeated_sfixed64: 60 |
| 190 | repeated_sfixed64: 132 |
| 191 | repeated_bool: false |
| 192 | repeated_bool: true |
| 193 | repeated_string: "QWERT" |
| 194 | repeated_string: "qwert" |
| 195 | repeated_bytes: "ASDF" |
| 196 | repeated_bytes: "asdf" |
| 197 | repeated_enum: ENUM_5 |
| 198 | repeated_enum: ENUM_4 |
| 199 | } |
| 200 | )"; |
| 201 | |
| 202 | class TestProtobufMutator : public ProtobufMutator { |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 203 | public: |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 204 | explicit TestProtobufMutator(bool keep_initialized) |
Vitaly Buka | ba12972 | 2016-12-14 17:29:15 -0800 | [diff] [blame] | 205 | : ProtobufMutator(17), random_(13) { |
| 206 | keep_initialized_ = keep_initialized; |
| 207 | } |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 208 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 209 | float MutateFloat(float value) override { |
| 210 | // Hack for tests. It's hard compare reals generated using random mutations. |
| 211 | return std::uniform_int_distribution<uint8_t>(-10, 10)(random_); |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 214 | double MutateDouble(double value) override { return MutateFloat(value); } |
| 215 | |
| 216 | private: |
| 217 | RandomEngine random_; |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 220 | std::vector<std::string> Split(const std::string& str) { |
| 221 | std::istringstream iss(str); |
| 222 | std::vector<std::string> result; |
| 223 | for (std::string line; std::getline(iss, line, '\n');) result.push_back(line); |
| 224 | return result; |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 227 | std::vector<std::pair<const char*, size_t>> GetFieldTestParams( |
| 228 | const std::vector<const char*>& tests) { |
| 229 | std::vector<std::pair<const char*, size_t>> results; |
| 230 | for (auto t : tests) { |
| 231 | auto lines = Split(t); |
| 232 | for (size_t i = 0; i != lines.size(); ++i) { |
| 233 | if (lines[i].find(':') != std::string::npos) results.push_back({t, i}); |
Vitaly Buka | 781853c | 2016-11-21 23:09:35 -0800 | [diff] [blame] | 234 | } |
Vitaly Buka | 781853c | 2016-11-21 23:09:35 -0800 | [diff] [blame] | 235 | } |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 236 | return results; |
Vitaly Buka | 00b6107 | 2016-10-19 16:22:51 -0700 | [diff] [blame] | 237 | } |
Vitaly Buka | ac2b7bf | 2016-11-21 14:07:02 -0800 | [diff] [blame] | 238 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 239 | std::vector<std::pair<const char*, size_t>> GetMessageTestParams( |
| 240 | const std::vector<const char*>& tests) { |
| 241 | std::vector<std::pair<const char*, size_t>> results; |
| 242 | for (auto t : tests) { |
| 243 | auto lines = Split(t); |
| 244 | for (size_t i = 0; i != lines.size(); ++i) { |
| 245 | if (lines[i].find("{}") != std::string::npos) results.push_back({t, i}); |
| 246 | } |
| 247 | } |
| 248 | return results; |
| 249 | } |
Vitaly Buka | ac2b7bf | 2016-11-21 14:07:02 -0800 | [diff] [blame] | 250 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 251 | void LoadMessage(const std::string& text_message, protobuf::Message* message) { |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 252 | message->Clear(); |
| 253 | TextFormat::Parser parser; |
| 254 | parser.AllowPartialMessage(true); |
| 255 | EXPECT_TRUE(parser.ParseFromString(text_message, message)); |
| 256 | } |
Vitaly Buka | ac2b7bf | 2016-11-21 14:07:02 -0800 | [diff] [blame] | 257 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 258 | bool LoadWithoutLine(const std::string& text_message, size_t line, |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 259 | protobuf::Message* message) { |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 260 | std::ostringstream oss; |
| 261 | auto lines = Split(text_message); |
| 262 | for (size_t i = 0; i != lines.size(); ++i) { |
| 263 | if (i != line) oss << lines[i] << '\n'; |
| 264 | } |
| 265 | message->Clear(); |
| 266 | TextFormat::Parser parser; |
| 267 | parser.AllowPartialMessage(true); |
| 268 | return parser.ParseFromString(oss.str(), message); |
| 269 | } |
Vitaly Buka | ac2b7bf | 2016-11-21 14:07:02 -0800 | [diff] [blame] | 270 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 271 | bool LoadWithChangedLine(const std::string& text_message, size_t line, |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 272 | protobuf::Message* message, int value) { |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 273 | auto lines = Split(text_message); |
| 274 | std::ostringstream oss; |
| 275 | for (size_t i = 0; i != lines.size(); ++i) { |
| 276 | if (i != line) { |
| 277 | oss << lines[i] << '\n'; |
| 278 | } else { |
| 279 | std::string s = lines[i]; |
| 280 | s.resize(s.find(':') + 2); |
Vitaly Buka | 781853c | 2016-11-21 23:09:35 -0800 | [diff] [blame] | 281 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 282 | if (lines[i].back() == '\"') { |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 283 | // strings |
| 284 | s += value ? "\"\\" + std::to_string(value) + "\"" : "\"\""; |
| 285 | } else if (lines[i].back() == 'e') { |
| 286 | // bools |
| 287 | s += value ? "true" : "false"; |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 288 | } else { |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 289 | s += std::to_string(value); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 290 | } |
| 291 | oss << s << '\n'; |
| 292 | } |
| 293 | } |
| 294 | message->Clear(); |
| 295 | TextFormat::Parser parser; |
| 296 | parser.AllowPartialMessage(true); |
| 297 | return parser.ParseFromString(oss.str(), message); |
| 298 | } |
Vitaly Buka | 781853c | 2016-11-21 23:09:35 -0800 | [diff] [blame] | 299 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 300 | template <class Message> |
| 301 | bool Mutate(const Message& from, const Message& to) { |
Vitaly Buka | 43f748f | 2017-01-05 20:59:32 -0800 | [diff] [blame] | 302 | EXPECT_FALSE(MessageDifferencer::Equals(from, to)); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 303 | |
| 304 | TestProtobufMutator mutator(false); |
| 305 | |
| 306 | for (int j = 0; j < 1000000; ++j) { |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 307 | Message message; |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 308 | message.CopyFrom(from); |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 309 | mutator.Mutate(&message, 1000); |
Vitaly Buka | 43f748f | 2017-01-05 20:59:32 -0800 | [diff] [blame] | 310 | if (MessageDifferencer::Equals(message, to)) return true; |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | class ProtobufMutatorTest { |
| 316 | protected: |
| 317 | std::string test_message_; |
| 318 | size_t field_; |
| 319 | Msg from_; |
| 320 | Msg to_; |
| 321 | }; |
| 322 | |
| 323 | class ProtobufMutatorFieldTest |
| 324 | : public ProtobufMutatorTest, |
| 325 | public TestWithParam<std::pair<const char*, size_t>> { |
| 326 | protected: |
| 327 | void SetUp() override { |
| 328 | test_message_ = GetParam().first; |
| 329 | field_ = GetParam().second; |
| 330 | } |
| 331 | }; |
| 332 | |
| 333 | INSTANTIATE_TEST_CASE_P(AllTest, ProtobufMutatorFieldTest, |
| 334 | ValuesIn(GetFieldTestParams( |
| 335 | {kRequiredFields, kOptionalFields, kRepeatedFields, |
| 336 | kRequiredNestedFields, kOptionalNestedFields, |
| 337 | kRepeatedNestedFields}))); |
| 338 | |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 339 | TEST_P(ProtobufMutatorFieldTest, Initialized) { |
| 340 | LoadWithoutLine(test_message_, field_, &from_); |
| 341 | TestProtobufMutator mutator(true); |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 342 | mutator.Mutate(&from_, 1000); |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 343 | EXPECT_TRUE(from_.IsInitialized()); |
| 344 | } |
| 345 | |
| 346 | TEST_P(ProtobufMutatorFieldTest, DeleteField) { |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 347 | LoadMessage(test_message_, &from_); |
| 348 | LoadWithoutLine(test_message_, field_, &to_); |
| 349 | EXPECT_TRUE(Mutate(from_, to_)); |
| 350 | } |
| 351 | |
| 352 | TEST_P(ProtobufMutatorFieldTest, InsertField) { |
| 353 | LoadWithoutLine(test_message_, field_, &from_); |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 354 | LoadWithChangedLine(test_message_, field_, &to_, 0); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 355 | EXPECT_TRUE(Mutate(from_, to_)); |
| 356 | } |
| 357 | |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 358 | TEST_P(ProtobufMutatorFieldTest, ChangeField) { |
| 359 | LoadWithChangedLine(test_message_, field_, &from_, 0); |
| 360 | LoadWithChangedLine(test_message_, field_, &to_, 1); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 361 | EXPECT_TRUE(Mutate(from_, to_)); |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 362 | EXPECT_TRUE(Mutate(to_, from_)); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 365 | TEST_P(ProtobufMutatorFieldTest, CopyField) { |
| 366 | Msg msg1; |
| 367 | LoadWithChangedLine(test_message_, field_, &msg1, 7); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 368 | |
Vitaly Buka | a3e59c7 | 2016-12-06 16:53:56 -0800 | [diff] [blame] | 369 | Msg msg2; |
| 370 | LoadWithChangedLine(test_message_, field_, &msg2, 0); |
| 371 | |
| 372 | from_.add_repeated_msg()->CopyFrom(msg1); |
| 373 | from_.add_repeated_msg()->CopyFrom(msg2); |
| 374 | |
| 375 | to_.add_repeated_msg()->CopyFrom(msg1); |
| 376 | to_.add_repeated_msg()->CopyFrom(msg1); |
| 377 | EXPECT_TRUE(Mutate(from_, to_)); |
| 378 | |
| 379 | to_.Clear(); |
| 380 | to_.add_repeated_msg()->CopyFrom(msg2); |
| 381 | to_.add_repeated_msg()->CopyFrom(msg2); |
| 382 | EXPECT_TRUE(Mutate(from_, to_)); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 385 | class ProtobufMutatorSingleFieldTest : public ProtobufMutatorFieldTest {}; |
| 386 | |
| 387 | INSTANTIATE_TEST_CASE_P(AllTest, ProtobufMutatorSingleFieldTest, |
| 388 | ValuesIn(GetFieldTestParams({ |
| 389 | kRequiredFields, kOptionalFields, |
| 390 | kRequiredNestedFields, kOptionalNestedFields, |
| 391 | }))); |
| 392 | |
| 393 | TEST_P(ProtobufMutatorSingleFieldTest, CrossOver) { |
| 394 | LoadWithoutLine(test_message_, field_, &from_); |
| 395 | LoadMessage(test_message_, &to_); |
| 396 | |
| 397 | EXPECT_FALSE(MessageDifferencer::Equals(from_, to_)); |
| 398 | TestProtobufMutator mutator(false); |
| 399 | |
| 400 | int match_from_ = 0; |
| 401 | int match_to_ = 0; |
| 402 | int iterations = 1000; |
| 403 | for (int j = 0; j < iterations; ++j) { |
| 404 | Msg message; |
| 405 | message.CopyFrom(from_); |
| 406 | mutator.CrossOver(to_, &message); |
| 407 | if (MessageDifferencer::Equals(message, to_)) ++match_to_; |
| 408 | if (MessageDifferencer::Equals(message, from_)) ++match_from_; |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 409 | } |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 410 | |
| 411 | EXPECT_LT(iterations * .4, match_from_); |
| 412 | EXPECT_GE(iterations * .6, match_from_); |
| 413 | EXPECT_LT(iterations * .4, match_to_); |
| 414 | EXPECT_GE(iterations * .6, match_to_); |
| 415 | } |
| 416 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 417 | template <typename T> |
| 418 | class ProtobufMutatorTypedTest : public ::testing::Test { |
| 419 | public: |
| 420 | using Message = T; |
| 421 | }; |
| 422 | |
| 423 | using ProtobufMutatorTypedTestTypes = testing::Types<Msg>; |
| 424 | TYPED_TEST_CASE(ProtobufMutatorTypedTest, ProtobufMutatorTypedTestTypes); |
| 425 | |
| 426 | TYPED_TEST(ProtobufMutatorTypedTest, CrossOverRepeated) { |
| 427 | typename TestFixture::Message m1; |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 428 | m1.add_repeated_int32(1); |
| 429 | m1.add_repeated_int32(2); |
| 430 | m1.add_repeated_int32(3); |
| 431 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 432 | typename TestFixture::Message m2; |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 433 | m2.add_repeated_int32(4); |
| 434 | m2.add_repeated_int32(5); |
| 435 | m2.add_repeated_int32(6); |
| 436 | |
| 437 | int iterations = 10000; |
| 438 | std::set<std::set<int>> sets; |
| 439 | TestProtobufMutator mutator(false); |
| 440 | for (int j = 0; j < iterations; ++j) { |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 441 | typename TestFixture::Message message; |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 442 | message.CopyFrom(m1); |
| 443 | mutator.CrossOver(m2, &message); |
| 444 | sets.insert( |
| 445 | {message.repeated_int32().begin(), message.repeated_int32().end()}); |
| 446 | } |
| 447 | |
| 448 | EXPECT_EQ(1 << 6, sets.size()); |
| 449 | } |
| 450 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 451 | TYPED_TEST(ProtobufMutatorTypedTest, CrossOverRepeatedMessages) { |
| 452 | typename TestFixture::Message m1; |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 453 | auto* rm1 = m1.add_repeated_msg(); |
| 454 | rm1->add_repeated_int32(1); |
| 455 | rm1->add_repeated_int32(2); |
| 456 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 457 | typename TestFixture::Message m2; |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 458 | auto* rm2 = m2.add_repeated_msg(); |
| 459 | rm2->add_repeated_int32(3); |
| 460 | rm2->add_repeated_int32(4); |
| 461 | rm2->add_repeated_int32(5); |
| 462 | rm2->add_repeated_int32(6); |
| 463 | |
| 464 | int iterations = 10000; |
| 465 | std::set<std::set<int>> sets; |
| 466 | TestProtobufMutator mutator(false); |
| 467 | for (int j = 0; j < iterations; ++j) { |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 468 | typename TestFixture::Message message; |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 469 | message.CopyFrom(m1); |
| 470 | mutator.CrossOver(m2, &message); |
| 471 | for (const auto& msg : message.repeated_msg()) |
| 472 | sets.insert({msg.repeated_int32().begin(), msg.repeated_int32().end()}); |
| 473 | } |
| 474 | |
| 475 | EXPECT_EQ(1 << 6, sets.size()); |
| 476 | } |
| 477 | |
Vitaly Buka | d3a76b8 | 2017-03-04 01:27:22 -0800 | [diff] [blame] | 478 | TYPED_TEST(ProtobufMutatorTypedTest, FailedMutations) { |
Vitaly Buka | 8aa7510 | 2016-12-09 20:01:15 -0800 | [diff] [blame] | 479 | TestProtobufMutator mutator(false); |
Vitaly Buka | d3a76b8 | 2017-03-04 01:27:22 -0800 | [diff] [blame] | 480 | size_t crossovers = 0; |
| 481 | for (int i = 0; i < 10000; ++i) { |
| 482 | typename TestFixture::Message messages[2]; |
| 483 | typename TestFixture::Message tmp; |
Vitaly Buka | 8aa7510 | 2016-12-09 20:01:15 -0800 | [diff] [blame] | 484 | for (int j = 0; j < 20; ++j) { |
Vitaly Buka | d3a76b8 | 2017-03-04 01:27:22 -0800 | [diff] [blame] | 485 | for (auto& m : messages) { |
| 486 | tmp.CopyFrom(m); |
| 487 | mutator.Mutate(&m, 1000); |
| 488 | // Mutate must not produce the same result. |
| 489 | EXPECT_FALSE(MessageDifferencer::Equals(m, tmp)); |
| 490 | } |
Vitaly Buka | 8aa7510 | 2016-12-09 20:01:15 -0800 | [diff] [blame] | 491 | } |
Vitaly Buka | d3a76b8 | 2017-03-04 01:27:22 -0800 | [diff] [blame] | 492 | |
| 493 | tmp.CopyFrom(messages[1]); |
| 494 | mutator.CrossOver(messages[0], &tmp); |
| 495 | if (MessageDifferencer::Equals(tmp, messages[1])) ++crossovers; |
Vitaly Buka | 8aa7510 | 2016-12-09 20:01:15 -0800 | [diff] [blame] | 496 | } |
Vitaly Buka | d3a76b8 | 2017-03-04 01:27:22 -0800 | [diff] [blame] | 497 | |
| 498 | // CrossOver may fail but very rare. |
| 499 | EXPECT_LT(crossovers, 100); |
Vitaly Buka | 8aa7510 | 2016-12-09 20:01:15 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 502 | TYPED_TEST(ProtobufMutatorTypedTest, Size) { |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 503 | const size_t kIterations = 10000; |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 504 | auto loop = [&](bool control) { |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 505 | typename TestFixture::Message message; |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 506 | std::string str; |
| 507 | TestProtobufMutator mutator(false); |
| 508 | const size_t kTargetSize = 1000; |
| 509 | size_t overflows = 0; |
| 510 | for (size_t j = 0; j < kIterations; ++j) { |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 511 | typename TestFixture::Message message2; |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 512 | message2.CopyFrom(message); |
| 513 | mutator.Mutate(&message2, |
| 514 | control ? kTargetSize - std::min(str.size(), kTargetSize) |
| 515 | : kTargetSize); |
Vitaly Buka | f86815c | 2017-02-27 14:19:19 -0800 | [diff] [blame] | 516 | std::string str2 = MessageToTextString(message2); |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 517 | if (str2.size() > kTargetSize) { |
| 518 | ++overflows; |
| 519 | } else if (str2.size() > kTargetSize - 150 || str2.size() > str.size()) { |
| 520 | message.CopyFrom(message2); |
| 521 | str = str2; |
| 522 | } |
| 523 | } |
| 524 | return overflows; |
| 525 | }; |
| 526 | |
Vitaly Buka | 4782c14 | 2017-03-04 00:12:32 -0800 | [diff] [blame] | 527 | EXPECT_GT(loop(false), kIterations * 0.1); |
| 528 | EXPECT_LE(loop(true), kIterations * 0.1); |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Vitaly Buka | 89098ff | 2017-03-03 14:48:04 -0800 | [diff] [blame] | 531 | class ProtobufMutatorMessagesTest : public ProtobufMutatorFieldTest {}; |
| 532 | |
| 533 | INSTANTIATE_TEST_CASE_P(AllTest, ProtobufMutatorMessagesTest, |
| 534 | ValuesIn(GetMessageTestParams({kMessages}))); |
| 535 | |
| 536 | TEST_P(ProtobufMutatorMessagesTest, DeletedMessage) { |
| 537 | LoadMessage(test_message_, &from_); |
| 538 | LoadWithoutLine(test_message_, field_, &to_); |
| 539 | EXPECT_TRUE(Mutate(from_, to_)); |
| 540 | } |
| 541 | |
| 542 | TEST_P(ProtobufMutatorMessagesTest, InsertMessage) { |
| 543 | LoadWithoutLine(test_message_, field_, &from_); |
| 544 | LoadMessage(test_message_, &to_); |
| 545 | EXPECT_TRUE(Mutate(from_, to_)); |
| 546 | } |
| 547 | |
Vitaly Buka | 432b545 | 2016-12-09 14:42:09 -0800 | [diff] [blame] | 548 | // TODO(vitalybuka): Special tests for oneof. |
| 549 | |
Vitaly Buka | 482c172 | 2016-12-14 14:48:32 -0800 | [diff] [blame] | 550 | TEST(ProtobufMutatorMessagesTest, UsageExample) { |
| 551 | SmallMessage message; |
| 552 | TestProtobufMutator mutator(false); |
| 553 | |
| 554 | // Test that we can generate all variation of the message. |
| 555 | std::set<std::string> mutations; |
| 556 | for (int j = 0; j < 1000; ++j) { |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 557 | mutator.Mutate(&message, 1000); |
Vitaly Buka | f86815c | 2017-02-27 14:19:19 -0800 | [diff] [blame] | 558 | std::string str = MessageToTextString(message); |
Vitaly Buka | 482c172 | 2016-12-14 14:48:32 -0800 | [diff] [blame] | 559 | mutations.insert(str); |
| 560 | } |
| 561 | |
| 562 | // 3 states for boolean and 5 for enum, including missing fields. |
Vitaly Buka | 0e1e46b | 2017-01-12 21:54:25 -0800 | [diff] [blame] | 563 | EXPECT_EQ(3u * 5u, mutations.size()); |
Vitaly Buka | 482c172 | 2016-12-14 14:48:32 -0800 | [diff] [blame] | 564 | } |
Vitaly Buka | e8c8643 | 2016-12-09 19:49:43 -0800 | [diff] [blame] | 565 | |
Vitaly Buka | 432b545 | 2016-12-09 14:42:09 -0800 | [diff] [blame] | 566 | } // namespace protobuf_mutator |