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.

DnsResourceRecord.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Created by robin on 1/9/18.
  3. //
  4. #include <gtest/gtest.h>
  5. #include <QByteArray>
  6. #include <QString>
  7. #include "dns/QDnsResourceRecord.h"
  8. struct DnsResourceRecordParseTestParams
  9. {
  10. bool isValid;
  11. const char* hexData;
  12. quint32 pos;
  13. const char* label;
  14. quint16 type;
  15. quint16 clazz;
  16. quint32 ttl;
  17. const char* hexData2;
  18. };
  19. class DnsResourceRecordParseTest : public ::testing::TestWithParam<DnsResourceRecordParseTestParams>
  20. {
  21. };
  22. TEST_P(DnsResourceRecordParseTest, parse)
  23. {
  24. auto params = GetParam();
  25. auto data = QByteArray::fromHex(params.hexData);
  26. auto rr = QDns::QDnsResourceRecord::parse(data, params.pos);
  27. ASSERT_EQ(rr.isValid(), params.isValid);
  28. if (rr.isValid())
  29. {
  30. ASSERT_STREQ(rr.getName().toStdString().c_str(), params.label);
  31. ASSERT_EQ(rr.getType(), params.type);
  32. ASSERT_EQ(rr.getClass(), params.clazz);
  33. ASSERT_EQ(rr.getTtl(), params.ttl);
  34. ASSERT_EQ(rr.getData().toHex(), QString(params.hexData2).remove(" "));
  35. }
  36. }
  37. INSTANTIATE_TEST_CASE_P(DnsResourceRecordParseTestInst,
  38. DnsResourceRecordParseTest,
  39. ::testing::Values(
  40. DnsResourceRecordParseTestParams {false, "", 0},
  41. DnsResourceRecordParseTestParams {false, "00", 0},
  42. DnsResourceRecordParseTestParams {true,
  43. "e1bc818000010000000100000266720000010001c00c0006000100000e100030086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518",
  44. 20,
  45. "fr",
  46. 6,
  47. 1,
  48. 3600,
  49. "086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518"
  50. },
  51. DnsResourceRecordParseTestParams {true,
  52. "1c588580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
  53. 32,
  54. "www.rthoni.com",
  55. 5,
  56. 1,
  57. 600,
  58. "057365727633c010"
  59. },
  60. DnsResourceRecordParseTestParams {true,
  61. "1c588580000100020000000003777777067274686f6e6903636f6d0000010001c00c00050001000002580008057365727633c010c02c00010001000002580004d5f6343d",
  62. 52,
  63. "serv3.rthoni.com",
  64. 1,
  65. 1,
  66. 600,
  67. "d5f6343d"
  68. }
  69. ));
  70. struct DnsResourceRecordSerializeTestParams
  71. {
  72. const char* hexData;
  73. const char* name;
  74. quint16 type;
  75. quint16 clazz;
  76. quint32 ttl;
  77. const char* hexData2;
  78. };
  79. class DnsResourceRecordSerializeTest : public ::testing::TestWithParam<DnsResourceRecordSerializeTestParams>
  80. {
  81. };
  82. TEST_P(DnsResourceRecordSerializeTest, serialize)
  83. {
  84. auto params = GetParam();
  85. QDns::QDnsResourceRecord rr;
  86. rr.setName(params.name);
  87. rr.setType(params.type);
  88. rr.setClass(params.clazz);
  89. rr.setTtl(params.ttl);
  90. rr.setData(QByteArray::fromHex(params.hexData2));
  91. ASSERT_EQ(rr.serialize().toHex(), QString(params.hexData).remove(" "));
  92. }
  93. INSTANTIATE_TEST_CASE_P(DnsResourceRecordSerializeTestInst,
  94. DnsResourceRecordSerializeTest,
  95. ::testing::Values(
  96. DnsResourceRecordSerializeTestParams {"026672000006000100000e100030086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518",
  97. "fr",
  98. 6,
  99. 1,
  100. 3600,
  101. "086e736d6173746572036e6963c00c0a686f73746d6173746572c029849e46cf00000e10000007080036ee8000001518"
  102. },
  103. DnsResourceRecordSerializeTestParams {"03777777067274686f6e6903636f6d0000050001000002580008057365727633c010",
  104. "www.rthoni.com",
  105. 5,
  106. 1,
  107. 600,
  108. "057365727633c010"
  109. }
  110. ));