You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 11KB

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