Skip to content

Commit 86ea4dd

Browse files
committed
pytrap: add edge cases test for copyMessage()
1 parent ceccd55 commit 86ea4dd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

pytrap/test/unirectemplate_unittest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,49 @@ def runTest(self):
890890
pytrap.UnirecIPAddr("10.0.0.1"), "TIME_FIRST": pytrap.UnirecTime(1669885132, 853),
891891
"ABC": 123, "TEXT2": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "STREAMBYTES2": b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "UNKNOWN": "unknown text"})
892892

893+
class CopyMessageEdgeCasesTest(unittest.TestCase):
894+
def runTest(self):
895+
import pytrap
896+
data = { "SRC_IP": pytrap.UnirecIPAddr("10.0.0.1"), "DST_IP": pytrap.UnirecIPAddr("10.0.0.1"),
897+
"TIME_FIRST": pytrap.UnirecTime(1669885132, 853),
898+
"TIME_LAST": pytrap.UnirecTime(1669885132, 853),
899+
"ABC": 123, "BCD": 321, "TEXT": "some_text", "TEXT2": "some_text2",
900+
"STREAMBYTES": b"abc\x01\x02\x00\x03", "STREAMBYTES2": b"abc\x02\x03\x00\x04"}
901+
a = pytrap.UnirecTemplate("ipaddr DST_IP,ipaddr SRC_IP,time TIME_FIRST,time TIME_LAST,uint32 ABC,uint32 BCD,string TEXT,string TEXT2,bytes STREAMBYTES,bytes STREAMBYTES2")
902+
b = pytrap.UnirecTemplate("ipaddr SRC_IP,time TIME_FIRST,uint32 ABC,string TEXT2,bytes STREAMBYTES2,string UNKNOWN")
903+
with self.assertRaises(TypeError):
904+
# both a and b have no allocated data
905+
b.copyMessage(a)
906+
a.createMessage(100)
907+
a.setFromDict(data)
908+
with self.assertRaises(TypeError):
909+
# b has no allocated data
910+
b.copyMessage(a)
911+
912+
b.createMessage(100)
913+
# recreate a
914+
a = pytrap.UnirecTemplate("ipaddr DST_IP,ipaddr SRC_IP,time TIME_FIRST,time TIME_LAST,uint32 ABC,uint32 BCD,string TEXT,string TEXT2,bytes STREAMBYTES,bytes STREAMBYTES2")
915+
with self.assertRaises(TypeError):
916+
# a has no allocated data
917+
b.copyMessage(a)
918+
919+
a.createMessage(100)
920+
a.setFromDict(data)
921+
b = pytrap.UnirecTemplate("")
922+
b.createMessage()
923+
b.copyMessage(a)
924+
self.assertEqual(b.getDict(), {})
925+
926+
del(a)
927+
del(b)
928+
929+
a = pytrap.UnirecTemplate("")
930+
a.createMessage()
931+
b = pytrap.UnirecTemplate("ipaddr DST_IP,time TIME_FIRST,string TEXT")
932+
b.createMessage(100)
933+
b.copyMessage(a)
934+
self.assertEqual(b.getDict(), {"DST_IP": pytrap.UnirecIPAddr("::"), "TIME_FIRST": pytrap.UnirecTime(0, 0), "TEXT": ""})
935+
893936

894937
class AllocateBigMessage(unittest.TestCase):
895938
def runTest(self):

0 commit comments

Comments
 (0)