소스 검색

Added DnsQuery serialize() method and associated tests; modified DnsQuery to use QDataStream

develop
Robin Thoni 6 년 전
부모
커밋
616d98988b
3개의 변경된 파일77개의 추가작업 그리고 14개의 파일을 삭제
  1. 3
    1
      src/modules/dns/includes/dns/QDnsQuery.h
  2. 16
    3
      src/modules/dns/src/QDnsQuery.cpp
  3. 58
    10
      tests/DnsQuery.cpp

+ 3
- 1
src/modules/dns/includes/dns/QDnsQuery.h 파일 보기

@@ -17,7 +17,9 @@ namespace QDns
17 17
         QDnsQuery();
18 18
 
19 19
         static QDnsQuery parse(const QByteArray& packetData, quint32 pos);
20
-        QByteArray serialize(const QByteArray& data) const;
20
+        static QDnsQuery parse(const QByteArray& packetData, QDataStream& dataStream);
21
+        QByteArray serialize() const;
22
+        void serialize(QDataStream& dataStream) const;
21 23
 
22 24
         bool isValid() const;
23 25
 

+ 16
- 3
src/modules/dns/src/QDnsQuery.cpp 파일 보기

@@ -18,9 +18,14 @@ namespace QDns
18 18
 
19 19
     QDnsQuery QDnsQuery::parse(const QByteArray& packetData, quint32 pos)
20 20
     {
21
-        QDnsQuery query;
22 21
         QDataStream dataStream(packetData);
23 22
         dataStream.skipRawData(pos);
23
+        return parse(packetData, dataStream);
24
+    }
25
+
26
+    QDnsQuery QDnsQuery::parse(const QByteArray& packetData, QDataStream& dataStream)
27
+    {
28
+        QDnsQuery query;
24 29
 
25 30
         if (dataStream.device()->bytesAvailable() < 5)
26 31
         {
@@ -43,14 +48,22 @@ namespace QDns
43 48
         return query;
44 49
     }
45 50
 
46
-    QByteArray QDnsQuery::serialize(const QByteArray& data) const // TODO
51
+    QByteArray QDnsQuery::serialize() const
47 52
     {
48 53
         QByteArray byteArray;
49 54
         QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
50
-
55
+        serialize(dataStream);
51 56
         return byteArray;
52 57
     }
53 58
 
59
+    void QDnsQuery::serialize(QDataStream& dataStream) const
60
+    {
61
+        auto label = QDnsLabel::serialize(m_name);
62
+        dataStream.writeRawData(label.constData(), label.length());
63
+        dataStream << m_type;
64
+        dataStream << m_class;
65
+    }
66
+
54 67
     bool QDnsQuery::isValid() const
55 68
     {
56 69
         return m_isValid;

+ 58
- 10
tests/DnsQuery.cpp 파일 보기

@@ -8,7 +8,7 @@
8 8
 
9 9
 #include "dns/QDnsQuery.h"
10 10
 
11
-struct DnsQueryTestParams
11
+struct DnsQueryParseTestParams
12 12
 {
13 13
     bool isValid;
14 14
     const char* hexData;
@@ -18,11 +18,11 @@ struct DnsQueryTestParams
18 18
     quint16 clazz;
19 19
 };
20 20
 
21
-class DnsQueryTest : public ::testing::TestWithParam<DnsQueryTestParams>
21
+class DnsQueryParseTest : public ::testing::TestWithParam<DnsQueryParseTestParams>
22 22
 {
23 23
 };
24 24
 
25
-TEST_P(DnsQueryTest, parse)
25
+TEST_P(DnsQueryParseTest, parse)
26 26
 {
27 27
     auto params = GetParam();
28 28
     auto data = QByteArray::fromHex(params.hexData);
@@ -38,26 +38,26 @@ TEST_P(DnsQueryTest, parse)
38 38
     }
39 39
 }
40 40
 
41
-INSTANTIATE_TEST_CASE_P(DnsQueryTestInst,
42
-                        DnsQueryTest,
41
+INSTANTIATE_TEST_CASE_P(DnsQueryParseTestInst,
42
+                        DnsQueryParseTest,
43 43
                         ::testing::Values(
44
-                                DnsQueryTestParams {false, "", 0},
45
-                                DnsQueryTestParams {false, "00", 0},
46
-                                DnsQueryTestParams {true,
44
+                                DnsQueryParseTestParams {false, "", 0},
45
+                                DnsQueryParseTestParams {false, "00", 0},
46
+                                DnsQueryParseTestParams {true,
47 47
                                                     "0377777706676f6f676c650266720000010001",
48 48
                                                     0,
49 49
                                                     "www.google.fr",
50 50
                                                     1,
51 51
                                                     1
52 52
                                 },
53
-                                DnsQueryTestParams {true,
53
+                                DnsQueryParseTestParams {true,
54 54
                                                     "03777777067274686f6e6903636f6d0000030008",
55 55
                                                     0,
56 56
                                                     "www.rthoni.com",
57 57
                                                     3,
58 58
                                                     8
59 59
                                 },
60
-                                DnsQueryTestParams {true,
60
+                                DnsQueryParseTestParams {true,
61 61
                                                     "0003777777067274686f6e6903636f6d0000030008",
62 62
                                                     1,
63 63
                                                     "www.rthoni.com",
@@ -65,3 +65,51 @@ INSTANTIATE_TEST_CASE_P(DnsQueryTestInst,
65 65
                                                     8
66 66
                                 }
67 67
                         ));
68
+
69
+
70
+
71
+struct DnsQuerySerializeTestParams
72
+{
73
+    const char* hexData;
74
+
75
+    const char* name;
76
+    quint16 type;
77
+    quint16 clazz;
78
+};
79
+
80
+class DnsQuerySerializeTest : public ::testing::TestWithParam<DnsQuerySerializeTestParams>
81
+{
82
+};
83
+
84
+TEST_P(DnsQuerySerializeTest, serialize)
85
+{
86
+    auto params = GetParam();
87
+    auto data = QByteArray::fromHex(params.hexData);
88
+    QDns::QDnsQuery query;
89
+
90
+    query.setName(params.name);
91
+    query.setType(params.type);
92
+    query.setClass(params.clazz);
93
+
94
+    ASSERT_EQ(query.serialize().toHex(), QString(params.hexData).remove(" "));
95
+}
96
+
97
+INSTANTIATE_TEST_CASE_P(DnsQuerySerializeTestInst,
98
+                        DnsQuerySerializeTest,
99
+                        ::testing::Values(
100
+                                DnsQuerySerializeTestParams {"0266720000020008",
101
+                                                             "fr",
102
+                                                             2,
103
+                                                             8
104
+                                },
105
+                                DnsQuerySerializeTestParams {"06676f6f676c650266720000010001",
106
+                                                             "google.fr",
107
+                                                             1,
108
+                                                             1
109
+                                },
110
+                                DnsQuerySerializeTestParams {"03777777067274686f6e6903636f6d0000010001",
111
+                                                             "www.rthoni.com",
112
+                                                             1,
113
+                                                             1
114
+                                }
115
+                        ));

Loading…
취소
저장