diff options
-rw-r--r-- | src/lib/dns/labelsequence.cc | 9 | ||||
-rw-r--r-- | src/lib/dns/labelsequence.h | 10 | ||||
-rw-r--r-- | src/lib/dns/tests/labelsequence_unittest.cc | 87 |
3 files changed, 106 insertions, 0 deletions
diff --git a/src/lib/dns/labelsequence.cc b/src/lib/dns/labelsequence.cc index 6e778f42ae..b56f224243 100644 --- a/src/lib/dns/labelsequence.cc +++ b/src/lib/dns/labelsequence.cc @@ -29,6 +29,15 @@ LabelSequence::getData(size_t *len) const { return &(data_[offsets_[first_label_]]); } +void +LabelSequence::getOffsetData(size_t* len, + uint8_t placeholder[Name::MAX_LABELS]) const { + *len = last_label_ - first_label_; + for (size_t i = 0; i < *len; i++) { + placeholder[i] = offsets_[first_label_ + i] - offsets_[first_label_]; + } +} + size_t LabelSequence::getDataLength() const { // If the labelsequence is absolute, the current last_label_ falls diff --git a/src/lib/dns/labelsequence.h b/src/lib/dns/labelsequence.h index c2149f0ce8..019250e72c 100644 --- a/src/lib/dns/labelsequence.h +++ b/src/lib/dns/labelsequence.h @@ -96,6 +96,16 @@ public: /// \return Pointer to the wire-format data of this label sequence const uint8_t* getData(size_t* len) const; + /// \brief Return the offset data for this LabelSequence + /// + /// The offsets are returned in the <code>placeholder</code> array. + /// + /// \param len Pointer to a size_t where the number of offsets + /// will be stored + /// \param placeholder Array where the offset data will be returned + void getOffsetData(size_t* len, + uint8_t placeholder[Name::MAX_LABELS]) const; + /// \brief Return the length of the wire-format data of this LabelSequence /// /// This method returns the number of octets for the data that would diff --git a/src/lib/dns/tests/labelsequence_unittest.cc b/src/lib/dns/tests/labelsequence_unittest.cc index 6aca1b92ca..2b52dd125f 100644 --- a/src/lib/dns/tests/labelsequence_unittest.cc +++ b/src/lib/dns/tests/labelsequence_unittest.cc @@ -400,6 +400,93 @@ TEST_F(LabelSequenceTest, getData) { getDataCheck("\000", 1, ls7); }; +TEST_F(LabelSequenceTest, getOffsetData) { + size_t len; + uint8_t placeholder[Name::MAX_LABELS]; + + Name nx("x.isc.example.org"); + LabelSequence lsx(nx); + + // x.isc.example.org. + lsx.getOffsetData(&len, placeholder); + EXPECT_EQ(5, len); + EXPECT_EQ(0, placeholder[0]); + EXPECT_EQ(2, placeholder[1]); + EXPECT_EQ(6, placeholder[2]); + EXPECT_EQ(14, placeholder[3]); + EXPECT_EQ(18, placeholder[4]); + + lsx.stripLeft(2); + + // example.org. + lsx.getOffsetData(&len, placeholder); + EXPECT_EQ(3, len); + EXPECT_EQ(0, placeholder[0]); + EXPECT_EQ(8, placeholder[1]); + EXPECT_EQ(12, placeholder[2]); + + lsx.stripLeft(1); + + // org. + lsx.getOffsetData(&len, placeholder); + EXPECT_EQ(2, len); + EXPECT_EQ(0, placeholder[0]); + EXPECT_EQ(4, placeholder[1]); + + lsx.stripLeft(1); + + // . + lsx.getOffsetData(&len, placeholder); + EXPECT_EQ(1, len); + EXPECT_EQ(0, placeholder[0]); + + Name ny("y.isc.example.org"); + LabelSequence lsy(ny); + + // y.isc.example.org. + lsy.getOffsetData(&len, placeholder); + EXPECT_EQ(5, len); + EXPECT_EQ(0, placeholder[0]); + EXPECT_EQ(2, placeholder[1]); + EXPECT_EQ(6, placeholder[2]); + EXPECT_EQ(14, placeholder[3]); + EXPECT_EQ(18, placeholder[4]); + + lsy.stripRight(1); + + // y.isc.example.org + lsy.getOffsetData(&len, placeholder); + EXPECT_EQ(4, len); + EXPECT_EQ(0, placeholder[0]); + EXPECT_EQ(2, placeholder[1]); + EXPECT_EQ(6, placeholder[2]); + EXPECT_EQ(14, placeholder[3]); + + lsy.stripRight(1); + + // y.isc.example + lsy.getOffsetData(&len, placeholder); + EXPECT_EQ(3, len); + EXPECT_EQ(0, placeholder[0]); + EXPECT_EQ(2, placeholder[1]); + EXPECT_EQ(6, placeholder[2]); + + lsy.stripLeft(1); + + // isc.example + lsy.getOffsetData(&len, placeholder); + EXPECT_EQ(2, len); + EXPECT_EQ(0, placeholder[0]); + EXPECT_EQ(4, placeholder[1]); + + lsy.stripLeft(1); + + // example + lsy.getOffsetData(&len, placeholder); + EXPECT_EQ(1, len); + EXPECT_EQ(0, placeholder[0]); +}; + TEST_F(LabelSequenceTest, stripLeft) { EXPECT_TRUE(ls1.equals(ls3)); ls1.stripLeft(0); |