Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DnsLabel.cpp 5.3KB

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/QDnsLabel.h"
  8. struct DnsLabelParseTestParams
  9. {
  10. bool isValid;
  11. const char* hexData;
  12. quint32 pos;
  13. const char* label;
  14. };
  15. class DnsLabelParseTest : public ::testing::TestWithParam<DnsLabelParseTestParams>
  16. {
  17. };
  18. TEST_P(DnsLabelParseTest, parse)
  19. {
  20. auto params = GetParam();
  21. auto data = QByteArray::fromHex(params.hexData);
  22. auto label = QDns::QDnsLabel::parse(data, params.pos);
  23. if (params.isValid)
  24. {
  25. ASSERT_FALSE(label.isNull());
  26. ASSERT_STREQ(params.label, label.toStdString().c_str());
  27. }
  28. else
  29. {
  30. ASSERT_TRUE(label.isNull());
  31. }
  32. }
  33. INSTANTIATE_TEST_CASE_P(DnsLabelParseTestInst,
  34. DnsLabelParseTest,
  35. ::testing::Values(
  36. DnsLabelParseTestParams {false, "", 0},
  37. DnsLabelParseTestParams {true, "00", 0, ""},
  38. DnsLabelParseTestParams {true,
  39. "06676f6f676c6502667200",
  40. 0,
  41. "google.fr"
  42. },
  43. DnsLabelParseTestParams {true,
  44. "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
  45. 12,
  46. "google.fr"
  47. },
  48. DnsLabelParseTestParams {true,
  49. "587a8180000100010000000006676f6f676c650266720000010001c00c000100010000012c0004acd912a3",
  50. 27,
  51. "google.fr"
  52. },
  53. DnsLabelParseTestParams {true,
  54. "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
  55. 12,
  56. "www.rthoni.com"
  57. },
  58. DnsLabelParseTestParams {true,
  59. "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
  60. 32,
  61. "www.rthoni.com"
  62. },
  63. DnsLabelParseTestParams {true,
  64. "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
  65. 44,
  66. "serv3.rthoni.com"
  67. },
  68. DnsLabelParseTestParams {false,
  69. "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 c02c c02c00010001000002580004d5f6343d",
  70. 44
  71. },
  72. DnsLabelParseTestParams {false,
  73. "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 802c c02c00010001000002580004d5f6343d",
  74. 44
  75. },
  76. DnsLabelParseTestParams {false,
  77. "3dcd8580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633 402c c02c00010001000002580004d5f6343d",
  78. 44
  79. }
  80. ));
  81. struct DnsLabelSerializeTestParams
  82. {
  83. const char* label;
  84. const char* hexData;
  85. };
  86. class DnsLabelSerializeTest : public ::testing::TestWithParam<DnsLabelSerializeTestParams>
  87. {
  88. };
  89. TEST_P(DnsLabelSerializeTest, parse)
  90. {
  91. auto params = GetParam();
  92. auto label = QDns::QDnsLabel::serialize(params.label);
  93. auto data = QByteArray::fromHex(params.hexData);
  94. ASSERT_EQ(label, data);
  95. }
  96. INSTANTIATE_TEST_CASE_P(DnsLabelSerializeTestInst,
  97. DnsLabelSerializeTest,
  98. ::testing::Values(
  99. DnsLabelSerializeTestParams {"", "00"},
  100. DnsLabelSerializeTestParams {"com", "03636f6d00"},
  101. DnsLabelSerializeTestParams {"fr", "02667200"},
  102. DnsLabelSerializeTestParams {"google.fr", "06676f6f676c6502667200"},
  103. DnsLabelSerializeTestParams {"www.rthoni.com", "03777777067274686f6e6903636f6d00"}
  104. ));