Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DnsQuery.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Created by robin on 1/9/18.
  3. //
  4. #include <gtest/gtest.h>
  5. #include <QByteArray>
  6. #include <QString>
  7. #include "dns/QDnsQuery.h"
  8. struct DnsQueryParseTestParams
  9. {
  10. bool isValid;
  11. const char* hexData;
  12. quint32 pos;
  13. const char* label;
  14. quint16 type;
  15. quint16 clazz;
  16. };
  17. class DnsQueryParseTest : public ::testing::TestWithParam<DnsQueryParseTestParams>
  18. {
  19. };
  20. TEST_P(DnsQueryParseTest, parse)
  21. {
  22. auto params = GetParam();
  23. auto data = QByteArray::fromHex(params.hexData);
  24. auto query = QDns::QDnsQuery::parse(data, params.pos);
  25. ASSERT_EQ(query.isValid(), params.isValid);
  26. if (query.isValid())
  27. {
  28. ASSERT_STREQ(query.getName().toStdString().c_str(), params.label);
  29. ASSERT_EQ(query.getType(), params.type);
  30. ASSERT_EQ(query.getClass(), params.clazz);
  31. }
  32. }
  33. INSTANTIATE_TEST_CASE_P(DnsQueryParseTestInst,
  34. DnsQueryParseTest,
  35. ::testing::Values(
  36. DnsQueryParseTestParams {false, "", 0},
  37. DnsQueryParseTestParams {false, "00", 0},
  38. DnsQueryParseTestParams {true,
  39. "0377777706676f6f676c650266720000010001",
  40. 0,
  41. "www.google.fr",
  42. 1,
  43. 1
  44. },
  45. DnsQueryParseTestParams {true,
  46. "03777777067274686f6e6903636f6d0000030008",
  47. 0,
  48. "www.rthoni.com",
  49. 3,
  50. 8
  51. },
  52. DnsQueryParseTestParams {true,
  53. "0003777777067274686f6e6903636f6d0000030008",
  54. 1,
  55. "www.rthoni.com",
  56. 3,
  57. 8
  58. }
  59. ));
  60. struct DnsQuerySerializeTestParams
  61. {
  62. const char* hexData;
  63. const char* name;
  64. quint16 type;
  65. quint16 clazz;
  66. };
  67. class DnsQuerySerializeTest : public ::testing::TestWithParam<DnsQuerySerializeTestParams>
  68. {
  69. };
  70. TEST_P(DnsQuerySerializeTest, serialize)
  71. {
  72. auto params = GetParam();
  73. auto data = QByteArray::fromHex(params.hexData);
  74. QDns::QDnsQuery query;
  75. query.setName(params.name);
  76. query.setType(params.type);
  77. query.setClass(params.clazz);
  78. ASSERT_EQ(query.serialize().toHex(), QString(params.hexData).remove(" "));
  79. }
  80. INSTANTIATE_TEST_CASE_P(DnsQuerySerializeTestInst,
  81. DnsQuerySerializeTest,
  82. ::testing::Values(
  83. DnsQuerySerializeTestParams {"0266720000020008",
  84. "fr",
  85. 2,
  86. 8
  87. },
  88. DnsQuerySerializeTestParams {"06676f6f676c650266720000010001",
  89. "google.fr",
  90. 1,
  91. 1
  92. },
  93. DnsQuerySerializeTestParams {"03777777067274686f6e6903636f6d0000010001",
  94. "www.rthoni.com",
  95. 1,
  96. 1
  97. }
  98. ));