Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DnsHeader.cpp 11KB

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