|
@@ -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
|
{
|