Skip to content

Commit 271321e

Browse files
committed
More commsdsl2wireshark unittests.
1 parent b925d4d commit 271321e

14 files changed

Lines changed: 125 additions & 29 deletions

app/commsdsl2comms/test/test47/Schema.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<schema name="test47"
3-
endian="big"
4-
version="5">
5-
2+
<schema name="test47" endian="big" version="5">
63
<description>
74
Testing platforms and extra bundles.
85
</description>

app/commsdsl2wireshark/src/WiresharkDataField.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,12 @@ std::string WiresharkDataField::wiresharkDissectLengthCodeInternal() const
232232

233233
if (m_prefixField != nullptr) {
234234
static const std::string Templ =
235-
"local prefix_tree = #^#TREE#$#:add(#^#PROTO#$#, #^#TVB#$#(#^#NEXT_OFFSET#$#, 0))\n"
236-
"prefix_tree:set_hidden(true)\n"
237-
"#^#RESULT#$#, #^#NEXT_OFFSET#$# = #^#READ_FUNC#$#(#^#TVB#$#, prefix_tree, #^#NEXT_OFFSET#$#, #^#LIMIT#$#, #^#FIELD#$#)\n"
235+
"#^#RESULT#$#, #^#NEXT_OFFSET#$# = #^#READ_FUNC#$#(#^#TVB#$#, #^#TREE#$#, #^#NEXT_OFFSET#$#, #^#LIMIT#$#)\n"
238236
"if #^#RESULT#$# ~= #^#SUCCESS#$# then\n"
239237
" return #^#RESULT#$#\n"
240238
"end\n"
241239
"\n"
242-
"local len = #^#VALUE_FUNC#$#(#^#FIELD#$#)"
240+
"local len = #^#VALUE_FUNC#$#()"
243241
;
244242

245243
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
@@ -250,8 +248,7 @@ std::string WiresharkDataField::wiresharkDissectLengthCodeInternal() const
250248
{"TVB", wiresharkTvbStr()},
251249
{"TREE", wiresharkTreeStr()},
252250
{"LIMIT", wiresharkOffsetLimitStr()},
253-
{"FIELD", m_prefixField->wiresharkFieldObjName()},
254-
{"VALUE_FUNC", wiresharkValueFuncName()},
251+
{"VALUE_FUNC", m_prefixField->wiresharkValueFuncName()},
255252
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::WiresharkStatusCode::Success)},
256253
{"PROTO", Wireshark::wiresharkProtocolObjName(wiresharkGenerator)},
257254
};

app/commsdsl2wireshark/src/WiresharkField.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,11 @@ std::string WiresharkField::wiresharkDissectCodeImpl(const WiresharkField* refFi
798798
auto prependFileName = relPath + strings::genPrependFileSuffixStr();
799799
auto extendFileName = relPath + strings::genExtendFileSuffixStr();
800800

801-
bool replaced = false;
802801
bool extended = false;
803802
util::GenReplacementMap repl = {
804803
{"REG", wiresharkFieldRegistration(refField)},
805804
{"NAME", wiresharkDissectName(refField)},
806-
{"REPLACE", wiresharkGenerator.genReadCodeInjectCode(replaceFileName, "Replace this function body", &replaced)},
805+
{"REPLACE", m_customCode.m_read},
807806
{"PREPEND", wiresharkGenerator.genReadCodeInjectCode(prependFileName, "Prepend here")},
808807
{"EXTEND", wiresharkGenerator.genReadCodeInjectCode(extendFileName, "Extend function above", &extended)},
809808
{"NAME_VAR", wiresharkNameDefInternal(refField)},
@@ -816,7 +815,7 @@ std::string WiresharkField::wiresharkDissectCodeImpl(const WiresharkField* refFi
816815
repl["MEMBERS"] = wiresharkMembersDissectCodeImpl();
817816
}
818817

819-
if (!replaced) {
818+
if (!m_customCode.m_hasRead) {
820819
repl["BODY"] = wiresharkDissectBodyInternal(refField);
821820
}
822821

@@ -1033,16 +1032,15 @@ std::string WiresharkField::wiresharkValidFuncCodeImpl(const WiresharkField* ref
10331032
auto replaceFileName = relPath + strings::genReplaceFileSuffixStr();
10341033
auto extendFileName = relPath + strings::genExtendFileSuffixStr();
10351034

1036-
bool replaced = false;
10371035
bool extended = false;
10381036
util::GenReplacementMap repl = {
10391037
{"NAME", wiresharkValidFuncName(refField)},
1040-
{"REPLACE", wiresharkGenerator.genReadCodeInjectCode(replaceFileName, "Replace this function body", &replaced)},
1038+
{"REPLACE", m_customCode.m_valid},
10411039
{"EXTEND", wiresharkGenerator.genReadCodeInjectCode(extendFileName, "Extend function above", &extended)},
10421040
{"FIELD", wiresharkFieldStr()},
10431041
};
10441042

1045-
if (!replaced) {
1043+
if (!m_customCode.m_hasValid) {
10461044
repl["BODY"] = wiresharkValidFuncBodyImpl(refField);
10471045
}
10481046

@@ -1259,7 +1257,8 @@ bool WiresharkField::wiresharkHasOverrideCode() const
12591257
return
12601258
m_customCode.m_hasRead ||
12611259
m_customCode.m_hasValid ||
1262-
m_customCode.m_hasName;
1260+
m_customCode.m_hasName ||
1261+
m_customCode.m_hasValue;
12631262
}
12641263

12651264
std::string WiresharkField::wiresharkDissectSignature()
@@ -1586,16 +1585,15 @@ std::string WiresharkField::wiresharkValueFuncCodeInternal(const WiresharkField*
15861585
auto replaceFileName = relPath + strings::genReplaceFileSuffixStr();
15871586
auto extendFileName = relPath + strings::genExtendFileSuffixStr();
15881587

1589-
bool replaced = false;
15901588
bool extended = false;
15911589
util::GenReplacementMap repl = {
15921590
{"NAME", wiresharkValueFuncName(refField)},
1593-
{"REPLACE", wiresharkGenerator.genReadCodeInjectCode(replaceFileName, "Replace this function body", &replaced)},
1591+
{"REPLACE", m_customCode.m_value},
15941592
{"EXTEND", wiresharkGenerator.genReadCodeInjectCode(extendFileName, "Extend function above", &extended)},
15951593
{"FIELD", wiresharkFieldStr()},
15961594
};
15971595

1598-
if (!replaced) {
1596+
if (!m_customCode.m_hasValue) {
15991597
repl["BODY"] = wiresharkValueFuncBodyImpl(refField);
16001598
}
16011599

app/commsdsl2wireshark/src/WiresharkIdLayer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ std::string WiresharkIdLayer::wiresharkExtraDissectCodeImpl() const
7777
DissectMap map;
7878
for (auto* m : messages) {
7979
auto& wiresharkMsg = WiresharkMessage::wiresharkCast(*m);
80-
map[m->genParseObj().parseId()].push_back(wiresharkMsg.wiresharkDissectName());
80+
auto dissectName = wiresharkMsg.wiresharkDissectName();
81+
if (dissectName.empty()) {
82+
continue;
83+
}
84+
85+
map[m->genParseObj().parseId()].push_back(std::move(dissectName));
8186
}
8287

8388
util::GenStringsList elems;

app/commsdsl2wireshark/src/WiresharkMessage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ WiresharkMessage::~WiresharkMessage() = default;
4242

4343
std::string WiresharkMessage::wiresharkDissectName() const
4444
{
45+
if (!genIsReferenced()) {
46+
return strings::genEmptyString();
47+
}
48+
4549
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
4650
return wiresharkGenerator.wiresharkDissectNameFor(*this);
4751
}

app/commsdsl2wireshark/src/WiresharkStringField.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,12 @@ std::string WiresharkStringField::wiresharkDissectLengthCodeInternal() const
267267

268268
if (m_prefixField != nullptr) {
269269
static const std::string Templ =
270-
"local prefix_tree = #^#TREE#$#:add(#^#PROTO#$#, #^#TVB#$#(#^#NEXT_OFFSET#$#, 0))\n"
271-
"prefix_tree:set_hidden(true)\n"
272-
"#^#RESULT#$#, #^#NEXT_OFFSET#$# = #^#READ_FUNC#$#(#^#TVB#$#, prefix_tree, #^#NEXT_OFFSET#$#, #^#LIMIT#$#, #^#FIELD#$#)\n"
270+
"#^#RESULT#$#, #^#NEXT_OFFSET#$# = #^#READ_FUNC#$#(#^#TVB#$#, #^#TREE#$#, #^#NEXT_OFFSET#$#, #^#LIMIT#$#)\n"
273271
"if #^#RESULT#$# ~= #^#SUCCESS#$# then\n"
274272
" return #^#RESULT#$#\n"
275273
"end\n"
276274
"\n"
277-
"local len = #^#VALUE_FUNC#$#(#^#FIELD#$#)"
275+
"local len = #^#VALUE_FUNC#$#()"
278276
;
279277

280278
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
@@ -285,7 +283,6 @@ std::string WiresharkStringField::wiresharkDissectLengthCodeInternal() const
285283
{"TVB", wiresharkTvbStr()},
286284
{"TREE", wiresharkTreeStr()},
287285
{"LIMIT", wiresharkOffsetLimitStr()},
288-
{"FIELD", m_prefixField->wiresharkFieldObjName()},
289286
{"VALUE_FUNC", m_prefixField->wiresharkValueFuncName()},
290287
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::WiresharkStatusCode::Success)},
291288
{"PROTO", Wireshark::wiresharkProtocolObjName(wiresharkGenerator)},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- TODO: implement custom read
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- TODO: implement custom read
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--force-platform Plat1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
3+
from commsdsl_pcap_gen import *
4+
5+
def do_frame(id, payload):
6+
prefix = struct.pack('>B', id)
7+
return prefix + payload
8+
9+
def pcap1(f):
10+
seq = 1000
11+
msg3_payload = struct.pack('')
12+
msg3 = do_frame(3, msg3_payload)
13+
msg4_payload = struct.pack('')
14+
msg4 = do_frame(4, msg4_payload) # Expected invalid id
15+
header = commsdsl_create_ethernet_ip_tcp_headers(len(msg3) + len(msg4), seq)
16+
commsdsl_write_packet(f, header + msg3 + msg4, time.time())
17+
18+
def main():
19+
with open(sys.argv[1], 'wb') as f:
20+
commsdsl_write_pcap_header(f)
21+
pcap1(f)
22+
23+
if __name__ == '__main__':
24+
main()

0 commit comments

Comments
 (0)