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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "sdp/Sdp.h"
#include "sdp/ParsingResultComparer.h"
#include "sdp/SipccSdpParser.h"
#include "sdp/RsdparsaSdpParser.h"
#include "sdp/SdpTelemetry.h"
#include <string>
#include <ostream>
#include <regex>
#include "mozilla/Assertions.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Logging.h"
using mozilla::LogLevel;
static mozilla::LazyLogModule sSdpDiffLogger("sdpdiff_logger");
#define LOGD(msg) MOZ_LOG(sSdpDiffLogger, LogLevel::Debug, msg)
#define LOGE(msg) MOZ_LOG(sSdpDiffLogger, LogLevel::Error, msg)
#define LOG_EXPECT(result, expect, msg) \
{ \
if (((expect) == SdpComparisonResult::Equal) == (result)) { \
LOGD(msg); \
} else { \
LOGE(("UNEXPECTED COMPARISON RESULT: vvvvvv")); \
LOGE(msg); \
} \
}
namespace mozilla {
using AttributeType = SdpAttribute::AttributeType;
template <typename T>
std::string ToString(const T& serializable) {
std::ostringstream os;
os << serializable;
return os.str();
}
bool ParsingResultComparer::Compare(const Results& aResA, const Results& aResB,
const std::string& aOriginalSdp,
const SdpPref::AlternateParseModes& aMode) {
MOZ_ASSERT(aResA, "aResA must not be a nullptr");
MOZ_ASSERT(aResB, "aResB must not be a nullptr");
MOZ_ASSERT(aResA->ParserName() != aResB->ParserName(),
"aResA and aResB must be from different parsers");
SdpTelemetry::RecordCompare(aResA, aResB, aMode);
ParsingResultComparer comparer;
if (!aResA->Sdp() || !aResB->Sdp()) {
return !aResA->Sdp() && !aResB->Sdp();
}
if (SipccSdpParser::IsNamed(aResA->ParserName())) {
MOZ_ASSERT(RsdparsaSdpParser::IsNamed(aResB->ParserName()));
return comparer.Compare(*aResB->Sdp(), *aResA->Sdp(), aOriginalSdp,
SdpComparisonResult::Equal);
}
MOZ_ASSERT(SipccSdpParser::IsNamed(aResB->ParserName()));
MOZ_ASSERT(RsdparsaSdpParser::IsNamed(aResA->ParserName()));
return comparer.Compare(*aResA->Sdp(), *aResB->Sdp(), aOriginalSdp,
SdpComparisonResult::Equal);
}
bool ParsingResultComparer::Compare(const Sdp& rsdparsaSdp, const Sdp& sipccSdp,
const std::string& originalSdp,
const SdpComparisonResult expect) {
mOriginalSdp = originalSdp;
const std::string sipccSdpStr = sipccSdp.ToString();
const std::string rsdparsaSdpStr = rsdparsaSdp.ToString();
bool result = rsdparsaSdpStr == sipccSdpStr;
LOG_EXPECT(result, expect, ("The original sdp: \n%s", mOriginalSdp.c_str()));
if (result) {
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF,
u"serialization_is_equal"_ns, 1);
LOG_EXPECT(result, expect, ("Serialization is equal"));
return result;
}
// Do a deep comparison
result = true;
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF,
u"serialization_is_not_equal"_ns, 1);
LOG_EXPECT(result, expect,
("Serialization is not equal\n"
" --- Sipcc SDP ---\n"
"%s\n"
"--- Rsdparsa SDP ---\n"
"%s\n",
sipccSdpStr.c_str(), rsdparsaSdpStr.c_str()));
const std::string rsdparsaOriginStr = ToString(rsdparsaSdp.GetOrigin());
const std::string sipccOriginStr = ToString(sipccSdp.GetOrigin());
// Compare the session level
if (rsdparsaOriginStr != sipccOriginStr) {
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF, u"o="_ns,
1);
result = false;
LOG_EXPECT(result, expect,
("origin is not equal\nrust origin: %s\nsipcc origin: %s",
rsdparsaOriginStr.c_str(), sipccOriginStr.c_str()));
}
if (MOZ_LOG_TEST(sSdpDiffLogger, LogLevel::Debug)) {
const auto rust_sess_attr_count = rsdparsaSdp.GetAttributeList().Count();
const auto sipcc_sess_attr_count = sipccSdp.GetAttributeList().Count();
if (rust_sess_attr_count != sipcc_sess_attr_count) {
LOG_EXPECT(false, expect,
("Session level attribute count is NOT equal, rsdparsa: %u, "
"sipcc: %u\n",
rust_sess_attr_count, sipcc_sess_attr_count));
}
}
result &= CompareAttrLists(rsdparsaSdp.GetAttributeList(),
sipccSdp.GetAttributeList(), -1);
const uint32_t sipccMediaSecCount =
static_cast<uint32_t>(sipccSdp.GetMediaSectionCount());
const uint32_t rsdparsaMediaSecCount =
static_cast<uint32_t>(rsdparsaSdp.GetMediaSectionCount());
if (sipccMediaSecCount != rsdparsaMediaSecCount) {
result = false;
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF,
u"inequal_msec_count"_ns, 1);
LOG_EXPECT(result, expect,
("Media section count is NOT equal, rsdparsa: %d, sipcc: %d \n",
rsdparsaMediaSecCount, sipccMediaSecCount));
}
for (size_t i = 0; i < std::min(sipccMediaSecCount, rsdparsaMediaSecCount);
i++) {
result &= CompareMediaSections(rsdparsaSdp.GetMediaSection(i),
sipccSdp.GetMediaSection(i));
}
return result;
}
bool ParsingResultComparer::CompareMediaSections(
const SdpMediaSection& rustMediaSection,
const SdpMediaSection& sipccMediaSection,
const SdpComparisonResult expect) const {
bool result = true;
auto trackMediaLineMismatch = [&result, &expect](
auto rustValue, auto sipccValue,
const nsString& valueDescription) {
result = false;
nsString typeStr = u"m="_ns;
typeStr += valueDescription;
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF, typeStr,
1);
LOG_EXPECT(result, expect,
("The media line values %s are not equal\n"
"rsdparsa value: %s\n"
"sipcc value: %s\n",
NS_LossyConvertUTF16toASCII(valueDescription).get(),
ToString(rustValue).c_str(), ToString(sipccValue).c_str()));
};
auto compareMediaLineValue = [trackMediaLineMismatch](
auto rustValue, auto sipccValue,
const nsString& valueDescription) {
if (rustValue != sipccValue) {
trackMediaLineMismatch(rustValue, sipccValue, valueDescription);
}
};
auto compareSimpleMediaLineValue =
[&rustMediaSection, &sipccMediaSection, compareMediaLineValue](
auto valGetFuncPtr, const nsString& valueDescription) {
compareMediaLineValue((rustMediaSection.*valGetFuncPtr)(),
(sipccMediaSection.*valGetFuncPtr)(),
valueDescription);
};
compareSimpleMediaLineValue(&SdpMediaSection::GetMediaType, u"media_type"_ns);
compareSimpleMediaLineValue(&SdpMediaSection::GetPort, u"port"_ns);
compareSimpleMediaLineValue(&SdpMediaSection::GetPortCount, u"port_count"_ns);
compareSimpleMediaLineValue(&SdpMediaSection::GetProtocol, u"protocol"_ns);
compareSimpleMediaLineValue(&SdpMediaSection::IsReceiving,
u"is_receiving"_ns);
compareSimpleMediaLineValue(&SdpMediaSection::IsSending, u"is_sending"_ns);
compareSimpleMediaLineValue(&SdpMediaSection::GetDirection, u"direction"_ns);
compareSimpleMediaLineValue(&SdpMediaSection::GetLevel, u"level"_ns);
compareMediaLineValue(ToString(rustMediaSection.GetConnection()),
ToString(sipccMediaSection.GetConnection()),
u"connection"_ns);
result &= CompareAttrLists(rustMediaSection.GetAttributeList(),
sipccMediaSection.GetAttributeList(),
static_cast<int>(rustMediaSection.GetLevel()));
return result;
}
bool ParsingResultComparer::CompareAttrLists(
const SdpAttributeList& rustAttrlist, const SdpAttributeList& sipccAttrlist,
int level, const SdpComparisonResult expect) const {
bool result = true;
for (size_t i = AttributeType::kFirstAttribute;
i <= static_cast<size_t>(AttributeType::kLastAttribute); i++) {
const AttributeType type = static_cast<AttributeType>(i);
std::string attrStr;
if (type != AttributeType::kDirectionAttribute) {
attrStr = "a=" + SdpAttribute::GetAttributeTypeString(type);
} else {
attrStr = "a=_direction_attribute_";
}
if (sipccAttrlist.HasAttribute(type, false)) {
auto sipccAttrStr = ToString(*sipccAttrlist.GetAttribute(type, false));
if (!rustAttrlist.HasAttribute(type, false)) {
result = false;
nsString typeStr;
typeStr.AssignASCII(attrStr.c_str());
typeStr += u"_missing"_ns;
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF,
typeStr, 1);
LOG_EXPECT(result, expect,
("Rust is missing the attribute: %s\n", attrStr.c_str()));
LOG_EXPECT(result, expect,
("Rust is missing: %s\n", sipccAttrStr.c_str()));
continue;
}
auto rustAttrStr = ToString(*rustAttrlist.GetAttribute(type, false));
if (rustAttrStr != sipccAttrStr) {
if (type == AttributeType::kFmtpAttribute) {
if (rustAttrlist.GetFmtp() == sipccAttrlist.GetFmtp()) {
continue;
}
}
std::string originalAttrStr = GetAttributeLines(attrStr, level);
if (rustAttrStr != originalAttrStr) {
result = false;
nsString typeStr;
typeStr.AssignASCII(attrStr.c_str());
typeStr += u"_inequal"_ns;
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF,
typeStr, 1);
LOG_EXPECT(result, expect,
("%s is neither equal to sipcc nor to the orginal sdp\n"
"--------------rsdparsa attribute---------------\n"
"%s"
"--------------sipcc attribute---------------\n"
"%s"
"--------------original attribute---------------\n"
"%s\n",
attrStr.c_str(), rustAttrStr.c_str(),
sipccAttrStr.c_str(), originalAttrStr.c_str()));
} else {
LOG_EXPECT(
result, expect,
("But the rust serialization is equal to the orignal sdp\n"));
}
}
} else {
if (rustAttrlist.HasAttribute(type, false)) {
nsString typeStr;
typeStr.AssignASCII(attrStr.c_str());
typeStr += u"_unexpected"_ns;
Telemetry::ScalarAdd(Telemetry::ScalarID::WEBRTC_SDP_PARSER_DIFF,
typeStr, 1);
}
}
}
return result;
}
std::vector<std::string> SplitLines(const std::string& sdp) {
std::stringstream ss(sdp);
std::string to;
std::vector<std::string> lines;
while (std::getline(ss, to, '\n')) {
lines.push_back(to);
}
return lines;
}
std::string ParsingResultComparer::GetAttributeLines(
const std::string& attrType, int level) const {
std::vector<std::string> lines = SplitLines(mOriginalSdp);
std::string attrToFind = attrType + ":";
std::string attrLines;
int currentLevel = -1;
// Filters rtcp-fb lines that contain "x-..." types
// This is because every SDP from Edge contains these rtcp-fb x- types
// for example: a=rtcp-fb:121 x-foo
std::regex customRtcpFbLines(R"(a\=rtcp\-fb\:(\d+|\*).* x\-.*)");
for (auto& line : lines) {
if (line.find("m=") == 0) {
if (level > currentLevel) {
attrLines.clear();
currentLevel++;
} else {
break;
}
} else if (line.find(attrToFind) == 0) {
if (std::regex_match(line, customRtcpFbLines)) {
continue;
}
attrLines += (line + '\n');
}
}
return attrLines;
}
} // namespace mozilla
|