Переглянути джерело

Added DnsLabel and associated tests; refactored tests

develop
Robin Thoni 6 роки тому
джерело
коміт
0de2bf94a1

+ 2
- 0
src/modules/dns/CMakeLists.txt Переглянути файл

@@ -6,6 +6,8 @@ include_directories(includes)
6 6
 add_library(qdnsagg-dns
7 7
         src/QDnsHeader.cpp
8 8
         includes/dns/QDnsHeader.h
9
+        src/QDnsLabel.cpp
10
+        includes/dns/QDnsLabel.h
9 11
         )
10 12
 
11 13
 target_link_libraries(qdnsagg-dns Qt5::Core Qt5::Network)

+ 23
- 0
src/modules/dns/includes/dns/QDnsLabel.h Переглянути файл

@@ -0,0 +1,23 @@
1
+//
2
+// Created by robin on 1/9/18.
3
+//
4
+
5
+#ifndef QDNSAGG_QDNSLABEL_H
6
+#define QDNSAGG_QDNSLABEL_H
7
+
8
+#include <QString>
9
+
10
+namespace QDns
11
+{
12
+
13
+    class QDnsLabel
14
+    {
15
+    public:
16
+        QDnsLabel() = delete;
17
+
18
+        static QString parse(const QByteArray& data, quint32 pos, quint8 maxRecursion = 5);
19
+    };
20
+
21
+}
22
+
23
+#endif //QDNSAGG_QDNSLABEL_H

+ 80
- 0
src/modules/dns/src/QDnsLabel.cpp Переглянути файл

@@ -0,0 +1,80 @@
1
+//
2
+// Created by robin on 1/9/18.
3
+//
4
+
5
+#include "dns/QDnsLabel.h"
6
+#include <QDataStream>
7
+
8
+namespace QDns
9
+{
10
+
11
+    QString QDnsLabel::parse(const QByteArray& data, quint32 pos, quint8 maxRecursion)
12
+    {
13
+        if (maxRecursion == 0)
14
+        {
15
+            return QString::null;
16
+        }
17
+
18
+        QString fullLabel = "";
19
+        QDataStream dataStream(data);
20
+        dataStream.skipRawData(pos);
21
+
22
+        if (dataStream.device()->bytesAvailable() < 1)
23
+        {
24
+            return QString::null;
25
+        }
26
+
27
+        quint8 labelSize;
28
+        dataStream >> labelSize;
29
+        while (labelSize != 0)
30
+        {
31
+            if (labelSize >> 6 == 3)
32
+            {
33
+                if (dataStream.device()->bytesAvailable() < 1)
34
+                {
35
+                    return QString::null;
36
+                }
37
+                quint8 labelSize2;
38
+                dataStream >> labelSize2;
39
+                quint16 pointer = ((labelSize << 8) | labelSize2) - (quint16) 49152;
40
+                auto labelEnd = parse(data, pointer, maxRecursion - (quint8) 1);
41
+                if (labelEnd.isNull())
42
+                {
43
+                    return QString::null;
44
+                }
45
+                if (!fullLabel.isEmpty())
46
+                {
47
+                    fullLabel.append(".");
48
+                }
49
+                fullLabel.append(labelEnd);
50
+                break;
51
+            }
52
+            else if (labelSize >> 6 == 0)
53
+            {
54
+
55
+                if (dataStream.device()->bytesAvailable() < labelSize + 1)
56
+                {
57
+                    return QString::null;
58
+                }
59
+
60
+                char label[labelSize];
61
+                int read = dataStream.readRawData(label, labelSize);
62
+                if (read != labelSize)
63
+                {
64
+                    return QString::null;
65
+                }
66
+                if (!fullLabel.isEmpty())
67
+                {
68
+                    fullLabel.append(".");
69
+                }
70
+                fullLabel.append(QByteArray(label, labelSize));
71
+                dataStream >> labelSize;
72
+            }
73
+            else
74
+            {
75
+                return QString::null;
76
+            }
77
+        }
78
+        return fullLabel;
79
+    }
80
+}

+ 2
- 0
tests/CMakeLists.txt Переглянути файл

@@ -7,6 +7,8 @@ include_directories(../src/modules/dns/includes)
7 7
 
8 8
 add_executable(qdnsagg-tests
9 9
         main.cpp
10
+        DnsHeader.cpp
11
+        DnsLabel.cpp
10 12
         )
11 13
 
12 14
 target_link_libraries(qdnsagg-tests gtest pthread qdnsagg-dns)

+ 86
- 0
tests/DnsLabel.cpp Переглянути файл

@@ -0,0 +1,86 @@
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/QDnsLabel.h"
10
+
11
+struct DnsLabelTestParams
12
+{
13
+    bool isValid;
14
+    const char* hexData;
15
+    quint32 pos;
16
+    const char* label;
17
+};
18
+
19
+class DnsLabelTest : public ::testing::TestWithParam<DnsLabelTestParams>
20
+{
21
+};
22
+
23
+TEST_P(DnsLabelTest, parse)
24
+{
25
+    auto params = GetParam();
26
+    auto data = QByteArray::fromHex(params.hexData);
27
+    auto label = QDns::QDnsLabel::parse(data, params.pos);
28
+    if (params.isValid)
29
+    {
30
+        ASSERT_FALSE(label.isNull());
31
+        ASSERT_STREQ(params.label, label.toStdString().c_str());
32
+    }
33
+    else
34
+    {
35
+        ASSERT_TRUE(label.isNull());
36
+    }
37
+}
38
+
39
+INSTANTIATE_TEST_CASE_P(DnsLabelTestInst,
40
+                        DnsLabelTest,
41
+                        ::testing::Values(
42
+                                DnsLabelTestParams {false, "", 0},
43
+                                DnsLabelTestParams {true, "00", 0, ""},
44
+                                DnsLabelTestParams {true,
45
+                                                    "06676f6f676c6502667200",
46
+                                                    0,
47
+                                                    "google.fr"
48
+                                },
49
+                                DnsLabelTestParams {true,
50
+                                                    "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
51
+                                                    12,
52
+                                                    "google.fr"
53
+                                },
54
+                                DnsLabelTestParams {true,
55
+                                                    "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
56
+                                                    27,
57
+                                                    "google.fr"
58
+                                },
59
+                                DnsLabelTestParams {true,
60
+                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
61
+                                                    12,
62
+                                                    "www.rthoni.com"
63
+                                },
64
+                                DnsLabelTestParams {true,
65
+                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
66
+                                                    32,
67
+                                                    "www.rthoni.com"
68
+                                },
69
+                                DnsLabelTestParams {true,
70
+                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
71
+                                                    44,
72
+                                                    "serv3.rthoni.com"
73
+                                },
74
+                                DnsLabelTestParams {false,
75
+                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 c02c c02c00010001000002580004d5f6343d",
76
+                                                    44
77
+                                },
78
+                                DnsLabelTestParams {false,
79
+                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 802c c02c00010001000002580004d5f6343d",
80
+                                                    44
81
+                                },
82
+                                DnsLabelTestParams {false,
83
+                                                    "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 402c c02c00010001000002580004d5f6343d",
84
+                                                    44
85
+                                }
86
+                        ));

+ 0
- 258
tests/main.cpp Переглянути файл

@@ -1,262 +1,4 @@
1 1
 #include <gtest/gtest.h>
2
-#include <QByteArray>
3
-#include <QString>
4
-
5
-#include "dns/QDnsHeader.h"
6
-
7
-struct DNSHeaderTestParams
8
-{
9
-    bool isValid;
10
-    const char* hexData;
11
-    quint16 id;
12
-
13
-    quint16 flags;
14
-    bool flagQuery;
15
-    quint8 flagOpcode;
16
-    bool flagAuthoritative;
17
-    bool flagTruncated;
18
-    bool flagRecursionDesired;
19
-    bool flagRecursionAvailable;
20
-    bool flagReserved0;
21
-    bool flagAnswerAuthenticated;
22
-    bool flagNonAuthenticatedAcceptable;
23
-    quint8 flagReplyCode;
24
-
25
-    quint16 questionCount;
26
-    quint16 answerCount;
27
-    quint16 authorityCount;
28
-    quint16 additionalCount;
29
-};
30
-
31
-class DNSHeaderTest : public ::testing::TestWithParam<DNSHeaderTestParams>
32
-{
33
-};
34
-
35
-TEST_P(DNSHeaderTest, parseAndGetters)
36
-{
37
-    auto params = GetParam();
38
-    auto data = QByteArray::fromHex(params.hexData);
39
-    auto header = QDns::QDnsHeader::parse(data);
40
-    ASSERT_EQ(header.isValid(), params.isValid);
41
-
42
-    if (header.isValid())
43
-    {
44
-        ASSERT_EQ(header.getId(), params.id);
45
-
46
-        ASSERT_EQ(header.getFlags(), params.flags);
47
-        ASSERT_EQ(header.isQuery(), params.flagQuery);
48
-        ASSERT_EQ(header.getOpcode(), params.flagOpcode);
49
-        ASSERT_EQ(header.isAuthoritative(), params.flagAuthoritative);
50
-        ASSERT_EQ(header.isTruncated(), params.flagTruncated);
51
-        ASSERT_EQ(header.isRecursionDesired(), params.flagRecursionDesired);
52
-        ASSERT_EQ(header.isRecursionAvailable(), params.flagRecursionAvailable);
53
-        ASSERT_EQ(header.isAnswerAuthenticated(), params.flagAnswerAuthenticated);
54
-        ASSERT_EQ(header.isNonAuthenticatedAcceptable(), params.flagNonAuthenticatedAcceptable);
55
-        ASSERT_EQ(header.getReplyCode(), params.flagReplyCode);
56
-
57
-        ASSERT_EQ(header.getQuestionCount(), params.questionCount);
58
-        ASSERT_EQ(header.getAnswerCount(), params.answerCount);
59
-        ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
60
-        ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
61
-
62
-        ASSERT_EQ(header.serialize().toHex(), QString(params.hexData).remove(" "));
63
-    }
64
-}
65
-
66
-TEST_P(DNSHeaderTest, settersAndGetters1)
67
-{
68
-    auto params = GetParam();
69
-
70
-    if (!params.isValid)
71
-    {
72
-        return;
73
-    }
74
-
75
-    auto data = QByteArray::fromHex(params.hexData);
76
-    QDns::QDnsHeader header;
77
-
78
-    header.setId(params.id);
79
-    header.setFlags(params.flags);
80
-    header.setQuestionCount(params.questionCount);
81
-    header.setAnswerCount(params.answerCount);
82
-    header.setAuthorityCount(params.authorityCount);
83
-    header.setAdditionalCount(params.additionalCount);
84
-
85
-    ASSERT_EQ(header.getId(), params.id);
86
-
87
-    ASSERT_EQ(header.getFlags(), params.flags);
88
-    ASSERT_EQ(header.isQuery(), params.flagQuery);
89
-    ASSERT_EQ(header.getOpcode(), params.flagOpcode);
90
-    ASSERT_EQ(header.isAuthoritative(), params.flagAuthoritative);
91
-    ASSERT_EQ(header.isTruncated(), params.flagTruncated);
92
-    ASSERT_EQ(header.isRecursionDesired(), params.flagRecursionDesired);
93
-    ASSERT_EQ(header.isRecursionAvailable(), params.flagRecursionAvailable);
94
-    ASSERT_EQ(header.isAnswerAuthenticated(), params.flagAnswerAuthenticated);
95
-    ASSERT_EQ(header.isNonAuthenticatedAcceptable(), params.flagNonAuthenticatedAcceptable);
96
-    ASSERT_EQ(header.getReplyCode(), params.flagReplyCode);
97
-
98
-    ASSERT_EQ(header.getQuestionCount(), params.questionCount);
99
-    ASSERT_EQ(header.getAnswerCount(), params.answerCount);
100
-    ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
101
-    ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
102
-
103
-    ASSERT_EQ(header.serialize().toHex(), QString(params.hexData).remove(" "));
104
-}
105
-
106
-TEST_P(DNSHeaderTest, settersAndGetters2)
107
-{
108
-    auto params = GetParam();
109
-
110
-    if (!params.isValid)
111
-    {
112
-        return;
113
-    }
114
-
115
-    auto data = QByteArray::fromHex(params.hexData);
116
-    QDns::QDnsHeader header;
117
-
118
-    header.setId(params.id);
119
-    header.setIsQuery(params.flagQuery);
120
-    header.setOpcode(params.flagOpcode);
121
-    header.setIsAuthoritative(params.flagAuthoritative);
122
-    header.setIsTruncated(params.flagTruncated);
123
-    header.setIsRecursionDesired(params.flagRecursionDesired);
124
-    header.setIsRecursionAvailable(params.flagRecursionAvailable);
125
-    header.setFlagBits(9, (quint16) params.flagReserved0, 1);
126
-    header.setIsAnswerAuthenticated(params.flagAnswerAuthenticated);
127
-    header.setNonAuthenticatedAcceptable(params.flagNonAuthenticatedAcceptable);
128
-    header.setReplyCode(params.flagReplyCode);
129
-    header.setQuestionCount(params.questionCount);
130
-    header.setAnswerCount(params.answerCount);
131
-    header.setAuthorityCount(params.authorityCount);
132
-    header.setAdditionalCount(params.additionalCount);
133
-
134
-    ASSERT_EQ(header.getId(), params.id);
135
-
136
-    ASSERT_EQ(header.getFlags(), params.flags);
137
-    ASSERT_EQ(header.isQuery(), params.flagQuery);
138
-    ASSERT_EQ(header.getOpcode(), params.flagOpcode);
139
-    ASSERT_EQ(header.isAuthoritative(), params.flagAuthoritative);
140
-    ASSERT_EQ(header.isTruncated(), params.flagTruncated);
141
-    ASSERT_EQ(header.isRecursionDesired(), params.flagRecursionDesired);
142
-    ASSERT_EQ(header.isRecursionAvailable(), params.flagRecursionAvailable);
143
-    ASSERT_EQ(header.isAnswerAuthenticated(), params.flagAnswerAuthenticated);
144
-    ASSERT_EQ(header.isNonAuthenticatedAcceptable(), params.flagNonAuthenticatedAcceptable);
145
-    ASSERT_EQ(header.getReplyCode(), params.flagReplyCode);
146
-
147
-    ASSERT_EQ(header.getQuestionCount(), params.questionCount);
148
-    ASSERT_EQ(header.getAnswerCount(), params.answerCount);
149
-    ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
150
-    ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
151
-
152
-    ASSERT_EQ(header.serialize().toHex(), QString(params.hexData).remove(" "));
153
-}
154
-
155
-INSTANTIATE_TEST_CASE_P(DNSHeaderTestInst,
156
-                        DNSHeaderTest,
157
-                        ::testing::Values(
158
-                                DNSHeaderTestParams {false, ""},
159
-                                DNSHeaderTestParams {false, "00"},
160
-                                DNSHeaderTestParams {true,
161
-                                                     "3c dc 01 00 00 01 00 00  00 00 00 00",
162
-                                                     0x3cdc,
163
-
164
-                                                     0x0100,
165
-                                                     false,
166
-                                                     0,
167
-                                                     false,
168
-                                                     false,
169
-                                                     true,
170
-                                                     false,
171
-                                                     false,
172
-                                                     false,
173
-                                                     false,
174
-                                                     0,
175
-
176
-                                                     1,
177
-                                                     0,
178
-                                                     0,
179
-                                                     0
180
-                                },
181
-                                DNSHeaderTestParams {true,
182
-                                                     "3c dc 81 80 00 01 00 01  00 00 00 00",
183
-                                                     0x3cdc,
184
-                                                     0x8180,
185
-                                                     true,
186
-                                                     0,
187
-                                                     false,
188
-                                                     false,
189
-                                                     true,
190
-                                                     true,
191
-                                                     false,
192
-                                                     false,
193
-                                                     false,
194
-                                                     0,
195
-
196
-                                                     1,
197
-                                                     1,
198
-                                                     0,
199
-                                                     0
200
-                                },
201
-
202
-                                DNSHeaderTestParams {true,
203
-                                                     "3c dc aa aa aa aa 55 55  55 ff ff 55",
204
-                                                     0x3cdc,
205
-                                                     0xaaaa,
206
-                                                     true,
207
-                                                     5,
208
-                                                     false,
209
-                                                     true,
210
-                                                     false,
211
-                                                     true,
212
-                                                     false,
213
-                                                     true,
214
-                                                     false,
215
-                                                     10,
216
-
217
-                                                     43690,
218
-                                                     21845,
219
-                                                     22015,
220
-                                                     65365
221
-                                },
222
-                                DNSHeaderTestParams {true,
223
-                                                     "3c dc 55 55 55 ff aa aa  ff 55 55 55",
224
-                                                     0x3cdc,
225
-                                                     0x5555,
226
-                                                     false,
227
-                                                     10,
228
-                                                     true,
229
-                                                     false,
230
-                                                     true,
231
-                                                     false,
232
-                                                     true,
233
-                                                     false,
234
-                                                     true,
235
-                                                     5,
236
-
237
-                                                     22015,
238
-                                                     43690,
239
-                                                     65365,
240
-                                                     21845
241
-                                }
242
-                        ));
243
-
244
-TEST(DNSHeaderTest, getFlagBits)
245
-{
246
-    QDns::QDnsHeader header;
247
-
248
-    header.setFlags(0x5555);
249
-    ASSERT_EQ(header.getFlagBits(0, 1), 0);
250
-    ASSERT_EQ(header.getFlagBits(3, 4), 5);
251
-}
252
-
253
-TEST(DNSHeaderTest, getFlagBitIndex)
254
-{
255
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(0), 15);
256
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(1), 14);
257
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(14), 1);
258
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(15), 0);
259
-}
260 2
 
261 3
 int main(int argc, char* argv[])
262 4
 {

Завантаження…
Відмінити
Зберегти