Browse Source

Added DnsResourceRecord and associated tests; code cleaning

develop
Robin Thoni 6 years ago
parent
commit
c7048e5849

+ 2
- 0
src/modules/dns/CMakeLists.txt View File

@@ -10,6 +10,8 @@ add_library(qdnsagg-dns
10 10
         includes/dns/QDnsLabel.h
11 11
         src/QDnsQuery.cpp
12 12
         includes/dns/QDnsQuery.h
13
+        src/QDnsResourceRecord.cpp
14
+        includes/dns/QDnsResourceRecord.h
13 15
         )
14 16
 
15 17
 target_link_libraries(qdnsagg-dns Qt5::Core Qt5::Network)

+ 59
- 0
src/modules/dns/includes/dns/QDnsResourceRecord.h View File

@@ -0,0 +1,59 @@
1
+//
2
+// Created by robin on 1/10/18.
3
+//
4
+
5
+#ifndef QDNSAGG_RESOURCERECORD_H
6
+#define QDNSAGG_RESOURCERECORD_H
7
+
8
+#include <qplatformdefs.h>
9
+#include <QByteArray>
10
+#include <QDataStream>
11
+
12
+namespace QDns
13
+{
14
+
15
+    class QDnsResourceRecord
16
+    {
17
+    public:
18
+        QDnsResourceRecord();
19
+
20
+        static QDnsResourceRecord parse(const QByteArray& packetData, quint32 pos);
21
+        static QDnsResourceRecord parse(const QByteArray& packetData, QDataStream& dataStream);
22
+        QByteArray serialize() const;
23
+        void serialize(QDataStream& dataStream) const;
24
+
25
+        bool isValid() const;
26
+
27
+        const QString& getName() const;
28
+
29
+        void setName(const QString& name);
30
+
31
+        quint16 getType() const;
32
+
33
+        void setType(quint16 type);
34
+
35
+        quint16 getClass() const;
36
+
37
+        void setClass(quint16 aClass);
38
+
39
+        quint32 getTtl() const;
40
+
41
+        void setTtl(quint32 ttl);
42
+
43
+        const QByteArray& getData() const;
44
+
45
+        void setData(const QByteArray& data);
46
+
47
+    private:
48
+        bool m_isValid;
49
+
50
+        QString m_name;
51
+        quint16 m_type;
52
+        quint16 m_class;
53
+        quint32 m_ttl;
54
+        QByteArray m_data;
55
+    };
56
+
57
+}
58
+
59
+#endif //QDNSAGG_RESOURCERECORD_H

+ 6
- 14
src/modules/dns/src/QDnsQuery.cpp View File

@@ -27,24 +27,17 @@ namespace QDns
27 27
     {
28 28
         QDnsQuery query;
29 29
 
30
-        if (dataStream.device()->bytesAvailable() < 5)
30
+        query.m_name = QDnsLabel::parse(packetData, dataStream);
31
+        if (query.m_name.isNull() || dataStream.device()->bytesAvailable() < 4)
31 32
         {
32 33
             query.m_isValid = false;
33 34
         }
34 35
         else
35 36
         {
36
-            query.m_name = QDnsLabel::parse(packetData, dataStream);
37
-            if (query.m_name.isNull() || dataStream.device()->bytesAvailable() < 4)
38
-            {
39
-                query.m_isValid = false;
40
-            }
41
-            else
42
-            {
43
-                dataStream >> query.m_type;
44
-                dataStream >> query.m_class;
45
-            }
46
-
37
+            dataStream >> query.m_type;
38
+            dataStream >> query.m_class;
47 39
         }
40
+
48 41
         return query;
49 42
     }
50 43
 
@@ -58,8 +51,7 @@ namespace QDns
58 51
 
59 52
     void QDnsQuery::serialize(QDataStream& dataStream) const
60 53
     {
61
-        auto label = QDnsLabel::serialize(m_name);
62
-        dataStream.writeRawData(label.constData(), label.length());
54
+        QDnsLabel::serialize(m_name, dataStream);
63 55
         dataStream << m_type;
64 56
         dataStream << m_class;
65 57
     }

+ 126
- 0
src/modules/dns/src/QDnsResourceRecord.cpp View File

@@ -0,0 +1,126 @@
1
+//
2
+// Created by robin on 1/10/18.
3
+//
4
+
5
+#include "dns/QDnsLabel.h"
6
+#include "dns/QDnsResourceRecord.h"
7
+
8
+namespace QDns
9
+{
10
+
11
+    QDnsResourceRecord::QDnsResourceRecord()
12
+        : m_isValid(true)
13
+    {
14
+    }
15
+
16
+    QDnsResourceRecord QDnsResourceRecord::parse(const QByteArray& packetData, quint32 pos)
17
+    {
18
+        QDataStream dataStream(packetData);
19
+        dataStream.skipRawData(pos);
20
+        return parse(packetData, dataStream);
21
+    }
22
+
23
+    QDnsResourceRecord QDnsResourceRecord::parse(const QByteArray& packetData, QDataStream& dataStream)
24
+    {
25
+        QDnsResourceRecord rr;
26
+
27
+        rr.m_name = QDnsLabel::parse(packetData, dataStream);
28
+        if (rr.m_name.isNull() || dataStream.device()->bytesAvailable() < 10)
29
+        {
30
+            rr.m_isValid = false;
31
+        }
32
+        else
33
+        {
34
+            dataStream >> rr.m_type;
35
+            dataStream >> rr.m_class;
36
+            dataStream >> rr.m_ttl;
37
+            quint16 length;
38
+            dataStream >> length;
39
+            if (dataStream.device()->bytesAvailable() < length)
40
+            {
41
+                rr.m_isValid = false;
42
+            }
43
+            else
44
+            {
45
+                char data[length];
46
+                dataStream.readRawData(data, length);
47
+                rr.m_data.append(data, length);
48
+            }
49
+        }
50
+        return rr;
51
+    }
52
+
53
+    QByteArray QDnsResourceRecord::serialize() const
54
+    {
55
+        QByteArray byteArray;
56
+        QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
57
+        serialize(dataStream);
58
+        return byteArray;
59
+    }
60
+
61
+    void QDnsResourceRecord::serialize(QDataStream& dataStream) const
62
+    {
63
+        auto label = QDnsLabel::serialize(m_name);
64
+        dataStream.writeRawData(label.constData(), label.length());
65
+        dataStream << m_type;
66
+        dataStream << m_class;
67
+        dataStream << m_ttl;
68
+        dataStream << (quint16) m_data.length();
69
+        dataStream.writeRawData(m_data.constData(), m_data.length());
70
+    }
71
+
72
+    bool QDnsResourceRecord::isValid() const
73
+    {
74
+        return m_isValid;
75
+    }
76
+
77
+    const QString& QDnsResourceRecord::getName() const
78
+    {
79
+        return m_name;
80
+    }
81
+
82
+    void QDnsResourceRecord::setName(const QString& name)
83
+    {
84
+        m_name = name;
85
+    }
86
+
87
+    quint16 QDnsResourceRecord::getType() const
88
+    {
89
+        return m_type;
90
+    }
91
+
92
+    void QDnsResourceRecord::setType(quint16 type)
93
+    {
94
+        m_type = type;
95
+    }
96
+
97
+    quint16 QDnsResourceRecord::getClass() const
98
+    {
99
+        return m_class;
100
+    }
101
+
102
+    void QDnsResourceRecord::setClass(quint16 aClass)
103
+    {
104
+        m_class = aClass;
105
+    }
106
+
107
+    quint32 QDnsResourceRecord::getTtl() const
108
+    {
109
+        return m_ttl;
110
+    }
111
+
112
+    void QDnsResourceRecord::setTtl(quint32 ttl)
113
+    {
114
+        m_ttl = ttl;
115
+    }
116
+
117
+    const QByteArray& QDnsResourceRecord::getData() const
118
+    {
119
+        return m_data;
120
+    }
121
+
122
+    void QDnsResourceRecord::setData(const QByteArray& data)
123
+    {
124
+        m_data = data;
125
+    }
126
+}

+ 2
- 1
tests/CMakeLists.txt View File

@@ -9,7 +9,8 @@ add_executable(qdnsagg-tests
9 9
         main.cpp
10 10
         DnsHeader.cpp
11 11
         DnsLabel.cpp
12
-        DnsQuery.cpp)
12
+        DnsQuery.cpp
13
+        DnsResourceRecord.cpp)
13 14
 
14 15
 target_link_libraries(qdnsagg-tests gtest pthread qdnsagg-dns)
15 16
 add_test(qdnsagg-tests ${CMAKE_CURRENT_BINARY_DIR}/qdnsagg-tests)

+ 24
- 24
tests/DnsLabel.cpp View File

@@ -42,46 +42,46 @@ INSTANTIATE_TEST_CASE_P(DnsLabelParseTestInst,
42 42
                                 DnsLabelParseTestParams {false, "", 0},
43 43
                                 DnsLabelParseTestParams {true, "00", 0, ""},
44 44
                                 DnsLabelParseTestParams {true,
45
-                                                    "06676f6f676c6502667200",
46
-                                                    0,
47
-                                                    "google.fr"
45
+                                                         "06676f6f676c6502667200",
46
+                                                         0,
47
+                                                         "google.fr"
48 48
                                 },
49 49
                                 DnsLabelParseTestParams {true,
50
-                                                    "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
51
-                                                    12,
52
-                                                    "google.fr"
50
+                                                         "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
51
+                                                         12,
52
+                                                         "google.fr"
53 53
                                 },
54 54
                                 DnsLabelParseTestParams {true,
55
-                                                    "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
56
-                                                    27,
57
-                                                    "google.fr"
55
+                                                         "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
56
+                                                         27,
57
+                                                         "google.fr"
58 58
                                 },
59 59
                                 DnsLabelParseTestParams {true,
60
-                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
61
-                                                    12,
62
-                                                    "www.rthoni.com"
60
+                                                         "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
61
+                                                         12,
62
+                                                         "www.rthoni.com"
63 63
                                 },
64 64
                                 DnsLabelParseTestParams {true,
65
-                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
66
-                                                    32,
67
-                                                    "www.rthoni.com"
65
+                                                         "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
66
+                                                         32,
67
+                                                         "www.rthoni.com"
68 68
                                 },
69 69
                                 DnsLabelParseTestParams {true,
70
-                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
71
-                                                    44,
72
-                                                    "serv3.rthoni.com"
70
+                                                         "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
71
+                                                         44,
72
+                                                         "serv3.rthoni.com"
73 73
                                 },
74 74
                                 DnsLabelParseTestParams {false,
75
-                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 c02c c02c00010001000002580004d5f6343d",
76
-                                                    44
75
+                                                         "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 c02c c02c00010001000002580004d5f6343d",
76
+                                                         44
77 77
                                 },
78 78
                                 DnsLabelParseTestParams {false,
79
-                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 802c c02c00010001000002580004d5f6343d",
80
-                                                    44
79
+                                                         "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 802c c02c00010001000002580004d5f6343d",
80
+                                                         44
81 81
                                 },
82 82
                                 DnsLabelParseTestParams {false,
83
-                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 402c c02c00010001000002580004d5f6343d",
84
-                                                    44
83
+                                                         "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 402c c02c00010001000002580004d5f6343d",
84
+                                                         44
85 85
                                 }
86 86
                         ));
87 87
 

+ 0
- 1
tests/DnsQuery.cpp View File

@@ -84,7 +84,6 @@ class DnsQuerySerializeTest : public ::testing::TestWithParam<DnsQuerySerializeT
84 84
 TEST_P(DnsQuerySerializeTest, serialize)
85 85
 {
86 86
     auto params = GetParam();
87
-    auto data = QByteArray::fromHex(params.hexData);
88 87
     QDns::QDnsQuery query;
89 88
 
90 89
     query.setName(params.name);

+ 125
- 0
tests/DnsResourceRecord.cpp View File

@@ -0,0 +1,125 @@
1
+//
2
+// Created by robin on 1/9/18.
3
+//
4
+
5
+#include <gtest/gtest.h>
6
+#include <QByteArray>
7
+#include <QString>
8
+
9
+#include "dns/QDnsResourceRecord.h"
10
+
11
+struct DnsResourceRecordParseTestParams
12
+{
13
+    bool isValid;
14
+    const char* hexData;
15
+    quint32 pos;
16
+    const char* label;
17
+    quint16 type;
18
+    quint16 clazz;
19
+    quint32 ttl;
20
+    const char* hexData2;
21
+};
22
+
23
+class DnsResourceRecordParseTest : public ::testing::TestWithParam<DnsResourceRecordParseTestParams>
24
+{
25
+};
26
+
27
+TEST_P(DnsResourceRecordParseTest, parse)
28
+{
29
+    auto params = GetParam();
30
+    auto data = QByteArray::fromHex(params.hexData);
31
+    auto rr = QDns::QDnsResourceRecord::parse(data, params.pos);
32
+
33
+    ASSERT_EQ(rr.isValid(), params.isValid);
34
+
35
+    if (rr.isValid())
36
+    {
37
+         ASSERT_STREQ(rr.getName().toStdString().c_str(), params.label);
38
+         ASSERT_EQ(rr.getType(), params.type);
39
+         ASSERT_EQ(rr.getClass(), params.clazz);
40
+         ASSERT_EQ(rr.getTtl(), params.ttl);
41
+         ASSERT_EQ(rr.getData().toHex(), QString(params.hexData2).remove(" "));
42
+    }
43
+}
44
+
45
+INSTANTIATE_TEST_CASE_P(DnsResourceRecordParseTestInst,
46
+                        DnsResourceRecordParseTest,
47
+                        ::testing::Values(
48
+                                DnsResourceRecordParseTestParams {false, "", 0},
49
+                                DnsResourceRecordParseTestParams {false, "00", 0},
50
+                                DnsResourceRecordParseTestParams {true,
51
+                                                                  "e1bc818000010000000100000266720000010001c00c0006000100000e100030086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518",
52
+                                                                  20,
53
+                                                                  "fr",
54
+                                                                  6,
55
+                                                                  1,
56
+                                                                  3600,
57
+                                                                  "086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518"
58
+                                },
59
+                                DnsResourceRecordParseTestParams {true,
60
+                                                                  "1c588580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
61
+                                                                  32,
62
+                                                                  "www.rthoni.com",
63
+                                                                  5,
64
+                                                                  1,
65
+                                                                  600,
66
+                                                                  "057365727633c010"
67
+                                },
68
+                                DnsResourceRecordParseTestParams {true,
69
+                                                                  "1c588580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
70
+                                                                  52,
71
+                                                                  "serv3.rthoni.com",
72
+                                                                  1,
73
+                                                                  1,
74
+                                                                  600,
75
+                                                                  "d5f6343d"
76
+                                }
77
+                        ));
78
+
79
+struct DnsResourceRecordSerializeTestParams
80
+{
81
+    const char* hexData;
82
+
83
+    const char* name;
84
+    quint16 type;
85
+    quint16 clazz;
86
+    quint32 ttl;
87
+    const char* hexData2;
88
+};
89
+
90
+class DnsResourceRecordSerializeTest : public ::testing::TestWithParam<DnsResourceRecordSerializeTestParams>
91
+{
92
+};
93
+
94
+TEST_P(DnsResourceRecordSerializeTest, serialize)
95
+{
96
+    auto params = GetParam();
97
+    QDns::QDnsResourceRecord rr;
98
+
99
+    rr.setName(params.name);
100
+    rr.setType(params.type);
101
+    rr.setClass(params.clazz);
102
+    rr.setTtl(params.ttl);
103
+    rr.setData(QByteArray::fromHex(params.hexData2));
104
+
105
+    ASSERT_EQ(rr.serialize().toHex(), QString(params.hexData).remove(" "));
106
+}
107
+
108
+INSTANTIATE_TEST_CASE_P(DnsResourceRecordSerializeTestInst,
109
+                        DnsResourceRecordSerializeTest,
110
+                        ::testing::Values(
111
+                                DnsResourceRecordSerializeTestParams {"026672000006000100000e100030086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518",
112
+                                                                      "fr",
113
+                                                                      6,
114
+                                                                      1,
115
+                                                                      3600,
116
+                                                                      "086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518"
117
+                                },
118
+                                DnsResourceRecordSerializeTestParams {"03777777067274686f6e6903636f6d0000050001000002580008057365727633c010",
119
+                                                                      "www.rthoni.com",
120
+                                                                      5,
121
+                                                                      1,
122
+                                                                      600,
123
+                                                                      "057365727633c010"
124
+                                }
125
+                        ));

Loading…
Cancel
Save