Browse Source

added serialize() method to QDnsHeader and associated tests

develop
Robin Thoni 6 years ago
parent
commit
75fe4585ca
3 changed files with 42 additions and 1 deletions
  1. 1
    0
      src/modules/dns/includes/dns/QDnsHeader.h
  2. 17
    0
      src/modules/dns/src/QDnsHeader.cpp
  3. 24
    1
      tests/main.cpp

+ 1
- 0
src/modules/dns/includes/dns/QDnsHeader.h View File

@@ -17,6 +17,7 @@ namespace QDNS
17 17
         QDnsHeader();
18 18
 
19 19
         static QDnsHeader parse(const QByteArray& data);
20
+        QByteArray serialize() const;
20 21
 
21 22
         bool isValid() const;
22 23
 

+ 17
- 0
src/modules/dns/src/QDnsHeader.cpp View File

@@ -230,4 +230,21 @@ namespace QDNS
230 230
         m_additionalCount = additionalCount;
231 231
     }
232 232
 
233
+    QByteArray QDnsHeader::serialize() const
234
+    {
235
+        if (isValid())
236
+        {
237
+            QByteArray data;
238
+            QDataStream ds(&data, QIODevice::WriteOnly);
239
+            ds << m_id;
240
+            ds << m_flags;
241
+            ds << m_questionCount;
242
+            ds << m_answerCount;
243
+            ds << m_authorityCount;
244
+            ds << m_additionalCount;
245
+            return data;
246
+        }
247
+        return QByteArray();
248
+    }
249
+
233 250
 }

+ 24
- 1
tests/main.cpp View File

@@ -1,5 +1,6 @@
1 1
 #include <gtest/gtest.h>
2
-#include <QtCore/QByteArray>
2
+#include <QByteArray>
3
+#include <QString>
3 4
 
4 5
 #include "dns/QDnsHeader.h"
5 6
 
@@ -57,12 +58,24 @@ TEST_P(DNSHeaderTest, parseAndGetters)
57 58
         ASSERT_EQ(header.getAnswerCount(), params.answerCount);
58 59
         ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
59 60
         ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
61
+
62
+        ASSERT_EQ(header.serialize().toHex(), QString(params.hexData).remove(" "));
63
+    }
64
+    else
65
+    {
66
+        ASSERT_EQ(header.serialize().size(), 0);
60 67
     }
61 68
 }
62 69
 
63 70
 TEST_P(DNSHeaderTest, settersAndGetters1)
64 71
 {
65 72
     auto params = GetParam();
73
+
74
+    if (!params.isValid)
75
+    {
76
+        return;
77
+    }
78
+
66 79
     auto data = QByteArray::fromHex(params.hexData);
67 80
     QDNS::QDnsHeader header;
68 81
 
@@ -90,11 +103,19 @@ TEST_P(DNSHeaderTest, settersAndGetters1)
90 103
     ASSERT_EQ(header.getAnswerCount(), params.answerCount);
91 104
     ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
92 105
     ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
106
+
107
+    ASSERT_EQ(header.serialize().toHex(), QString(params.hexData).remove(" "));
93 108
 }
94 109
 
95 110
 TEST_P(DNSHeaderTest, settersAndGetters2)
96 111
 {
97 112
     auto params = GetParam();
113
+
114
+    if (!params.isValid)
115
+    {
116
+        return;
117
+    }
118
+
98 119
     auto data = QByteArray::fromHex(params.hexData);
99 120
     QDNS::QDnsHeader header;
100 121
 
@@ -131,6 +152,8 @@ TEST_P(DNSHeaderTest, settersAndGetters2)
131 152
     ASSERT_EQ(header.getAnswerCount(), params.answerCount);
132 153
     ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
133 154
     ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
155
+
156
+    ASSERT_EQ(header.serialize().toHex(), QString(params.hexData).remove(" "));
134 157
 }
135 158
 
136 159
 INSTANTIATE_TEST_CASE_P(DNSHeaderTestInst,

Loading…
Cancel
Save