Browse Source

Added DnsLabel serialize() method and associated tests

develop
Robin Thoni 6 years ago
parent
commit
e395b75c7a
3 changed files with 73 additions and 16 deletions
  1. 3
    0
      src/modules/dns/includes/dns/QDnsLabel.h
  2. 25
    0
      src/modules/dns/src/QDnsLabel.cpp
  3. 45
    16
      tests/DnsLabel.cpp

+ 3
- 0
src/modules/dns/includes/dns/QDnsLabel.h View File

@@ -17,6 +17,9 @@ namespace QDns
17 17
 
18 18
         static QString parse(const QByteArray& packetData, quint32 pos, quint8 maxRecursion = 5);
19 19
         static QString parse(const QByteArray& packetData, QDataStream& dataStream, quint8 maxRecursion = 5);
20
+
21
+        static QByteArray serialize(const QString& label);
22
+        static void serialize(const QString& label, QDataStream& dataStream);
20 23
     };
21 24
 
22 25
 }

+ 25
- 0
src/modules/dns/src/QDnsLabel.cpp View File

@@ -84,4 +84,29 @@ namespace QDns
84 84
         }
85 85
         return fullLabel;
86 86
     }
87
+
88
+    QByteArray QDnsLabel::serialize(const QString& label)
89
+    {
90
+        QByteArray data;
91
+        QDataStream dataStream(&data, QIODevice::WriteOnly);
92
+        serialize(label, dataStream);
93
+        return data;
94
+    }
95
+
96
+    void QDnsLabel::serialize(const QString& label, QDataStream& dataStream)
97
+    {
98
+        if (label.size() != 0)
99
+        {
100
+            for (const auto& part : label.split("."))
101
+            {
102
+                dataStream << (quint8) part.length();
103
+                for (const auto& c : part)
104
+                {
105
+                    dataStream << (quint8) c.toLatin1();
106
+                }
107
+            }
108
+        }
109
+        dataStream << (quint8) 0;
110
+    }
111
+
87 112
 }

+ 45
- 16
tests/DnsLabel.cpp View File

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

Loading…
Cancel
Save