Explorar el Código

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

develop
Robin Thoni hace 6 años
padre
commit
616d98988b
Se han modificado 3 ficheros con 77 adiciones y 14 borrados
  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 Ver fichero

17
         QDnsQuery();
17
         QDnsQuery();
18
 
18
 
19
         static QDnsQuery parse(const QByteArray& packetData, quint32 pos);
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
         bool isValid() const;
24
         bool isValid() const;
23
 
25
 

+ 16
- 3
src/modules/dns/src/QDnsQuery.cpp Ver fichero

18
 
18
 
19
     QDnsQuery QDnsQuery::parse(const QByteArray& packetData, quint32 pos)
19
     QDnsQuery QDnsQuery::parse(const QByteArray& packetData, quint32 pos)
20
     {
20
     {
21
-        QDnsQuery query;
22
         QDataStream dataStream(packetData);
21
         QDataStream dataStream(packetData);
23
         dataStream.skipRawData(pos);
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
         if (dataStream.device()->bytesAvailable() < 5)
30
         if (dataStream.device()->bytesAvailable() < 5)
26
         {
31
         {
43
         return query;
48
         return query;
44
     }
49
     }
45
 
50
 
46
-    QByteArray QDnsQuery::serialize(const QByteArray& data) const // TODO
51
+    QByteArray QDnsQuery::serialize() const
47
     {
52
     {
48
         QByteArray byteArray;
53
         QByteArray byteArray;
49
         QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
54
         QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
50
-
55
+        serialize(dataStream);
51
         return byteArray;
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
     bool QDnsQuery::isValid() const
67
     bool QDnsQuery::isValid() const
55
     {
68
     {
56
         return m_isValid;
69
         return m_isValid;

+ 58
- 10
tests/DnsQuery.cpp Ver fichero

8
 
8
 
9
 #include "dns/QDnsQuery.h"
9
 #include "dns/QDnsQuery.h"
10
 
10
 
11
-struct DnsQueryTestParams
11
+struct DnsQueryParseTestParams
12
 {
12
 {
13
     bool isValid;
13
     bool isValid;
14
     const char* hexData;
14
     const char* hexData;
18
     quint16 clazz;
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
     auto params = GetParam();
27
     auto params = GetParam();
28
     auto data = QByteArray::fromHex(params.hexData);
28
     auto data = QByteArray::fromHex(params.hexData);
38
     }
38
     }
39
 }
39
 }
40
 
40
 
41
-INSTANTIATE_TEST_CASE_P(DnsQueryTestInst,
42
-                        DnsQueryTest,
41
+INSTANTIATE_TEST_CASE_P(DnsQueryParseTestInst,
42
+                        DnsQueryParseTest,
43
                         ::testing::Values(
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
                                                     "0377777706676f6f676c650266720000010001",
47
                                                     "0377777706676f6f676c650266720000010001",
48
                                                     0,
48
                                                     0,
49
                                                     "www.google.fr",
49
                                                     "www.google.fr",
50
                                                     1,
50
                                                     1,
51
                                                     1
51
                                                     1
52
                                 },
52
                                 },
53
-                                DnsQueryTestParams {true,
53
+                                DnsQueryParseTestParams {true,
54
                                                     "03777777067274686f6e6903636f6d0000030008",
54
                                                     "03777777067274686f6e6903636f6d0000030008",
55
                                                     0,
55
                                                     0,
56
                                                     "www.rthoni.com",
56
                                                     "www.rthoni.com",
57
                                                     3,
57
                                                     3,
58
                                                     8
58
                                                     8
59
                                 },
59
                                 },
60
-                                DnsQueryTestParams {true,
60
+                                DnsQueryParseTestParams {true,
61
                                                     "0003777777067274686f6e6903636f6d0000030008",
61
                                                     "0003777777067274686f6e6903636f6d0000030008",
62
                                                     1,
62
                                                     1,
63
                                                     "www.rthoni.com",
63
                                                     "www.rthoni.com",
65
                                                     8
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…
Cancelar
Guardar