diff options
author | Razvan Becheriu <razvan@isc.org> | 2021-03-17 01:43:59 +0100 |
---|---|---|
committer | Razvan Becheriu <razvan@isc.org> | 2021-05-05 19:12:18 +0200 |
commit | 2c5ac0a5018102bb40867918285d43f503f9b26f (patch) | |
tree | e76cc96b82ae7de41dc0b15123ec255b4af40384 /src/lib/eval | |
parent | [#1680] use consistent format (diff) | |
download | kea-2c5ac0a5018102bb40867918285d43f503f9b26f.tar.xz kea-2c5ac0a5018102bb40867918285d43f503f9b26f.zip |
[#1680] all tokens must not throw on empty string
Diffstat (limited to 'src/lib/eval')
-rw-r--r-- | src/lib/eval/tests/token_unittest.cc | 105 | ||||
-rw-r--r-- | src/lib/eval/token.cc | 49 |
2 files changed, 122 insertions, 32 deletions
diff --git a/src/lib/eval/tests/token_unittest.cc b/src/lib/eval/tests/token_unittest.cc index 8368362e75..620372ac92 100644 --- a/src/lib/eval/tests/token_unittest.cc +++ b/src/lib/eval/tests/token_unittest.cc @@ -765,13 +765,7 @@ TEST_F(TokenTest, addressToText) { TokenPtr address((new TokenIpAddressToText())); std::vector<uint8_t> bytes; - std::string value; - values_.push(value); - - // Invalid data size fails. - EXPECT_THROW(address->evaluate(*pkt4_, values_), EvalTypeError); - - value = "10.0.0.1"; + std::string value = "10.0.0.1"; values_.push(value); // Invalid data size fails. @@ -794,6 +788,17 @@ TEST_F(TokenTest, addressToText) { // Check that the evaluation put its value on the values stack. ASSERT_EQ(2, values_.size()); + values_.push(std::string()); + EXPECT_NO_THROW(address->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(3, values_.size()); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + // Check IPv6 address EXPECT_EQ(11, values_.top().size()); EXPECT_EQ("2001:db8::1", values_.top()); @@ -824,23 +829,7 @@ TEST_F(TokenTest, integerToText) { TokenPtr uint32token((new TokenUInt32ToText())); std::vector<uint8_t> bytes; - std::string value; - - // Invalid data size fails. - values_.push(value); - EXPECT_THROW(int8token->evaluate(*pkt4_, values_), EvalTypeError); - values_.push(value); - EXPECT_THROW(int16token->evaluate(*pkt4_, values_), EvalTypeError); - values_.push(value); - EXPECT_THROW(int32token->evaluate(*pkt4_, values_), EvalTypeError); - values_.push(value); - EXPECT_THROW(uint8token->evaluate(*pkt4_, values_), EvalTypeError); - values_.push(value); - EXPECT_THROW(uint16token->evaluate(*pkt4_, values_), EvalTypeError); - values_.push(value); - EXPECT_THROW(uint32token->evaluate(*pkt4_, values_), EvalTypeError); - - value = "0123456789"; + std::string value = "0123456789"; // Invalid data size fails. values_.push(value); @@ -900,6 +889,74 @@ TEST_F(TokenTest, integerToText) { // Check that the evaluation put its value on the values stack. ASSERT_EQ(6, values_.size()); + value = ""; + + values_.push(value); + EXPECT_NO_THROW(int8token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(7, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(int16token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(8, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(int32token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(9, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(uint8token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(10, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(uint16token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(11, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(uint32token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(12, values_.size()); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + // Check uint32 EXPECT_EQ(10, values_.top().size()); EXPECT_EQ("4294967295", values_.top()); diff --git a/src/lib/eval/token.cc b/src/lib/eval/token.cc index a14af3bc0a..f27eb69f7d 100644 --- a/src/lib/eval/token.cc +++ b/src/lib/eval/token.cc @@ -113,10 +113,13 @@ TokenIpAddressToText::evaluate(Pkt& /*pkt*/, ValueStack& values) { isc_throw(EvalBadStack, "Incorrect empty stack."); } + size_t size; string op = values.top(); - values.pop(); - uint8_t size = op.size(); + if (!(size = op.size())) { + return; + } + values.pop(); if ((size != sizeof(uint32_t)) && (size != INET_ADDRSTRLEN)) { isc_throw(EvalTypeError, "Can not convert to valid address."); @@ -144,10 +147,15 @@ TokenInt8ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) { isc_throw(EvalBadStack, "Incorrect empty stack."); } + size_t size; string op = values.top(); + + if (!(size = op.size())) { + return; + } values.pop(); - if (op.size() != sizeof(int8_t)) { + if (size != sizeof(int8_t)) { isc_throw(EvalTypeError, "Can not convert to valid int8."); } @@ -168,10 +176,15 @@ TokenInt16ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) { isc_throw(EvalBadStack, "Incorrect empty stack."); } + size_t size; string op = values.top(); + + if (!(size = op.size())) { + return; + } values.pop(); - if (op.size() != sizeof(int16_t)) { + if (size != sizeof(int16_t)) { isc_throw(EvalTypeError, "Can not convert to valid int16."); } @@ -194,10 +207,15 @@ TokenInt32ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) { isc_throw(EvalBadStack, "Incorrect empty stack."); } + size_t size; string op = values.top(); + + if (!(size = op.size())) { + return; + } values.pop(); - if (op.size() != sizeof(int32_t)) { + if (size != sizeof(int32_t)) { isc_throw(EvalTypeError, "Can not convert to valid int32."); } @@ -220,10 +238,15 @@ TokenUInt8ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) { isc_throw(EvalBadStack, "Incorrect empty stack."); } + size_t size; string op = values.top(); + + if (!(size = op.size())) { + return; + } values.pop(); - if (op.size() != sizeof(uint8_t)) { + if (size != sizeof(uint8_t)) { isc_throw(EvalTypeError, "Can not convert to valid uint8."); } @@ -244,10 +267,15 @@ TokenUInt16ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) { isc_throw(EvalBadStack, "Incorrect empty stack."); } + size_t size; string op = values.top(); + + if (!(size = op.size())) { + return; + } values.pop(); - if (op.size() != sizeof(uint16_t)) { + if (size != sizeof(uint16_t)) { isc_throw(EvalTypeError, "Can not convert to valid uint16."); } @@ -270,10 +298,15 @@ TokenUInt32ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) { isc_throw(EvalBadStack, "Incorrect empty stack."); } + size_t size; string op = values.top(); + + if (!(size = op.size())) { + return; + } values.pop(); - if (op.size() != sizeof(uint32_t)) { + if (size != sizeof(uint32_t)) { isc_throw(EvalTypeError, "Can not convert to valid uint32."); } |