Skip to content

Commit 59b74ae

Browse files
committed
More unittests for commsdsl2wireshark.
1 parent 2e548e5 commit 59b74ae

24 files changed

Lines changed: 483 additions & 119 deletions

app/commsdsl2comms/test/test32/Schema.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<special name="LongPresent" val="0xff" />
5555
</int>
5656

57-
<bundle name="ComplexLength" semanticType="length" valueOverride="replace" readOverride="replace" refreshOverride="replace">
57+
<bundle name="ComplexLength" semanticType="length" valueOverride="replace">
5858
<ref field="ShortLength" name="Short" />
5959
<optional name="Long" cond="$Short = ShortLength.LongPresent">
6060
<int name="ActLong" type="uint16" />

app/commsdsl2comms/test/test32/src/include/test32/field/ComplexLength.h.read

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/commsdsl2comms/test/test32/src/include/test32/field/ComplexLength.h.refresh

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/commsdsl2wireshark/src/Wireshark.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
253253
for (auto* f : frames) {
254254
assert(f != nullptr);
255255
auto& wiresharkFrame = WiresharkFrame::wiresharkCast(*f);
256+
if (!wiresharkFrame.wiresharkValidFrame()) {
257+
continue;
258+
}
259+
256260
static const std::string FrameTempl =
257261
"result, next_offset = #^#NAME#$#(tvb, tree)\n"
258262
"if result == #^#SUCCESS#$# then\n"

app/commsdsl2wireshark/src/WiresharkBundleField.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ std::string WiresharkBundleField::wiresharkDissectBodyImpl([[maybe_unused]] cons
111111
hasLimit = true;
112112

113113
static const std::string LimitTempl =
114-
"local next_limit = #^#VALUE_FUNC#$#(#^#FIELD#$#)\n"
114+
"local next_limit = #^#NEXT_OFFSET#$# + #^#VALUE_FUNC#$#(#^#FIELD#$#)\n"
115115
"if #^#LIMIT#$# < next_limit then\n"
116116
" return #^#NOT_ENOUGH_DATA#$#, #^#OFFSET#$#\n"
117117
"end\n"
@@ -124,18 +124,23 @@ std::string WiresharkBundleField::wiresharkDissectBodyImpl([[maybe_unused]] cons
124124
{"FIELD", f->wiresharkFieldObjName()},
125125
{"NOT_ENOUGH_DATA", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::WiresharkStatusCode::NotEnoughData)},
126126
{"OFFSET", wiresharkOffsetStr()},
127+
{"NEXT_OFFSET", wiresharkNextOffsetStr()},
127128
};
128129

129130
members.push_back(util::genProcessTemplate(LimitTempl, limitRepl));
130131
}
131132

133+
if (hasLimit) {
134+
members.push_back(wiresharkNextOffsetStr() + " = next_limit");
135+
}
136+
132137
static const std::string Templ =
133138
"local #^#RANGE#$# = #^#TVB#$#(#^#OFFSET#$#, #^#LIMIT#$# - #^#OFFSET#$#)\n"
134139
"local #^#SUBTREE#$# = #^#TREE#$#:add(#^#FIELD#$#, #^#RANGE#$#)\n"
135140
"#^#MEMBERS#$#\n"
136141
"local members_len = #^#NEXT_OFFSET#$# - #^#OFFSET#$#\n"
137142
"#^#SUBTREE#$#:set_len(members_len)\n"
138-
"if members_len == 0 then"
143+
"if members_len == 0 then\n"
139144
" #^#SUBTREE#$#:set_hidden(true)\n"
140145
" #^#SUBTREE#$# = #^#TREE#$#:add(#^#FIELD#$#, #^#TVB#$#(#^#OFFSET#$#, 0))\n"
141146
"end\n"

app/commsdsl2wireshark/src/WiresharkField.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,36 @@ bool WiresharkField::wiresharkHasTrivialValid() const
224224

225225
std::size_t WiresharkField::wiresharkMinFieldLength(const WiresharkField* refField) const
226226
{
227-
const auto* genField = &m_genField;
228-
if (refField != nullptr) {
229-
genField = &(refField->wiresharkGenField());
227+
if (refField == nullptr) {
228+
refField = this;
230229
}
231230

232-
auto parseObj = genField->genParseObj();
231+
auto parseObj = refField->wiresharkGenField().genParseObj();
233232
auto len = parseObj.parseMinLength();
234-
auto* bitfieldParent = wiresharkParentBitfield();
233+
auto* bitfieldParent = refField->wiresharkParentBitfield();
235234
if (bitfieldParent == nullptr) {
236235
return len;
237236
}
238237

239238
return std::max(len, bitfieldParent->wiresharkMinFieldLength());
240239
}
241240

241+
std::size_t WiresharkField::wiresharkMaxFieldLength(const WiresharkField* refField) const
242+
{
243+
if (refField == nullptr) {
244+
refField = this;
245+
}
246+
247+
auto parseObj = refField->wiresharkGenField().genParseObj();
248+
auto len = parseObj.parseMaxLength();
249+
auto* bitfieldParent = refField->wiresharkParentBitfield();
250+
if (bitfieldParent == nullptr) {
251+
return len;
252+
}
253+
254+
return std::max(len, bitfieldParent->wiresharkMaxFieldLength());
255+
}
256+
242257
std::string WiresharkField::wiresharkDslCondToString(
243258
const WiresharkGenerator& generator,
244259
const WiresharkFieldsList& siblings,
@@ -1075,7 +1090,7 @@ std::string WiresharkField::wiresharkForcedIntegralFieldType(const WiresharkFiel
10751090
refField = this;
10761091
}
10771092

1078-
auto* parentBitfield = wiresharkParentBitfield();
1093+
auto* parentBitfield = refField->wiresharkParentBitfield();
10791094
if (parentBitfield == nullptr) {
10801095
return strings::genEmptyString();
10811096
}
@@ -1175,7 +1190,7 @@ unsigned WiresharkField::wiresharkForcedBitLength(const WiresharkField* refField
11751190
refField = this;
11761191
}
11771192

1178-
auto* parentBitfield = wiresharkParentBitfield();
1193+
auto* parentBitfield = refField->wiresharkParentBitfield();
11791194
if (parentBitfield == nullptr) {
11801195
return 0U;
11811196
}

app/commsdsl2wireshark/src/WiresharkField.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class WiresharkField
7676

7777
bool wiresharkHasTrivialValid() const;
7878
std::size_t wiresharkMinFieldLength(const WiresharkField* refField = nullptr) const;
79+
std::size_t wiresharkMaxFieldLength(const WiresharkField* refField = nullptr) const;
7980

8081
static std::string wiresharkDslCondToString(
8182
const WiresharkGenerator& generator,

app/commsdsl2wireshark/src/WiresharkFrame.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,22 @@ WiresharkFrame::~WiresharkFrame() = default;
4141

4242
std::string WiresharkFrame::wiresharkDissectName() const
4343
{
44+
if (!m_validFrame) {
45+
[[maybe_unused]] static constexpr bool Should_not_happen = false;
46+
assert(Should_not_happen);
47+
return strings::genEmptyString();
48+
}
49+
4450
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
4551
return wiresharkGenerator.wiresharkDissectNameFor(*this);
4652
}
4753

4854
std::string WiresharkFrame::wiresharkDissectCode() const
4955
{
56+
if (!m_validFrame) {
57+
return strings::genEmptyString();
58+
}
59+
5060
static const std::string Templ =
5161
"#^#LAYERS#$#\n"
5262
"#^#FUNC_LIST#$#\n"
@@ -112,6 +122,11 @@ bool WiresharkFrame::wiresharkNeedsOptionalModeDefinition() const
112122
});
113123
}
114124

125+
bool WiresharkFrame::wiresharkValidFrame() const
126+
{
127+
return m_validFrame;
128+
}
129+
115130
bool WiresharkFrame::genPrepareImpl()
116131
{
117132
if (!GenBase::genPrepareImpl()) {

app/commsdsl2wireshark/src/WiresharkFrame.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class WiresharkFrame final : public commsdsl::gen::GenFrame
5151
std::string wiresharkDissectCode() const;
5252
std::string wiresharkExtractorsRegCode() const;
5353
bool wiresharkNeedsOptionalModeDefinition() const;
54+
bool wiresharkValidFrame() const;
5455

5556
protected:
5657
virtual bool genPrepareImpl() override;

app/commsdsl2wireshark/src/WiresharkIntField.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ const std::string& WiresharkIntField::wiresharkIntegralType(ParseIntField::Parse
9898
return iter->second;
9999
}
100100

101-
std::string WiresharkIntField::wiresharkTvbRangeAccessIntegralValue(ParseIntField::ParseType type, ParseEndian endian, std::size_t len)
101+
std::string WiresharkIntField::wiresharkTvbRangeAccessIntegralValue(ParseIntField::ParseType type, ParseEndian endian, std::size_t len, bool forceUnsigned)
102102
{
103103
std::string prefix;
104104
if (endian == ParseEndian::ParseEndian_Little) {
105105
prefix = strings::genLittleEndianPrefixStr();
106106
}
107107

108-
if (GenIntField::genIsUnsignedType(type)) {
108+
if (forceUnsigned || GenIntField::genIsUnsignedType(type)) {
109109
prefix += 'u';
110110
}
111111

@@ -256,8 +256,20 @@ std::string WiresharkIntField::wiresharkValidFuncBodyImpl([[maybe_unused]] const
256256
auto& ranges = parseObj.parseValidRanges();
257257
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
258258
auto numToStr =
259-
[this](std::intmax_t value)
259+
[this, parseObj](std::intmax_t value)
260260
{
261+
auto scaling = parseObj.parseScaling();
262+
if (scaling.first != scaling.second) {
263+
if (genIsUnsignedType()) {
264+
return
265+
std::to_string(
266+
(static_cast<double>(static_cast<std::uintmax_t>(value)) * static_cast<double>(scaling.first)) /
267+
static_cast<double>(scaling.second));
268+
}
269+
270+
return std::to_string((static_cast<double>(value) * static_cast<double>(scaling.first)) / static_cast<double>(scaling.second));
271+
}
272+
261273
if (genIsUnsignedType()) {
262274
return std::to_string(static_cast<std::uintmax_t>(value));
263275
}
@@ -460,7 +472,7 @@ std::string WiresharkIntField::wiresharkValDeclCodeInternal() const
460472
;
461473

462474
util::GenReplacementMap repl = {
463-
{"ACC", wiresharkTvbRangeAccessIntegralValue(parseObj.parseType(), parseObj.parseEndian(), parseObj.parseMinLength())},
475+
{"ACC", wiresharkTvbRangeAccessIntegralValue(parseObj.parseType(), parseObj.parseEndian(), parseObj.parseMinLength(), !parseObj.parseSignExt())},
464476
{"VAL", wiresharkValStr()},
465477
{"RANGE", wiresharkRangeStr()},
466478
};
@@ -505,19 +517,14 @@ std::string WiresharkIntField::wiresharkSerOffsetCodeInternal(bool& hasVal) cons
505517
}
506518

507519
hasVal = true;
508-
auto minLen = parseObj.parseMinLength();
509-
bool nonStandardLen = (minLen & (minLen - 1)) != 0;
510-
if (nonStandardLen && parseObj.parseSignExt() && (minLen != parseObj.parseMaxLength())) {
511-
// TODO: implement sign extension
512-
assert(false);
513-
}
514-
515520
static const std::string Templ =
521+
"#^#TONUMBER#$#\n"
516522
"#^#VAL#$# = #^#VAL#$# - (#^#OFFSET#$#)";
517523

518524
util::GenReplacementMap repl = {
519525
{"OFFSET", std::to_string(serOffset)},
520526
{"VAL", wiresharkValStr()},
527+
{"TONUMBER", wiresharkValToNumberCodeInternal()},
521528
};
522529

523530
return util::genProcessTemplate(Templ, repl);
@@ -533,12 +540,14 @@ std::string WiresharkIntField::wiresharkScalingCodeInternal(bool& hasVal) const
533540

534541
hasVal = true;
535542
static const std::string Templ =
543+
"#^#TONUMBER#$#\n"
536544
"#^#VAL#$# = (#^#VAL#$# * #^#NUM#$#) / #^#DENUM#$#\n";
537545

538546
util::GenReplacementMap repl = {
539547
{"VAL", wiresharkValStr()},
540548
{"NUM", std::to_string(scaling.first)},
541549
{"DENUM", std::to_string(scaling.second)},
550+
{"TONUMBER", wiresharkValToNumberCodeInternal()},
542551
};
543552

544553
return util::genProcessTemplate(Templ, repl);
@@ -564,4 +573,18 @@ std::string WiresharkIntField::wiresharkDisplayOffsetCodeInternal(bool& hasVal)
564573
return util::genProcessTemplate(Templ, repl);
565574
}
566575

576+
std::string WiresharkIntField::wiresharkValToNumberCodeInternal() const
577+
{
578+
static const std::string Templ =
579+
"if type(#^#VAL#$#) ~= \"number\" then\n"
580+
" #^#VAL#$# = #^#VAL#$#:tonumber()\n"
581+
"end";
582+
583+
util::GenReplacementMap repl = {
584+
{"VAL", wiresharkValStr()},
585+
};
586+
587+
return util::genProcessTemplate(Templ, repl);
588+
}
589+
567590
} // namespace commsdsl2wireshark

0 commit comments

Comments
 (0)