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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. QDns::QDnsQuery query;
  74. query.setName(params.name);
  75. query.setType(params.type);
  76. query.setClass(params.clazz);
  77. ASSERT_EQ(query.serialize().toHex(), QString(params.hexData).remove(" "));
  78. }
  79. INSTANTIATE_TEST_CASE_P(DnsQuerySerializeTestInst,
  80. DnsQuerySerializeTest,
  81. ::testing::Values(
  82. DnsQuerySerializeTestParams {"0266720000020008",
  83. "fr",
  84. 2,
  85. 8
  86. },
  87. DnsQuerySerializeTestParams {"06676f6f676c650266720000010001",
  88. "google.fr",
  89. 1,
  90. 1
  91. },
  92. DnsQuerySerializeTestParams {"03777777067274686f6e6903636f6d0000010001",
  93. "www.rthoni.com",
  94. 1,
  95. 1
  96. }
  97. ));