File: message_matcher.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (220 lines) | stat: -rw-r--r-- 8,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2018, 2025, Oracle and/or its affiliates.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2.0,
 * as published by the Free Software Foundation.
 *
 * This program is designed to work with certain software (including
 * but not limited to OpenSSL) that is licensed under separate terms,
 * as designated in a particular file or component or in included license
 * documentation.  The authors of MySQL hereby grant you an additional
 * permission to link the program and your derivative works with the
 * separately licensed software that they have either included with
 * the program or referenced in the documentation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License, version 2.0, for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */

#include "plugin/x/tests/driver/common/message_matcher.h"
#include "plugin/x/tests/driver/parsers/message_parser.h"

#include <vector>

#ifdef GetMessage
#undef GetMessage
#endif

using Message = xcl::XProtocol::Message;
using FieldDescriptor = google::protobuf::FieldDescriptor;
using CppType = FieldDescriptor::CppType;

namespace {

int field_size(const Message &msg, const FieldDescriptor *field) {
  return msg.GetReflection()->FieldSize(msg, field);
}

bool has_field(const Message &msg, const FieldDescriptor *field) {
  return msg.GetReflection()->HasField(msg, field);
}

bool match_field_value(const Message &expected_msg, const Message &msg,
                       const FieldDescriptor *field) {
  if (expected_msg.GetTypeName() == Mysqlx::Notice::Frame().GetTypeName() &&
      field->name() == "payload") {
    auto expected_notice =
        reinterpret_cast<const Mysqlx::Notice::Frame *>(&expected_msg);
    auto notice = reinterpret_cast<const Mysqlx::Notice::Frame *>(&msg);

    if (expected_notice->has_type() && notice->has_type() &&
        expected_notice->type() == notice->type()) {
      std::unique_ptr<Message> expected_notice_payload{
          parser::get_notice_message_from_text(
              static_cast<Mysqlx::Notice::Frame_Type>(expected_notice->type()),
              "", nullptr, true)};
      std::unique_ptr<Message> actual_notice_payload{
          parser::get_notice_message_from_text(
              static_cast<Mysqlx::Notice::Frame_Type>(expected_notice->type()),
              "", nullptr, true)};

      if (nullptr == expected_notice_payload.get()) {
        return nullptr == actual_notice_payload.get();
      }

      if (!actual_notice_payload->ParsePartialFromString(notice->payload()))
        return false;

      if (!expected_notice_payload->ParsePartialFromString(
              expected_notice->payload()))
        return false;

      return message_match_with_expectations(*expected_notice_payload,
                                             *actual_notice_payload);
    }
  }
  switch (field->cpp_type()) {
    case CppType::CPPTYPE_BOOL:
      return expected_msg.GetReflection()->GetBool(expected_msg, field) ==
             msg.GetReflection()->GetBool(msg, field);

    case CppType::CPPTYPE_DOUBLE:
      return expected_msg.GetReflection()->GetDouble(expected_msg, field) ==
             msg.GetReflection()->GetDouble(msg, field);

    case CppType::CPPTYPE_FLOAT:
      return expected_msg.GetReflection()->GetFloat(expected_msg, field) ==
             msg.GetReflection()->GetFloat(msg, field);

    case CppType::CPPTYPE_ENUM:
      return expected_msg.GetReflection()
                 ->GetEnum(expected_msg, field)
                 ->name() == msg.GetReflection()->GetEnum(msg, field)->name();

    case CppType::CPPTYPE_INT32:
      return expected_msg.GetReflection()->GetInt32(expected_msg, field) ==
             msg.GetReflection()->GetInt32(msg, field);

    case CppType::CPPTYPE_INT64:
      return expected_msg.GetReflection()->GetInt64(expected_msg, field) ==
             msg.GetReflection()->GetInt64(msg, field);

    case CppType::CPPTYPE_MESSAGE:
      return message_match_with_expectations(
          expected_msg.GetReflection()->GetMessage(expected_msg, field),
          msg.GetReflection()->GetMessage(msg, field));

    case CppType::CPPTYPE_STRING:
      return expected_msg.GetReflection()->GetString(expected_msg, field) ==
             msg.GetReflection()->GetString(msg, field);

    case CppType::CPPTYPE_UINT32:
      return expected_msg.GetReflection()->GetUInt32(expected_msg, field) ==
             msg.GetReflection()->GetUInt32(msg, field);

    case CppType::CPPTYPE_UINT64:
      return expected_msg.GetReflection()->GetUInt64(expected_msg, field) ==
             msg.GetReflection()->GetUInt64(msg, field);

    default:
      return false;
  }
}

bool match_field_value(const Message &expected_msg, const Message &msg,
                       const FieldDescriptor *field, const int index) {
  switch (field->cpp_type()) {
    case CppType::CPPTYPE_BOOL:
      return expected_msg.GetReflection()->GetRepeatedBool(expected_msg, field,
                                                           index) ==
             msg.GetReflection()->GetRepeatedBool(msg, field, index);

    case CppType::CPPTYPE_DOUBLE:
      return expected_msg.GetReflection()->GetRepeatedDouble(expected_msg,
                                                             field, index) ==
             msg.GetReflection()->GetRepeatedDouble(msg, field, index);

    case CppType::CPPTYPE_FLOAT:
      return expected_msg.GetReflection()->GetRepeatedFloat(expected_msg, field,
                                                            index) ==
             msg.GetReflection()->GetRepeatedFloat(msg, field, index);

    case CppType::CPPTYPE_ENUM:
      return expected_msg.GetReflection()
                 ->GetRepeatedEnum(expected_msg, field, index)
                 ->name() ==
             msg.GetReflection()->GetRepeatedEnum(msg, field, index)->name();

    case CppType::CPPTYPE_INT32:
      return expected_msg.GetReflection()->GetRepeatedInt32(expected_msg, field,
                                                            index) ==
             msg.GetReflection()->GetRepeatedInt32(msg, field, index);

    case CppType::CPPTYPE_INT64:
      return expected_msg.GetReflection()->GetRepeatedInt64(expected_msg, field,
                                                            index) ==
             msg.GetReflection()->GetRepeatedInt64(msg, field, index);

    case CppType::CPPTYPE_MESSAGE:
      return message_match_with_expectations(
          expected_msg.GetReflection()->GetRepeatedMessage(expected_msg, field,
                                                           index),
          msg.GetReflection()->GetRepeatedMessage(msg, field, index));

    case CppType::CPPTYPE_STRING:
      return expected_msg.GetReflection()->GetRepeatedString(expected_msg,
                                                             field, index) ==
             msg.GetReflection()->GetRepeatedString(msg, field, index);

    case CppType::CPPTYPE_UINT32:
      return expected_msg.GetReflection()->GetRepeatedUInt32(expected_msg,
                                                             field, index) ==
             msg.GetReflection()->GetRepeatedUInt32(msg, field, index);

    case CppType::CPPTYPE_UINT64:
      return expected_msg.GetReflection()->GetRepeatedUInt64(expected_msg,
                                                             field, index) ==
             msg.GetReflection()->GetRepeatedUInt64(msg, field, index);

    default:
      return false;
  }
}

}  // namespace

bool message_match_with_expectations(const Message &expected_msg,
                                     const Message &msg) {
  std::vector<const FieldDescriptor *> expected_fields;

  if (expected_msg.GetTypeName() != msg.GetTypeName()) return false;

  expected_msg.GetReflection()->ListFields(expected_msg, &expected_fields);

  for (const auto field : expected_fields) {
    if (!field->is_repeated()) {
      if (has_field(expected_msg, field) && !has_field(msg, field))
        return false;

      if (!match_field_value(expected_msg, msg, field)) return false;
    } else {
      const auto expected_size = field_size(expected_msg, field);
      const auto actual_size = field_size(msg, field);

      if (expected_size != actual_size) return false;

      for (int i = 0; i < expected_size; ++i) {
        if (!match_field_value(expected_msg, msg, field, i)) return false;
      }
    }
  }

  return true;
}